home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1798 < prev    next >
Internet Message Format  |  1990-12-28  |  21KB

  1. From: smidt@fy.chalmers.se (Peter Smidt)
  2. Newsgroups: alt.sources
  3. Subject: mfold - Maaniker's fold and column making
  4. Message-ID: <1990Sep9.164538.6621@fy.chalmers.se>
  5. Date: 9 Sep 90 16:45:38 GMT
  6.  
  7. Submitted-by: smidt@cd.chalmers.se
  8. Archive-name: mfold.shar/part01 (one part)
  9.  
  10. ---- Cut Here and unpack ----
  11. #!/bin/sh
  12. # This is mfold.shar, a shell archive (shar 3.21)
  13. # made 09/09/1990 14:40 UTC by smidt@sponsz
  14. # Source directory /nfs/alcazar/u/smidt/src/mfold/exp
  15. #
  16. # existing files will NOT be overwritten
  17. #
  18. # This shar contains:
  19. # length  mode       name
  20. # ------ ---------- ------------------------------------------
  21. #    351 -rw-r--r-- README
  22. #     94 -rw-r--r-- makefile
  23. #   3621 -rw-r--r-- mfold.1
  24. #  13825 -rw-r--r-- mfold.c
  25. #
  26. if touch 2>&1 | fgrep '[-amc]' > /dev/null
  27.  then TOUCH=touch
  28.  else TOUCH=true
  29. fi
  30. # ============= README ==============
  31. if test X"$1" != X"-c" -a -f 'README'; then
  32.     echo "File already exists: skipping 'README'"
  33. else
  34. echo "x - extracting README (Text)"
  35. sed 's/^Y//' << 'XXX' > README &&
  36. YThe program mfold is a simpel folding and column making program.
  37. Yunpack the files from the shar file put them in a empty
  38. Ydirectory, type 'make' and you'll get the executable. A manual
  39. Yis included in the shar file.
  40. Y
  41. YBug reports, ideas, or patches could be sent to;
  42. YPeter Smidt   smidt@cd.chalmers.se    or
  43. YPeter Smidt   smidt@fy.chalmers.se
  44. Y
  45. Y/Maaniker
  46. XXX
  47. $TOUCH -am 0909162990 README &&
  48. chmod 0644 README ||
  49. echo "restore of README failed"
  50. set `wc -c README`;Wc_c=$1
  51. if test "$Wc_c" != "351"; then
  52.     echo original size 351, current size $Wc_c
  53. fi
  54. fi
  55. # ============= makefile ==============
  56. if test X"$1" != X"-c" -a -f 'makefile'; then
  57.     echo "File already exists: skipping 'makefile'"
  58. else
  59. echo "x - extracting makefile (Text)"
  60. sed 's/^Y//' << 'XXX' > makefile &&
  61. Ynyfold: nyfold.o
  62. Y    cc nyfold.o -o mfold
  63. Y    rm -f nyfold.o
  64. Y
  65. Ynyfold.o: nyfold.c
  66. Y    cc -c nyfold.c -g
  67. XXX
  68. $TOUCH -am 0909162690 makefile &&
  69. chmod 0644 makefile ||
  70. echo "restore of makefile failed"
  71. set `wc -c makefile`;Wc_c=$1
  72. if test "$Wc_c" != "94"; then
  73.     echo original size 94, current size $Wc_c
  74. fi
  75. fi
  76. # ============= mfold.1 ==============
  77. if test X"$1" != X"-c" -a -f 'mfold.1'; then
  78.     echo "File already exists: skipping 'mfold.1'"
  79. else
  80. echo "x - extracting mfold.1 (Text)"
  81. sed 's/^Y//' << 'XXX' > mfold.1 &&
  82. Y.\" Shareware 1990 of Chalmers Computer Society.
  83. Y.\"
  84. Y.\"    @(#)mfold.1    (G|teborg) 1990-09-07
  85. Y.\"
  86. Y.TH MFOLD 1 "September 7, 1990"
  87. Y.UC
  88. Y.SH NAME
  89. Ymfold \- Maaniker's fold and column making
  90. Y.SH SYNOPSIS
  91. Y.B mfold
  92. Y[
  93. Y.B \-sn -ln -en -wn -f -d
  94. Y.B -rn
  95. Y.B -tn -cn -pn -n -L -Bn
  96. Y.B -D '<text>' -i '<text>'
  97. Y]
  98. Y.SH DESCRIPTION
  99. Y.I Mfold
  100. Yreads text files from standard input and writes to standard
  101. Youtput.
  102. Y.I Mfold
  103. Yconcatenates and folds the text in one or more columns
  104. Ywithout breaking in the middle of a word. Tabs and newlines are
  105. Yconverted to blank space. Multiple blank space is substituted with
  106. Yone single blank space. Some flags don't do anything if they aren't
  107. Yused together with either '-c' or '-d'.
  108. Y.PP
  109. YThere is a limited amount of options:
  110. Y.TP
  111. Y.B \-sn
  112. YStart line for
  113. Y.I mfold,
  114. Ywhere n is the line number. The preceding text
  115. Yis passed through
  116. Y.I mfold
  117. Ywithout being changed. If an end-of-file character is encountered
  118. Ybefore line n,
  119. Y.I mfold
  120. Ywill end the execution.
  121. Y.TP
  122. Y.B \-i
  123. YInsert text at the beginning of every line, or with '\-c', at every
  124. Ycolumn. This option as the last flag doesn't need any argument if
  125. Ya null argument is wanted. A space is required between the flag
  126. Yand the argument. Tabs may be present in the insert text. Default
  127. Yinsert text is a tab.
  128. Y.TP
  129. Y.B \-ln
  130. YLenght for output lines, excluding any insert text and excluding
  131. Yany delimiter text. Where n is the number of characters. Default
  132. Ylength is 76.
  133. Y.TP
  134. Y.B \-en
  135. Y.I Mfold
  136. Ywill end the folding at input line n. Text coming after this
  137. Yline is not being changed by
  138. Y.I mfold.
  139. Y.TP
  140. Y.B \-wn
  141. YSpecify the width n, of the not folded lines to avoid overrunning the
  142. Yinternal reverse buffer when using the '-d' flag. '-w' is not always
  143. Yprovided. Run
  144. Y.I mfold,
  145. Ylook at the result and decide if you want to specify the full
  146. Ywidth. Default is 80 characters.
  147. Y.TP
  148. Y.B \-f
  149. YFill each line with blank space to its full line length.
  150. Y.TP
  151. Y.B \-rn
  152. YFills the lines with extra blank space up to an amount of
  153. Yn blank spaces, to get an even right margin. The words will get
  154. Ya little random placement on the lines.
  155. Y.TP
  156. Y.B \-d
  157. YReverse the text for dyslexics.
  158. Y.TP
  159. Y.B \-tn
  160. YExpanding the tabs to spaces. Where n is the number of
  161. Yspaces. Default is four spaces. Use only with '-d'.
  162. Y.TP
  163. Y.B \-cn
  164. YSpecifies columns where n is the number of columns. Text is inserted at
  165. Yevery column.
  166. Y.TP
  167. Y.B \-pn
  168. YSpecifies page length n, meaningfull when used with the '-c'
  169. Yflag. Default are 40 lines per page.
  170. Y.TP
  171. Y.B \-n
  172. YMany newlines in a row is not substituted with one single 
  173. Yblank space, which is otherwise the default. Instead they are
  174. Yleft without change, but may be put in a separate column.
  175. Y.TP
  176. Y.B \-L
  177. YWrite a ^L (newpage) at the end of each page. Use with '-c'.
  178. Y.TP
  179. Y.B \-Bn
  180. YWrite n newlines at the bottom (end) of the page. Default is
  181. Yone newline. Use when the number of columns are greater then one.
  182. Y.TP
  183. Y.B \-D
  184. YSpecify column delimiter. Default is three blank spaces. Tabs may be
  185. Ypresent in the delimiter. This option as the last flag doesn't need
  186. Yany argument if a null argument is wanted.
  187. Y.SH ERRORS
  188. YYou will notice them, when you give bad flags.
  189. Y.SH AUTHOR
  190. YPeter Smidt, Chalmers Computer Society.
  191. Y.SH SEE ALSO
  192. Yawk(1), sed(1), lex(1), nroff(1), fmt(1), fold(1), rev(1)
  193. Y.SH BUGS
  194. YIf underlining is present it will get messed up with the
  195. Ytext. The '-c' and the '-d' flags
  196. Y.I may
  197. Yproduce some extra
  198. Yunnecessary blank space at the end of the lines. When
  199. Ya word is longer than the line length, the word may be
  200. Ycut at an inappropriate place. Everything except blank space,
  201. Ynewline and tab counts as parts of words.
  202. Y.SH BUG REPORTS TO
  203. YPeter Smidt  smidt@cd.chalmers.se    or
  204. Y.br
  205. YPeter Smidt  smidt@fy.chalmers.se
  206. XXX
  207. $TOUCH -am 0909162690 mfold.1 &&
  208. chmod 0644 mfold.1 ||
  209. echo "restore of mfold.1 failed"
  210. set `wc -c mfold.1`;Wc_c=$1
  211. if test "$Wc_c" != "3621"; then
  212.     echo original size 3621, current size $Wc_c
  213. fi
  214. fi
  215. # ============= mfold.c ==============
  216. if test X"$1" != X"-c" -a -f 'mfold.c'; then
  217.     echo "File already exists: skipping 'mfold.c'"
  218. else
  219. echo "x - extracting mfold.c (Text)"
  220. sed 's/^Y//' << 'XXX' > mfold.c &&
  221. Y#include <stdio.h>
  222. Y#include <curses.h>
  223. Y#include <strings.h>
  224. Y
  225. Y#define DEF_LINE_LEN    76
  226. Y#define DEF_WORD_LEN    40
  227. Y#define DEF_TAB_LEN        4
  228. Y#define FULL_WIDTH        80
  229. Y#define PAGE_LEN        40
  230. Y#define POS1            1
  231. Y#define DO_ALL1 \
  232. Y    col_pos = POS1;\
  233. Y    for ( doo = 0; doo < ins_len; doo++ ) {\
  234. Y        putcolu(insert[doo]);\
  235. Y    }
  236. Y#define BOT_LINES        1
  237. Y#define STR                15
  238. Y#define TEST_STR        25
  239. Y#define GET_NUM_STR        25
  240. Y
  241. Yint colu_chars, colu_lines;
  242. Yint page_len = PAGE_LEN, columns = 1, lin = 0, bot_lines = BOT_LINES;
  243. Yint got_newpage = FALSE, got_dyslexi = FALSE, full_line = FULL_WIDTH;
  244. Yint tab_len = DEF_TAB_LEN, ins_tabs = 0, del_len, got_fill = FALSE;
  245. Yint got_mrandom = FALSE, got_bot = FALSE, ins_len, mrandom = 1;
  246. Ychar **cur_page, *malloc(), *delim = "   ", **glob_argv;
  247. Y
  248. Ymain(argc, argv)
  249. Ychar *argv[];
  250. Yint argc;
  251. Y{
  252. Y    int col_pos = 1, j = 0, k, in, doo, line_len = DEF_LINE_LEN, row_count = 1;
  253. Y    int cnt_nwl = 0, new_wo_le, dummy = TRUE, end = 2;
  254. Y    int width = FULL_WIDTH, err = FALSE, in_2 = 0, in_3 = 0, tmp_chars = 0;
  255. Y    char *cur_word, *insert = "\t", *s = "Bad option '  '";
  256. Y    int index, start = 1, word_len = DEF_WORD_LEN;
  257. Y    int got_start = FALSE, got_line_len = FALSE, got_insert = FALSE;
  258. Y    int got_tab_len = FALSE, got_end = FALSE, got_width = FALSE;
  259. Y    int got_columns = FALSE, got_page_len = FALSE;
  260. Y    int got_newlines = FALSE, got_delim = FALSE;
  261. Y    int index_start = 0, index_insert = 0, index_line_len = 0;
  262. Y    int index_tab_len = 0, index_end = 0, index_width = 0;
  263. Y    int index_columns = 0, index_page_len = 0, index_delim = 0;
  264. Y    int index_bot = 0, index_mrandom = 0;
  265. Y    glob_argv = argv;
  266. Y    for ( index = 1; index < argc; index++) {
  267. Y        if ( argv[index][0] != '-' ) {
  268. Y            if ( index != 1 ) {
  269. Y                if ( argv[index - 1][0] == '-' ) {
  270. Y                    if ( argv[index - 1][1] == 'i' ) {
  271. Y                        index_insert = index;
  272. Y                    } else if ( argv[index - 1][1] == 'D' ) {
  273. Y                        index_delim = index;
  274. Y                    } else {
  275. Y                        err = TRUE;
  276. Y                    }
  277. Y                } else {
  278. Y                    err = TRUE;
  279. Y                }
  280. Y            } else {
  281. Y                err = TRUE;
  282. Y            }
  283. Y            if ( err ) {
  284. Y                fprintf(stderr, "Flag '%s' not allowed.\n", argv[index]);
  285. Y                usage(11);
  286. Y            }
  287. Y        }
  288. Y        doo = index_insert != index && index_delim != index;
  289. Y        switch ( argv[index][1] ) {
  290. Y            case 'i':
  291. Y                check_it(&got_insert, "-i", 8, doo, &index_insert, index + 1);
  292. Y                break;
  293. Y            case 's':
  294. Y                check_it(&got_start, "-s", 9, doo, &index_start, index);
  295. Y                break;
  296. Y            case 'l':
  297. Y                check_it(&got_line_len, "-l", 10, doo, &index_line_len, index);
  298. Y                break;
  299. Y            case 'e':
  300. Y                check_it(&got_end, "-e", 10, doo, &index_end, index);
  301. Y                break;
  302. Y            case 'w':
  303. Y                check_it(&got_width, "-w", 10, doo, &index_width, index);
  304. Y                break;
  305. Y            case 'r':
  306. Y                check_it(&got_mrandom, "-r", 37, doo, &index_mrandom, index);
  307. Y                break;
  308. Y            case 'd':
  309. Y                check_it(&got_dyslexi, "-d", 11, doo, &dummy, index);
  310. Y                break;
  311. Y            case 't':
  312. Y                check_it(&got_tab_len, "-t", 13, doo, &index_tab_len, index);
  313. Y                break;
  314. Y            case 'c':
  315. Y                check_it(&got_columns, "-c", 15, doo, &index_columns, index);
  316. Y                break;
  317. Y            case 'p':
  318. Y                check_it(&got_page_len, "-p", 16, doo, &index_page_len, index);
  319. Y                break;
  320. Y            case 'B':
  321. Y                check_it(&got_bot, "-B", 17, doo, &index_bot, index);
  322. Y                break;
  323. Y            case 'f':
  324. Y                check_it(&got_fill, "-f", 33, doo, &dummy, index);
  325. Y                break;
  326. Y            case 'n':
  327. Y                check_it(&got_newlines, "-n", 18, doo, &dummy, index);
  328. Y                break;
  329. Y            case 'L':
  330. Y                check_it(&got_newpage, "-L", 19, doo, &dummy, index);
  331. Y                break;
  332. Y            case 'D':
  333. Y                check_it(&got_delim, "-D", 36, doo, &index_delim, index + 1);
  334. Y                break;
  335. Y            case '\0':
  336. Y                write_err(doo, "Empty flag '-'", 31);
  337. Y                break;
  338. Y            default:
  339. Y                s[12] = argv[index][0];
  340. Y                s[13] = argv[index][1];
  341. Y                write_err(doo, s, 20);
  342. Y                break;
  343. Y        }
  344. Y    }
  345. Y    ext_num(got_start, &start, argv[index_start],
  346. Y    "Line zero, for start, not allowed", 21);
  347. Y    ext_num(got_page_len, &page_len, argv[index_page_len],
  348. Y    "Page length zero, not allowed", 24);
  349. Y    ext_num(got_tab_len, &tab_len, argv[index_tab_len],
  350. Y    "Tab length zero, not allowed", 27);
  351. Y    ext_num(got_line_len, &line_len, argv[index_line_len],
  352. Y    "Line length zero, not allowed", 25);
  353. Y    write_err(line_len < 2, "Too short line length, not allowed", 32);
  354. Y    ext_num(got_end, &end, argv[index_end],
  355. Y    "End length zero, not allowed", 25);
  356. Y    write_err(got_end && end <= start,
  357. Y    "End not greater than start line, not allowed", 33);
  358. Y    ext_num(got_width, &width, argv[index_width],
  359. Y    "Line length zero, not allowed", 25);
  360. Y    ext_num(got_mrandom, &mrandom, argv[index_mrandom],
  361. Y    "Zero fill length, not allowed", 36);
  362. Y    if ( got_bot ) get_num(&bot_lines, argv[index_bot]);
  363. Y    if ( got_insert ) {
  364. Y        if ( index_insert < argc ) {
  365. Y            insert = argv[index_insert];
  366. Y        } else {
  367. Y            insert = "";
  368. Y        }
  369. Y    }
  370. Y    if ( got_delim ) {
  371. Y        if ( index_insert < argc ) {
  372. Y            delim = argv[index_delim];
  373. Y        } else {
  374. Y            delim = "";
  375. Y        }
  376. Y    }
  377. Y    del_len = strlen(delim);
  378. Y    if ( got_columns ) {
  379. Y        get_num(&columns, argv[index_columns]);
  380. Y        write_err(columns == 0, "columns count zero, not allowed", 26);
  381. Y        write_err(line_len % columns,
  382. Y        "Lines not an even multiple of columns length", 27);
  383. Y    }
  384. Y/* colu_chars is the chars on one column. colu_lines is the total number of
  385. Ylines in all the columns in one page. page_len is the number of lines in one
  386. Ypage. */
  387. Y    ins_len = strlen(insert);
  388. Y    colu_chars = line_len / columns + ins_len;
  389. Y    colu_lines = page_len * columns;
  390. Y    write_err( !(cur_page = (char**) malloc(colu_lines * sizeof(char*))),
  391. Y    "Can not malloc that page length", 39);
  392. Y    for ( in = 0; in < colu_lines; in++ ) {
  393. Y        if ( !(cur_page[in] = malloc(colu_chars * sizeof(char))) ) {
  394. Y            write_err(TRUE, "Can not malloc that page length", 40);
  395. Y        }
  396. Y    }
  397. Y    for ( doo = 0; doo < ins_len; doo++ ) {
  398. Y        if ( insert[doo] == '\t' ) {
  399. Y            ins_tabs++;
  400. Y        }
  401. Y    }
  402. Y    full_line = line_len + ins_len * columns + del_len * ( columns - 1);
  403. Y    full_line += ( tab_len - 1 ) * columns * ins_tabs;
  404. Y    line_len = line_len / columns;
  405. Y    word_len = line_len;
  406. Y    write_err( !(cur_word = malloc(word_len * sizeof(char))),
  407. Y    "Can not malloc that word (line?) length", 41);
  408. Y    if ( width > full_line ) initrev(width);
  409. Y    else initrev(full_line);
  410. Y    while ( row_count < start ) {
  411. Y        in = getchar();
  412. Y        if ( in == EOF ) exit(0);
  413. Y        if ( in == '\n' ) {
  414. Y            if ( got_dyslexi ) {
  415. Y                flushrev();
  416. Y            }
  417. Y            putchar('\n');
  418. Y            row_count++;
  419. Y        } else if ( got_dyslexi ) {
  420. Y            putrev(in);
  421. Y        } else {
  422. Y            putchar(in);
  423. Y        }
  424. Y    }
  425. Y    if ( !got_end ) end = row_count + 1;
  426. Y    lin = ( ( start - 1 ) % page_len ) * columns;
  427. Y    new_wo_le = word_len - 1;
  428. Y/* ******************** The fold follows ******************** */
  429. Y    while ( 1 ) {
  430. Y        DO_ALL1
  431. Y        while ( 1 ) {
  432. Y            if ( row_count > end ) {
  433. Y                in = EOF;
  434. Y            } else if ( j == new_wo_le && tmp_chars == 0 ) {
  435. Y                in_2 = getchar();
  436. Y                if ( in_2 == ' ' || in_2 == '\n' || in_2 == '\t' ) {
  437. Y                    in = in_2;
  438. Y                } else {
  439. Y                    in_3 = getchar();
  440. Y                    if ( in_3 == ' ' || in_3 == '\n' || in_3 == '\t' ) {
  441. Y                        in = in_2;
  442. Y                        tmp_chars = 1;
  443. Y                    } else {
  444. Y                        in = '-';
  445. Y                        tmp_chars = 2;
  446. Y                    }
  447. Y                }
  448. Y            } else if ( j == word_len ) { 
  449. Y                in = ' ';
  450. Y            } else if ( tmp_chars > 0 ) {
  451. Y                if ( tmp_chars == 1 && j == 0 ) {
  452. Y                    in = in_3;
  453. Y                    tmp_chars = 0;
  454. Y                } else if ( tmp_chars == 1 && j == 1 ) {
  455. Y                    in_2 = in_3;
  456. Y                    tmp_chars = 0;
  457. Y                    if ( line_len != 2 || in_2 == ' ' || in_2 == '\n'
  458. Y                    || in_2 == '\t' ) {
  459. Y                        in = in_2;
  460. Y                    } else {
  461. Y                        in_3 = getchar();
  462. Y                        if ( in_3 == ' ' || in_3 == '\n' || in_3 == '\t' ) {
  463. Y                            in = in_2;
  464. Y                            tmp_chars = 1;
  465. Y                        } else {
  466. Y                            in = '-';
  467. Y                            tmp_chars = 2;
  468. Y                        }
  469. Y                    }
  470. Y                } else {
  471. Y                    in = in_2;
  472. Y                    tmp_chars = 1;
  473. Y                }
  474. Y            } else { 
  475. Y                in = getchar();
  476. Y            }
  477. Y            if ( cnt_nwl > 1 && got_newlines && in != '\n' ) {
  478. Y                putcolu('\n');
  479. Y                while ( --cnt_nwl ) {
  480. Y                    putcolu(' ');
  481. Y                    putcolu('\n');
  482. Y                }
  483. Y                DO_ALL1
  484. Y            }
  485. Y            if ( in != '\n' ) cnt_nwl = 0;
  486. Y            if ( in == '\n' || in == '\t' ) {
  487. Y                if ( in == '\n' ) {
  488. Y                    if ( got_end ) row_count++;
  489. Y                    if ( got_newlines ) cnt_nwl++;
  490. Y                }
  491. Y                in = ' ';
  492. Y            }
  493. Y            if ( in == EOF ) {
  494. Y                putcolu('\n');
  495. Y                flushpage(columns);
  496. Y                if ( !got_end ) exit(0);
  497. Y                else break;
  498. Y            }
  499. Y            if ( in != ' ' ) {
  500. Y                write_err( j >= word_len || j < 0 ,
  501. Y                "Internal error or to long text word", 3);
  502. Y                cur_word[j++] = in;
  503. Y            } else {
  504. Y                if ( col_pos != POS1 && (col_pos + j) <= line_len &&
  505. Y                j != 0 ) {
  506. Y                    putcolu(' ');
  507. Y                    col_pos++;
  508. Y                } else if ( (col_pos + j) > line_len && col_pos != POS1 ) {
  509. Y                    putcolu('\n');
  510. Y                    DO_ALL1
  511. Y                }
  512. Y                for ( k = 0; k < j; k++ ) {
  513. Y                    putcolu(cur_word[k]);
  514. Y                }
  515. Y                col_pos += j;
  516. Y                j = 0;
  517. Y            }
  518. Y            if ( col_pos == line_len ) break;
  519. Y        }
  520. Y        if ( in == EOF ) break;
  521. Y        putcolu('\n');
  522. Y    }
  523. Y    if ( cnt_nwl > 1 && got_newlines ) {
  524. Y        while ( cnt_nwl-- ) putcolu('\n');
  525. Y    }
  526. Y    while ( 1 ) {
  527. Y        in = getchar();
  528. Y        if ( in == EOF ) exit(0);
  529. Y        if ( in == '\n' ) {
  530. Y            if ( got_dyslexi ) {
  531. Y                flushrev();
  532. Y            }
  533. Y            putchar('\n');
  534. Y        } else if ( got_dyslexi ) {
  535. Y            putrev(in);
  536. Y        } else {
  537. Y            putchar(in);
  538. Y        }
  539. Y    }
  540. Y}
  541. Y
  542. Ychar *buff;
  543. Yint gl_i = 0, len;
  544. Y/* lin is the line index in the one long column before it is pasted out onto the
  545. Ypage. len is the lenght of each line in the one long column. */
  546. Y
  547. Yputrev(c)
  548. Y{
  549. Y    int i;
  550. Y
  551. Y    if ( gl_i >= 0 && gl_i < len ) {
  552. Y        if ( c != '\t' ) {
  553. Y            buff[gl_i++] = c;
  554. Y        } else {
  555. Y            int k = tab_len - gl_i % tab_len;
  556. Y            for ( i = 0; i < k; i++ ) {
  557. Y                putrev(' ');
  558. Y            }
  559. Y        }
  560. Y    } else {
  561. Y        write_err(TRUE,
  562. Y        "Internal error in reverse buffer. Specify bigger buffer", 4);
  563. Y    }
  564. Y}
  565. Y
  566. Yflushrev() {
  567. Y    int i;
  568. Y
  569. Y    for ( i = len - 1; i > 0; i--) {
  570. Y        if ( buff[i] != '\0' ) {
  571. Y            putchar(buff[i]);
  572. Y            buff[i] = '\0';
  573. Y        } else {
  574. Y            putchar(' ');
  575. Y        }
  576. Y    }
  577. Y    if ( buff[0] != ' ' ) {
  578. Y        putchar(buff[0]);
  579. Y    }
  580. Y    gl_i = 0;
  581. Y}
  582. Y
  583. Yinitrev(l)
  584. Yint l;
  585. Y{
  586. Y    int i;
  587. Y
  588. Y    len = l;
  589. Y    write_err( !(buff = malloc(len * sizeof(char))),
  590. Y    "Can not malloc that internal reverse buffer length", 42);
  591. Y    for ( i = 0; i < len; i++ ) {
  592. Y        buff[i] = '\0';
  593. Y    }
  594. Y}
  595. Y
  596. Ycheck_it(got_flag, s, err, boole, index_flag, index)
  597. Yint *got_flag, *index_flag;
  598. Ychar *s;
  599. Y{
  600. Y    test(*got_flag, s, err);
  601. Y    test_two(boole, index_flag, index, got_flag);
  602. Y}
  603. Y
  604. Ytest(flag, s1, err)
  605. Ychar *s1;
  606. Y{
  607. Y    char *s2 = "Multiple '  ' not allowed";
  608. Y
  609. Y    s2[10] = s1[0];
  610. Y    s2[11] = s1[1];
  611. Y    write_err(flag, s2, err);
  612. Y}
  613. Y
  614. Ytest_two(boole, index_flag, index, got_flag)
  615. Yint *index_flag, *got_flag;
  616. Y{
  617. Y    if ( boole ) {
  618. Y        if ( *index_flag && glob_argv[index][2] != '\0' ) {
  619. Y            fprintf(stderr, "Flag '%c%c' doesn't want any argument.\n",
  620. Y            glob_argv[index][0], glob_argv[index][1]);
  621. Y            usage(35);
  622. Y        }
  623. Y        *index_flag = index;
  624. Y        *got_flag = TRUE;
  625. Y    }
  626. Y}
  627. Y
  628. Ywrite_err(flag, s, err)
  629. Ychar *s;
  630. Y{
  631. Y    if ( flag ) {
  632. Y        fprintf(stderr, "%s.\n", s);
  633. Y        usage(err);
  634. Y    }
  635. Y}
  636. Y
  637. Yext_num(bulle, tal, arg, s, err)
  638. Yint *tal;
  639. Ychar *arg, *s;
  640. Y{
  641. Y    if ( bulle ) {
  642. Y        get_num(tal, arg);
  643. Y        write_err(*tal == 0, s, err);
  644. Y    }
  645. Y}
  646. Y
  647. Yget_num(number, argv) 
  648. Yint *number;
  649. Ychar argv[];
  650. Y{
  651. Y    int k, in, tmp;
  652. Y    char *s = "Not a number in flag '  '";
  653. Y
  654. Y    s[22] = argv[0];
  655. Y    s[23] = argv[1];
  656. Y    *number = 0;
  657. Y    k = strlen(argv);
  658. Y    write_err(k == 1, s, 9);
  659. Y    for( in = 2; in < k; in++) {
  660. Y        tmp = argv[in] - '0';
  661. Y        write_err(tmp < 0 || tmp > 9, "Bad flag, N-A-P-N", 5);
  662. Y        *number = tmp + *number * 10;
  663. Y    }
  664. Y}
  665. Y
  666. Yusage(t) {
  667. Y    fprintf(stderr, "[ %d ]  Usage: mfold [ -sn -ln -en -wn -f -d -rn -tn -cn -pn -n -L -Bn\n-D '<text>' -i '<text>' ]\n", t);
  668. Y    exit(t);
  669. Y}
  670. Y
  671. Yint col = 0;
  672. Y
  673. Yputcolu(c)
  674. Ychar c;
  675. Y{
  676. Y    if ( c == '\n' || col == colu_chars ) {
  677. Y        advance_line();
  678. Y        return;
  679. Y    }
  680. Y    cur_page[lin][col++] = c;
  681. Y}
  682. Y
  683. Yadvance_line() {
  684. Y        col = 0;
  685. Y        if ( ++lin == colu_lines ) {
  686. Y            flushpage(columns);
  687. Y            end_page();
  688. Y        }
  689. Y}
  690. Y
  691. Yend_page() {
  692. Y    int i;
  693. Y
  694. Y    if ( columns > 1 || got_bot ) {
  695. Y        for ( i = 0; i < bot_lines; i++ ) {
  696. Y            putchar('\n');
  697. Y        }
  698. Y    }
  699. Y    if ( got_newpage ) putchar(12);        /* 12 == ^L  (ascii) */
  700. Y}
  701. Y
  702. Yflushpage(columns) {
  703. Y    int line_sta = 0, cs, tmpl, lin_diff, lin_end;
  704. Y    int end_col = columns - 1, lin_sto = colu_lines, end_char = colu_chars - 1;
  705. Y
  706. Y    for ( lin = 0; lin < colu_lines; lin++ ) {
  707. Y        if ( cur_page[lin][0] != '\0' ) {
  708. Y            line_sta = lin;
  709. Y            break;
  710. Y        }
  711. Y    }
  712. Y    for ( lin = line_sta; lin < colu_lines; lin += columns ) {
  713. Y        if ( cur_page[lin][0] == '\0' ) {
  714. Y            lin_sto =  lin;
  715. Y            break;
  716. Y        }
  717. Y    }
  718. Y    lin_diff = (lin_sto - line_sta) / columns;
  719. Y    lin_end = line_sta + lin_diff;
  720. Y    for ( lin = line_sta; lin < lin_end; lin++) {
  721. Y        if ( cur_page[lin][0] == ' ' && cur_page[lin][1] == '\0'
  722. Y        && columns == 1 ) {
  723. Y            putchar('\n');
  724. Y        } else {
  725. Y            for ( cs = 0; cs < columns; cs++ ) {
  726. Y                tmpl = lin + lin_diff * cs;
  727. Y                if ( cur_page[tmpl][end_char] == '\0' && got_mrandom ) {
  728. Y                    fill_sp(tmpl, end_char);
  729. Y                }
  730. Y                for ( col = 0; col < colu_chars; col++ ) {
  731. Y                    if ( cur_page[tmpl][col] == '\0' ) {
  732. Y                        if ( cs == end_col && !got_fill ) break;
  733. Y                        if ( got_dyslexi ) putrev(' ');
  734. Y                        else putchar(' ');
  735. Y                    } else {
  736. Y                        if ( got_dyslexi ) putrev(cur_page[tmpl][col]);
  737. Y                        else putchar(cur_page[tmpl][col]);
  738. Y                        cur_page[tmpl][col] = '\0';
  739. Y                    }
  740. Y                }
  741. Y                if ( cs < end_col ) {
  742. Y                    for ( col = 0; col < del_len; col++ ) {
  743. Y                        if ( got_dyslexi ) putrev(delim[col]);
  744. Y                        else putchar(delim[col]);
  745. Y                    }
  746. Y                }
  747. Y            }
  748. Y        }
  749. Y        if ( got_dyslexi ) flushrev();
  750. Y        putchar('\n');
  751. Y    }
  752. Y    lin = col = 0;
  753. Y}
  754. Y
  755. Yfill_sp(line, end_char) {
  756. Y    int pass = 0, last, nulls = end_char, words = 0, i, found_sp = FALSE;
  757. Y    int found_word = FALSE, moves, new_end, old_end;
  758. Y
  759. Y    while ( cur_page[line][--nulls] == '\0' && nulls != 1 );
  760. Y    nulls = end_char - nulls;
  761. Y    last = end_char - nulls;
  762. Y    for ( i = ins_len; i <= last; i++) {
  763. Y        if ( cur_page[line][i] != ' ' ) {
  764. Y            if ( !found_word ) {
  765. Y                words++;
  766. Y                found_word = TRUE;
  767. Y            }
  768. Y        } else {
  769. Y            found_word = FALSE;
  770. Y        }
  771. Y    }
  772. Y    if ( words < 2 ) return;
  773. Y    old_end = last;
  774. Y    while ( ++pass < mrandom ) {
  775. Y        if ( words > nulls ) moves = nulls;
  776. Y        else moves = words - 1;
  777. Y        i = moves;
  778. Y        new_end = moves + old_end;
  779. Y        last = new_end;
  780. Y        while( 1 ) {
  781. Y            cur_page[line][new_end--] = cur_page[line][old_end--];
  782. Y            if ( old_end < 0 ) break;
  783. Y            if ( cur_page[line][old_end] == ' ' ) {
  784. Y                if ( !found_sp ) {
  785. Y                    if ( moves-- > 0 ) cur_page[line][new_end--] = ' ' ;
  786. Y                }
  787. Y                found_sp = TRUE;
  788. Y            } else {
  789. Y                found_sp = FALSE;
  790. Y            }
  791. Y        }
  792. Y        if ( cur_page[line][end_char] != '\0' ) return;
  793. Y        nulls = nulls - i;
  794. Y        old_end = last;
  795. Y    }
  796. Y}
  797. XXX
  798. $TOUCH -am 0909162690 mfold.c &&
  799. chmod 0644 mfold.c ||
  800. echo "restore of mfold.c failed"
  801. set `wc -c mfold.c`;Wc_c=$1
  802. if test "$Wc_c" != "13825"; then
  803.     echo original size 13825, current size $Wc_c
  804. fi
  805. fi
  806. exit 0
  807. -- 
  808. +=======================================+
  809. "The whole valley is like a smorgasbord."
  810.                     -- TREMORS
  811.