Annotation of researchv10no/cmd/asa.c, revision 1.1

1.1     ! root        1: /*
        !             2:  *     asa - interpret asa carriage control characters
        !             3:  *
        !             4:  *     This program is designed to make sense out of the output
        !             5:  *     of fortran programs whose authors have used asa carriage
        !             6:  *     control characters.  It processes either the files
        !             7:  *     whose names are given as arguments or the standard input
        !             8:  *     if no file names are given.  The first character of each
        !             9:  *     line is assumed to be a control character: the meanings
        !            10:  *     of the control characters are:
        !            11:  *
        !            12:  *     ' '     single-space before printing
        !            13:  *     '0'     double-space before printing
        !            14:  *     '-'     triple-space before printing
        !            15:  *     '1'     new page before printing
        !            16:  *     '+'     do not space at all before printing
        !            17:  *
        !            18:  *     A line beginning with '+' will overprint the previous line.
        !            19:  *
        !            20:  *     Lines beginning with other than the above characters are
        !            21:  *     treated as if they began with ' '; if any such lines appear,
        !            22:  *     an appropriate diagnostic will appear on the standard error
        !            23:  *     file before termination.  The return code will be zero unless
        !            24:  *     one or more input files could not be opened.
        !            25:  *
        !            26:  *     The program forces the first line of each input file to
        !            27:  *     start on a new page.
        !            28:  */
        !            29: 
        !            30: #include <stdio.h>
        !            31: typedef char bool;
        !            32: #define true 1
        !            33: #define false 0
        !            34: 
        !            35: /* program name, for diagnostics */
        !            36: char *pgmname;
        !            37: 
        !            38: /* count of lines with bad control characters */
        !            39: long badlines = 0;
        !            40: 
        !            41: /* buffer for the standard output */
        !            42: char stdbuf[BUFSIZ];
        !            43: 
        !            44: /* number of errors detected */
        !            45: int retcode = 0;
        !            46: 
        !            47: main (c, v)
        !            48:        int c;
        !            49:        char **v;
        !            50: {
        !            51:        setbuf (stdout, stdbuf);
        !            52:        pgmname = v[0];
        !            53: 
        !            54:        /* were any files given, or do we process stdin? */
        !            55:        if (c <= 1)
        !            56: 
        !            57:                /* process standard input */
        !            58:                dofile ("standard input");
        !            59:        else {
        !            60:                register int i;
        !            61: 
        !            62:                /* one iteration per input file */
        !            63:                for (i = 1; i < c; i++) {
        !            64:                        if (freopen (v[i], "r", stdin) == NULL) {
        !            65:                                fprintf (stderr, "%s: cannot open %s\n",
        !            66:                                        pgmname, v[i]);
        !            67:                                retcode++;
        !            68:                        } else
        !            69:                                dofile (v[i]);
        !            70:                }
        !            71:        }
        !            72: 
        !            73:        /* report invalid input lines -- dofile increments badlines */
        !            74:        if (badlines)
        !            75:                fprintf (stderr, "%s: %ld invalid input line%s\n",
        !            76:                        pgmname, badlines, badlines>1? "s": "");
        !            77: 
        !            78:        if (ferror (stdout)) {
        !            79:                fprintf (stderr, "%s: output error\n", pgmname);
        !            80:                retcode++;
        !            81:        }
        !            82: 
        !            83:        return retcode;
        !            84: }
        !            85: 
        !            86: /*
        !            87:  *     dofile - process the standard input.
        !            88:  *
        !            89:  *     This program is called once for each input file, with stdin
        !            90:  *     redirected to the file.  The "fname" argument is used
        !            91:  *     for writing diagnostic messages only.
        !            92:  */
        !            93: 
        !            94: /* true only when the first output character has not been written */
        !            95: bool firstout = true;
        !            96: 
        !            97: dofile (fname)
        !            98:        char *fname;
        !            99: {
        !           100:        register int c;
        !           101: 
        !           102:        /* true if we are about to read the first character in a line */
        !           103:        bool firstchar = true;
        !           104: 
        !           105:        /* true if we are about to write the first line for the file */
        !           106:        bool firstline = true;
        !           107: 
        !           108:        while ((c = getchar()) != EOF) {
        !           109: 
        !           110:                /* separate input files by formfeeds */
        !           111:                if (firstline && !firstout)
        !           112:                                putchar ('\f');
        !           113: 
        !           114:                if (firstchar) {
        !           115:                        switch (c) {
        !           116: 
        !           117:                        /* new page */
        !           118:                        case '1':
        !           119:                                if (!firstline)
        !           120:                                        putchar ('\f');
        !           121:                                break;
        !           122: 
        !           123:                        /* triple space */
        !           124:                        case '-':
        !           125:                                if (!firstline)
        !           126:                                        putchar ('\n');
        !           127:                                putchar ('\n');
        !           128:                                putchar ('\n');
        !           129:                                break;
        !           130: 
        !           131:                        /* double space */
        !           132:                        case '0':
        !           133:                                if (!firstline)
        !           134:                                        putchar ('\n');
        !           135:                                putchar ('\n');
        !           136:                                break;
        !           137: 
        !           138:                        /* single space and invalid characters */
        !           139:                        default:
        !           140:                                badlines++;
        !           141:                        /* no break */
        !           142:                        case '\n':
        !           143:                        case ' ':
        !           144:                                if (!firstline)
        !           145:                                        putchar ('\n');
        !           146:                                break;
        !           147: 
        !           148:                        /* no space at all */
        !           149:                        case '+':
        !           150:                                if (!firstline)
        !           151:                                        putchar ('\r');
        !           152:                                break;
        !           153:                        }
        !           154:                        firstline = false;
        !           155:                        firstout = false;
        !           156:                        if (c != '\n')
        !           157:                                firstchar = false;
        !           158:                } else {
        !           159:                        /* not the first character of an input line */
        !           160:                        if (c == '\n')
        !           161:                                firstchar = true;
        !           162:                        else
        !           163:                                putchar (c);
        !           164:                }
        !           165:        }
        !           166: 
        !           167:        /* end the file with a newline if any output appeared */
        !           168:        if (!firstline)
        !           169:                putchar ('\n');
        !           170: 
        !           171:        if (ferror (stdin)) {
        !           172:                fprintf (stderr, "%s: input error on %s\n", pgmname, fname);
        !           173:                retcode++;
        !           174:        }
        !           175: }

unix.superglobalmegacorp.com

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