mc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. /*
  10. * mc - columnate
  11. *
  12. * mc[-][-LINEWIDTH][-t][file...]
  13. * - causes break on colon
  14. * -LINEWIDTH sets width of line in which to columnate(default 80)
  15. * -t suppresses expanding multiple blanks into tabs
  16. *
  17. */
  18. #include <u.h>
  19. #include <libc.h>
  20. #include <draw.h>
  21. #include <bio.h>
  22. #define WIDTH 80
  23. #define TAB 4
  24. #define WORD_ALLOC_QUANTA 1024
  25. #define ALLOC_QUANTA 4096
  26. int linewidth=WIDTH;
  27. int mintab=1;
  28. int colonflag=0;
  29. int tabflag=0; /* -t flag turned off forever */
  30. Rune *cbuf, *cbufp;
  31. Rune **word;
  32. int maxwidth=0;
  33. int nalloc=ALLOC_QUANTA;
  34. int nwalloc=WORD_ALLOC_QUANTA;
  35. int nchars=0;
  36. int nwords=0;
  37. int tabwidth=0;
  38. Font *font;
  39. Biobuf bin;
  40. Biobuf bout;
  41. void getwidth(void), readbuf(int), error(char *);
  42. void scanwords(void), columnate(void), morechars(void);
  43. int wordwidth(Rune*, int);
  44. int nexttab(int);
  45. void
  46. main(int argc, char *argv[])
  47. {
  48. int i;
  49. int lineset;
  50. int ifd;
  51. lineset = 0;
  52. Binit(&bout, 1, OWRITE);
  53. while(argc > 1 && argv[1][0] == '-'){
  54. --argc; argv++;
  55. switch(argv[0][1]){
  56. case '\0':
  57. colonflag = 1;
  58. break;
  59. case 't':
  60. tabflag = 0;
  61. break;
  62. default:
  63. linewidth = atoi(&argv[0][1]);
  64. if(linewidth <= 1)
  65. linewidth = WIDTH;
  66. lineset = 1;
  67. break;
  68. }
  69. }
  70. if(lineset == 0){
  71. getwidth();
  72. if(linewidth <= 1){
  73. linewidth = WIDTH;
  74. font = nil;
  75. }
  76. }
  77. cbuf = cbufp = malloc(ALLOC_QUANTA*(sizeof *cbuf));
  78. word = malloc(WORD_ALLOC_QUANTA*(sizeof *word));
  79. if(word == 0 || cbuf == 0)
  80. error("out of memory");
  81. if(argc == 1)
  82. readbuf(0);
  83. else{
  84. for(i = 1; i < argc; i++){
  85. if((ifd = open(*++argv, OREAD)) == -1)
  86. fprint(2, "mc: can't open %s (%r)\n", *argv);
  87. else{
  88. readbuf(ifd);
  89. Bflush(&bin);
  90. close(ifd);
  91. }
  92. }
  93. }
  94. columnate();
  95. exits(0);
  96. }
  97. void
  98. error(char *s)
  99. {
  100. fprint(2, "mc: %s\n", s);
  101. exits(s);
  102. }
  103. void
  104. readbuf(int fd)
  105. {
  106. int lastwascolon = 0;
  107. int32_t c;
  108. int linesiz = 0;
  109. Binit(&bin, fd, OREAD);
  110. do{
  111. if(nchars++ >= nalloc)
  112. morechars();
  113. *cbufp++ = c = Bgetrune(&bin);
  114. linesiz++;
  115. if(c == '\t') {
  116. cbufp[-1] = L' ';
  117. while(linesiz%TAB != 0) {
  118. if(nchars++ >= nalloc)
  119. morechars();
  120. *cbufp++ = L' ';
  121. linesiz++;
  122. }
  123. }
  124. if(colonflag && c == ':')
  125. lastwascolon++;
  126. else if(lastwascolon){
  127. if(c == '\n'){
  128. --nchars; /* skip newline */
  129. *cbufp = L'\0';
  130. while(nchars > 0 && cbuf[--nchars] != '\n')
  131. ;
  132. if(nchars)
  133. nchars++;
  134. columnate();
  135. if (nchars)
  136. Bputc(&bout, '\n');
  137. Bprint(&bout, "%S", cbuf+nchars);
  138. nchars = 0;
  139. cbufp = cbuf;
  140. }
  141. lastwascolon = 0;
  142. }
  143. if(c == '\n')
  144. linesiz = 0;
  145. }while(c >= 0);
  146. }
  147. void
  148. scanwords(void)
  149. {
  150. Rune *p, *q;
  151. int i, w;
  152. nwords=0;
  153. maxwidth=0;
  154. for(p = q = cbuf, i = 0; i < nchars; i++){
  155. if(*p++ == L'\n'){
  156. if(nwords >= nwalloc){
  157. nwalloc += WORD_ALLOC_QUANTA;
  158. if((word = realloc(word, nwalloc*sizeof(*word)))==0)
  159. error("out of memory");
  160. }
  161. word[nwords++] = q;
  162. p[-1] = L'\0';
  163. w = wordwidth(q, p-q-1);
  164. if(w > maxwidth)
  165. maxwidth = w;
  166. q = p;
  167. }
  168. }
  169. }
  170. void
  171. columnate(void)
  172. {
  173. int i, j;
  174. int words_per_line;
  175. int nlines;
  176. int col;
  177. int endcol;
  178. scanwords();
  179. if(nwords==0)
  180. return;
  181. maxwidth = nexttab(maxwidth+mintab-1);
  182. words_per_line = linewidth/maxwidth;
  183. if(words_per_line <= 0)
  184. words_per_line = 1;
  185. nlines=(nwords+words_per_line-1)/words_per_line;
  186. for(i = 0; i < nlines; i++){
  187. col = endcol = 0;
  188. for(j = i; j < nwords; j += nlines){
  189. endcol += maxwidth;
  190. Bprint(&bout, "%S", word[j]);
  191. col += wordwidth(word[j], runestrlen(word[j]));
  192. if(j+nlines < nwords){
  193. if(tabflag) {
  194. while(col < endcol){
  195. Bputc(&bout, '\t');
  196. col = nexttab(col);
  197. }
  198. }else{
  199. while(col < endcol){
  200. Bputc(&bout, ' ');
  201. col++;
  202. }
  203. }
  204. }
  205. }
  206. Bputc(&bout, '\n');
  207. }
  208. }
  209. int
  210. wordwidth(Rune *w, int nw)
  211. {
  212. if(font)
  213. return runestringnwidth(font, w, nw);
  214. return nw;
  215. }
  216. int
  217. nexttab(int col)
  218. {
  219. if(tabwidth){
  220. col += tabwidth;
  221. col -= col%tabwidth;
  222. return col;
  223. }
  224. return col+1;
  225. }
  226. void
  227. morechars(void)
  228. {
  229. nalloc += ALLOC_QUANTA;
  230. if((cbuf = realloc(cbuf, nalloc*sizeof(*cbuf))) == 0)
  231. error("out of memory");
  232. cbufp = cbuf+nchars-1;
  233. }
  234. /*
  235. * These routines discover the width of the display.
  236. * It takes some work. If we do the easy calls to the
  237. * draw library, the screen flashes due to repainting
  238. * when mc exits.
  239. */
  240. jmp_buf drawjmp;
  241. void
  242. terror(Display *d, char *c)
  243. {
  244. longjmp(drawjmp, 1);
  245. }
  246. void
  247. getwidth(void)
  248. {
  249. int n, fd;
  250. char buf[128], *f[10], *p;
  251. if(access("/dev/acme", OREAD) >= 0){
  252. if((fd = open("/dev/acme/ctl", OREAD)) < 0)
  253. return;
  254. n = read(fd, buf, sizeof buf-1);
  255. close(fd);
  256. if(n <= 0)
  257. return;
  258. buf[n] = 0;
  259. n = tokenize(buf, f, nelem(f));
  260. if(n < 7)
  261. return;
  262. if((font = openfont(nil, f[6])) == nil)
  263. return;
  264. if(n >= 8)
  265. tabwidth = atoi(f[7]);
  266. else
  267. tabwidth = 4*stringwidth(font, "0");
  268. mintab = stringwidth(font, "0");
  269. linewidth = atoi(f[5]);
  270. tabflag = 1;
  271. return;
  272. }
  273. if((p = getenv("font")) == nil)
  274. return;
  275. if((font = openfont(nil, p)) == nil)
  276. return;
  277. if((fd = open("/dev/window", OREAD)) < 0){
  278. font = nil;
  279. return;
  280. }
  281. n = read(fd, buf, 5*12);
  282. close(fd);
  283. if(n < 5*12){
  284. font = nil;
  285. return;
  286. }
  287. buf[n] = 0;
  288. /* window stucture:
  289. 4 bit left edge
  290. 1 bit gap
  291. 12 bit scrollbar
  292. 4 bit gap
  293. text
  294. 4 bit right edge
  295. */
  296. linewidth = atoi(buf+3*12) - atoi(buf+1*12) - (4+1+12+4+4);
  297. mintab = stringwidth(font, "0");
  298. if((p = getenv("tabstop")) != nil)
  299. tabwidth = atoi(p)*stringwidth(font, "0");
  300. if(tabwidth == 0)
  301. tabwidth = 4*stringwidth(font, "0");
  302. tabflag = 1;
  303. }