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

  1. From: smidt@fy.chalmers.se (Peter Smidt)
  2. Newsgroups: alt.sources
  3. Subject: mfold - Maaniker's fold and column making (version 1.04)
  4. Message-ID: <1990Oct11.065031.13387@fy.chalmers.se>
  5. Date: 11 Oct 90 06:50:31 GMT
  6.  
  7. No big changes. The doc and man pages have got improved english
  8. thanks to some helpsome people. Mfold now reads input from the
  9. command line (one doesn't need to write 'mfold < my_file').
  10. A bug (or two?) are removed.
  11.  
  12. mfold (version 1.04)
  13.  
  14. Submitted-by: smidt@tapioca
  15. Archive-name: mfold/part01
  16.  
  17. ---- Cut Here and feed the following to sh ----
  18. #!/bin/sh
  19. # This is -V, a shell archive (produced by shar 3.49)
  20. # To extract the files from this archive, save it to a file, remove
  21. # everything above the "!/bin/sh" line above, and type "sh file_name".
  22. #
  23. # made 10/11/1990 06:25 UTC by smidt@tapioca
  24. # Source directory /nfs/alcazar/u/smidt/src/mfold/exp
  25. #
  26. # existing files will NOT be overwritten unless -c is specified
  27. #
  28. # This shar contains:
  29. # length  mode       name
  30. # ------ ---------- ------------------------------------------
  31. #    597 -rw-r--r-- README
  32. #     87 -rw-r--r-- makefile
  33. #   3817 -rw-r--r-- mfold.1
  34. #  14166 -rw-r--r-- mfold.c
  35. #   2727 -rw-r--r-- mfold_doc
  36. #
  37. # ============= README ==============
  38. if test -f 'README' -a X"$1" != X"-c"; then
  39.     echo 'x - skipping README (File already exists)'
  40. else
  41. echo 'x - extracting README (Text)'
  42. sed 's/^X//' << 'SHAR_EOF' > 'README' &&
  43. Version 1.04, last change 30 september 1990.
  44. X
  45. The program mfold is a simple folding and column making program.
  46. unpack the files from the shar file put them in a empty
  47. directory, type 'make' and you'll get the executable. A manual
  48. is included in the shar file.
  49. X
  50. A bug have been removed and some optimization have been done since the first
  51. posting to alt.sources.
  52. X
  53. Thanks to all the people who have sent typo fixes.
  54. X
  55. A more exact documentation is in mfold_doc.
  56. X
  57. X
  58. Bug reports, ideas, or patches could be sent to;
  59. Peter Smidt   smidt@cd.chalmers.se    or
  60. Peter Smidt   smidt@fy.chalmers.se
  61. X
  62. /Maaniker
  63. SHAR_EOF
  64. chmod 0644 README ||
  65. echo 'restore of README failed'
  66. Wc_c="`wc -c < 'README'`"
  67. test 597 -eq "$Wc_c" ||
  68.     echo 'README: original size 597, current size' "$Wc_c"
  69. fi
  70. # ============= makefile ==============
  71. if test -f 'makefile' -a X"$1" != X"-c"; then
  72.     echo 'x - skipping makefile (File already exists)'
  73. else
  74. echo 'x - extracting makefile (Text)'
  75. sed 's/^X//' << 'SHAR_EOF' > 'makefile' &&
  76. mfold: mfold.o
  77. X    cc mfold.o -o mfold
  78. X    rm -f mfold.o
  79. X
  80. mfold.o: mfold.c
  81. X    cc -c mfold.c -O
  82. SHAR_EOF
  83. chmod 0644 makefile ||
  84. echo 'restore of makefile failed'
  85. Wc_c="`wc -c < 'makefile'`"
  86. test 87 -eq "$Wc_c" ||
  87.     echo 'makefile: original size 87, current size' "$Wc_c"
  88. fi
  89. # ============= mfold.1 ==============
  90. if test -f 'mfold.1' -a X"$1" != X"-c"; then
  91.     echo 'x - skipping mfold.1 (File already exists)'
  92. else
  93. echo 'x - extracting mfold.1 (Text)'
  94. sed 's/^X//' << 'SHAR_EOF' > 'mfold.1' &&
  95. .\" Public Domain 1990 of Chalmers Computer Society.
  96. .\"
  97. .\"    @(#)mfold.1    (G|teborg) 1990-09-11
  98. .\"
  99. .TH MFOLD 1 "September 11, 1990"
  100. .UC
  101. .SH NAME
  102. mfold \- Maaniker's fold and column making
  103. .SH SYNOPSIS
  104. .B mfold
  105. [
  106. .B \-sn -ln -en -wn -f -d
  107. .B -rn
  108. .B -tn -cn -pn -n -L -Bn
  109. .B -D '<text>' -i '<text>'
  110. .B file
  111. ]
  112. .SH DESCRIPTION
  113. .I Mfold
  114. reads text files from standard input and writes to standard
  115. output. A filename 'file' could be used instead of standard input.
  116. .I Mfold
  117. concatenates and folds the text in one or more columns
  118. without breaking in the middle of a word. Tabs and newlines are
  119. converted to blank space. Multiple blank space is substituted with
  120. one single blank space. Some flags don't do anything if they aren't
  121. used together with either '-c' or '-d'.
  122. .PP
  123. There is a limited amount of options:
  124. .TP
  125. .B \-sn
  126. Start line for
  127. .I mfold,
  128. where n is the line number. The preceding text
  129. is passed through
  130. .I mfold
  131. without being changed. If an end-of-file character is encountered
  132. before line n,
  133. .I mfold
  134. will end the execution.
  135. .TP
  136. .B \-i
  137. Insert text at the beginning of every line, or with '\-c', at every
  138. column. This option as the last flag doesn't need any argument if
  139. a null argument is wanted. A space is required between the flag
  140. and the argument. Tabs may be present in the insert text. Default
  141. insert text is a null string.
  142. .TP
  143. .B \-ln
  144. Length for output lines, excluding any insert text and excluding
  145. any delimiter text. Where n is the number of characters. Default
  146. length is 80.
  147. .TP
  148. .B \-en
  149. .I Mfold
  150. will end the folding at input line n. Text coming after this
  151. line is not being changed by
  152. .I mfold.
  153. .TP
  154. .B \-wn
  155. Specify the width n, of the not folded lines to avoid overrunning the
  156. internal reverse buffer when using the '-d' flag. '-w' is not always
  157. provided. Run
  158. .I mfold,
  159. look at the result and decide if you want to specify the full
  160. width. Default is 80 characters.
  161. .TP
  162. .B \-f
  163. Fill each line with blank space to its full line length.
  164. .TP
  165. .B \-rn
  166. Fill the space between the words in the lines with
  167. .br
  168. (n \- 1) extra blank spaces, until the lines get an even right
  169. margin. The words will get a little random placement on the
  170. lines. If the lines become very ugly, specify a smaller n.
  171. .TP
  172. .B \-d
  173. Reverse the text for dyslexics.
  174. .TP
  175. .B \-tn
  176. Expand the tabs to spaces, where n is the number of
  177. spaces. Default is four spaces. Use only with '-d'.
  178. .TP
  179. .B \-cn
  180. Specify columns where n is the number of columns. Text is inserted at
  181. every column.
  182. .TP
  183. .B \-pn
  184. Specify page length n. Meaningful when used with the '-c'
  185. flag. Default is 40 lines per page.
  186. .TP
  187. .B \-n
  188. Many newlines in a row is not substituted with one single 
  189. blank space, which is otherwise the default. Instead they are
  190. left without change, but may be put in a separate column.
  191. .TP
  192. .B \-L
  193. Write a ^L (newpage) at the end of each page. Use with '-c'.
  194. .TP
  195. .B \-Bn
  196. Write n newlines at the bottom of each page. Default is
  197. one newline. Use when the number of columns is greater then one.
  198. .TP
  199. .B \-D
  200. Specify column delimiter. Default is three blank spaces. Tabs may be
  201. present in the delimiter. This option as the last flag doesn't need
  202. any argument if a null argument is wanted.
  203. .SH ERRORS
  204. You will notice them, when you give bad flags.
  205. .SH AUTHOR
  206. Peter Smidt, Chalmers Computer Society.
  207. .SH SEE ALSO
  208. awk(1), sed(1), lex(1), nroff(1), fmt(1), fold(1), rev(1)
  209. .SH BUGS
  210. If underlining is present it will get messed up with the
  211. text. The '-c' flag
  212. .I may
  213. produce some extra unnecessary blank space at the end of the
  214. lines. When a word is longer than the line length, the word may be
  215. cut at an inappropriate place. Everything except blank space,
  216. newline and tab counts as parts of words.
  217. .I Mfold
  218. doesn't want the input file to begin with the '-' character.
  219. .SH BUG REPORTS TO
  220. Peter Smidt  smidt@cd.chalmers.se    or
  221. .br
  222. Peter Smidt  smidt@fy.chalmers.se
  223. SHAR_EOF
  224. chmod 0644 mfold.1 ||
  225. echo 'restore of mfold.1 failed'
  226. Wc_c="`wc -c < 'mfold.1'`"
  227. test 3817 -eq "$Wc_c" ||
  228.     echo 'mfold.1: original size 3817, current size' "$Wc_c"
  229. fi
  230. # ============= mfold.c ==============
  231. if test -f 'mfold.c' -a X"$1" != X"-c"; then
  232.     echo 'x - skipping mfold.c (File already exists)'
  233. else
  234. echo 'x - extracting mfold.c (Text)'
  235. sed 's/^X//' << 'SHAR_EOF' > 'mfold.c' &&
  236. #include <stdio.h>
  237. #include <curses.h>
  238. X
  239. #define DEF_LINE_LEN    80
  240. #define DEF_WORD_LEN    40
  241. #define DEF_TAB_LEN        4
  242. #define FULL_WIDTH        80
  243. #define PAGE_LEN        40
  244. #define POS1            1
  245. #define DO_ALL1 \
  246. X    col_pos = POS1;\
  247. X    for ( doo = 0; doo < ins_len; doo++ ) {\
  248. X        putcolu(insert[doo]);\
  249. X    }
  250. #define BOT_LINES        1
  251. #define STR                15
  252. #define TEST_STR        25
  253. #define GET_NUM_STR        25
  254. X
  255. int colu_chars, colu_lines;
  256. int page_len = PAGE_LEN, columns = 1, lin = 0, bot_lines = BOT_LINES;
  257. int got_newpage = FALSE, got_dyslexi = FALSE, full_line = FULL_WIDTH;
  258. int tab_len = DEF_TAB_LEN, ins_tabs = 0, del_len, got_fill = FALSE;
  259. int got_mrandom = FALSE, got_bot = FALSE, ins_len, mrandom = 1;
  260. char **cur_page, *malloc(), *delim = "   ", **glob_argv;
  261. X
  262. main(argc, argv)
  263. char *argv[];
  264. int argc;
  265. {
  266. X    int col_pos = 1, j = 0, k, in, doo, line_len = DEF_LINE_LEN, row_count = 1;
  267. X    int cnt_nwl = 0, new_wo_le, dummy = TRUE, end = 2;
  268. X    int width = FULL_WIDTH, in_2 = 0, in_3 = 0, tmp_chars = 0;
  269. X    char *cur_word, *insert = "", *s = "Bad option '  '";
  270. X    int index, start = 1, word_len = DEF_WORD_LEN;
  271. X    int got_start = FALSE, got_line_len = FALSE, got_insert = FALSE;
  272. X    int got_tab_len = FALSE, got_end = FALSE, got_width = FALSE;
  273. X    int got_columns = FALSE, got_page_len = FALSE;
  274. X    int got_newlines = FALSE, got_delim = FALSE, got_file = FALSE;
  275. X    int index_start = 0, index_insert = 0, index_line_len = 0;
  276. X    int index_tab_len = 0, index_end = 0, index_width = 0;
  277. X    int index_columns = 0, index_page_len = 0, index_delim = 0;
  278. X    int index_bot = 0, index_mrandom = 0, index_file = 0;
  279. X    FILE *fil = stdin;
  280. X    glob_argv = argv;
  281. X    for ( index = 1; index < argc; index++) {
  282. X        if ( argv[index][0] != '-' ) {
  283. X            if ( index == 1 || (argv[index - 1][0] == '-' &&
  284. X            argv[index - 1][0] != 'i' && argv[index - 1][0] != 'D' || index > 2
  285. X            && argv[index - 2][0] == '-' && (argv[index - 2][1] == 'i'
  286. X            || argv[index - 2][1] == 'D')) && !got_file ) {
  287. X                got_file = TRUE;
  288. X                index_file = index;
  289. X            } else {
  290. X                fprintf(stderr, "Option '%s' not allowed.\n", argv[index]);
  291. X                usage(11);
  292. X            }
  293. X        }
  294. X        doo = index_insert != index && index_delim != index
  295. X        && index_file != index;
  296. X        switch ( argv[index][1] ) {
  297. X            case 'i':
  298. X                check_it(&got_insert, "-i", 8, doo, &index_insert, index + 1);
  299. X                break;
  300. X            case 's':
  301. X                check_it(&got_start, "-s", 9, doo, &index_start, index);
  302. X                break;
  303. X            case 'l':
  304. X                check_it(&got_line_len, "-l", 10, doo, &index_line_len, index);
  305. X                break;
  306. X            case 'e':
  307. X                check_it(&got_end, "-e", 10, doo, &index_end, index);
  308. X                break;
  309. X            case 'w':
  310. X                check_it(&got_width, "-w", 10, doo, &index_width, index);
  311. X                break;
  312. X            case 'r':
  313. X                check_it(&got_mrandom, "-r", 37, doo, &index_mrandom, index);
  314. X                break;
  315. X            case 'd':
  316. X                check_it(&got_dyslexi, "-d", 11, doo, &dummy, index);
  317. X                break;
  318. X            case 't':
  319. X                check_it(&got_tab_len, "-t", 13, doo, &index_tab_len, index);
  320. X                break;
  321. X            case 'c':
  322. X                check_it(&got_columns, "-c", 15, doo, &index_columns, index);
  323. X                break;
  324. X            case 'p':
  325. X                check_it(&got_page_len, "-p", 16, doo, &index_page_len, index);
  326. X                break;
  327. X            case 'B':
  328. X                check_it(&got_bot, "-B", 17, doo, &index_bot, index);
  329. X                break;
  330. X            case 'f':
  331. X                check_it(&got_fill, "-f", 33, doo, &dummy, index);
  332. X                break;
  333. X            case 'n':
  334. X                check_it(&got_newlines, "-n", 18, doo, &dummy, index);
  335. X                break;
  336. X            case 'L':
  337. X                check_it(&got_newpage, "-L", 19, doo, &dummy, index);
  338. X                break;
  339. X            case 'D':
  340. X                check_it(&got_delim, "-D", 36, doo, &index_delim, index + 1);
  341. X                break;
  342. X            case '\0':
  343. X                write_err(doo, "Empty flag '-'", 31);
  344. X                break;
  345. X            default:
  346. X                s[12] = argv[index][0];
  347. X                s[13] = argv[index][1];
  348. X                write_err(doo, s, 20);
  349. X                break;
  350. X        }
  351. X    }
  352. X    ext_num(got_start, &start, argv[index_start],
  353. X    "Line zero, for start, not allowed", 21);
  354. X    ext_num(got_page_len, &page_len, argv[index_page_len],
  355. X    "Page length zero, not allowed", 24);
  356. X    ext_num(got_tab_len, &tab_len, argv[index_tab_len],
  357. X    "Tab length zero, not allowed", 27);
  358. X    ext_num(got_line_len, &line_len, argv[index_line_len],
  359. X    "Line length zero, not allowed", 25);
  360. X    write_err(line_len < 2, "Too short line length, not allowed", 32);
  361. X    ext_num(got_end, &end, argv[index_end],
  362. X    "End length zero, not allowed", 25);
  363. X    write_err(got_end && end <= start,
  364. X    "End not greater than start line, not allowed", 33);
  365. X    ext_num(got_width, &width, argv[index_width],
  366. X    "Line length zero, not allowed", 25);
  367. X    ext_num(got_mrandom, &mrandom, argv[index_mrandom],
  368. X    "Zero fill length, not allowed", 36);
  369. X    if ( got_bot ) get_num(&bot_lines, argv[index_bot]);
  370. X    if ( got_file ) {
  371. X        write_err( (fil = fopen(argv[index_file], "r")) == NULL,
  372. X        "Unable to open your text file", 50);
  373. X    }
  374. X    if ( got_insert ) {
  375. X        if ( index_insert < argc ) {
  376. X            insert = argv[index_insert];
  377. X        } else {
  378. X            insert = "";
  379. X        }
  380. X    }
  381. X    if ( got_delim ) {
  382. X        if ( index_delim < argc ) {
  383. X            delim = argv[index_delim];
  384. X        } else {
  385. X            delim = "";
  386. X        }
  387. X    }
  388. X    del_len = str_len(delim);
  389. X    if ( got_columns ) {
  390. X        get_num(&columns, argv[index_columns]);
  391. X        write_err(columns == 0, "columns count zero, not allowed", 26);
  392. X        write_err(line_len % columns,
  393. X        "Lines not an even multiple of columns length", 27);
  394. X    }
  395. /* colu_chars is the chars on one column. colu_lines is the total number of
  396. lines in all the columns in one page. page_len is the number of lines in one
  397. page. */
  398. X    ins_len = str_len(insert);
  399. X    colu_chars = line_len / columns + ins_len;
  400. X    colu_lines = page_len * columns;
  401. X    write_err( !(cur_page = (char**) malloc(colu_lines * sizeof(char*))),
  402. X    "Can not malloc that page length", 39);
  403. X    for ( in = 0; in < colu_lines; in++ ) {
  404. X        if ( !(cur_page[in] = malloc(colu_chars * sizeof(char))) ) {
  405. X            write_err(TRUE, "Can not malloc that page length", 40);
  406. X        }
  407. X    }
  408. X    for ( doo = 0; doo < ins_len; doo++ ) {
  409. X        if ( insert[doo] == '\t' ) {
  410. X            ins_tabs++;
  411. X        }
  412. X    }
  413. X    full_line = line_len + ins_len * columns + del_len * ( columns - 1);
  414. X    full_line += ( tab_len - 1 ) * columns * ins_tabs;
  415. X    line_len = line_len / columns;
  416. X    word_len = line_len;
  417. X    write_err( !(cur_word = malloc(word_len * sizeof(char))),
  418. X    "Can not malloc that word (line?) length", 41);
  419. X    if ( width > full_line ) initrev(width);
  420. X    else initrev(full_line);
  421. /* ************* Write text according to the '-s' flag **** */
  422. X    while ( row_count < start ) {
  423. X        in = getc(fil);
  424. X        if ( in == EOF ) exit(0);
  425. X        if ( in == '\n' ) {
  426. X            if ( got_dyslexi ) {
  427. X                flushrev();
  428. X            }
  429. X            putchar('\n');
  430. X            row_count++;
  431. X        } else if ( got_dyslexi ) {
  432. X            putrev(in);
  433. X        } else {
  434. X            putchar(in);
  435. X        }
  436. X    }
  437. X    if ( !got_end ) end = row_count + 1;
  438. X    lin = ( ( start - 1 ) % page_len ) * columns;
  439. X    new_wo_le = word_len - 1;
  440. /* ******************** The fold follows ******************** */
  441. X    DO_ALL1
  442. X    while ( 1 ) {
  443. X        if ( row_count > end ) {
  444. X            in = EOF;
  445. X        } else if ( j == new_wo_le && tmp_chars == 0 ) {
  446. X            in_2 = getc(fil);
  447. X            if ( in_2 == ' ' || in_2 == '\n' || in_2 == '\t' ) {
  448. X                in = in_2;
  449. X            } else {
  450. X                in_3 = getc(fil);
  451. X                if ( in_3 == ' ' || in_3 == '\n' || in_3 == '\t' ) {
  452. X                    in = in_2;
  453. X                    tmp_chars = 1;
  454. X                } else {
  455. X                    in = '-';
  456. X                    tmp_chars = 2;
  457. X                }
  458. X            }
  459. X        } else if ( j == word_len ) { 
  460. X            in = ' ';
  461. X        } else if ( tmp_chars > 0 ) {
  462. X            if ( tmp_chars == 1 && j == 0 ) {
  463. X                in = in_3;
  464. X                tmp_chars = 0;
  465. X            } else if ( tmp_chars == 1 && j == 1 ) {
  466. X                in_2 = in_3;
  467. X                tmp_chars = 0;
  468. X                if ( line_len != 2 || in_2 == ' ' || in_2 == '\n'
  469. X                || in_2 == '\t' ) {
  470. X                    in = in_2;
  471. X                } else {
  472. X                    in_3 = getc(fil);
  473. X                    if ( in_3 == ' ' || in_3 == '\n' || in_3 == '\t' ) {
  474. X                        in = in_2;
  475. X                        tmp_chars = 1;
  476. X                    } else {
  477. X                        in = '-';
  478. X                        tmp_chars = 2;
  479. X                    }
  480. X                }
  481. X            } else {
  482. X                in = in_2;
  483. X                tmp_chars = 1;
  484. X            }
  485. X        } else { 
  486. X            in = getc(fil);
  487. X        }
  488. X        if ( in != '\n' ) {
  489. X            if ( cnt_nwl > 1 && got_newlines ) {
  490. X                while ( --cnt_nwl ) {
  491. X                    putcolu('\n');
  492. X                    putcolu(' ');
  493. X                }
  494. X                putcolu('\n');
  495. X                DO_ALL1
  496. X            }
  497. X            cnt_nwl = 0;
  498. X            if ( in == '\t' ) in = ' ';
  499. X        } else {
  500. X            if ( got_end ) row_count++;
  501. X            if ( got_newlines ) cnt_nwl++;
  502. X            in = ' ';
  503. X        }
  504. X        if ( in == EOF ) {
  505. X            putcolu('\n');
  506. X            flushpage(columns);
  507. X            if ( !got_end ) exit(0);
  508. X            else break;
  509. X        }
  510. X        if ( in != ' ' ) {
  511. X            write_err( j >= word_len || j < 0 ,
  512. X            "Internal error or to long text word", 3);
  513. X            cur_word[j++] = in;
  514. X        } else {
  515. X            if ( col_pos != POS1 && (col_pos + j) <= line_len &&
  516. X            j != 0 ) {
  517. X                putcolu(' ');
  518. X                col_pos++;
  519. X            } else if ( (col_pos + j) > line_len && col_pos != POS1 ) {
  520. X                putcolu('\n');
  521. X                DO_ALL1
  522. X            }
  523. X            for ( k = 0; k < j; k++ ) {
  524. X                putcolu(cur_word[k]);
  525. X            }
  526. X            col_pos += j;
  527. X            j = 0;
  528. X        }
  529. X    }
  530. /* ***************** End of the fold ********************** */
  531. /* ************* Write text according to the '-e' flag **** */
  532. X    if ( cnt_nwl > 1 && got_newlines ) {
  533. X        while ( --cnt_nwl ) {
  534. X            putcolu('\n');
  535. X            putcolu(' ');
  536. X        }
  537. X        putcolu('\n');
  538. X        flushpage(columns);
  539. X    }
  540. X    while ( 1 ) {
  541. X        in = getc(fil);
  542. X        if ( in == EOF ) exit(0);
  543. X        if ( in == '\n' ) {
  544. X            if ( got_dyslexi ) {
  545. X                flushrev();
  546. X            }
  547. X            putchar('\n');
  548. X        } else if ( got_dyslexi ) {
  549. X            putrev(in);
  550. X        } else {
  551. X            putchar(in);
  552. X        }
  553. X    }
  554. }
  555. X
  556. char *buff;
  557. int gl_i = 0, len;
  558. /* lin is the line index in the one long column before it is pasted out onto the
  559. page. len is the lenght of each line in the one long column. */
  560. X
  561. putrev(c)
  562. {
  563. X    int i;
  564. X
  565. X    if ( gl_i >= 0 && gl_i < len ) {
  566. X        if ( c != '\t' ) {
  567. X            buff[gl_i++] = c;
  568. X        } else {
  569. X            int k = tab_len - gl_i % tab_len;
  570. X            for ( i = 0; i < k; i++ ) {
  571. X                putrev(' ');
  572. X            }
  573. X        }
  574. X    } else {
  575. X        write_err(TRUE,
  576. X        "Internal error in reverse buffer. Specify bigger buffer", 4);
  577. X    }
  578. }
  579. X
  580. flushrev() {
  581. X    int i, first = 0;
  582. X
  583. X    if ( buff[first] == ' ' ) while ( buff[++first] == ' ' );
  584. X    for ( i = len - 1; i >= first; i--) {
  585. X        if ( buff[i] != '\0' ) {
  586. X            putchar(buff[i]);
  587. X            buff[i] = '\0';
  588. X        } else {
  589. X            putchar(' ');
  590. X        }
  591. X    }
  592. X    gl_i = 0;
  593. }
  594. X
  595. initrev(l)
  596. int l;
  597. {
  598. X    int i;
  599. X
  600. X    len = l;
  601. X    write_err( !(buff = malloc((len + 1) * sizeof(char))),
  602. X    "Can not malloc that internal reverse buffer length", 42);
  603. X    for ( i = 0; i < len; i++ ) {
  604. X        buff[i] = '\0';
  605. X    }
  606. X    buff[len] = '#'; /* To help flushrev swallow blank lines */
  607. }
  608. X
  609. check_it(got_flag, s, err, boole, index_flag, index)
  610. int *got_flag, *index_flag;
  611. char *s;
  612. {
  613. X    test(*got_flag, s, err);
  614. X    test_two(boole, index_flag, index, got_flag);
  615. }
  616. X
  617. test(flag, s1, err)
  618. char *s1;
  619. {
  620. X    char *s2 = "Multiple '  ' not allowed";
  621. X
  622. X    s2[10] = s1[0];
  623. X    s2[11] = s1[1];
  624. X    write_err(flag, s2, err);
  625. }
  626. X
  627. test_two(boole, index_flag, index, got_flag)
  628. int *index_flag, *got_flag;
  629. {
  630. X    if ( boole ) {
  631. X        if ( *index_flag && glob_argv[index][2] != '\0' ) {
  632. X            fprintf(stderr, "Flag '%c%c' doesn't want any argument.\n",
  633. X            glob_argv[index][0], glob_argv[index][1]);
  634. X            usage(35);
  635. X        }
  636. X        *index_flag = index;
  637. X        *got_flag = TRUE;
  638. X    }
  639. }
  640. X
  641. write_err(flag, s, err)
  642. char *s;
  643. {
  644. X    if ( flag ) {
  645. X        fprintf(stderr, "%s.\n", s);
  646. X        usage(err);
  647. X    }
  648. }
  649. X
  650. ext_num(bulle, tal, arg, s, err)
  651. int *tal;
  652. char *arg, *s;
  653. {
  654. X    if ( bulle ) {
  655. X        get_num(tal, arg);
  656. X        write_err(*tal == 0, s, err);
  657. X    }
  658. }
  659. X
  660. get_num(number, argv) 
  661. int *number;
  662. char argv[];
  663. {
  664. X    int k, in, tmp;
  665. X    char *s = "Not a number in flag '  '";
  666. X
  667. X    s[22] = argv[0];
  668. X    s[23] = argv[1];
  669. X    *number = 0;
  670. X    k = str_len(argv);
  671. X    write_err(k == 1, s, 9);
  672. X    for( in = 2; in < k; in++) {
  673. X        tmp = argv[in] - '0';
  674. X        write_err(tmp < 0 || tmp > 9, "Bad flag, N-A-P-N", 5);
  675. X        *number = tmp + *number * 10;
  676. X    }
  677. }
  678. X
  679. str_len(s)
  680. char *s;
  681. {
  682. X    int m = 0;
  683. X
  684. X    if ( s[m] != '\0' ) while ( s[++m] != '\0' );
  685. X    return m;
  686. }
  687. X
  688. usage(t) {
  689. X    fprintf(stderr, "[ %d ]  Usage: mfold [ -sn -ln -en -wn -f -d -rn -tn -cn -pn -n -L -Bn\n-D '<text>' -i '<text>' file]\n", t);
  690. X    exit(t);
  691. }
  692. X
  693. int col = 0;
  694. X
  695. putcolu(c)
  696. char c;
  697. {
  698. X    if ( c == '\n' || col == colu_chars ) {
  699. X        advance_line();
  700. X        return;
  701. X    }
  702. X    cur_page[lin][col++] = c;
  703. }
  704. X
  705. advance_line() {
  706. X        col = 0;
  707. X        if ( ++lin == colu_lines ) {
  708. X            flushpage(columns);
  709. X            end_page();
  710. X        }
  711. }
  712. X
  713. end_page() {
  714. X    int i;
  715. X
  716. X    if ( columns > 1 || got_bot ) {
  717. X        for ( i = 0; i < bot_lines; i++ ) {
  718. X            putchar('\n');
  719. X        }
  720. X    }
  721. X    if ( got_newpage ) putchar(12);        /* 12 == ^L  (ascii) */
  722. }
  723. X
  724. flushpage(columns) {
  725. X    int line_sta = 0, cs, tmpl, lin_diff, lin_end;
  726. X    int end_col = columns - 1, lin_sto = colu_lines, end_char = colu_chars - 1;
  727. X
  728. X    for ( lin = 0; lin < colu_lines; lin++ ) {
  729. X        if ( cur_page[lin][0] != '\0' ) {
  730. X            line_sta = lin;
  731. X            break;
  732. X        }
  733. X    }
  734. X    for ( lin = line_sta; lin < colu_lines; lin += columns ) {
  735. X        if ( cur_page[lin][0] == '\0' ) {
  736. X            lin_sto =  lin;
  737. X            break;
  738. X        }
  739. X    }
  740. X    lin_diff = (lin_sto - line_sta) / columns;
  741. X    lin_end = line_sta + lin_diff;
  742. X    for ( lin = line_sta; lin < lin_end; lin++) {
  743. X        if ( cur_page[lin][0] != ' ' || cur_page[lin][1] != '\0'
  744. X        || columns != 1 ) {
  745. X            for ( cs = 0; cs < columns; cs++ ) {
  746. X                tmpl = lin + lin_diff * cs;
  747. X                if ( cur_page[tmpl][end_char] == '\0' && got_mrandom ) {
  748. X                    fill_sp(tmpl, end_char);
  749. X                }
  750. X                for ( col = 0; col < colu_chars; col++ ) {
  751. X                    if ( cur_page[tmpl][col] == '\0' ) {
  752. X                        if ( cs == end_col && !got_fill ) break;
  753. X                        if ( got_dyslexi ) putrev(' ');
  754. X                        else putchar(' ');
  755. X                    } else {
  756. X                        if ( got_dyslexi ) putrev(cur_page[tmpl][col]);
  757. X                        else putchar(cur_page[tmpl][col]);
  758. X                        cur_page[tmpl][col] = '\0';
  759. X                    }
  760. X                }
  761. X                if ( cs < end_col ) {
  762. X                    for ( col = 0; col < del_len; col++ ) {
  763. X                        if ( got_dyslexi ) putrev(delim[col]);
  764. X                        else putchar(delim[col]);
  765. X                    }
  766. X                }
  767. X            }
  768. X            if ( got_dyslexi ) flushrev();
  769. X        }
  770. X        putchar('\n');
  771. X    }
  772. X    lin = col = 0;
  773. }
  774. X
  775. fill_sp(line, end_char) {
  776. X    int pass = 0, last, nulls = end_char, words = 0, i, found_sp = FALSE;
  777. X    int found_word = FALSE, moves, new_end, old_end;
  778. X
  779. X    while ( cur_page[line][--nulls] == '\0' && nulls != 1 );
  780. X    nulls = end_char - nulls;
  781. X    last = end_char - nulls;
  782. X    for ( i = ins_len; i <= last; i++) {
  783. X        if ( cur_page[line][i] != ' ' ) {
  784. X            if ( !found_word ) {
  785. X                words++;
  786. X                found_word = TRUE;
  787. X            }
  788. X        } else {
  789. X            found_word = FALSE;
  790. X        }
  791. X    }
  792. X    if ( words < 2 ) return;
  793. X    old_end = last;
  794. X    while ( ++pass < mrandom ) {
  795. X        if ( words > nulls ) moves = nulls;
  796. X        else moves = words - 1;
  797. X        i = moves;
  798. X        new_end = moves + old_end;
  799. X        last = new_end;
  800. X        while( 1 ) {
  801. X            cur_page[line][new_end--] = cur_page[line][old_end--];
  802. X            if ( old_end < 0 ) break;
  803. X            if ( cur_page[line][old_end] == ' ' ) {
  804. X                if ( !found_sp ) {
  805. X                    if ( moves-- > 0 ) cur_page[line][new_end--] = ' ' ;
  806. X                }
  807. X                found_sp = TRUE;
  808. X            } else {
  809. X                found_sp = FALSE;
  810. X            }
  811. X        }
  812. X        if ( cur_page[line][end_char] != '\0' ) return;
  813. X        nulls = nulls - i;
  814. X        old_end = last;
  815. X    }
  816. }
  817. SHAR_EOF
  818. chmod 0644 mfold.c ||
  819. echo 'restore of mfold.c failed'
  820. Wc_c="`wc -c < 'mfold.c'`"
  821. test 14166 -eq "$Wc_c" ||
  822.     echo 'mfold.c: original size 14166, current size' "$Wc_c"
  823. fi
  824. # ============= mfold_doc ==============
  825. if test -f 'mfold_doc' -a X"$1" != X"-c"; then
  826.     echo 'x - skipping mfold_doc (File already exists)'
  827. else
  828. echo 'x - extracting mfold_doc (Text)'
  829. sed 's/^X//' << 'SHAR_EOF' > 'mfold_doc' &&
  830. The documentation follows:
  831. X
  832. The program has some large main parts:
  833. X
  834. X    1) One part scans the options and their arguments from the command line.
  835. X
  836. X    2) One part reads text input which not should be changed before
  837. X    the folding and writes the text to standard output.
  838. X
  839. X    3) One part does the actual folding and inserts text from
  840. X    the '-i' flag. This part also cuts the words if they are too
  841. X    long and puts a '-' at the end of the cut word.
  842. X
  843. X    4) One part reads input text which not should be changed after
  844. X    the folding, and writes the text to standard output.
  845. X
  846. Variables with names 'got_blabla' and 'index_blabla' refer to
  847. the different flags the program can receive. The program doesn't
  848. use getopts(3) and therefore has a more rigid option format
  849. in the shell. But it's easier to handle errors this way.
  850. X
  851. The '-i' and '-D' options are handled as special cases by mfold.
  852. Any input file is also handled as a special case by mfold.
  853. Remaining options are handled in one separate switch construct.
  854. X
  855. The text which is to be folded is read into an internal two
  856. dimensional 'page' buffer. This buffer has the width of one column
  857. and the length of all the lines in ALL columns on one page. The buffer
  858. is named 'cur_page' and is a global variable.
  859. X
  860. The folding is done in a 'while ( 1 )' loop. The first thing done
  861. in this loop is a large 'if' statement, about 44 lines long. This
  862. 'if' statement checks if a word is larger than the line length and then
  863. cuts the word, saves some characters in some temporary variables, named
  864. 'in_2' and 'in_3'. This construction can handle 2 (two) character
  865. long lines. Then some newline handling is done, see the '-n'
  866. option in the manual to understand the source easier. An 'if'
  867. statement checks if the next character is a blank space or not a
  868. blank space. If it is a space, the word in 'cur_word' is written to
  869. the output, and if it's not, the character is put into 'cur_word'.
  870. When encountering an EOF, mfold exits the 'while ( 1 )' loop.
  871. X
  872. The output is not sent to standard output directly. Instead a
  873. function 'putcolu' is used. 'putcolu' writes to the internal
  874. page buffer 'cur_page'. When 'cur_page' is full, the function
  875. 'flush_page' writes 'cur_page to the output. But if the '-d'
  876. flag is used the output is written to 'putrev' instead, where
  877. the function 'flushrev' flushes the internal reverse script
  878. one line buffer. When the '-r' flag is set, the function
  879. 'fill_sp' fills blank space until every line is right justified.
  880. X
  881. There are some more help functions, but they are very simple
  882. and should be understood without explanation... :-) ?
  883. X
  884. X
  885. Bug reports, ideas, or patches could be sent to;
  886. Peter Smidt   smidt@cd.chalmers.se    or
  887. Peter Smidt   smidt@fy.chalmers.se
  888. X
  889. /Maaniker
  890. SHAR_EOF
  891. chmod 0644 mfold_doc ||
  892. echo 'restore of mfold_doc failed'
  893. Wc_c="`wc -c < 'mfold_doc'`"
  894. test 2727 -eq "$Wc_c" ||
  895.     echo 'mfold_doc: original size 2727, current size' "$Wc_c"
  896. fi
  897. exit 0
  898. -- 
  899. +=======================================+
  900. "The whole valley is like a smorgasbord."
  901.                     -- TREMORS
  902.