1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ctype.h>
/*
* block up paragraphs, possibly with indentation
*/
int extraindent = 0; /* how many spaces to indent all lines */
int indent = 0; /* current value of indent, before extra indent */
int length = 70; /* how many columns per output line */
int join = 1; /* can lines be joined? */
int maxtab = 8;
Biobuf bin;
Biobuf bout;
typedef struct Word Word;
struct Word{
int bol;
int indent;
char text[1];
};
void fmt(void);
void
usage(void)
{
fprint(2, "usage: %s [-j] [-i indent] [-l length] [file...]\n", argv0);
exits("usage");
}
void
main(int argc, char **argv)
{
int i, f;
char *s, *err;
ARGBEGIN{
case 'i':
extraindent = atoi(EARGF(usage()));
break;
case 'j':
join = 0;
break;
case 'w':
case 'l':
length = atoi(EARGF(usage()));
break;
default:
usage();
}ARGEND
if(length <= indent){
fprint(2, "%s: line length<=indentation\n", argv0);
exits("length");
}
s=getenv("tabstop");
if(s!=nil && atoi(s)>0)
maxtab=atoi(s);
err = nil;
Binit(&bout, 1, OWRITE);
if(argc <= 0){
Binit(&bin, 0, OREAD);
fmt();
}else{
for(i=0; i<argc; i++){
f = open(argv[i], OREAD);
if(f < 0){
fprint(2, "%s: can't open %s: %r\n", argv0, argv[i]);
err = "open";
}else{
Binit(&bin, f, OREAD);
fmt();
Bterm(&bin);
if(i != argc-1)
Bputc(&bout, '\n');
}
}
}
exits(err);
}
int
indentof(char **linep)
{
int i, ind;
char *line;
ind = 0;
line = *linep;
for(i=0; line[i]; i++)
switch(line[i]){
default:
*linep = line;
return ind;
case ' ':
ind++;
break;
case '\t':
ind += maxtab;
ind -= ind%maxtab;
break;
}
/* plain white space doesn't change the indent */
*linep = "";
return indent;
}
Word**
addword(Word **words, int *nwordp, char *s, int l, int indent, int bol)
{
Word *w;
w = malloc(sizeof(Word)+l+1);
memmove(w->text, s, l);
w->text[l] = '\0';
w->indent = indent;
w->bol = bol;
words = realloc(words, (*nwordp+1)*sizeof(Word*));
words[(*nwordp)++] = w;
return words;
}
Word**
parseline(char *line, Word **words, int *nwordp)
{
int ind, l, bol;
ind = indentof(&line);
indent = ind;
bol = 1;
for(;;){
/* find next word */
while(*line==' ' || *line=='\t')
line++;
if(*line == '\0'){
if(bol)
return addword(words, nwordp, "", 0, -1, bol);
break;
}
/* how long is this word? */
for(l=0; line[l]; l++)
if(line[l]==' ' || line[l]=='\t')
break;
words = addword(words, nwordp, line, l, indent, bol);
bol = 0;
line += l;
}
return words;
}
void
printindent(int w)
{
while(w >= maxtab){
Bputc(&bout, '\t');
w -= maxtab;
}
while(w > 0){
Bputc(&bout, ' ');
w--;
}
}
/* give extra space if word ends with period, etc. */
int
nspaceafter(char *s)
{
int n;
n = strlen(s);
if(n < 2)
return 1;
if(isupper((uchar)s[0]) && n < 4)
return 1;
if(strchr(".!?", s[n-1]) != nil)
return 2;
return 1;
}
void
printwords(Word **w, int nw)
{
int i, j, n, col, nsp;
/* one output line per loop */
for(i=0; i<nw; ){
/* if it's a blank line, print it */
if(w[i]->indent == -1){
Bputc(&bout, '\n');
if(++i == nw) /* out of words */
break;
}
/* emit leading indent */
col = extraindent+w[i]->indent;
printindent(col);
/* emit words until overflow; always emit at least one word */
for(n=0;; n++){
Bprint(&bout, "%s", w[i]->text);
col += utflen(w[i]->text);
if(++i == nw)
break; /* out of words */
if(w[i]->indent != w[i-1]->indent)
break; /* indent change */
nsp = nspaceafter(w[i-1]->text);
if(col+nsp+utflen(w[i]->text) > extraindent+length)
break; /* fold line */
if(!join && w[i]->bol)
break;
for(j=0; j<nsp; j++)
Bputc(&bout, ' '); /* emit space; another word will follow */
col += nsp;
}
/* emit newline */
Bputc(&bout, '\n');
}
}
void
fmt(void)
{
char *s;
int i, nw;
Word **w;
nw = 0;
w = nil;
while((s = Brdstr(&bin, '\n', 1)) != nil){
w = parseline(s, w, &nw);
free(s);
}
printwords(w, nw);
for(i=0; i<nw; i++)
free(w[i]);
free(w);
}
|