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