home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / txtut122.zip / textutil / src / pr.c < prev    next >
C/C++ Source or Header  |  1998-04-11  |  68KB  |  2,479 lines

  1. /* pr -- convert text files for printing.
  2.    Copyright (C) 1988, 1991, 1995, 1996 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software Foundation,
  16.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. /*  Author: Pete TerMaat.  */
  19.  
  20. /* Things to watch: Sys V screws up on ...
  21.    pr -n -3 -s: /usr/dict/words
  22.    pr -m -o10 -n /usr/dict/words{,,,}
  23.    pr -6 -a -n -o5 /usr/dict/words
  24.  
  25.    Ideas:
  26.  
  27.    Keep a things_to_do list of functions to call when we know we have
  28.    something to print.  Cleaner than current series of checks.
  29.  
  30.    Improve the printing of control prefixes.
  31.  
  32.  
  33.    Options:
  34.  
  35.    +FIRST_PAGE[:LAST_PAGE]
  36.         begin [stop] printing with page FIRST_[LAST_]PAGE
  37.  
  38.    -COLUMN    Produce output that is COLUMN columns wide and print
  39.         columns down.
  40.                 Balance columns on the last page is automatically set.
  41.  
  42.    -a        Print columns across rather than down.  The input
  43.         one
  44.         two
  45.         three
  46.         four
  47.         will be printed as
  48.         one    two    three
  49.         four
  50.  
  51.    -b        Balance columns on the last page.
  52.                 -b is no longer an independent option. It's always used
  53.         together with -COLUMN (unless -a is used) to get a
  54.         consistent formulation with "FF set by hand" in input
  55.         files. Each formfeed found terminates the number of lines
  56.         to be read with the actual page. The situation for
  57.         printing columns down is equivalent to that on the last
  58.         page. So we need a balancing.
  59.  
  60.         We do not yet eliminate source text dealing with -COLUMN
  61.         only. Tune this if it proved to be a satisfactory
  62.         procedure.
  63.  
  64.         Keeping -b as an underground option guarantees some
  65.         downward compatibility. Utilities using pr with -b
  66.         (a most frequently used form) still work as usual.
  67.  
  68.    -c        Print unprintable characters as control prefixes.
  69.         Control-g is printed as ^G.
  70.  
  71.    -d        Double space the output.
  72.  
  73.    -e[c[k]]    Expand tabs to spaces on input.  Optional argument C
  74.         is the input tab character. (Default is `\t'.)  Optional
  75.         argument K is the input tab character's width.  (Default is 8.)
  76.  
  77.    -F
  78.    -f        Use formfeeds instead of newlines to separate pages.
  79.         A three line HEADER is used, no TRAILER (without -f
  80.         both HEADER and TRAILER are made of five lines).
  81.  
  82.    -h HEADER    Replace the filename in the header with the string HEADER.
  83.                 Checking and left-hand-side truncation of the length of the
  84.                 standard and custom header string. A centered header is used.
  85.                 The format of date and time has been shortened
  86.         to  yy-mm-dd HH:MM  to give place to a maximal filename
  87.         information.
  88.                 -h ""  now prints a blank line header. -h"" shows an error.
  89.  
  90.    -i[c[k]]    Replace spaces with tabs on output.  Optional argument
  91.         C is the output tab character.  (Default is `\t'.)  Optional
  92.         argument K is the output tab character's width.  (Default
  93.         is 8.)
  94.  
  95.   -j            Merge full lines, turns off -w line truncation, no column
  96.                 alignment, -s[STRING] sets separators, works with all
  97.         column options (-COLUMN | -a -COLUMN | -m).
  98.  
  99.    -l lines    Set the page length to LINES.  Default is 66.
  100.  
  101.    -m        Print files in parallel; pad_across_to align columns;
  102.         truncate lines and print separator strings;
  103.         Do it also with empty columns to get a continuous line
  104.         numbering and column marking by separators throughout
  105.         the whole merged file.
  106.  
  107.         Empty pages in some input files produce empty columns
  108.         [marked by separators] in the common pages. Completely
  109.         empty common pages show no column separators at all.
  110.  
  111.         The layout of a merged page is ruled by the largest form
  112.         feed distance of the single pages at that page. Shorter
  113.         columns will be filled up with empty lines.
  114.  
  115.         Together with -j option join lines of full length and
  116.         in addition set separators when -s option is used.
  117.  
  118.    -n[c[k]]    Precede each column with a line number.
  119.         (With parallel files, precede each line with a line
  120.         number.)  Optional argument C is the character to print
  121.         after each number.  (Default `\t'.)  Optional argument
  122.         k is the number of digits per line number.  (Default 5.)
  123.         Default counting starts with 1st line of input file.
  124.  
  125.   -N number    Start counting with  number  at 1st line of first page
  126.         printed.
  127.  
  128.    -o offset    Offset each line with a margin OFFSET spaces wide.
  129.         Total page width is the size of this offset plus the
  130.         width set with `-w'.
  131.  
  132.    -r        Ignore files that can't be opened.
  133.  
  134.   -s[STRING]    Separate columns by any string STRING.
  135.                 Don't use -s "STRING".
  136.         without -s: default separator 'space' s used,
  137.         same as -s" ",
  138.         with -s only: no separator is used, same as -s"".
  139.             Quotes should be used with blanks and some shell active
  140.         characters.
  141.  
  142.    -t        Do not print headers or footers but retain form feeds
  143.         set in input files (some page layout is not changed).
  144.  
  145.    -T        Do not print headers or footers, eliminate form feeds
  146.         in input files.
  147.  
  148.    -v        Print unprintable characters as escape sequences.
  149.         Control-G becomes \007.
  150.  
  151.    -w width    Set the page width to WIDTH characters.
  152.         (In pr versions newer than 1.14 -s option does no longer
  153.         affect -w option.)
  154.  
  155.             With/without  -w width  the header line is truncated.
  156.             Default is 72 characters.
  157.             With  -w width  text lines will be truncated, unless -j is
  158.         used. Together with one of the column options
  159.         (-COLUMN| -a -COLUMN| -m) column alignment is used.
  160.             Without  -w PAGE_WIDTH
  161.             - but with one of the column options default truncation of
  162.           72 characters is used (to keep downward compatibility
  163.           and to simplify most frequently met column tasks).
  164.           Column alignment and column separators are used.
  165.             - and without any of the column options no line truncation
  166.            is used (to keep downward compatibility and to meet most
  167.           frequent tasks). That's equivalent to  -w 72 -j .
  168. */
  169.  
  170.  
  171. #include <config.h>
  172.  
  173. #include <stdio.h>
  174. #include <getopt.h>
  175. #include <sys/types.h>
  176. #include <time.h>
  177. #include "system.h"
  178. #include "error.h"
  179. #include "xstrtol.h"
  180.  
  181. #if HAVE_LIMITS_H
  182. #include <limits.h>
  183. #endif
  184.  
  185. char *xmalloc ();
  186. char *xrealloc ();
  187.  
  188. #ifndef UINT_MAX
  189. #define UINT_MAX ((unsigned int) ~(unsigned int) 0)
  190. #endif
  191.  
  192. #ifndef INT_MAX
  193. #define INT_MAX ((int) (UINT_MAX >> 1))
  194. #endif
  195.  
  196. #ifndef TRUE
  197. #define TRUE    1
  198. #define FALSE    0
  199. #endif
  200.  
  201. /* Used with start_position in the struct COLUMN described below.
  202.    If start_position == ANYWHERE, we aren't truncating columns and
  203.    can begin printing a column anywhere.  Otherwise we must pad to
  204.    the horizontal position start_position. */
  205. #define ANYWHERE    0
  206.  
  207. /* Each column has one of these structures allocated for it.
  208.    If we're only dealing with one file, fp is the same for all
  209.    columns.
  210.  
  211.    The general strategy is to spend time setting up these column
  212.    structures (storing columns if necessary), after which printing
  213.    is a matter of flitting from column to column and calling
  214.    print_func.
  215.  
  216.    Parallel files, single files printing across in multiple
  217.    columns, and single files printing down in multiple columns all
  218.    fit the same printing loop.
  219.  
  220.    print_func        Function used to print lines in this column.
  221.             If we're storing this column it will be
  222.             print_stored(), Otherwise it will be read_line().
  223.  
  224.    char_func        Function used to process characters in this column.
  225.             If we're storing this column it will be store_char(),
  226.             otherwise it will be print_char().
  227.  
  228.    current_line        Index of the current entry in line_vector, which
  229.             contains the index of the first character of the
  230.             current line in buff[].
  231.  
  232.    lines_stored        Number of lines in this column which are stored in
  233.             buff.
  234.  
  235.    lines_to_print    If we're storing this column, lines_to_print is
  236.             the number of stored_lines which remain to be
  237.             printed.  Otherwise it is the number of lines
  238.             we can print without exceeding lines_per_body.
  239.  
  240.    start_position    The horizontal position we want to be in before we
  241.             print the first character in this column.
  242.  
  243.    numbered        True means precede this column with a line number. */
  244.  
  245. struct COLUMN
  246.   {
  247.     FILE *fp;            /* Input stream for this column. */
  248.     char *name;            /* File name. */
  249.     enum
  250.       {
  251.     OPEN,
  252.     FF_FOUND,        /* used with -b option, set with \f, changed
  253.                    to ON_HOLD after print_header */
  254.     ON_HOLD,        /* Hit a form feed. */
  255.     CLOSED
  256.       }
  257.     status;            /* Status of the file pointer. */
  258.     int (*print_func) ();    /* Func to print lines in this col. */
  259.     void (*char_func) ();    /* Func to print/store chars in this col. */
  260.     int current_line;        /* Index of current place in line_vector. */
  261.     int lines_stored;        /* Number of lines stored in buff. */
  262.     int lines_to_print;        /* No. lines stored or space left on page. */
  263.     int start_position;        /* Horizontal position of first char. */
  264.     int numbered;
  265.     int full_page_printed;    /* True means printed without a FF found. */
  266.  
  267.     /* p->full_page_printed  controls a special case of "FF set by hand":
  268.        True means a full page has been printed without FF found. To avoid an
  269.        additional empty page we have to ignore a FF immediately following in
  270.        the next line. */
  271.   };
  272.  
  273. typedef struct COLUMN COLUMN;
  274.  
  275. #define NULLCOL (COLUMN *)0
  276.  
  277. static int char_to_clump __P ((int c));
  278. static int read_line __P ((COLUMN *p));
  279. static int print_page __P ((void));
  280. static int print_stored __P ((COLUMN *p));
  281. static int open_file __P ((char *name, COLUMN *p));
  282. static int skip_to_page __P ((int page));
  283. static void print_header __P ((void));
  284. static void pad_across_to __P ((int position));
  285. static void number __P ((COLUMN *p));
  286. static void getoptarg __P ((char *arg, char switch_char, char *character, int *number));
  287. static void usage __P ((int status));
  288. static void print_files __P ((int number_of_files, char **av));
  289. static void init_parameters __P ((int number_of_files));
  290. static void init_header __P ((char *filename, int desc));
  291. static void init_store_cols __P ((void));
  292. static void store_columns __P ((void));
  293. static void balance __P ((int total_stored));
  294. static void store_char __P ((int c));
  295. static void pad_down __P ((int lines));
  296. static void read_rest_of_line __P ((COLUMN *p));
  297. static void skip_read __P ((COLUMN *p, int column_number));
  298. static void print_char __P ((int c));
  299. static void cleanup __P ((void));
  300. static void first_last_page __P ((char *pages));
  301. static void print_sep_string __P ((void));
  302. static void separator_string __P ((const char *optarg_S));
  303.  
  304. /* The name under which this program was invoked. */
  305. char *program_name;
  306.  
  307. /* All of the columns to print.  */
  308. static COLUMN *column_vector;
  309.  
  310. /* When printing a single file in multiple downward columns,
  311.    we store the leftmost columns contiguously in buff.
  312.    To print a line from buff, get the index of the first character
  313.    from line_vector[i], and print up to line_vector[i + 1]. */
  314. static char *buff;
  315.  
  316. /* Index of the position in buff where the next character
  317.    will be stored. */
  318. static int buff_current;
  319.  
  320. /* The number of characters in buff.
  321.    Used for allocation of buff and to detect overflow of buff. */
  322. static int buff_allocated;
  323.  
  324. /* Array of indices into buff.
  325.    Each entry is an index of the first character of a line.
  326.    This is used when storing lines to facilitate shuffling when
  327.    we do column balancing on the last page. */
  328. static int *line_vector;
  329.  
  330. /* Array of horizonal positions.
  331.    For each line in line_vector, end_vector[line] is the horizontal
  332.    position we are in after printing that line.  We keep track of this
  333.    so that we know how much we need to pad to prepare for the next
  334.    column. */
  335. static int *end_vector;
  336.  
  337. /* (-m) True means we're printing multiple files in parallel. */
  338. static int parallel_files = FALSE;
  339.  
  340. /* (-m) True means a line starts with some empty columns (some files
  341.    already CLOSED or ON_HOLD) which we have to align. */
  342. static int align_empty_cols;
  343.  
  344. /* (-m) True means we have not yet found any printable column in a line.
  345.    align_empty_cols = TRUE  has to be maintained. */
  346. static int empty_line;
  347.  
  348. /* (-m) False means printable column output precedes a form feed found.
  349.    Column align is done only once. No additional action with that form
  350.    feed.
  351.    True means we found only a form feed in a column. Maybe we have to do
  352.    some column align with that form feed. */
  353. static int FF_only;
  354.  
  355. /* (-[0-9]+) True means we're given an option explicitly specifying
  356.    number of columns.  Used to detect when this option is used with -m. */
  357. static int explicit_columns = FALSE;
  358.  
  359. /* (-t|-T) False means we aren't printing headers and footers. */
  360. static int extremities = TRUE;
  361.  
  362. /* (-t) True means we retain all FF set by hand in input files.
  363.    False is set with -T option. */
  364. static int keep_FF = FALSE;
  365. static int print_a_FF = FALSE;
  366.  
  367. /* True means we need to print a header as soon as we know we've got input
  368.    to print after it. */
  369. static int print_a_header;
  370.  
  371. /* (-h) True means we're using the standard header rather than a
  372.    customized one specified by the -h flag. */
  373. static int standard_header = TRUE;
  374.  
  375. /* (-f) True means use formfeeds instead of newlines to separate pages. */
  376. static int use_form_feed = FALSE;
  377.  
  378. /* True means we have read the standard input. */
  379. static int have_read_stdin = FALSE;
  380.  
  381. /* True means the -a flag has been given. */
  382. static int print_across_flag = FALSE;
  383.  
  384. /* True means we're printing one file in multiple (>1) downward columns. */
  385. static int storing_columns = TRUE;
  386.  
  387. /* (-b) True means balance columns on the last page as Sys V does. */
  388. /* That's no longer an independent option. With storing_columns = TRUE
  389.    balance_columns = TRUE is used too (s. function init_parameters).
  390.    We get a consistent formulation with "FF set by hand" in input files. */
  391. static int balance_columns = FALSE;
  392.  
  393. /* (-l) Number of lines on a page, including header and footer lines. */
  394. static int lines_per_page = 66;
  395.  
  396. /* Number of lines in the header and footer can be reset to 0 using
  397.    the -t flag. */
  398. static int lines_per_header = 5;
  399. static int lines_per_body;
  400. static int lines_per_footer = 5;
  401.  
  402. /* (-w) Width in characters of the page.  Does not include the width of
  403.    the margin. */
  404. static int chars_per_line = 72;
  405.  
  406. /* (-w) True means we truncate lines longer than chars_per_column. */
  407. static int truncate_lines = FALSE;
  408.  
  409. /* (-j) True means we join lines without any line truncation. -j
  410.    dominates -w option. */
  411. static int join_lines = FALSE;
  412.  
  413. /* Number of characters in a column.  Based on col_sep_length and
  414.    page width. */
  415. static int chars_per_column;
  416.  
  417. /* (-e) True means convert tabs to spaces on input. */
  418. static int untabify_input = FALSE;
  419.  
  420. /* (-e) The input tab character. */
  421. static char input_tab_char = '\t';
  422.  
  423. /* (-e) Tabstops are at chars_per_tab, 2*chars_per_tab, 3*chars_per_tab, ...
  424.    where the leftmost column is 1. */
  425. static int chars_per_input_tab = 8;
  426.  
  427. /* (-i) True means convert spaces to tabs on output. */
  428. static int tabify_output = FALSE;
  429.  
  430. /* (-i) The output tab character. */
  431. static char output_tab_char = '\t';
  432.  
  433. /* (-i) The width of the output tab. */
  434. static int chars_per_output_tab = 8;
  435.  
  436. /* Keeps track of pending white space.  When we hit a nonspace
  437.    character after some whitespace, we print whitespace, tabbing
  438.    if necessary to get to output_position + spaces_not_printed. */
  439. static int spaces_not_printed;
  440.  
  441. /* (-o) Number of spaces in the left margin (tabs used when possible). */
  442. static int chars_per_margin = 0;
  443.  
  444. /* Position where the next character will fall.
  445.    Leftmost position is 0 + chars_per_margin.
  446.    Rightmost position is chars_per_margin + chars_per_line - 1.
  447.    This is important for converting spaces to tabs on output. */
  448. static int output_position;
  449.  
  450. /* Horizontal position relative to the current file.
  451.    (output_position depends on where we are on the page;
  452.    input_position depends on where we are in the file.)
  453.    Important for converting tabs to spaces on input. */
  454. static int input_position;
  455.  
  456. /* Count number of failed opens so we can exit with nonzero
  457.    status if there were any.  */
  458. static int failed_opens = 0;
  459.  
  460. /* The number of spaces taken up if we print a tab character with width
  461.    c_ from position h_. */
  462. #define TAB_WIDTH(c_, h_) ((c_) - ((h_) % (c_)))
  463.  
  464. /* The horizontal position we'll be at after printing a tab character
  465.    of width c_ from the position h_. */
  466. #define POS_AFTER_TAB(c_, h_) ((h_) + TAB_WIDTH (c_, h_))
  467.  
  468. /* (-NNN) Number of columns of text to print. */
  469. static int columns = 1;
  470.  
  471. /* (+NNN:MMM) Page numbers on which to begin and stop printing. */
  472. static int first_page_number = 1;
  473. static int last_page_number = 0;
  474.  
  475. /* Number of files open (not closed, not on hold). */
  476. static int files_ready_to_read = 0;
  477.  
  478. /* Current page number.  Displayed in header. */
  479. static int page_number;
  480.  
  481. /* Current line number.  Displayed when -n flag is specified.
  482.  
  483.    When printing files in parallel (-m flag), line numbering is as follows:
  484.    1    foo     goo     moo
  485.    2    hoo     too     zoo
  486.  
  487.    When printing files across (-a flag), ...
  488.    1    foo     2       moo     3       goo
  489.    4    hoo     3       too     6       zoo
  490.  
  491.    Otherwise, line numbering is as follows:
  492.    1    foo     3       goo     5       too
  493.    2    moo     4       hoo     6       zoo */
  494. static int line_number;
  495.  
  496. /* (-n) True means lines should be preceded by numbers. */
  497. static int numbered_lines = FALSE;
  498.  
  499. /* (-n) Character which follows each line number. */
  500. static char number_separator = '\t';
  501.  
  502. /* (-n) line counting starts with 1st line of input file (not with 1st
  503.    line of 1st page printed). */
  504. static int line_count = 1;
  505.  
  506. /* (-n) True means counting of skipped lines starts with 1st line of
  507.    input file. False means -N option is used in addition, counting of
  508.    skipped lines not required. */
  509. static int skip_count = TRUE;
  510.  
  511. /* (-N) Counting starts with start_line_number = NUMBER at 1st line of
  512.    first page printed, usually not 1st page of input file. */
  513. static int start_line_num = 1;
  514.  
  515. /* (-n) Width in characters of a line number. */
  516. static int chars_per_number = 5;
  517.  
  518. /* Used when widening the first column to accommodate numbers -- only
  519.    needed when printing files in parallel.  Includes width of both the
  520.    number and the number_separator. */
  521. static int number_width;
  522.  
  523. /* Buffer sprintf uses to format a line number. */
  524. static char *number_buff;
  525.  
  526. /* (-v) True means unprintable characters are printed as escape sequences.
  527.    control-g becomes \007. */
  528. static int use_esc_sequence = FALSE;
  529.  
  530. /* (-c) True means unprintable characters are printed as control prefixes.
  531.    control-g becomes ^G. */
  532. static int use_cntrl_prefix = FALSE;
  533.  
  534. /* (-d) True means output is double spaced. */
  535. static int double_space = FALSE;
  536.  
  537. /* Number of files opened initially in init_files.  Should be 1
  538.    unless we're printing multiple files in parallel. */
  539. static int total_files = 0;
  540.  
  541. /* (-r) True means don't complain if we can't open a file. */
  542. static int ignore_failed_opens = FALSE;
  543.  
  544. /* (-s) True means we separate columns with a specified string.
  545.    -s option does not affect line truncation nor column alignment. */
  546. static int use_col_separator = FALSE;
  547.  
  548. /* String used to separate columns if the -s option has been specified.
  549.    Default value with -s is a space. */
  550. static char *col_sep_string;
  551. static int col_sep_length = 0;
  552. static char *column_separator = " ";
  553.  
  554. /* Number of separator characters waiting to be printed as soon as we
  555.    know that we have any input remaining to be printed. */
  556. static int separators_not_printed;
  557.  
  558. /* Position we need to pad to, as soon as we know that we have input
  559.    remaining to be printed. */
  560. static int padding_not_printed;
  561.  
  562. /* True means we should pad the end of the page.  Remains false until we
  563.    know we have a page to print. */
  564. static int pad_vertically;
  565.  
  566. /* (-h) String of characters used in place of the filename in the header. */
  567. static char *custom_header;
  568.  
  569. /* String containing the date, filename or custom header, and "Page ". */
  570. static char *header;
  571.  
  572. static int *clump_buff;
  573.  
  574. /* If nonzero, display usage information and exit.  */
  575. static int show_help;
  576.  
  577. /* If nonzero, print the version on standard output then exit.  */
  578. static int show_version;
  579.  
  580. /* True means we read the line no. lines_per_body in skip_read
  581.    called by skip_to_page. That variable controls the coincidence of a
  582.    "FF set by hand" and "full_page_printed", see above the definition of
  583.    structure COLUMN. */
  584. static int last_line = FALSE;
  585.  
  586. /* If nonzero, print a non-variable date and time with the header
  587.    -h HEADER using pr test-suite */
  588. static int test_suite;
  589.  
  590. static struct option const long_options[] =
  591. {
  592.   {"help", no_argument, &show_help, 1},
  593.   {"version", no_argument, &show_version, 1},
  594.   {"test", no_argument, &test_suite, 1},
  595.   {0, 0, 0, 0}
  596. };
  597.  
  598. /* Return the number of columns that have either an open file or
  599.    stored lines. */
  600.  
  601. static int
  602. cols_ready_to_print (void)
  603. {
  604.   COLUMN *q;
  605.   int i;
  606.   int n;
  607.  
  608.   n = 0;
  609.   for (q = column_vector, i = 0; i < columns; ++q, ++i)
  610.     if (q->status == OPEN ||
  611.     q->status == FF_FOUND ||    /* With -b: To print a header only */
  612.     (storing_columns && q->lines_stored > 0 && q->lines_to_print > 0))
  613.       ++n;
  614.   return n;
  615. }
  616.  
  617. /* Estimate first_ / last_page_number
  618.    using option +FIRST_PAGE:LAST_PAGE */
  619.  
  620. static void
  621. first_last_page (char *pages)
  622. {
  623.   char *str1;
  624.  
  625.   if (*pages == ':')
  626.     {
  627.       error (0, 0, _("invalid range of page numbers: `%s'"), pages);
  628.       usage (2);
  629.     }
  630.  
  631.   str1 = strchr (pages, ':');
  632.   if (str1 != NULL)
  633.     *str1 = '\0';
  634.  
  635.   {
  636.     long int tmp_long;
  637.     if (xstrtol (pages, NULL, 10, &tmp_long, "") != LONGINT_OK
  638.     || tmp_long <= 0 || tmp_long > INT_MAX)
  639.       error (EXIT_FAILURE, 0, _("invalid starting page number: `%s'"),
  640.          pages);
  641.     first_page_number = (int) tmp_long;
  642.   }
  643.  
  644.   if (str1 == NULL)
  645.     return;
  646.  
  647.   {
  648.     long int tmp_long;
  649.     if (xstrtol (str1 + 1, NULL, 10, &tmp_long, "") != LONGINT_OK
  650.     || tmp_long <= 0 || tmp_long > INT_MAX)
  651.       error (EXIT_FAILURE, 0, _("invalid ending page number: `%s'"),
  652.          str1 + 1);
  653.     last_page_number = (int) tmp_long;
  654.   }
  655.  
  656.   if (first_page_number > last_page_number)
  657.     error (EXIT_FAILURE, 0,
  658.        _("starting page number is larger than ending page number"));
  659. }
  660.  
  661. /* Estimate length of col_sep_string with option -s[STRING] */
  662.  
  663. static void
  664. separator_string (const char *optarg_S)
  665. {
  666.   col_sep_length = (int) strlen (optarg_S);
  667.   col_sep_string = (char *) xmalloc (col_sep_length + 1);
  668.   strcpy (col_sep_string, optarg_S);
  669. }
  670.  
  671. int
  672. main (int argc, char **argv)
  673. {
  674.   int c;
  675.   int accum = 0;
  676.   int n_files;
  677.   char **file_names;
  678.  
  679. #ifdef __EMX__
  680. _wildcard(&argc, &argv);
  681. #endif
  682.  
  683.  
  684.   program_name = argv[0];
  685. #ifndef __EMX__
  686.   setlocale (LC_ALL, "");
  687.   bindtextdomain (PACKAGE, LOCALEDIR);
  688.   textdomain (PACKAGE);
  689. #endif
  690.   n_files = 0;
  691.   file_names = (argc > 1
  692.         ? (char **) xmalloc ((argc - 1) * sizeof (char *))
  693.         : NULL);
  694.  
  695.   while (1)
  696.     {
  697.       c = getopt_long (argc, argv,
  698.                "-0123456789abcde::fFh:i::jl:mn::N:o:rs::tTvw:",
  699.                long_options, NULL);
  700.       if (c == 1)        /* Non-option argument. */
  701.     {
  702.       char *s;
  703.       s = optarg;
  704.       if (*s == '+')
  705.         {
  706.           ++s;
  707.           first_last_page (s);
  708.         }
  709.       else
  710.         {
  711.           file_names[n_files++] = optarg;
  712.         }
  713.     }
  714.       else
  715.     {
  716.       if (ISDIGIT (c))
  717.         {
  718.           accum = accum * 10 + c - '0';
  719.           continue;
  720.         }
  721.       else
  722.         {
  723.           if (accum > 0)
  724.         {
  725.           columns = accum;
  726.           explicit_columns = TRUE;
  727.           accum = 0;
  728.         }
  729.         }
  730.     }
  731.  
  732.       if (c == 1)
  733.     continue;
  734.  
  735.       if (c == EOF)
  736.     break;
  737.  
  738.       switch (c)
  739.     {
  740.     case 0:        /* getopt long option */
  741.       break;
  742.  
  743.     case 'a':
  744.       print_across_flag = TRUE;
  745.       storing_columns = FALSE;
  746.       break;
  747.     case 'b':
  748.       balance_columns = TRUE;
  749.       break;
  750.     case 'c':
  751.       use_cntrl_prefix = TRUE;
  752.       break;
  753.     case 'd':
  754.       double_space = TRUE;
  755.       break;
  756.     case 'e':
  757.       if (optarg)
  758.         getoptarg (optarg, 'e', &input_tab_char,
  759.                &chars_per_input_tab);
  760.       /* Could check tab width > 0. */
  761.       untabify_input = TRUE;
  762.       break;
  763.     case 'f':
  764.     case 'F':
  765.       use_form_feed = TRUE;
  766.       break;
  767.     case 'h':
  768.       custom_header = optarg;
  769.       standard_header = FALSE;
  770.       break;
  771.     case 'i':
  772.       if (optarg)
  773.         getoptarg (optarg, 'i', &output_tab_char,
  774.                &chars_per_output_tab);
  775.       /* Could check tab width > 0. */
  776.       tabify_output = TRUE;
  777.       break;
  778.     case 'j':
  779.       join_lines = TRUE;
  780.       break;
  781.     case 'l':
  782.       {
  783.         long int tmp_long;
  784.         if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
  785.         || tmp_long <= 0 || tmp_long > INT_MAX)
  786.           {
  787.         error (EXIT_FAILURE, 0,
  788.                _("`-l PAGE_LENGTH' invalid number of lines: `%s'"),
  789.                optarg);
  790.           }
  791.         lines_per_page = (int) tmp_long;
  792.         break;
  793.       }
  794.     case 'm':
  795.       parallel_files = TRUE;
  796.       storing_columns = FALSE;
  797.       break;
  798.     case 'n':
  799.       numbered_lines = TRUE;
  800.       if (optarg)
  801.         getoptarg (optarg, 'n', &number_separator,
  802.                &chars_per_number);
  803.       break;
  804.     case 'N':
  805.       skip_count = FALSE;
  806.       {
  807.         long int tmp_long;
  808.         if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
  809.         || tmp_long > INT_MAX)
  810.           {
  811.         error (EXIT_FAILURE, 0,
  812.                _("`-N NUMBER' invalid starting line number: `%s'"),
  813.                optarg);
  814.           }
  815.         start_line_num = (int) tmp_long;
  816.         break;
  817.       }
  818.     case 'o':
  819.       {
  820.         long int tmp_long;
  821.         if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
  822.         || tmp_long <= 0 || tmp_long > INT_MAX)
  823.           error (EXIT_FAILURE, 0,
  824.              _("`-o MARGIN' invalid line offset: `%s'"), optarg);
  825.         chars_per_margin = (int) tmp_long;
  826.         break;
  827.       }
  828.     case 'r':
  829.       ignore_failed_opens = TRUE;
  830.       break;
  831.     case 's':
  832.       use_col_separator = TRUE;
  833.       if (optarg)
  834.         separator_string (optarg);
  835.       break;
  836.     case 't':
  837.       extremities = FALSE;
  838.       keep_FF = TRUE;
  839.       break;
  840.     case 'T':
  841.       extremities = FALSE;
  842.       keep_FF = FALSE;
  843.       break;
  844.     case 'v':
  845.       use_esc_sequence = TRUE;
  846.       break;
  847.     case 'w':
  848.       truncate_lines = TRUE;
  849.       {
  850.         long int tmp_long;
  851.         if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
  852.         || tmp_long <= 0 || tmp_long > INT_MAX)
  853.           error (EXIT_FAILURE, 0,
  854.          _("`-w PAGE_WIDTH' invalid column number: `%s'"), optarg);
  855.         chars_per_line = (int) tmp_long;
  856.         break;
  857.       }
  858.     default:
  859.       usage (2);
  860.       break;
  861.     }
  862.     }
  863.  
  864.   if (show_version)
  865.     {
  866.       printf ("pr (%s) %s\n", GNU_PACKAGE, VERSION);
  867.       exit (EXIT_SUCCESS);
  868.     }
  869.  
  870.   if (show_help)
  871.     usage (0);
  872.  
  873.   if (parallel_files && explicit_columns)
  874.     error (EXIT_FAILURE, 0,
  875.      _("Cannot specify number of columns when printing in parallel."));
  876.  
  877.   if (parallel_files && print_across_flag)
  878.     error (EXIT_FAILURE, 0,
  879.        _("Cannot specify both printing across and printing in parallel."));
  880.  
  881.   for (; optind < argc; optind++)
  882.     {
  883.       file_names[n_files++] = argv[optind];
  884.     }
  885.  
  886.   if (n_files == 0)
  887.     {
  888.       /* No file arguments specified;  read from standard input.  */
  889.       print_files (0, NULL);
  890.     }
  891.   else
  892.     {
  893.       if (parallel_files)
  894.     print_files (n_files, file_names);
  895.       else
  896.     {
  897.       int i;
  898.       for (i = 0; i < n_files; i++)
  899.         print_files (1, &file_names[i]);
  900.     }
  901.     }
  902.  
  903.   cleanup ();
  904.  
  905.   if (have_read_stdin && fclose (stdin) == EOF)
  906.     error (EXIT_FAILURE, errno, _("standard input"));
  907.   if (ferror (stdout) || fclose (stdout) == EOF)
  908.     error (EXIT_FAILURE, errno, _("write error"));
  909.   if (failed_opens > 0)
  910.     exit (EXIT_FAILURE);
  911.   exit (EXIT_SUCCESS);
  912. }
  913.  
  914. /* Parse options of the form -scNNN.
  915.  
  916.    Example: -nck, where 'n' is the option, c is the optional number
  917.    separator, and k is the optional width of the field used when printing
  918.    a number. */
  919.  
  920. static void
  921. getoptarg (char *arg, char switch_char, char *character, int *number)
  922. {
  923.   if (!ISDIGIT (*arg))
  924.     *character = *arg++;
  925.   if (*arg)
  926.     {
  927.       long int tmp_long;
  928.       if (xstrtol (arg, NULL, 10, &tmp_long, "") != LONGINT_OK
  929.       || tmp_long <= 0 || tmp_long > INT_MAX)
  930.     {
  931.       error (0, 0,
  932.          _("`-%c' extra characters or invalid number in the argument: `%s'"),
  933.          switch_char, arg);
  934.       usage (2);
  935.     }
  936.       *number = (int) tmp_long;
  937.     }
  938. }
  939.  
  940. /* Set parameters related to formatting. */
  941.  
  942. static void
  943. init_parameters (int number_of_files)
  944. {
  945.   int chars_used_by_number = 0;
  946.  
  947.   if (use_form_feed)
  948.     {
  949.       lines_per_header = 3;
  950.       lines_per_footer = 0;
  951.     }
  952.  
  953.   lines_per_body = lines_per_page - lines_per_header - lines_per_footer;
  954.   if (lines_per_body <= 0)
  955.     {
  956.       extremities = FALSE;
  957.       keep_FF = TRUE;
  958.     }
  959.   if (extremities == FALSE)
  960.     lines_per_body = lines_per_page;
  961.  
  962.   if (double_space)
  963.     lines_per_body = lines_per_body / 2;
  964.  
  965.   /* If input is stdin, cannot print parallel files.  BSD dumps core
  966.      on this. */
  967.   if (number_of_files == 0)
  968.     parallel_files = FALSE;
  969.  
  970.   if (parallel_files)
  971.     columns = number_of_files;
  972.  
  973.   /* One file, multi columns down: -b option is set to get a consistent
  974.      formulation with "FF set by hand" in input files. */
  975.   if (storing_columns)
  976.     balance_columns = TRUE;
  977.  
  978.   /* Tabification is assumed for multiple columns. */
  979.   if (columns > 1)
  980.     {
  981.       if (!use_col_separator)
  982.     {
  983.       col_sep_string = column_separator;
  984.       col_sep_length = 1;
  985.       use_col_separator = TRUE;
  986.     }
  987.  
  988.       truncate_lines = TRUE;
  989.       untabify_input = TRUE;
  990.       tabify_output = TRUE;
  991.     }
  992.   else
  993.     storing_columns = FALSE;
  994.  
  995.   /* -j dominates -w in any case */
  996.   if (join_lines)
  997.     truncate_lines = FALSE;
  998.  
  999.   if (numbered_lines)
  1000.     {
  1001.       line_count = start_line_num;
  1002.       if (number_separator == input_tab_char)
  1003.     {
  1004.       number_width = chars_per_number +
  1005.         TAB_WIDTH (chars_per_input_tab,
  1006.                (chars_per_margin + chars_per_number));
  1007.     }
  1008.       else
  1009.     number_width = chars_per_number + 1;
  1010.       /* The number is part of the column width unless we are
  1011.          printing files in parallel. */
  1012.       if (parallel_files)
  1013.     chars_used_by_number = number_width;
  1014.     }
  1015.  
  1016.   chars_per_column = (chars_per_line - chars_used_by_number -
  1017.               (columns - 1) * col_sep_length) / columns;
  1018.  
  1019.   if (chars_per_column < 1)
  1020.     error (EXIT_FAILURE, 0, _("page width too narrow"));
  1021.  
  1022.   if (numbered_lines)
  1023.     {
  1024.       if (number_buff != NULL)
  1025.     free (number_buff);
  1026.       number_buff = (char *) xmalloc (2 * chars_per_number);
  1027.     }
  1028.  
  1029.   /* Pick the maximum between the tab width and the width of an
  1030.      escape sequence. */
  1031.   if (clump_buff != NULL)
  1032.     free (clump_buff);
  1033.   clump_buff = (int *) xmalloc ((chars_per_input_tab > 4
  1034.                  ? chars_per_input_tab : 4) * sizeof (int));
  1035. }
  1036.  
  1037. /* Open the necessary files,
  1038.    maintaining a COLUMN structure for each column.
  1039.  
  1040.    With multiple files, each column p has a different p->fp.
  1041.    With single files, each column p has the same p->fp.
  1042.    Return 1 if (number_of_files > 0) and no files can be opened,
  1043.    0 otherwise.
  1044.  
  1045.    With each column/file p, p->full_page_printed is initialized,
  1046.    see also open_file.  */
  1047.  
  1048. static int
  1049. init_fps (int number_of_files, char **av)
  1050. {
  1051.   int i, files_left;
  1052.   COLUMN *p;
  1053.   FILE *firstfp;
  1054.   char *firstname;
  1055.  
  1056.   total_files = 0;
  1057.  
  1058.   if (column_vector != NULLCOL)
  1059.     free ((char *) column_vector);
  1060.   column_vector = (COLUMN *) xmalloc (columns * sizeof (COLUMN));
  1061.  
  1062.   if (parallel_files)
  1063.     {
  1064.       files_left = number_of_files;
  1065.       for (p = column_vector; files_left--; ++p, ++av)
  1066.     {
  1067.       if (open_file (*av, p) == 0)
  1068.         {
  1069.           --p;
  1070.           --columns;
  1071.         }
  1072.     }
  1073.       if (columns == 0)
  1074.     return 1;
  1075.       init_header ("", -1);
  1076.     }
  1077.   else
  1078.     {
  1079.       p = column_vector;
  1080.       if (number_of_files > 0)
  1081.     {
  1082.       if (open_file (*av, p) == 0)
  1083.         return 1;
  1084.       init_header (*av, fileno (p->fp));
  1085.       p->lines_stored = 0;
  1086.     }
  1087.       else
  1088.     {
  1089.       p->name = _("standard input");
  1090.       p->fp = stdin;
  1091.       have_read_stdin = TRUE;
  1092.       p->status = OPEN;
  1093.       p->full_page_printed = FALSE;
  1094.       ++total_files;
  1095.       init_header ("", -1);
  1096.       p->lines_stored = 0;
  1097.     }
  1098.  
  1099.       firstname = p->name;
  1100.       firstfp = p->fp;
  1101.       for (i = columns - 1, ++p; i; --i, ++p)
  1102.     {
  1103.       p->name = firstname;
  1104.       p->fp = firstfp;
  1105.       p->status = OPEN;
  1106.       p->full_page_printed = FALSE;
  1107.       p->lines_stored = 0;
  1108.     }
  1109.     }
  1110.   files_ready_to_read = total_files;
  1111.   return 0;
  1112. }
  1113.  
  1114. /* Determine print_func and char_func, the functions
  1115.    used by each column for printing and/or storing.
  1116.  
  1117.    Determine the horizontal position desired when we begin
  1118.    printing a column (p->start_position). */
  1119.  
  1120. static void
  1121. init_funcs (void)
  1122. {
  1123.   int i, h, h_next;
  1124.   COLUMN *p;
  1125.  
  1126.   h = chars_per_margin;
  1127.  
  1128.   if (!truncate_lines)
  1129.     h_next = ANYWHERE;
  1130.   else
  1131.     {
  1132.       /* When numbering lines of parallel files, we enlarge the
  1133.          first column to accomodate the number.  Looks better than
  1134.          the Sys V approach. */
  1135.       if (parallel_files && numbered_lines)
  1136.     h_next = h + chars_per_column + number_width;
  1137.       else
  1138.     h_next = h + chars_per_column;
  1139.     }
  1140.  
  1141.   /* Enlarge p->start_position of first column to use the same form of
  1142.      padding_not_printed with all columns. */
  1143.   h = h + col_sep_length;
  1144.  
  1145.   /* This loop takes care of all but the rightmost column. */
  1146.  
  1147.   for (p = column_vector, i = 1; i < columns; ++p, ++i)
  1148.     {
  1149.       if (storing_columns)    /* One file, multi columns down. */
  1150.     {
  1151.       p->char_func = store_char;
  1152.       p->print_func = print_stored;
  1153.     }
  1154.       else
  1155.     /* One file, multi columns across; or parallel files.  */
  1156.     {
  1157.       p->char_func = print_char;
  1158.       p->print_func = read_line;
  1159.     }
  1160.  
  1161.       /* Number only the first column when printing files in
  1162.          parallel. */
  1163.       p->numbered = numbered_lines && (!parallel_files || i == 1);
  1164.       p->start_position = h;
  1165.  
  1166.       /* If we don't truncate lines, all start_positions are
  1167.          ANYWHERE, except the first column's start_position when
  1168.          using a margin. */
  1169.  
  1170.       if (!truncate_lines)
  1171.     {
  1172.       h = ANYWHERE;
  1173.       h_next = ANYWHERE;
  1174.     }
  1175.       else
  1176.     {
  1177.       h = h_next + col_sep_length;
  1178.       h_next = h + chars_per_column;
  1179.     }
  1180.     }
  1181.  
  1182.   /* The rightmost column.
  1183.  
  1184.      Doesn't need to be stored unless we intend to balance
  1185.      columns on the last page. */
  1186.   if (storing_columns && balance_columns)
  1187.     {
  1188.       p->char_func = store_char;
  1189.       p->print_func = print_stored;
  1190.     }
  1191.   else
  1192.     {
  1193.       p->char_func = print_char;
  1194.       p->print_func = read_line;
  1195.     }
  1196.  
  1197.   p->numbered = numbered_lines && (!parallel_files || i == 1);
  1198.   p->start_position = h;
  1199. }
  1200.  
  1201. /* Open a file.  Return nonzero if successful, zero if failed.
  1202.  
  1203.    With each file p, p->full_page_printed is initialized,
  1204.    see also init_fps. */
  1205.  
  1206. static int
  1207. open_file (char *name, COLUMN *p)
  1208. {
  1209.   if (!strcmp (name, "-"))
  1210.     {
  1211.       p->name = _("standard input");
  1212.       p->fp = stdin;
  1213.       have_read_stdin = 1;
  1214.     }
  1215.   else
  1216.     {
  1217.       p->name = name;
  1218.       p->fp = fopen (name, "r");
  1219.     }
  1220.   if (p->fp == NULL)
  1221.     {
  1222.       ++failed_opens;
  1223.       if (!ignore_failed_opens)
  1224.     error (0, errno, "%s", name);
  1225.       return 0;
  1226.     }
  1227.   p->status = OPEN;
  1228.   p->full_page_printed = FALSE;
  1229.   ++total_files;
  1230.   return 1;
  1231. }
  1232.  
  1233. /* Close the file in P.
  1234.  
  1235.    If we aren't dealing with multiple files in parallel, we change
  1236.    the status of all columns in the column list to reflect the close. */
  1237.  
  1238. static void
  1239. close_file (COLUMN *p)
  1240. {
  1241.   COLUMN *q;
  1242.   int i;
  1243.  
  1244.   if (p->status == CLOSED)
  1245.     return;
  1246.   if (ferror (p->fp))
  1247.     error (EXIT_FAILURE, errno, "%s", p->name);
  1248.   if (p->fp != stdin && fclose (p->fp) == EOF)
  1249.     error (EXIT_FAILURE, errno, "%s", p->name);
  1250.  
  1251.   if (!parallel_files)
  1252.     {
  1253.       for (q = column_vector, i = columns; i; ++q, --i)
  1254.     {
  1255.       q->status = CLOSED;
  1256.       if (q->lines_stored == 0)
  1257.         {
  1258.           q->lines_to_print = 0;
  1259.         }
  1260.     }
  1261.     }
  1262.   else
  1263.     {
  1264.       p->status = CLOSED;
  1265.       p->lines_to_print = 0;
  1266.     }
  1267.  
  1268.   --files_ready_to_read;
  1269. }
  1270.  
  1271. /* Put a file on hold until we start a new page,
  1272.    since we've hit a form feed.
  1273.  
  1274.    If we aren't dealing with parallel files, we must change the
  1275.    status of all columns in the column list. */
  1276.  
  1277. static void
  1278. hold_file (COLUMN *p)
  1279. {
  1280.   COLUMN *q;
  1281.   int i;
  1282.  
  1283.   if (!parallel_files)
  1284.     for (q = column_vector, i = columns; i; ++q, --i)
  1285.       {
  1286.     if (storing_columns)
  1287.       q->status = FF_FOUND;
  1288.     else
  1289.       q->status = ON_HOLD;
  1290.       }
  1291.   else
  1292.     p->status = ON_HOLD;
  1293.  
  1294.   p->lines_to_print = 0;
  1295.   --files_ready_to_read;
  1296. }
  1297.  
  1298. /* Undo hold_file -- go through the column list and change any
  1299.    ON_HOLD columns to OPEN.  Used at the end of each page. */
  1300.  
  1301. static void
  1302. reset_status (void)
  1303. {
  1304.   int i = columns;
  1305.   COLUMN *p;
  1306.  
  1307.   for (p = column_vector; i; --i, ++p)
  1308.     if (p->status == ON_HOLD)
  1309.       {
  1310.     p->status = OPEN;
  1311.     files_ready_to_read++;
  1312.       }
  1313.  
  1314.   if (storing_columns)
  1315.     files_ready_to_read = 1;
  1316. }
  1317.  
  1318. /* Print a single file, or multiple files in parallel.
  1319.  
  1320.    Set up the list of columns, opening the necessary files.
  1321.    Allocate space for storing columns, if necessary.
  1322.    Skip to first_page_number, if user has asked to skip leading pages.
  1323.    Determine which functions are appropriate to store/print lines
  1324.    in each column.
  1325.    Print the file(s). */
  1326.  
  1327. static void
  1328. print_files (int number_of_files, char **av)
  1329. {
  1330.   init_parameters (number_of_files);
  1331.   if (init_fps (number_of_files, av))
  1332.     return;
  1333.   if (storing_columns)
  1334.     init_store_cols ();
  1335.  
  1336.   if (first_page_number > 1)
  1337.     {
  1338.       if (!skip_to_page (first_page_number))
  1339.     return;
  1340.       else
  1341.     page_number = first_page_number;
  1342.     }
  1343.   else
  1344.     page_number = 1;
  1345.  
  1346.   init_funcs ();
  1347.  
  1348.   line_number = line_count;
  1349.   while (print_page ())
  1350.     ;
  1351. }
  1352.  
  1353. /* Estimate the number of characters taken up by a short format date and
  1354.    time:  "yy-mm-dd HH:MM"  and:  "Page NNNN". */
  1355. #define CHARS_FOR_DATE_AND_PAGE    23
  1356.  
  1357. /* Initialize header information.
  1358.    If DESC is non-negative, it is a file descriptor open to
  1359.    FILENAME for reading.
  1360.  
  1361.    Allocate space for a header string,
  1362.    Determine the time, insert file name or user-specified string.
  1363.    Make use of a centered header with left-hand-side truncation marked by
  1364.    a '*` in front, if necessary. */
  1365.  
  1366. static void
  1367. init_header (char *filename, int desc)
  1368. {
  1369.   int chars_per_middle, chars_free, lhs_blanks, rhs_blanks;
  1370.   char *f = filename;
  1371.   char *no_middle = "";
  1372.   char *header_text;
  1373.   struct tm *tmptr;
  1374.   struct stat st;
  1375.   char *datim = "- Date/Time --";
  1376.  
  1377.   if (filename == NULL)
  1378.     f = "";
  1379.  
  1380.   if (header != NULL)
  1381.     free (header);
  1382.   header = (char *) xmalloc (chars_per_line + 1);
  1383.  
  1384.   if (!standard_header && *custom_header == '\0')
  1385.     sprintf (header, "%s", " ");    /* blank line header */
  1386.   else
  1387.     {
  1388. #define T_BUF_FMT "%y-%m-%d %H:%M"    /* date/time short format */
  1389. #define T_BUF_SIZE 15
  1390.       char t_buf[T_BUF_SIZE];
  1391.  
  1392.       /* If parallel files or standard input, use current time. */
  1393.       if (desc < 0 || !strcmp (filename, "-") || fstat (desc, &st))
  1394.     st.st_mtime = time (NULL);
  1395.  
  1396.       tmptr = localtime (&st.st_mtime);
  1397.       strftime (t_buf, T_BUF_SIZE, T_BUF_FMT, tmptr);
  1398.  
  1399.       chars_per_middle = chars_per_line - CHARS_FOR_DATE_AND_PAGE;
  1400.       if (chars_per_middle < 3)
  1401.     {
  1402.       header_text = no_middle;    /* Nothing free for a heading */
  1403.       lhs_blanks = 1;
  1404.       rhs_blanks = 1;
  1405.     }
  1406.       else
  1407.     {
  1408.       header_text = standard_header ? f : custom_header;
  1409.       chars_free = chars_per_middle - (int) strlen (header_text);
  1410.       if (chars_free > 1)
  1411.         {
  1412.           lhs_blanks = chars_free / 2;    /* text not truncated */
  1413.           rhs_blanks = chars_free - lhs_blanks;
  1414.         }
  1415.       else
  1416.         {            /* lhs truncation */
  1417.           header_text = header_text - chars_free + 2;
  1418.           *header_text = '*';
  1419.           lhs_blanks = 1;
  1420.           rhs_blanks = 1;
  1421.         }
  1422.     }
  1423.  
  1424.       sprintf (header, _("%s%*s%s%*sPage"), (test_suite ? datim : t_buf),
  1425.            lhs_blanks, " ", header_text, rhs_blanks, " ");
  1426.     }
  1427. }
  1428.  
  1429. /* Set things up for printing a page
  1430.  
  1431.    Scan through the columns ...
  1432.    Determine which are ready to print
  1433.    (i.e., which have lines stored or open files)
  1434.    Set p->lines_to_print appropriately
  1435.    (to p->lines_stored if we're storing, or lines_per_body
  1436.    if we're reading straight from the file)
  1437.    Keep track of this total so we know when to stop printing */
  1438.  
  1439. static void
  1440. init_page (void)
  1441. {
  1442.   int j;
  1443.   COLUMN *p;
  1444.  
  1445.   if (storing_columns)
  1446.     {
  1447.       store_columns ();
  1448.       for (j = columns - 1, p = column_vector; j; --j, ++p)
  1449.     {
  1450.       p->lines_to_print = p->lines_stored;
  1451.     }
  1452.  
  1453.       /* Last column. */
  1454.       if (balance_columns)
  1455.     {
  1456.       p->lines_to_print = p->lines_stored;
  1457.     }
  1458.       /* Since we're not balancing columns, we don't need to store
  1459.          the rightmost column.   Read it straight from the file. */
  1460.       else
  1461.     {
  1462.       if (p->status == OPEN)
  1463.         {
  1464.           p->lines_to_print = lines_per_body;
  1465.         }
  1466.       else
  1467.         p->lines_to_print = 0;
  1468.     }
  1469.     }
  1470.   else
  1471.     for (j = columns, p = column_vector; j; --j, ++p)
  1472.       if (p->status == OPEN)
  1473.     {
  1474.       p->lines_to_print = lines_per_body;
  1475.     }
  1476.       else
  1477.     p->lines_to_print = 0;
  1478. }
  1479.  
  1480. /* Align empty columns and print separators.
  1481.    Empty columns will be formed by files with status ON_HOLD or CLOSED
  1482.    when printing multiple files in parallel. */
  1483.  
  1484. static void
  1485. align_column (COLUMN *p)
  1486. {
  1487.   padding_not_printed = p->start_position;
  1488.   if (padding_not_printed - col_sep_length > 0)
  1489.     {
  1490.       pad_across_to (padding_not_printed - col_sep_length);
  1491.       padding_not_printed = ANYWHERE;
  1492.     }
  1493.  
  1494.   if (use_col_separator)
  1495.     print_sep_string ();
  1496.  
  1497.   if (p->numbered)
  1498.     number (p);
  1499. }
  1500.  
  1501. /* Print one page.
  1502.  
  1503.    As long as there are lines left on the page and columns ready to print,
  1504.    Scan across the column list
  1505.    if the column has stored lines or the file is open
  1506.    pad to the appropriate spot
  1507.    print the column
  1508.    pad the remainder of the page with \n or \f as requested
  1509.    reset the status of all files -- any files which where on hold because
  1510.    of formfeeds are now put back into the lineup. */
  1511.  
  1512. static int
  1513. print_page (void)
  1514. {
  1515.   int j;
  1516.   int lines_left_on_page;
  1517.   COLUMN *p;
  1518.  
  1519.   /* Used as an accumulator (with | operator) of successive values of
  1520.      pad_vertically.  The trick is to set pad_vertically
  1521.      to zero before each run through the inner loop, then after that
  1522.      loop, it tells us whether a line was actually printed (whether a
  1523.      newline needs to be output -- or two for double spacing).  But those
  1524.      values have to be accumulated (in pv) so we can invoke pad_down
  1525.      properly after the outer loop completes. */
  1526.   int pv;
  1527.  
  1528.   init_page ();
  1529.  
  1530.   if (cols_ready_to_print () == 0)
  1531.     return FALSE;
  1532.  
  1533.   if (extremities)
  1534.     print_a_header = TRUE;
  1535.  
  1536.   /* Don't pad unless we know a page was printed. */
  1537.   pad_vertically = FALSE;
  1538.   pv = FALSE;
  1539.  
  1540.   lines_left_on_page = lines_per_body;
  1541.   if (double_space)
  1542.     lines_left_on_page *= 2;
  1543.  
  1544.   while (lines_left_on_page > 0 && cols_ready_to_print () > 0)
  1545.     {
  1546.       output_position = 0;
  1547.       spaces_not_printed = 0;
  1548.       separators_not_printed = 0;
  1549.       pad_vertically = FALSE;
  1550.       align_empty_cols = FALSE;
  1551.       empty_line = TRUE;
  1552.  
  1553.       for (j = 1, p = column_vector; j <= columns; ++j, ++p)
  1554.     {
  1555.       input_position = 0;
  1556.       if (p->lines_to_print > 0 || p->status == FF_FOUND)
  1557.         {
  1558.           FF_only = FALSE;
  1559.           padding_not_printed = p->start_position;
  1560.           if (!(p->print_func) (p))
  1561.         read_rest_of_line (p);
  1562.           pv |= pad_vertically;
  1563.  
  1564.           --p->lines_to_print;
  1565.           if (p->lines_to_print <= 0)
  1566.         {
  1567.           if (cols_ready_to_print () <= 0)
  1568.             break;
  1569.         }
  1570.  
  1571.           /* File p changed its status to ON_HOLD or CLOSED */
  1572.           if (parallel_files && p->status != OPEN)
  1573.         {
  1574.           if (empty_line)
  1575.             align_empty_cols = TRUE;
  1576.           else if (p->status == CLOSED ||
  1577.                (p->status == ON_HOLD && FF_only))
  1578.             align_column (p);
  1579.         }
  1580.         }
  1581.       else if (parallel_files)
  1582.         {
  1583.           /* File status ON_HOLD or CLOSED */
  1584.           if (empty_line)
  1585.         align_empty_cols = TRUE;
  1586.           else
  1587.         align_column (p);
  1588.         }
  1589.  
  1590.       /* We need it also with an empty column */
  1591.       if (use_col_separator)
  1592.         ++separators_not_printed;
  1593.     }
  1594.  
  1595.       if (pad_vertically)
  1596.     {
  1597.       putchar ('\n');
  1598.       --lines_left_on_page;
  1599.     }
  1600.  
  1601.       if (double_space && pv && extremities)
  1602.     {
  1603.       putchar ('\n');
  1604.       --lines_left_on_page;
  1605.     }
  1606.     }
  1607.  
  1608.   if (lines_left_on_page == 0)
  1609.     for (j = 1, p = column_vector; j <= columns; ++j, ++p)
  1610.       if (p->status == OPEN)
  1611.     p->full_page_printed = TRUE;
  1612.  
  1613.   pad_vertically = pv;
  1614.  
  1615.   if (pad_vertically && extremities)
  1616.     pad_down (lines_left_on_page + lines_per_footer);
  1617.   else if (keep_FF && print_a_FF)
  1618.     {
  1619.       putchar ('\f');
  1620.       print_a_FF = FALSE;
  1621.     }
  1622.  
  1623.   if (last_page_number && page_number > last_page_number)
  1624.     return FALSE;        /* Stop printing with LAST_PAGE */
  1625.  
  1626.   reset_status ();        /* Change ON_HOLD to OPEN. */
  1627.  
  1628.   return TRUE;            /* More pages to go. */
  1629. }
  1630.  
  1631. /* Allocate space for storing columns.
  1632.  
  1633.    This is necessary when printing multiple columns from a single file.
  1634.    Lines are stored consecutively in buff, separated by '\0'.
  1635.  
  1636.    The following doesn't apply any longer - any tuning possible?
  1637.    (We can't use a fixed offset since with the '-s' flag lines aren't
  1638.    truncated.)
  1639.  
  1640.    We maintain a list (line_vector) of pointers to the beginnings
  1641.    of lines in buff.  We allocate one more than the number of lines
  1642.    because the last entry tells us the index of the last character,
  1643.    which we need to know in order to print the last line in buff. */
  1644.  
  1645. static void
  1646. init_store_cols (void)
  1647. {
  1648.   int total_lines = lines_per_body * columns;
  1649.   int chars_if_truncate = total_lines * (chars_per_column + 1);
  1650.  
  1651.   if (line_vector != NULL)
  1652.     free ((int *) line_vector);
  1653.   /* FIXME: here's where it was allocated.  */
  1654.   line_vector = (int *) xmalloc ((total_lines + 1) * sizeof (int *));
  1655.  
  1656.   if (end_vector != NULL)
  1657.     free ((int *) end_vector);
  1658.   end_vector = (int *) xmalloc (total_lines * sizeof (int *));
  1659.  
  1660.   if (buff != NULL)
  1661.     free (buff);
  1662.   buff_allocated = (use_col_separator
  1663.             ? 2 * chars_if_truncate
  1664.             : chars_if_truncate);    /* Tune this. */
  1665.   buff = (char *) xmalloc (buff_allocated);
  1666. }
  1667.  
  1668. /* Store all but the rightmost column.
  1669.    (Used when printing a single file in multiple downward columns)
  1670.  
  1671.    For each column
  1672.    set p->current_line to be the index in line_vector of the
  1673.    first line in the column
  1674.    For each line in the column
  1675.    store the line in buff
  1676.    add to line_vector the index of the line's first char
  1677.    buff_start is the index in buff of the first character in the
  1678.    current line. */
  1679.  
  1680. static void
  1681. store_columns (void)
  1682. {
  1683.   int i, j;
  1684.   int line = 0;
  1685.   int buff_start;
  1686.   int last_col;            /* The rightmost column which will be saved in buff */
  1687.   COLUMN *p;
  1688.  
  1689.   buff_current = 0;
  1690.   buff_start = 0;
  1691.  
  1692.   if (balance_columns)
  1693.     last_col = columns;
  1694.   else
  1695.     last_col = columns - 1;
  1696.  
  1697.   for (i = 1, p = column_vector; i <= last_col; ++i, ++p)
  1698.     p->lines_stored = 0;
  1699.  
  1700.   for (i = 1, p = column_vector; i <= last_col && files_ready_to_read;
  1701.        ++i, ++p)
  1702.     {
  1703.       p->current_line = line;
  1704.       for (j = lines_per_body; j && files_ready_to_read; --j)
  1705.  
  1706.     if (p->status == OPEN)    /* Redundant.  Clean up. */
  1707.       {
  1708.         input_position = 0;
  1709.  
  1710.         if (!read_line (p))
  1711.           read_rest_of_line (p);
  1712.  
  1713.         if (p->status == OPEN
  1714.         || buff_start != buff_current)
  1715.           {
  1716.         ++p->lines_stored;
  1717.         line_vector[line] = buff_start;
  1718.         end_vector[line++] = input_position;
  1719.         buff_start = buff_current;
  1720.           }
  1721.       }
  1722.     }
  1723.  
  1724.   /* Keep track of the location of the last char in buff. */
  1725.   line_vector[line] = buff_start;
  1726.  
  1727.   if (balance_columns && p->lines_stored != lines_per_body)
  1728.     balance (line);
  1729. }
  1730.  
  1731. static void
  1732. balance (int total_stored)
  1733. {
  1734.   COLUMN *p;
  1735.   int i, lines;
  1736.   int first_line = 0;
  1737.  
  1738.   for (i = 1, p = column_vector; i <= columns; ++i, ++p)
  1739.     {
  1740.       lines = total_stored / columns;
  1741.       if (i <= total_stored % columns)
  1742.     ++lines;
  1743.  
  1744.       p->lines_stored = lines;
  1745.       p->current_line = first_line;
  1746.  
  1747.       first_line += lines;
  1748.     }
  1749. }
  1750.  
  1751. /* Store a character in the buffer. */
  1752.  
  1753. static void
  1754. store_char (int c)
  1755. {
  1756.   if (buff_current >= buff_allocated)
  1757.     {
  1758.       /* May be too generous. */
  1759.       buff_allocated = 2 * buff_allocated;
  1760.       buff = (char *) xrealloc (buff, buff_allocated * sizeof (char));
  1761.     }
  1762.   buff[buff_current++] = (char) c;
  1763. }
  1764.  
  1765. static void
  1766. number (COLUMN *p)
  1767. {
  1768.   int i;
  1769.   char *s;
  1770.  
  1771.   sprintf (number_buff, "%*d", chars_per_number, line_number++);
  1772.   s = number_buff;
  1773.   for (i = chars_per_number; i > 0; i--)
  1774.     (p->char_func) ((int) *s++);
  1775.  
  1776.   if (number_separator == input_tab_char)
  1777.     {
  1778.       i = number_width - chars_per_number;
  1779.       while (i-- > 0)
  1780.     (p->char_func) ((int) ' ');
  1781.     }
  1782.   else
  1783.     (p->char_func) ((int) number_separator);
  1784.  
  1785.   if (truncate_lines && !parallel_files)
  1786.     input_position += number_width;
  1787. }
  1788.  
  1789. /* Print (or store) padding until the current horizontal position
  1790.    is position. */
  1791.  
  1792. static void
  1793. pad_across_to (int position)
  1794. {
  1795.   register int h = output_position;
  1796.  
  1797.   if (tabify_output)
  1798.     spaces_not_printed = position - output_position;
  1799.   else
  1800.     {
  1801.       while (++h <= position)
  1802.     putchar (' ');
  1803.       output_position = position;
  1804.     }
  1805. }
  1806.  
  1807. /* Pad to the bottom of the page.
  1808.  
  1809.    If the user has requested a formfeed, use one.
  1810.    Otherwise, use newlines. */
  1811.  
  1812. static void
  1813. pad_down (int lines)
  1814. {
  1815.   register int i;
  1816.  
  1817.   if (use_form_feed)
  1818.     putchar ('\f');
  1819.   else
  1820.     for (i = lines; i; --i)
  1821.       putchar ('\n');
  1822. }
  1823.  
  1824. /* Read the rest of the line.
  1825.  
  1826.    Read from the current column's file until an end of line is
  1827.    hit.  Used when we've truncated a line and we no longer need
  1828.    to print or store its characters. */
  1829.  
  1830. static void
  1831. read_rest_of_line (COLUMN *p)
  1832. {
  1833.   register int c;
  1834.   FILE *f = p->fp;
  1835.  
  1836.   while ((c = getc (f)) != '\n')
  1837.     {
  1838.       if (c == '\f')
  1839.     {
  1840.       if ((c = getc (f)) != '\n')
  1841.         ungetc (c, f);
  1842.       if (keep_FF)
  1843.         print_a_FF = TRUE;
  1844.       hold_file (p);
  1845.       break;
  1846.     }
  1847.       else if (c == EOF)
  1848.     {
  1849.       close_file (p);
  1850.       break;
  1851.     }
  1852.     }
  1853. }
  1854.  
  1855. /* Read a line with skip_to_page.
  1856.  
  1857.    Read from the current column's file until an end of line is
  1858.    hit.  Used when we read full lines to skip pages.
  1859.    With skip_to_page we have to check for FF-coincidence which is done
  1860.    in function read_line otherwise.
  1861.    Count lines of skipped pages to find the line number of 1st page
  1862.    printed relative to 1st line of input file (start_line_num). */
  1863.  
  1864. static void
  1865. skip_read (COLUMN *p, int column_number)
  1866. {
  1867.   register int c;
  1868.   FILE *f = p->fp;
  1869.   int i, single_ff = FALSE;
  1870.   COLUMN *q;
  1871.  
  1872.   /* Read 1st character in a line or any character succeeding a FF */
  1873.   if ((c = getc (f)) == '\f' && p->full_page_printed)
  1874.     /* A FF-coincidence with a previous full_page_printed.
  1875.        To avoid an additional empty page, eliminate the FF */
  1876.     if ((c = getc (f)) == '\n')
  1877.       c = getc (f);
  1878.  
  1879.   p->full_page_printed = FALSE;
  1880.  
  1881.   /* 1st character a FF means a single FF without any printable
  1882.      characters. Don't count it as a line with -n option. */
  1883.   if (c == '\f')
  1884.     single_ff = TRUE;
  1885.  
  1886.   /* Preparing for a FF-coincidence: Maybe we finish that page
  1887.      without a FF found */
  1888.   if (last_line)
  1889.     p->full_page_printed = TRUE;
  1890.  
  1891.   while (c != '\n')
  1892.     {
  1893.       if (c == '\f')
  1894.     {
  1895.       /* No FF-coincidence possible,
  1896.          no catching up of a FF-coincidence with next page */
  1897.       if (last_line)
  1898.         {
  1899.           if (!parallel_files)
  1900.         for (q = column_vector, i = columns; i; ++q, --i)
  1901.           q->full_page_printed = FALSE;
  1902.           else
  1903.         p->full_page_printed = FALSE;
  1904.         }
  1905.  
  1906.       if ((c = getc (f)) != '\n')
  1907.         ungetc (c, f);
  1908.       hold_file (p);
  1909.       break;
  1910.     }
  1911.       else if (c == EOF)
  1912.     {
  1913.       close_file (p);
  1914.       break;
  1915.     }
  1916.       c = getc (f);
  1917.     }
  1918.  
  1919.   if (skip_count)
  1920.     if ((!parallel_files || column_number == 1) && !single_ff)
  1921.       ++line_count;
  1922. }
  1923.  
  1924. /* If we're tabifying output,
  1925.  
  1926.    When print_char encounters white space it keeps track
  1927.    of our desired horizontal position and delays printing
  1928.    until this function is called. */
  1929.  
  1930. static void
  1931. print_white_space (void)
  1932. {
  1933.   register int h_new;
  1934.   register int h_old = output_position;
  1935.   register int goal = h_old + spaces_not_printed;
  1936.  
  1937.   while (goal - h_old > 1
  1938.      && (h_new = POS_AFTER_TAB (chars_per_output_tab, h_old)) <= goal)
  1939.     {
  1940.       putchar (output_tab_char);
  1941.       h_old = h_new;
  1942.     }
  1943.   while (++h_old <= goal)
  1944.     putchar (' ');
  1945.  
  1946.   output_position = goal;
  1947.   spaces_not_printed = 0;
  1948. }
  1949.  
  1950. /* Print column separators.
  1951.  
  1952.    We keep a count until we know that we'll be printing a line,
  1953.    then print_sep_string() is called. */
  1954.  
  1955. static void
  1956. print_sep_string ()
  1957. {
  1958.   char *s;
  1959.   int l = col_sep_length;
  1960.  
  1961.   s = col_sep_string;
  1962.  
  1963.   if (spaces_not_printed > 0)
  1964.     print_white_space ();
  1965.  
  1966.   for (; separators_not_printed > 0; --separators_not_printed)
  1967.     {
  1968.       while (l-- > 0)
  1969.     putchar (*s++);
  1970.       output_position += col_sep_length;
  1971.     }
  1972. }
  1973.  
  1974. /* Print (or store, depending on p->char_func) a clump of N
  1975.    characters. */
  1976.  
  1977. static void
  1978. print_clump (COLUMN *p, int n, int *clump)
  1979. {
  1980.   while (n--)
  1981.     (p->char_func) (*clump++);
  1982. }
  1983.  
  1984. /* Print a character.
  1985.  
  1986.    Update the following comment: process-char hasn't been used any
  1987.    longer.
  1988.    If we're tabifying, all tabs have been converted to spaces by
  1989.    process_char().  Keep a count of consecutive spaces, and when
  1990.    a nonspace is encountered, call print_white_space() to print the
  1991.    required number of tabs and spaces. */
  1992.  
  1993. static void
  1994. print_char (int c)
  1995. {
  1996.   if (tabify_output)
  1997.     {
  1998.       if (c == ' ')
  1999.     {
  2000.       ++spaces_not_printed;
  2001.       return;
  2002.     }
  2003.       else if (spaces_not_printed > 0)
  2004.     print_white_space ();
  2005.  
  2006.       /* Nonprintables are assumed to have width 0, except '\b'. */
  2007.       if (!ISPRINT (c))
  2008.     {
  2009.       if (c == '\b')
  2010.         --output_position;
  2011.     }
  2012.       else
  2013.     ++output_position;
  2014.     }
  2015.   putchar (c);
  2016. }
  2017.  
  2018. /* Skip to page PAGE before printing. */
  2019.  
  2020. static int
  2021. skip_to_page (int page)
  2022. {
  2023.   int n, i, j;
  2024.   COLUMN *p;
  2025.  
  2026.   for (n = 1; n < page; ++n)
  2027.     {
  2028.       for (i = 1; i < lines_per_body; ++i)
  2029.     {
  2030.       for (j = 1, p = column_vector; j <= columns; ++j, ++p)
  2031.         if (p->status == OPEN)
  2032.           skip_read (p, j);
  2033.     }
  2034.       last_line = TRUE;
  2035.       for (j = 1, p = column_vector; j <= columns; ++j, ++p)
  2036.     if (p->status == OPEN)
  2037.       skip_read (p, j);
  2038.  
  2039.       if (storing_columns)    /* change FF_FOUND to ON_HOLD */
  2040.     for (j = 1, p = column_vector; j <= columns; ++j, ++p)
  2041.       p->status = ON_HOLD;
  2042.  
  2043.       reset_status ();
  2044.       last_line = FALSE;
  2045.     }
  2046.   return files_ready_to_read > 0;
  2047. }
  2048.  
  2049. /* Print a header.
  2050.  
  2051.    Formfeeds are assumed to use up two lines at the beginning of
  2052.    the page. */
  2053.  
  2054. static void
  2055. print_header (void)
  2056. {
  2057.   if (!use_form_feed)
  2058.     fprintf (stdout, "\n\n");
  2059.  
  2060.   output_position = 0;
  2061.   pad_across_to (chars_per_margin);
  2062.   print_white_space ();
  2063.  
  2064.   if (!standard_header && *custom_header == '\0')
  2065.     fprintf (stdout, "%s\n\n\n", header);
  2066.   else
  2067.     fprintf (stdout, "%s%5d\n\n\n", header, page_number++);
  2068.  
  2069.   print_a_header = FALSE;
  2070.   output_position = 0;
  2071. }
  2072.  
  2073. /* Print (or store, if p->char_func is store_char()) a line.
  2074.  
  2075.    Read a character to determine whether we have a line or not.
  2076.    (We may hit EOF, \n, or \f)
  2077.  
  2078.    Once we know we have a line,
  2079.    set pad_vertically = TRUE, meaning it's safe
  2080.    to pad down at the end of the page, since we do have a page.
  2081.    print a header if needed.
  2082.    pad across to padding_not_printed if needed.
  2083.    print any separators which need to be printed.
  2084.    print a line number if it needs to be printed.
  2085.  
  2086.    Print the clump which corresponds to the first character.
  2087.  
  2088.    Enter a loop and keep printing until an end of line condition
  2089.    exists, or until we exceed chars_per_column.
  2090.  
  2091.    Return FALSE if we exceed chars_per_column before reading
  2092.    an end of line character, TRUE otherwise. */
  2093.  
  2094. static int
  2095. read_line (COLUMN *p)
  2096. {
  2097.   register int c, chars;
  2098.   int last_input_position;
  2099.   int j, k;
  2100.   COLUMN *q;
  2101.  
  2102. /* Suppress `used before initialized' warning. */
  2103. #ifdef lint
  2104.   chars = 0;
  2105. #endif
  2106.  
  2107.   /* read 1st character in each line or any character succeeding a FF: */
  2108.   c = getc (p->fp);
  2109.  
  2110.   last_input_position = input_position;
  2111.  
  2112.   if (c == '\f' && p->full_page_printed)
  2113.     if ((c = getc (p->fp)) == '\n')
  2114.       c = getc (p->fp);
  2115.   p->full_page_printed = FALSE;
  2116.  
  2117.   switch (c)
  2118.     {
  2119.     case '\f':
  2120.       if ((c = getc (p->fp)) != '\n')
  2121.     ungetc (c, p->fp);
  2122.       FF_only = TRUE;
  2123.       if (print_a_header && !storing_columns)
  2124.     {
  2125.       pad_vertically = TRUE;
  2126.       print_header ();
  2127.     }
  2128.       else if (keep_FF)
  2129.     print_a_FF = TRUE;
  2130.       hold_file (p);
  2131.       return TRUE;
  2132.     case EOF:
  2133.       close_file (p);
  2134.       return TRUE;
  2135.     case '\n':
  2136.       break;
  2137.     default:
  2138.       chars = char_to_clump (c);
  2139.     }
  2140.  
  2141.   if (truncate_lines && input_position > chars_per_column)
  2142.     {
  2143.       input_position = last_input_position;
  2144.       return FALSE;
  2145.     }
  2146.  
  2147.   if (p->char_func != store_char)
  2148.     {
  2149.       pad_vertically = TRUE;
  2150.  
  2151.       if (print_a_header && !storing_columns)
  2152.     print_header ();
  2153.  
  2154.       if (parallel_files && align_empty_cols)
  2155.     {
  2156.       /* We have to align empty columns at the beginning of a line. */
  2157.       k = separators_not_printed;
  2158.       separators_not_printed = 0;
  2159.       for (j = 1, q = column_vector; j <= k; ++j, ++q)
  2160.         {
  2161.           align_column (q);
  2162.           separators_not_printed += 1;
  2163.         }
  2164.       padding_not_printed = p->start_position;
  2165.       if (truncate_lines)
  2166.         spaces_not_printed = chars_per_column;
  2167.       else
  2168.         spaces_not_printed = 0;
  2169.       align_empty_cols = FALSE;
  2170.     }
  2171.  
  2172.       if (padding_not_printed - col_sep_length > 0)
  2173.     {
  2174.       pad_across_to (padding_not_printed - col_sep_length);
  2175.       padding_not_printed = ANYWHERE;
  2176.     }
  2177.  
  2178.       if (use_col_separator)
  2179.     print_sep_string ();
  2180.     }
  2181.  
  2182.   if (p->numbered)
  2183.     number (p);
  2184.  
  2185.   empty_line = FALSE;
  2186.   if (c == '\n')
  2187.     return TRUE;
  2188.  
  2189.   print_clump (p, chars, clump_buff);
  2190.  
  2191.   for (;;)
  2192.     {
  2193.       c = getc (p->fp);
  2194.  
  2195.       switch (c)
  2196.     {
  2197.     case '\n':
  2198.       return TRUE;
  2199.     case '\f':
  2200.       if ((c = getc (p->fp)) != '\n')
  2201.         ungetc (c, p->fp);
  2202.       if (keep_FF)
  2203.         print_a_FF = TRUE;
  2204.       hold_file (p);
  2205.       return TRUE;
  2206.     case EOF:
  2207.       close_file (p);
  2208.       return TRUE;
  2209.     }
  2210.  
  2211.       last_input_position = input_position;
  2212.       chars = char_to_clump (c);
  2213.       if (truncate_lines && input_position > chars_per_column)
  2214.     {
  2215.       input_position = last_input_position;
  2216.       return FALSE;
  2217.     }
  2218.  
  2219.       print_clump (p, chars, clump_buff);
  2220.     }
  2221. }
  2222.  
  2223. /* Print a line from buff.
  2224.  
  2225.    If this function has been called, we know we have "something to
  2226.    print". But it remains to be seen whether we have a real text page
  2227.    or an empty page (a single form feed) with/without a header only.
  2228.    Therefore first we set pad_vertically to TRUE and print a header
  2229.    if necessary.
  2230.    If FF_FOUND and we are using -t|-T option we omit any newline by
  2231.    setting pad_vertically to FALSE (see print_page).
  2232.    Otherwise we pad across if necessary, print separators if necessary
  2233.    and text of COLUMN *p.
  2234.  
  2235.    Return TRUE, meaning there is no need to call read_rest_of_line. */
  2236.  
  2237. static int
  2238. print_stored (COLUMN *p)
  2239. {
  2240.   COLUMN *q;
  2241.   int i;
  2242.  
  2243.   int line = p->current_line++;
  2244.   register char *first = &buff[line_vector[line]];
  2245.   /* FIXME
  2246.      UMR: Uninitialized memory read:
  2247.      * This is occurring while in:
  2248.      print_stored   [pr.c:2239]
  2249.      * Reading 4 bytes from 0x5148c in the heap.
  2250.      * Address 0x5148c is 4 bytes into a malloc'd block at 0x51488 of 676 bytes
  2251.      * This block was allocated from:
  2252.      malloc         [rtlib.o]
  2253.      xmalloc        [xmalloc.c:94]
  2254.      init_store_cols [pr.c:1648]
  2255.      */
  2256.   register char *last = &buff[line_vector[line + 1]];
  2257.  
  2258.   pad_vertically = TRUE;
  2259.  
  2260.   if (print_a_header)
  2261.     print_header ();
  2262.  
  2263.   if (p->status == FF_FOUND)
  2264.     {
  2265.       for (i = 1, q = column_vector; i <= columns; ++i, ++q)
  2266.     q->status = ON_HOLD;
  2267.       if (column_vector->lines_to_print <= 0)
  2268.     {
  2269.       if (!extremities)
  2270.         pad_vertically = FALSE;
  2271.       return TRUE;        /* print a header only */
  2272.     }
  2273.     }
  2274.  
  2275.   if (padding_not_printed - col_sep_length > 0)
  2276.     {
  2277.       pad_across_to (padding_not_printed - col_sep_length);
  2278.       padding_not_printed = ANYWHERE;
  2279.     }
  2280.  
  2281.   if (use_col_separator)
  2282.     print_sep_string ();
  2283.  
  2284.   while (first != last)
  2285.     print_char (*first++);
  2286.  
  2287.   if (spaces_not_printed == 0)
  2288.     {
  2289.       output_position = p->start_position + end_vector[line];
  2290.       if (p->start_position - col_sep_length == chars_per_margin)
  2291.     output_position -= col_sep_length;
  2292.     }
  2293.  
  2294.   return TRUE;
  2295. }
  2296.  
  2297. /* Convert a character to the proper format and return the number of
  2298.    characters in the resulting clump.  Increment input_position by
  2299.    the width of the clump.
  2300.  
  2301.    Tabs are converted to clumps of spaces.
  2302.    Nonprintable characters may be converted to clumps of escape
  2303.    sequences or control prefixes.
  2304.  
  2305.    Note: the width of a clump is not necessarily equal to the number of
  2306.    characters in clump_buff.  (e.g, the width of '\b' is -1, while the
  2307.    number of characters is 1.) */
  2308.  
  2309. static int
  2310. char_to_clump (int c)
  2311. {
  2312.   register int *s = clump_buff;
  2313.   register int i;
  2314.   char esc_buff[4];
  2315.   int width;
  2316.   int chars;
  2317.  
  2318.   if (c == input_tab_char)
  2319.     {
  2320.       width = TAB_WIDTH (chars_per_input_tab, input_position);
  2321.  
  2322.       if (untabify_input)
  2323.     {
  2324.       for (i = width; i; --i)
  2325.         *s++ = ' ';
  2326.       chars = width;
  2327.     }
  2328.       else
  2329.     {
  2330.       *s = c;
  2331.       chars = 1;
  2332.     }
  2333.  
  2334.     }
  2335.   else if (!ISPRINT (c))
  2336.     {
  2337.       if (use_esc_sequence)
  2338.     {
  2339.       width = 4;
  2340.       chars = 4;
  2341.       *s++ = '\\';
  2342.       sprintf (esc_buff, "%03o", c);
  2343.       for (i = 0; i <= 2; ++i)
  2344.         *s++ = (int) esc_buff[i];
  2345.     }
  2346.       else if (use_cntrl_prefix)
  2347.     {
  2348.       if (c < 0200)
  2349.         {
  2350.           width = 2;
  2351.           chars = 2;
  2352.           *s++ = '^';
  2353.           *s++ = c ^ 0100;
  2354.         }
  2355.       else
  2356.         {
  2357.           width = 4;
  2358.           chars = 4;
  2359.           *s++ = '\\';
  2360.           sprintf (esc_buff, "%03o", c);
  2361.           for (i = 0; i <= 2; ++i)
  2362.         *s++ = (int) esc_buff[i];
  2363.         }
  2364.     }
  2365.       else if (c == '\b')
  2366.     {
  2367.       width = -1;
  2368.       chars = 1;
  2369.       *s = c;
  2370.     }
  2371.       else
  2372.     {
  2373.       width = 0;
  2374.       chars = 1;
  2375.       *s = c;
  2376.     }
  2377.     }
  2378.   else
  2379.     {
  2380.       width = 1;
  2381.       chars = 1;
  2382.       *s = c;
  2383.     }
  2384.  
  2385.   input_position += width;
  2386.   return chars;
  2387. }
  2388.  
  2389. /* We've just printed some files and need to clean up things before
  2390.    looking for more options and printing the next batch of files.
  2391.  
  2392.    Free everything we've xmalloc'ed, except `header'. */
  2393.  
  2394. static void
  2395. cleanup (void)
  2396. {
  2397.   if (number_buff)
  2398.     free (number_buff);
  2399.   if (clump_buff)
  2400.     free (clump_buff);
  2401.   if (column_vector)
  2402.     free (column_vector);
  2403.   if (line_vector)
  2404.     free (line_vector);
  2405.   if (end_vector)
  2406.     free (end_vector);
  2407.   if (buff)
  2408.     free (buff);
  2409. }
  2410.  
  2411. /* Complain, print a usage message, and die. */
  2412.  
  2413. static void
  2414. usage (int status)
  2415. {
  2416.   if (status != 0)
  2417.     fprintf (stderr, _("Try `%s --help' for more information.\n"),
  2418.          program_name);
  2419.   else
  2420.     {
  2421.       printf (_("\
  2422. Usage: %s [OPTION]... [FILE]...\n\
  2423. "),
  2424.           program_name);
  2425.  
  2426.       printf (_("\
  2427. Paginate or columnate FILE(s) for printing.\n\
  2428. \n\
  2429.   +FIRST_PAGE[:LAST_PAGE]\n\
  2430.                     begin [stop] printing with page FIRST_[LAST_]PAGE\n\
  2431.   -COLUMN           produce COLUMN-column output and print columns down,\n\
  2432.                     unless -a is used. Balance number of lines in the\n\
  2433.                     columns on each page.\n\
  2434.   -a                print columns across rather than down, used together\n\
  2435.                     with -COLUMN\n\
  2436.   -c                use hat notation (^G) and octal backslash notation\n\
  2437.   -d                double space the output\n\
  2438.   -e[CHAR[WIDTH]]   expand input CHARs (TABs) to tab WIDTH (8)\n\
  2439.   -F, -f            use form feeds instead of newlines to separate pages\n\
  2440.                     (by a 3-line page header with -f or a 5-line header\n\
  2441.                     and trailer without -f)\n\
  2442.   -h HEADER         use a centered HEADER instead of filename in page headers\n\
  2443.                     with long headers left-hand-side truncation may occur\n\
  2444.                     -h \"\" prints a blank line. Don't use -h\"\"\n\
  2445.   -i[CHAR[WIDTH]]   replace spaces with CHARs (TABs) to tab WIDTH (8)\n\
  2446.   -j                merge full lines, turns off -w line truncation, no column\n\
  2447.                     alignment, -s[STRING] sets separators\n\
  2448.   -l PAGE_LENGTH    set the page length to PAGE_LENGTH (66) lines\n\
  2449.                     (default number of lines of text 56, with -f 63)\n"));
  2450.       printf (_("\
  2451.   -m                print all files in parallel, one in each column,\n\
  2452.                     truncate lines, but join lines of full length with -j\n\
  2453.   -n[SEP[DIGITS]]   number lines, use DIGITS (5) digits, then SEP (TAB)\n\
  2454.                     default counting starts with 1st line of input file\n\
  2455.   -N NUMBER         start counting with NUMBER at 1st line of first\n\
  2456.                     page printed (see +FIRST_PAGE)\n\
  2457.   -o MARGIN         offset each line with MARGIN spaces (do not affect -w)\n\
  2458.   -r                inhibit warning when a file cannot be opened\n\
  2459.   -s[STRING]        separate columns by an optional STRING\n\
  2460.                     don't use -s \"STRING\" \n\
  2461.                     without -s: default sep. \'space\' used, same as -s\" \"\n\
  2462.                     -s only: no separator used, same as -s\"\" \n\
  2463.   -t                inhibit page headers and trailers\n\
  2464.   -T                inhibit page headers and trailers, eliminate any page\n\
  2465.                     layout by form feeds set in input files\n\
  2466.   -v                use octal backslash notation\n\
  2467.   -w PAGE_WIDTH     set page width to PAGE_WIDTH (72) columns, truncate\n\
  2468.                     lines (see also -j option)\n\
  2469.   --help            display this help and exit\n\
  2470.   --version         output version information and exit\n\
  2471. \n\
  2472. -T implied by -l nn when nn <= 10 or <= 3 with -f. With no FILE, or when\n\
  2473. FILE is -, read standard input.\n\
  2474. "));
  2475.       puts (_("\nReport bugs to textutils-bugs@gnu.ai.mit.edu"));
  2476.     }
  2477.   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  2478. }
  2479.