2011-03-30 12:46:40 +00:00
|
|
|
#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;
|
2012-10-23 00:08:35 +00:00
|
|
|
|
2011-03-30 12:46:40 +00:00
|
|
|
Biobuf bin;
|
|
|
|
Biobuf bout;
|
|
|
|
|
|
|
|
typedef struct Word Word;
|
2012-10-23 00:08:35 +00:00
|
|
|
struct Word
|
|
|
|
{
|
|
|
|
Word *next;
|
|
|
|
|
2011-03-30 12:46:40 +00:00
|
|
|
int indent;
|
2012-10-23 00:08:35 +00:00
|
|
|
int length;
|
|
|
|
char bol;
|
|
|
|
char text[];
|
2011-03-30 12:46:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
2012-10-23 00:08:35 +00:00
|
|
|
indentof(char *s)
|
2011-03-30 12:46:40 +00:00
|
|
|
{
|
2012-10-23 00:08:35 +00:00
|
|
|
int ind;
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
ind = 0;
|
2012-10-23 00:08:35 +00:00
|
|
|
for(; *s != '\0'; s++)
|
|
|
|
switch(*s){
|
2011-03-30 12:46:40 +00:00
|
|
|
default:
|
|
|
|
return ind;
|
|
|
|
case ' ':
|
|
|
|
ind++;
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
ind += maxtab;
|
|
|
|
ind -= ind%maxtab;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-23 00:08:35 +00:00
|
|
|
|
2011-03-30 12:46:40 +00:00
|
|
|
/* plain white space doesn't change the indent */
|
|
|
|
return indent;
|
|
|
|
}
|
|
|
|
|
2012-10-23 00:08:35 +00:00
|
|
|
Word*
|
|
|
|
newword(char *s, int n, int ind, int bol)
|
2011-03-30 12:46:40 +00:00
|
|
|
{
|
|
|
|
Word *w;
|
|
|
|
|
2012-10-23 00:08:35 +00:00
|
|
|
w = malloc(sizeof(Word) + n+1);
|
|
|
|
w->next = nil;
|
|
|
|
w->indent = ind;
|
2011-03-30 12:46:40 +00:00
|
|
|
w->bol = bol;
|
2012-10-23 00:08:35 +00:00
|
|
|
memmove(w->text, s, n);
|
|
|
|
w->text[n] = 0;
|
|
|
|
w->length = utflen(w->text);
|
|
|
|
return w;
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
|
|
|
|
2012-10-23 00:08:35 +00:00
|
|
|
Word*
|
|
|
|
getword(void)
|
2011-03-30 12:46:40 +00:00
|
|
|
{
|
2012-10-23 00:08:35 +00:00
|
|
|
static Word *head, *tail;
|
|
|
|
char *line, *s;
|
|
|
|
Word *w;
|
|
|
|
|
|
|
|
w = head;
|
|
|
|
if(w != nil){
|
|
|
|
head = w->next;
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
line = Brdstr(&bin, '\n', 1);
|
|
|
|
if(line == nil)
|
|
|
|
return nil;
|
|
|
|
tail = nil;
|
|
|
|
indent = indentof(line);
|
2011-03-30 12:46:40 +00:00
|
|
|
for(;;){
|
2012-10-23 00:08:35 +00:00
|
|
|
while(*line == ' ' || *line == '\t')
|
2011-03-30 12:46:40 +00:00
|
|
|
line++;
|
|
|
|
if(*line == '\0'){
|
2012-10-23 00:08:35 +00:00
|
|
|
if(head == nil)
|
|
|
|
return newword("", 0, -1, 1);
|
2011-03-30 12:46:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* how long is this word? */
|
2012-10-23 00:08:35 +00:00
|
|
|
for(s=line++; *line != '\0'; line++)
|
|
|
|
if(*line==' ' || *line=='\t')
|
2011-03-30 12:46:40 +00:00
|
|
|
break;
|
2012-10-23 00:08:35 +00:00
|
|
|
w = newword(s, line-s, indent, head==nil);
|
|
|
|
if(head == nil)
|
|
|
|
head = w;
|
|
|
|
else
|
|
|
|
tail->next = w;
|
|
|
|
tail = w;
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
2012-10-23 00:08:35 +00:00
|
|
|
w = head;
|
|
|
|
head = w->next;
|
|
|
|
return w;
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(s[0]) && n < 4)
|
|
|
|
return 1;
|
|
|
|
if(strchr(".!?", s[n-1]) != nil)
|
|
|
|
return 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-23 00:08:35 +00:00
|
|
|
fmt(void)
|
2011-03-30 12:46:40 +00:00
|
|
|
{
|
2012-10-23 00:08:35 +00:00
|
|
|
Word *w, *o;
|
|
|
|
int col, nsp;
|
2011-03-30 12:46:40 +00:00
|
|
|
|
2012-10-23 00:08:35 +00:00
|
|
|
w = getword();
|
|
|
|
while(w != nil){
|
|
|
|
if(w->indent == -1){
|
2011-03-30 12:46:40 +00:00
|
|
|
Bputc(&bout, '\n');
|
2012-10-23 00:08:35 +00:00
|
|
|
free(w);
|
|
|
|
w = getword();
|
|
|
|
if(w == nil)
|
2011-03-30 12:46:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-10-23 00:08:35 +00:00
|
|
|
col = w->indent;
|
|
|
|
printindent(extraindent+col);
|
2011-03-30 12:46:40 +00:00
|
|
|
/* emit words until overflow; always emit at least one word */
|
2012-10-23 00:08:35 +00:00
|
|
|
for(;;){
|
|
|
|
Bprint(&bout, "%s", w->text);
|
|
|
|
col += w->length;
|
|
|
|
o = w;
|
|
|
|
w = getword();
|
|
|
|
if(w == nil)
|
|
|
|
break;
|
|
|
|
if(w->indent != o->indent)
|
2011-03-30 12:46:40 +00:00
|
|
|
break; /* indent change */
|
2012-10-23 00:08:35 +00:00
|
|
|
nsp = nspaceafter(o->text);
|
|
|
|
if(col+nsp+w->length > length)
|
2011-03-30 12:46:40 +00:00
|
|
|
break; /* fold line */
|
2012-10-23 00:08:35 +00:00
|
|
|
if(!join && w->bol)
|
2011-03-30 12:46:40 +00:00
|
|
|
break;
|
2012-10-23 00:08:35 +00:00
|
|
|
while(--nsp >= 0){
|
2011-03-30 12:46:40 +00:00
|
|
|
Bputc(&bout, ' '); /* emit space; another word will follow */
|
2012-10-23 00:08:35 +00:00
|
|
|
col++;
|
|
|
|
}
|
|
|
|
free(o);
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
2012-10-23 00:08:35 +00:00
|
|
|
free(o);
|
2011-03-30 12:46:40 +00:00
|
|
|
Bputc(&bout, '\n');
|
|
|
|
}
|
|
|
|
}
|