fmt.c 4.4 KB

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