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

1.1     ! root        1: /*% cyntax % && cc -go # %
        !             2:  * block up paragraphs, possibly with indentation
        !             3:  */
        !             4: #include <libc.h>
        !             5: #include <stdio.h>
        !             6: #include <ctype.h>
        !             7: int indent=0;          /* how many spaces to indent */
        !             8: int length=70;         /* how many columns per output line */
        !             9: main(argc, argv)
        !            10: char *argv[];
        !            11: {
        !            12:        register i;
        !            13:        register FILE *f;
        !            14:        for(i=1;i!=argc && argv[i][0]=='-';i++){
        !            15:                switch(argv[i][1]){
        !            16:                case 'i':
        !            17:                        if(argv[i][2])
        !            18:                                indent=atoi(argv[i]+2);
        !            19:                        else if(i+1<argc){
        !            20:                                indent=atoi(argv[i+1]);
        !            21:                                i++;
        !            22:                        }
        !            23:                        else
        !            24:                                goto Usage;
        !            25:                        break;
        !            26:                case 'w':
        !            27:                case 'l':
        !            28:                        if(argv[i][2])
        !            29:                                length=atoi(argv[i]+2);
        !            30:                        else if(i+1<argc){
        !            31:                                length=atoi(argv[i+1]);
        !            32:                                i++;
        !            33:                        }
        !            34:                        else
        !            35:                                goto Usage;
        !            36:                        break;
        !            37:                default:
        !            38:                Usage:
        !            39:                        fprintf(stderr,
        !            40:                                "Usage: %s [-i number] [-l number] [file ...]\n",
        !            41:                                argv[0]);
        !            42:                        exit(1);
        !            43:                }
        !            44:        }
        !            45:        if(length<=indent){
        !            46:                fprintf(stderr, "%s: line length<=indentation\n", argv[0]);
        !            47:                exit(1);
        !            48:        }
        !            49:        if(i==argc)
        !            50:                fmt(stdin);
        !            51:        else{
        !            52:                for(;i!=argc;i++){
        !            53:                        f=fopen(argv[i], "r");
        !            54:                        if(f==0)
        !            55:                                perror(argv[i]);
        !            56:                        else{
        !            57:                                fmt(f);
        !            58:                                fclose(f);
        !            59:                                if(i!=argc-1)
        !            60:                                        putchar('\n');
        !            61:                        }
        !            62:                }
        !            63:        }
        !            64:        exit(0);
        !            65: }
        !            66: fmt(f)
        !            67: register FILE *f;
        !            68: {
        !            69:        register c;
        !            70:        while((c=getc(f))!=EOF)
        !            71:                outchar(c);
        !            72:        flush();
        !            73: }
        !            74: #define TAB 8
        !            75: #define        NWORD   (TAB*32)
        !            76: char word[NWORD+1];
        !            77: char *wp=word;
        !            78: int col=0;     /* output column number */
        !            79: int bol=1;     /* at beginning of output line? */
        !            80: int punct=0;   /* last character out was punctuation? */
        !            81: int newline=1; /* last char read was newline(1) or init space(2) */
        !            82: outchar(c){
        !            83:        switch(c){
        !            84:        case '\0':
        !            85:                break;
        !            86:        case '\n':
        !            87:                switch(newline){
        !            88:                case 0:
        !            89:                        outword();
        !            90:                        break;
        !            91:                case 1:
        !            92:                        flush();
        !            93:                case 2:
        !            94:                        putchar('\n');
        !            95:                        wp=word;
        !            96:                }
        !            97:                newline=1;
        !            98:                break;
        !            99:        case ' ':
        !           100:        case '\t':
        !           101:                switch(newline) {
        !           102:                case 0:
        !           103:                        outword();
        !           104:                        break;
        !           105:                case 1:
        !           106:                        flush();
        !           107:                        newline=2;
        !           108:                case 2:
        !           109:                        do {
        !           110:                                addchar(' ');
        !           111:                        } while(c=='\t' && (wp-word)%TAB);
        !           112:                }
        !           113:                break;
        !           114:        default:
        !           115:                addchar(c);
        !           116:                newline=0;
        !           117:        }
        !           118: }
        !           119: addchar(c)
        !           120: {
        !           121:        if(wp==&word[NWORD]) {
        !           122:                if(strchr(" \t",wp[-1]))
        !           123:                        wp=word;
        !           124:                outword();
        !           125:        }
        !           126:        *wp++=c;
        !           127: }
        !           128: outword(){
        !           129:        register i;
        !           130:        if(wp==word)
        !           131:                return;
        !           132:        if(wp-word+col+(bol?0:punct?2:1)>length){
        !           133:                putchar('\n');
        !           134:                col=0;
        !           135:                bol=1;
        !           136:        }
        !           137:        if(col==0){
        !           138:                for(i=0;i+8<=indent;i+=8)
        !           139:                        putchar('\t');
        !           140:                while(i++<indent)
        !           141:                        putchar(' ');
        !           142:                col=indent;
        !           143:        }
        !           144:        if(bol)
        !           145:                bol=0;
        !           146:        else{
        !           147:                if(punct){
        !           148:                        putchar(' ');
        !           149:                        col++;
        !           150:                }
        !           151:                putchar(' ');
        !           152:                col++;
        !           153:        }
        !           154:        *wp='\0';
        !           155:        puncttest();
        !           156:        printf("%s", word);
        !           157:        col+=wp-word;
        !           158:        wp=word;
        !           159: }
        !           160: /* is the word followed by major punctuation, .?:! */
        !           161: /* disregard short things followed by periods; they are probably
        !           162:    initials or titles like Mrs. and Dr. */
        !           163: puncttest()
        !           164: {
        !           165:        char *ep;
        !           166:        punct = 0;
        !           167:        for(ep=wp; --ep>=word; ) {
        !           168:                switch(*ep) {
        !           169:                case ')': case '\'': case '"':
        !           170:                        continue;
        !           171:                case '.':
        !           172:                        if(isupper(*word)&&ep-word<=3)
        !           173:                                return;
        !           174:                case '?': case ':': case '!':
        !           175:                        punct = 1;
        !           176:                default:
        !           177:                        return;
        !           178:                }
        !           179:        }
        !           180: }
        !           181: flush(){
        !           182:        outword();
        !           183:        if(col!=0){
        !           184:                putchar('\n');
        !           185:                col=0;
        !           186:                bol=1;
        !           187:        }
        !           188: }

unix.superglobalmegacorp.com

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