[Coco] Program to read text files
Barry Nelson
barry.nelson at amobiledevice.com
Fri Oct 2 02:08:21 EDT 2015
On Oct 1, 2015, at 10:53 PM, Robert Gault <robert.gault at att.net> wrote:
> Unfortunately the only program on the nightly build disks for reading
> text files is Edit. You should consider obtaining vu (for reading) and
> scred (for editing) text files.
I had the source code below floating around. I think I wrote much of it though I probably based it off other code. It compiles with the RadioShack OS-9 C compiler. It is a (sort of) port of the unix more utility.
============================================================================
#include <stdio.h>
#include <os9.h>
#include <ctype.h>
#define STDOUT 1
#define STDERR 2
#define SS_SCSIZ 38
#define TRUE 1
#define FALSE 0
int cflag=FALSE;
int lflag=FALSE;
int pflag=FALSE;
int first=TRUE;
int width;
int length;
int cc=0;
int lc=0;
char word[133];
char *wp=NULL;
int tfp;
FILE *fopen();
char *strcmp();
/*page*/
main(argc,argv)
int argc;
char **argv;
{
char *p, *q, c;
char name[32];
char param[32];
char fname[32];
char command[128];
FILE *path;
int nfiles;
int tp;
int pn;
long nc;
struct registers reg;
pflinit();
setbuf(stdout, 0);
reg.rg_a = STDOUT;
reg.rg_b = SS_SCSIZ;
reg.rg_x = 0;
reg.rg_y = 0;
reg.rg_u = 0;
if (_os9(I_GETSTT, ®) == -1)
exit (reg.rg_b);
width=reg.rg_x;
if (width<32 || width>132)
{
reg.rg_a = STDERR;
reg.rg_b = SS_SCSIZ;
reg.rg_x = 0;
reg.rg_y = 0;
reg.rg_u = 0;
if (_os9(I_GETSTT, ®) == -1)
exit (reg.rg_b);
}
width=reg.rg_x;
length=reg.rg_y;
if (*(p = *++argv) == '-')
{
while (c = *++p)
{
c=toupper(c);
switch (c)
{
case 'C' :
cflag = TRUE;
break;
case 'P' :
pflag = TRUE;
break;
case 'L' :
lflag = TRUE;
break;
default :
break;
}
}
--argc; /* adjust count for option */
++argv; /* next arg */
}
nfiles=argc;
if (--nfiles==0)
{
if (cflag)
printf("\n\f");
nc=0;
while((c=getchar())!=EOF && (c!=0x87 || nc++>0))
out(c);
if (c==0x87)
printf("Not a text file!\n");
}
while (--argc)
{
p = *argv++;
if (cflag)
printf("\n\f");
if (nfiles>1)
{
if (!first)
press();
else
first=FALSE;
printf("\n\fFile name=%s\n",p);
lc++;
}
if ((path=fopen(p,"r"))==NULL)
{
fprintf(stderr,"Can't open %s\n",p);
exit(errno);
}
/* Process file */
nc=0;
while((c=getc(path))!=EOF && (c!=0x87 || nc++>0))
out(c);
if (c==0x87)
printf("Not a text file!\n");
}
if (pflag)
press();
}
/*page*/
/*
** local getstat
** r_a = path number r_b = code
** r_x = parameter r_y = parameter
** r_u = parameter
*/
lgetstat(r_a, r_b, r_x, r_y, r_u)
char r_a, r_b;
int r_x, r_y, r_u;
{
struct registers reg;
reg.rg_a = r_a;
reg.rg_b = r_b;
reg.rg_x = r_x;
reg.rg_y = r_y;
reg.rg_u = r_u;
if (_os9(I_GETSTT, ®) == -1)
exit (reg.rg_b);
return (reg.rg_x);
}
press()
{
char c;
char *prompt="<SPACE>,<ENTER>,L,or Q: ";
int x;
if (rdchk(STDERR)>0)
{
read(STDERR,&c,1);
if (c!='\r' && c!='\n' && c>=' ')
putchar('\b');
if (c=='\b')
printf("\n");
}
else
if (lflag)
return(0);
if (lc>0)
{
lflag=FALSE;
printf(prompt);
c=-1;
while (c<32 && c!='\r' && c!='\n')
read(STDERR,&c,1);
c=toupper(c);
if (c!='\n' && c!='\r')
{
for (x=0 ; x<=strlen(prompt) ; x++)
{
putchar('\b');
putchar(' ');
putchar('\b');
}
putchar(' ');
putchar('\b');
}
else
{
lc=length-2;
for (x=0 ; x<strlen(prompt) ; x++)
putchar(' ');
for (x=0 ; x<strlen(prompt) ; x++)
putchar('\b');
return(0);
}
if (c=='Q')
exit(0);
if (c=='L')
lflag=TRUE;
else
lc=0;
}
}
out(c)
char c;
{
int n;
char ic;
ic=c;
if (c<0)
c='*';
if (c=='\n' || c=='\r')
{
lc++;
cc=0;
}
else
if (c=='\b')
{
cc--;
if (wp>word)
*wp-- = '\0';
}
else
if (c>=' ')
cc++;
if (c=='\f')
{
press();
cc=0;
lc=0;
}
if (c==' ' || wp==NULL || c=='\n' || c=='\r' || c=='\f')
{
wp=word;
*wp='\0';
}
else
if (c>' ')
{
*wp++=ic;
*wp='\0';
}
if (cc>=width && c!='\n' && c!='\r' && c!='\f')
{
if (c!=' ' && strlen(word)<width-1)
{
for (n=0 ; n<strlen(word)-1 ; n++)
{
putchar('\b');
putchar(' ');
putchar('\b');
}
putchar('\n');
cc=0;
lc++;
if (lc>=length-1 || rdchk(STDERR)>0)
{
press();
}
writeln(STDOUT,word,strlen(word)-1);
cc += strlen(word)-1;
out(ic);
return(0);
}
}
if (cc>=width)
{
cc -= width;
lc++;
}
putchar(ic);
if ((lc>=length-1 || rdchk(STDERR)>0) && (c=='\n' || c=='\r' || c=='\f'))
{
press();
cc=0;
wp=word;
*wp='\0';
}
}
rdchk(fdes)
int fdes;
{
struct registers reg;
reg.rg_a = fdes;
reg.rg_b = 1;
reg.rg_x = 0;
reg.rg_y = 0;
reg.rg_u = 0;
if (_os9(I_GETSTT, ®) == -1)
{
if (reg.rg_b!=0xf6)
{
errno=0;
return(0);
}
else
errno=reg.rg_b;
return(-1);
}
return(1);
}
More information about the Coco
mailing list