Annotation of 43BSD/contrib/apl/src/cata.c, revision 1.1.1.1

1.1       root        1: static char Sccsid[] = "cata.c @(#)cata.c      1.1     10/1/82 Berkeley ";
                      2: #
                      3: 
                      4: /*
                      5: 
                      6: cata.c -- cat files in apl format
                      7: 
                      8: Usage:  cata file1 file2 file3...
                      9:        catb file1 file2 file3.....
                     10:        cata [-bl] file1 file2 file3...
                     11: can also be used as a filter, i.e.  cat file ^ cata ^ opr
                     12: 
                     13: cat? copies each line from the input file to the output
                     14: file, preceding each line with the line number in brackets.
                     15: the first line of each file is assumed to be the
                     16: function name and is printed without a line number.
                     17: 
                     18: cata performs overprints by backspacing and printing
                     19: the overprint character directly on top of the base
                     20: character.
                     21: catb captures each overprinted character and prints
                     22: it on the line after its occurance, underneath
                     23: the character that formed the base of the overprint.
                     24: 
                     25: cata.c is the program used to generate the binary:
                     26: the binary is linked to two file names: cata and catb
                     27: the program checks to detemine which one was called
                     28: and executes accordingly.
                     29: cata and catb are generated by compiling cata.c :
                     30:        cc cata.c -m -O ; mv a.out cata
                     31:        ln cata catb
                     32: note:   cata and catb must be linked together
                     33: 
                     34: flags:
                     35: Two flags are currently available to cata.
                     36: -l disables the printing of line numbers.
                     37: -b puts cata into the catb mode of printing lines.
                     38: examples:
                     39:        cata -l w1      print w1 in cata mode without line numbers
                     40:        cata -b w1      print w1 in catb mode
                     41:        cata -bl w1     print w1 in catb mode without line numbers
                     42:        cata w1         print w1 in cata mode
                     43: 
                     44: as of 10/23/78 cata and catb are linked together.
                     45: using catb is equivilent to typing:  cata  -b
                     46: default argument is to print line numbers and use cata mode.
                     47: hopefully at the beginning of spring 79 semester,
                     48: catb will be removed and apl users will use
                     49: cata -b  to perform the same function.
                     50: 
                     51: 
                     52: author:  a.p. reeves
                     53: 10/21/78
                     54: modified for buffered i/o and fixed 'blank line' bug
                     55: added dual link ability
                     56: added -l and -b flags.
                     57: r.l. reeves
                     58: 
                     59: 01/15/79
                     60: modified for standard i/o package
                     61: John Bruner
                     62: */
                     63: 
                     64: #include <stdio.h>
                     65: 
                     66: FILE *infile;          /* input file pointer */
                     67: 
                     68: char *buf, line[128];  /* overprint buffer for catb mode */
                     69:                        /* buf will be called newprint buffer */
                     70: 
                     71: int catb =  0;         /* catb mode if non-zero */
                     72: int lflg  = 1;         /* list line numbers if non-zero */
                     73: 
                     74: main(argc,argv)
                     75: int argc;char **argv;
                     76: {
                     77:        
                     78:        int iarg;       /* index of string being processed from argument list */
                     79:        register int frstim;    /* first time flag--on if first time */
                     80:                                /* through main printing loop */
                     81:        register char *ap;      /* point to char in flag argument */
                     82:        extern char _sobuf[];
                     83:        
                     84:        /* force buffering */
                     85:        setbuf(stdout, _sobuf);
                     86:        
                     87:        /* set up newprint buffer to have leading tab */
                     88:        *line = '\t';
                     89:        buf = &line[1];
                     90:        
                     91:        /*
                     92:        * detect which file is being run of the two
                     93:        * possible file links.
                     94:        * set catb if file 'catb' is running
                     95:        */
                     96:        
                     97:        if(argv[0][3] == 'b')
                     98:                catb++;
                     99:        
                    100:        iarg = 1;
                    101:        
                    102:        /*
                    103:        * check for flag argument
                    104:        */
                    105:        
                    106:        if(argc > 1 && *argv[1] == '-') {
                    107:                ap = argv[1];
                    108:                while(*++ap) switch(*ap) {
                    109:                        case 'b':       /* set catb mode */
                    110:                                        catb++;
                    111:                                        break;
                    112:                        case 'l':       /* no line numbers */
                    113:                                        lflg = 0;
                    114:                                        break;
                    115:                }
                    116:                iarg++;
                    117:        }
                    118:        
                    119:        
                    120:        frstim = 1;     /* first time through main loop, print std input */
                    121:                        /* in case no files given */
                    122:        
                    123:        while (argc > iarg || frstim) {
                    124:        
                    125:                if(argc > iarg){
                    126:                        /* process argument list of files */
                    127:                        close(0);
                    128:                        if(open(argv[iarg], 0) != 0){
                    129:                                printf("can't open input file: %s\n", argv[iarg]);
                    130:                                exit(1);
                    131:                        }
                    132:                }
                    133:        
                    134:                catfil();       /* print file */
                    135:        
                    136:                frstim = 0;
                    137:        
                    138:                /* advance to next file in argument list */
                    139:                putchar('\n');
                    140:                iarg++;
                    141:        
                    142:        }       /* end while */
                    143:        
                    144:        /* dump buffers and exit */
                    145:        
                    146:        exit(0);
                    147: }      /* end main */
                    148: 
                    149: 
                    150: /*
                    151: * catfil -- routine to print current file to standard output
                    152: */
                    153: 
                    154: catfil() {
                    155: 
                    156:        register int new, orig; /* index into newprint buffer */
                    157:                                /* and original input buffer */
                    158:        int lincnt;             /* line counter */
                    159:        register char c;        /* character read in from input */
                    160: 
                    161: /* copy first line in file verbatim -- this is function name */
                    162:        printf("\n\n\t");
                    163:        while((c=getchar()) != EOF && putchar(c) != '\n');
                    164:        if(c == '\0') return;
                    165: 
                    166: /*
                    167: * copy remainder of file placing line numbers in front
                    168: * of each line.
                    169: */
                    170: 
                    171:        lincnt = 1;
                    172:        while((c=getchar()) != EOF){
                    173: 
                    174:                new = orig = 0;
                    175: 
                    176:                /* print line number */
                    177:                if(lflg) printf(" [%d]\t%c",lincnt,c);
                    178:                else printf("\t%c",c);
                    179: 
                    180:                /* if not end of line, copy until then */
                    181:                if( c != '\n' ) {
                    182: 
                    183:                        while((c = getchar()) && c != '\n'){
                    184:                           if(catb) {
                    185:                                if(c != '\b'){
                    186:                                        putchar (c);
                    187:                                        orig++;
                    188:                                } else {
                    189:                                        while(new < orig)  buf[new++] = ' ';
                    190:                                        c = buf[new++] = getchar();
                    191:                                }
                    192:                           } else {
                    193:                                putchar(c);
                    194:                           }
                    195:                        }
                    196: 
                    197:                        putchar('\n');
                    198: 
                    199:                        /* perform newprint if needed */
                    200:                        if(catb && new) {
                    201:                                buf[new++] = '\n';
                    202:                                buf[new] = '\0';
                    203:                                printf(line);
                    204:                        }
                    205: 
                    206:                }
                    207: 
                    208:                if(c == '\0') return;
                    209: 
                    210:                /* increment line counter */
                    211:                lincnt++;
                    212:        }
                    213: 
                    214:        return;
                    215: 
                    216: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.