fmt.c 3.8 KB

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