7 * block up paragraphs, possibly with indentation
10 int extraindent = 0; /* how many spaces to indent all lines */
11 int indent = 0; /* current value of indent, before extra indent */
12 int length = 70; /* how many columns per output line */
13 int join = 1; /* can lines be joined? */
18 typedef struct Word Word;
30 fprint(2, "usage: %s [-j] [-i indent] [-l length] [file...]\n", argv0);
35 main(int argc, char **argv)
42 extraindent = atoi(EARGF(usage()));
49 length = atoi(EARGF(usage()));
56 fprint(2, "%s: line length<=indentation\n", argv0);
61 if(s!=nil && atoi(s)>0)
64 Binit(&bout, 1, OWRITE);
66 Binit(&bin, 0, OREAD);
69 for(i=0; i<argc; i++){
70 f = open(argv[i], OREAD);
72 fprint(2, "%s: can't open %s: %r\n", argv0, argv[i]);
75 Binit(&bin, f, OREAD);
87 indentof(char **linep)
94 for(i=0; line[i]; i++)
108 /* plain white space doesn't change the indent */
114 addword(Word **words, int *nwordp, char *s, int l, int indent, int bol)
118 w = malloc(sizeof(Word)+l+1);
119 memmove(w->text, s, l);
123 words = realloc(words, (*nwordp+1)*sizeof(Word*));
124 words[(*nwordp)++] = w;
129 parseline(char *line, Word **words, int *nwordp)
133 ind = indentof(&line);
138 while(*line==' ' || *line=='\t')
142 return addword(words, nwordp, "", 0, -1, bol);
145 /* how long is this word? */
146 for(l=0; line[l]; l++)
147 if(line[l]==' ' || line[l]=='\t')
149 words = addword(words, nwordp, line, l, indent, bol);
169 /* give extra space if word ends with period, etc. */
178 if(isupper((uchar)s[0]) && n < 4)
180 if(strchr(".!?", s[n-1]) != nil)
187 printwords(Word **w, int nw)
189 int i, j, n, col, nsp;
191 /* one output line per loop */
193 /* if it's a blank line, print it */
194 if(w[i]->indent == -1){
196 if(++i == nw) /* out of words */
199 /* emit leading indent */
200 col = extraindent+w[i]->indent;
202 /* emit words until overflow; always emit at least one word */
204 Bprint(&bout, "%s", w[i]->text);
205 col += utflen(w[i]->text);
207 break; /* out of words */
208 if(w[i]->indent != w[i-1]->indent)
209 break; /* indent change */
210 nsp = nspaceafter(w[i-1]->text);
211 if(col+nsp+utflen(w[i]->text) > extraindent+length)
212 break; /* fold line */
213 if(!join && w[i]->bol)
216 Bputc(&bout, ' '); /* emit space; another word will follow */
233 while((s = Brdstr(&bin, '\n', 1)) != nil){
234 w = parseline(s, w, &nw);