home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 15 / CD_ASCQ_15_070894.iso / maj / 4266 / intprint.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  55KB  |  2,080 lines

  1. /************************************************************************/
  2. /* INTPRINT.C by Ralf Brown.  Donated to the Public Domain.        */
  3. /* Please do not remove my name from any copies or derivatives.        */
  4. /************************************************************************/
  5. /* Program History:                            */
  6. /*   v1.00  4/23/89  initial public release                */
  7. /*             with 4/30/89 list                    */
  8. /*   v1.10  5/21/89  added -I and -f                    */
  9. /*   v1.11  1/6/90   fixed #endif's for compilers which don't handle    */
  10. /*             labels                        */
  11. /*   v1.20  6/8/90   added -r                        */
  12. /*   v1.30  7/14/90  added -b, tables now stay aligned on odd indents    */
  13. /*   v1.40  10/6/90  added -B based on changes by Naoto Kimura, -w    */
  14. /*   v1.40a 5/6/91   HP LaserJet II support by Russ Herman        */
  15. /*   v1.41  7/9/91   HP PCL support by P.J.Farley III            */
  16. /*   v2.00  9/1/91   modular printer definitions            */
  17. /*             printing multipart interrupt list            */
  18. /*   v2.01  2/9/92   fixed summary entry for non-numeric AX= and AH=    */
  19. /*             smarter page breaks                */
  20. /*   v2.02  2/18/92  bugfix & isxdigit suggested by Aaron West        */
  21. /*   v2.10  3/14/92  updated to handle extra flags in headings        */
  22. /*   v2.11  5/23/92  bugfix pointed out by Joe White            */
  23. /*   v2.20  6/12/92  added -F based on code by Richard Brittain        */
  24. /*             added -H and Panasonic printer def by Lewis Paper    */
  25. /*   v2.21  10/14/92 fixed error in -H/-r interaction            */
  26. /*             updated for new 'Bitmask of' section        */
  27. /*   v2.22   2/15/93 exclude Index: by default, -x to force inclusion    */
  28. /*             changed 'Bitmask of' to 'Bitfields for'        */
  29. /*   v2.23   5/24/93 fix to allow INT/AL= to appear correctly in summary*/
  30. /*   v2.24   7/15/93 -k and infinite-length pages by Bent Lynggaard    */
  31. /*   v3.00   6/4/94  -T, -V, and multi-file break section skipping    */
  32. /*             major speedups; checked for BC++3.1 compatibility    */
  33. /************************************************************************/
  34. /* Recompiling:                                */
  35. /*   Turbo C / Borland C++                        */
  36. /*    tcc -mt -lt -O -a -Z -p -k- intprint                */
  37. /*      bcc -mt -lt -a -O1agim -p intprint.c                */
  38. /************************************************************************/
  39.  
  40. #include <ctype.h>
  41. #include <fcntl.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <sys/stat.h>        /* S_IREAD, S_IWRITE */
  46.  
  47. #define VERSION "3.00"
  48.  
  49. /***********************************************/
  50. /*    portability definitions               */
  51.  
  52. #define _other_        /* assume no system-specific match */
  53.  
  54. /*--------------------------------------------------*/
  55. /* first system: MS-DOS with Turbo/Borland C        */
  56.  
  57. #ifdef __TURBOC__
  58. #  define PROTOTYPES
  59. #  include <alloc.h>
  60. #  include <io.h>    /* open, close, read, lseek, etc. */
  61.    int _Cdecl isatty(int handle) ;
  62.  
  63.    /* definitions to reduce size of executable */
  64.    unsigned int _Cdecl _stklen = 1024 ;
  65.    #define close _close
  66.    #define read _read
  67.    #define write _write
  68.    void _Cdecl _setenvp(void) {} /* don't need environment--don't include it */
  69.    void *_Cdecl malloc(size_t size) { return sbrk(size) ; }
  70.    void _Cdecl free(void *var) { (void)var ; }
  71.    /* since our free() doesn't do anything, macro it out of existence */
  72.    #define free(p)
  73.  
  74.    #ifdef __BORLANDC__
  75.    void _Cdecl _setupio(void) {}
  76.    #pragma warn -eff
  77.    #endif
  78.  
  79. #undef _other_
  80. #endif /* __TURBOC__ */
  81.  
  82. #ifdef __MSDOS__
  83. #  define LINE_TERMINATOR '\n'
  84. #endif
  85.  
  86. /*--------------------------------------------------*/
  87. /*   Gnu C compiler                                 */
  88.  
  89. #ifdef __GNUC__
  90. #define PROTOTYPES
  91. #define NEED_ITOA
  92. #define NEED_ULTOA
  93. #define NEED_STRUPR
  94. #define NEED_STRNICMP
  95.  
  96. #undef _other_
  97. #endif /* __GNUC__ */
  98.  
  99. /*--------------------------------------------------*/
  100. /*  generic Unix definitions                        */
  101.  
  102. #ifdef unix
  103. #  include <sys/unistd.h>    /* open, close, read, lseek, etc. */
  104. #  include <sysent.h>        /* open, close, read, lseek, etc. */
  105. extern int isatty(int) ;
  106. #  define LINE_TERMINATOR '\n'
  107. #endif
  108.  
  109.  
  110. /*--------------------------------------------------*/
  111. /*  any other system                                */
  112.  
  113. #ifdef _other_
  114. /* unknown compiler/system, so set configuration #defines */
  115. #if 0  /* set to 1 if compiler supports ANSI-style prototypes, 0 otherwise */
  116. #define PROTOTYPES
  117. #endif
  118. #if 1  /* set to 0 if library contains strnicmp(), 1 otherwise */
  119. #define NEED_STRNICMP
  120. #endif
  121. #if 1  /* set to 0 if library contains isxdigit(), 1 otherwise */
  122. #define NEED_ISXDIGIT
  123. #endif
  124. #if 1  /* set to 0 if library contains strupr(), 1 otherwise */
  125. #define NEED_STRUPR
  126. #endif
  127. #if 1  /* set to 0 if library contains three-arg itoa(), 1 otherwise */
  128. #define NEED_ITOA
  129. #endif
  130. #if 1  /* set to 0 if library contains three-arg ultoa(), 1 otherwise */
  131. #define NEED_ULTOA
  132. #endif
  133.  
  134. /* the last character of the line termination sequence, i.e. '\n' for CRLF */
  135. /* and LF, '\r' if your system uses CR or LFCR */
  136. #define LINE_TERMINATOR '\n'
  137.  
  138. #endif /* _other_ */
  139.  
  140. /*--------------------------------------------------*/
  141. /*  catchall for macros which might not be defined  */
  142.  
  143. #ifndef O_BINARY
  144. #  define O_BINARY 0
  145. #endif
  146.  
  147. #ifndef _Cdecl
  148. #  define _Cdecl
  149. #endif
  150.  
  151. /***********************************************/
  152.  
  153. #ifndef FALSE
  154. #define FALSE 0
  155. #endif
  156. #ifndef TRUE
  157. #define TRUE !FALSE
  158. #endif
  159.  
  160. /***********************************************/
  161.  
  162. #define MAXLINE 82   /* at most 80 chars per line (plus CR and newline) */
  163. #define MAXPAGE 200  /* at most 200 lines per page */
  164.  
  165. #define lengthof(x) (sizeof(x)/sizeof(x[0]))
  166.  
  167. #define divider_line(line) (line[0] == '-' && memcmp(line+1,"-------",7) == 0)
  168. #ifdef __MSDOS__
  169. #define start_of_entry(s) (((int*)s)[0]==(256*'N'+'I')&&((int*)s)[1]==(256*' '+'T'))
  170. #define index_line(l) \
  171.   (((int*)l)[0]==(256*'n'+'I')&&((int*)l)[1]==(256*'e'+'d')&& \
  172.    ((int*)l)[2]==(256*':'+'x'))
  173. #else
  174. #define start_of_entry(s) (memcmp(s,"INT ",4) == 0)
  175. #define index_line(line) (line[0] == 'I' && memcmp(line+1,"ndex:",5) == 0)
  176. #endif
  177. #define section_start(line) is_keyword(line,section_start_keys,lengthof(section_start_keys))
  178. #define start_of_table(line) (is_keyword(line,table_start_keys,lengthof(table_start_keys)))
  179.    
  180. #define section_file_start(s) (s[0] == '-' && memcmp(s+1,"-------!---Section",18) == 0)
  181.  
  182.  
  183. /***********************************************/
  184. /*    replacement file I/O function macros     */
  185. /***********************************************/
  186.  
  187. typedef struct
  188.    {
  189.    int fd ;
  190.    int buf_maxsize ;
  191.    char *buf ;
  192.    unsigned long bufoffset ;
  193.    int bufsize ;
  194.    int bufpos ;
  195.    int write ;             /* file is output file if nonzero */
  196.    } IP_FILE ;
  197.  
  198. #define ip_putc(c,fp) \
  199.   ((fp)->buf[fp->bufpos++]=(c),\
  200.    ((fp)->bufpos>=(fp)->buf_maxsize&&ip_flush(fp)==-1)?-1:0)
  201.  
  202. /* output the indicated counted string to the given file */
  203. #define ip_putcstr(s,fp) ip_write((s)->str,(s)->len,fp)
  204. /* output the given string literal to the indicated file */
  205. #define ip_putlit(s,fp) ip_write((s),sizeof(s)-1,fp)
  206. /* output the given string variable to the indicated file */
  207. #define ip_puts(s, fp) ip_write(s,strlen(s),fp)
  208.  
  209. #ifdef __MSDOS__
  210. #define newline(fp) ip_write("\r\n",2,fp)
  211. #else
  212. #define newline(fp) ip_putc('\n',fp)
  213. #endif
  214.  
  215. /***********************************************/
  216.  
  217. typedef struct             /* a counted string */
  218.    {
  219.    int len ;             /* the string's length */
  220.    char *str ;             /* the actual contents of the string */
  221.    } cstr ;
  222.  
  223. #define CSTR(s) { sizeof(s)-1, (s) }  /* for defining a counted string literal */
  224. #define cstrlen(s) ((s)->len)     /* how long is the counted string? */
  225.  
  226. typedef struct
  227.    {
  228.    char *name ;            /* for selecting the appropriate printer */
  229.    cstr init1, init2 ;        /* initialization strings */
  230.    cstr marginl, marginc, marginr ; /* margins: duplex even, non-duplex, duplex odd */
  231.    cstr duplex_on ;        /* turn on duplex mode */
  232.    cstr term1, term2 ;        /* cleanup strings */
  233.    cstr bold_on, bold_off ;    /* boldface on/off */
  234.    int indent ;            /* how many extra spaces to indent */
  235.    int lines_per_page ;        /* how many lines to print on each page */
  236.    int page_length ;        /* how many lines on each page */
  237.    int page_width ;        /* how many printable columns per line? */
  238. #ifdef PROTOTYPES
  239.    void (*put_line)(IP_FILE *,int) ;/* function to call to print out divider line */
  240.    void (*set_typeface)(IP_FILE *,char *) ;
  241. #else
  242.    void (*put_line)() ;        /* function to call to print out divider line */
  243.    void (*set_typeface)() ;
  244. #endif /* PROTOTYPES */
  245.    int *flag ;            /* flag to set when using this printer definition */
  246.    } PRINTER_DEF ;
  247.  
  248. typedef struct filter_list
  249.    {
  250.    struct filter_list *next ;
  251.    char str[1] ;        /* will allocate enough for actual string */
  252.    } FILT_LIST ;
  253.  
  254. typedef struct
  255.    {
  256.    int part ;
  257.    int first_on_page ; /* TRUE if a new entry starts at the top of the page */
  258.    char desc[24] ;
  259.    int len ;
  260.    } HEADER ;
  261.  
  262. typedef struct
  263.    {
  264. /*   char *name ;*/
  265.    char name[14] ;
  266.    int length ;
  267.    } KEYWORDS ;
  268.  
  269. /***********************************************/
  270.  
  271. #ifdef PROTOTYPES
  272. void usage(void) ;
  273. void fatal(char *msg) ;
  274. void warning(char *msg) ;
  275. int unwanted_section(char *buf) ;
  276. IP_FILE *ip_fdopen(int fd,char *buf,int bufsiz, int maxsiz, int write) ;
  277. IP_FILE *ip_open_write(char *name, int trunc, char *buf, int bufsiz) ;
  278. IP_FILE *ip_open_read(char *name, char *buf, int bufsiz) ;
  279. int ip_close(IP_FILE *fp) ;
  280. unsigned long ip_fgets(char *buf, int max, IP_FILE *fp) ;
  281. int ip_write(char *buf, int count, IP_FILE *fp) ;
  282. int ip_flush(IP_FILE *fp) ;
  283. void get_raw_line(char *buf) ;
  284. void get_line(char *buf) ;
  285. void indent_to(int where,IP_FILE *fp) ;
  286. void put_line(IP_FILE *fp, int len) ;
  287. void HPPCL_put_line(IP_FILE *fp, int len) ;
  288. void HPPCL_set_typeface(IP_FILE *fp,char *typeface) ;
  289. int is_keyword(char *s, KEYWORDS *keys, unsigned int numkeys) ;
  290. void output_line(char *line,IP_FILE *fp) ;
  291. void fill_buffer(int lines, int lines_per_page) ;
  292. int find_page_break(int lines) ;
  293. int summarize(int line, int pages_printed) ;
  294. void start_format(char *line) ;
  295. void write_summary_header(IP_FILE *fp, char *title, int offsets, int tables) ;
  296. void show_offset(int line,IP_FILE *fp) ;
  297. void add_table(int i) ;
  298. FILT_LIST *add_filter_info(FILT_LIST *list,char *str) ;
  299. void build_filter_lists(char *file) ;
  300. int make_description(char *desc,int line) ;
  301. char *determine_heading(int last) ;
  302. void print_buffer(int last,int body_lines,int lines_per_page,int total_lines,
  303.           int use_FF) ;
  304. void select_printer(char *name) ;
  305. void display_printers(void) ;
  306. static void reset_printer_and_close(IP_FILE *fp) ;
  307. int _Cdecl main(int argc, char **argv) ;
  308. #else
  309. void put_line() ;
  310. void HPPCL_put_line() ;
  311. void HPPCL_set_typeface() ;
  312. void show_offset() ;
  313. #endif /* PROTOTYPES */
  314.  
  315. /***********************************************/
  316. /*    I/O buffers                   */
  317. /***********************************************/
  318.  
  319. char stderr_buf[256] ;
  320. char filter_buf[256] ;
  321. char infile_buf[8192] ;
  322. char outfile_buf[8192] ;
  323. char summary_buf[4096] ;
  324. char formats_buf[3072] ;
  325. char tables_buf[3072] ;
  326.  
  327. /***********************************************/
  328.  
  329. IP_FILE *err ;
  330. IP_FILE *infile ;
  331. IP_FILE *outfile ;
  332. char *input_file ;
  333. int input_file_namelen ;
  334.  
  335. char buffer[MAXPAGE][MAXLINE] ;
  336. unsigned long line_offsets[MAXPAGE] ;
  337. char num[6] ;
  338. int need_summary ;
  339. char summary_line[2*MAXLINE] ;
  340. int summary_line_len ;
  341.  
  342. int pages_printed = 0 ;
  343. int page_width = 0 ;        /* page width in characters, 0 = use prtdef */
  344. int indent = 0 ;            /* number of columns to indent lines */
  345. char *indent_string = NULL ;    /* what to add at start of line to indent */
  346. int indent_len = 0 ;            /* length of indent_string */
  347. int widow_length = 8 ;        /* number of lines to scan for good place to break */
  348. int page_numbers = FALSE ;    /* add page numbers to bottom of page? */
  349. int multi_file = FALSE ;    /* printing multipart interrupt list? */
  350. int out_of_files = FALSE ;    /* hit end of last file for multipart printing? */
  351. int do_summary = FALSE ;    /* create a one-line-per-call summary? */
  352. int do_tables = FALSE ;        /* create a one-line-per-table index? */
  353. int do_formats = FALSE ;    /* create a separate file with data structures? */
  354. int do_filter = FALSE ;        /* using a filtering file? */
  355. int do_headers = FALSE ;    /* add page headings? */
  356. int include_index_lines = FALSE ;
  357. int IBM_chars = FALSE ;        /* printer can handle IBM graphics characters */
  358. int boldface = FALSE ;        /* boldface titles and Return:/Notes: ? */
  359. int printer_bold = FALSE ;    /* boldface using printer control sequences? */
  360. int echo_format = FALSE ;
  361. int duplex = FALSE ;
  362. int HPPCL_mode = FALSE ;
  363. int show_offsets = FALSE ;
  364. int keep_divider_lines = FALSE ;
  365. IP_FILE *summary ;
  366. IP_FILE *tables ;
  367. IP_FILE *formats ;
  368. PRINTER_DEF *printer = NULL ;
  369.  
  370. unsigned long current_line_offset = 0 ;
  371. unsigned long offset_adjust = 0 ;
  372.  
  373. unsigned int first_page = 0 ;
  374. unsigned int last_page = ~0 ;
  375.  
  376. int prev_table = 0 ;
  377.  
  378. FILT_LIST *includes = NULL ;
  379. FILT_LIST *excludes = NULL ;
  380.  
  381. HEADER header_first = { 0, FALSE, "" } ;
  382. HEADER header_last = { 0, FALSE, "" } ;
  383.  
  384. /***********************************************/
  385.  
  386. PRINTER_DEF printers[] =
  387.    {
  388.      { "default",
  389.        CSTR(""), CSTR(""),
  390.        CSTR(""), CSTR(""), CSTR(""),
  391.        CSTR(""),
  392.        CSTR(""), CSTR(""),
  393.        CSTR(""), CSTR(""),
  394.        -1,
  395.        60,
  396.        0,
  397.        79,
  398.        put_line,
  399.        NULL,
  400.        NULL,
  401.      },
  402.      { "Epson FX80, 12 cpi",
  403.        CSTR("\033M"), CSTR(""),
  404.        CSTR("\033l\004"), CSTR("\033l\007"), CSTR("\033l\014"),
  405.        CSTR(""),
  406.        CSTR("\033P"), CSTR("\033l\000"),
  407.        CSTR("\033E"), CSTR("\033F"),
  408.        0,
  409.        60,
  410.        0,
  411.        87,    /* 96 - left margin - 1 right margin */
  412.        put_line,
  413.        NULL,
  414.        NULL,
  415.      },
  416.      { "Panasonic KX-P1124i / 10 cpi Epson",
  417.        CSTR(""), CSTR(""),
  418.        CSTR(""), CSTR(""), CSTR(""),
  419.        CSTR(""),
  420.        CSTR(""), CSTR(""),
  421.        CSTR("\033E"), CSTR("\033F"),
  422.        -1,
  423.        60,
  424.        0,
  425.        79,
  426.        put_line,
  427.        NULL,
  428.        NULL,
  429.      },
  430.      { "HP PCL",
  431.        CSTR("\033(8U"), CSTR(""),
  432.        CSTR("\033&a4c4L"), CSTR("\033&a8c8L"), CSTR("\033&a12c12L"),
  433.        CSTR("\033&l1S"),
  434.        CSTR("\033E"), CSTR(""),
  435.        CSTR("\033(s3B"), CSTR("\033(s0B"),
  436.        0,
  437.        69,
  438.        0,
  439.        87,    /* 96 - left margin - 1 right margin */
  440.        HPPCL_put_line,
  441.        HPPCL_set_typeface,
  442.        &HPPCL_mode,
  443.      },
  444. #define HPPCL_FONT_ON_A "\033(s0p12h10v0s0b"
  445. /* HP PCL4/5 Font select: Roman-8;Upright12Pitch10PointMediumWeight */
  446. #define HPPCL_FONT_ON_B "T\033&l6.8571C"
  447. /* HP PCL4/5 Font select: End typeface select;VMI=7LPI: (48/7)-48th's inches*/
  448. #define HPPCL_IBM_LN_A    "\033&f0S\033*p-15Y\033*c"
  449. /* HP PCL4/5 IBM Line:    Push Pos;Up 15/720";Hor.Rule ???/300ths" long */
  450. #define HPPCL_IBM_LN_B    "a3b0P\033&f1S"
  451. /* HP PCL4/5 IBM Line:     3/300ths" high,Print rule;Pop Position */
  452.      { "LaserJet II",
  453.        CSTR("\033(10U"),CSTR(""),
  454.        CSTR("\033&a4c4L"), CSTR("\033&a8c8L"), CSTR("\033&a12c12L"),
  455.        CSTR(""),
  456.        CSTR("\033E"),CSTR(""),
  457.        CSTR("\033(s3B"),CSTR("\033(s0B"),
  458.        0,
  459.        54,
  460.        60,
  461.        79,
  462.        put_line,
  463.        NULL,
  464.        &IBM_chars,
  465.      },
  466.    } ;
  467. #define NUM_PRINTERS lengthof(printers)
  468.  
  469. /***********************************************/
  470.  
  471. #define KEYWORD_ENTRY(s) { s, sizeof(s)-1 }
  472.  
  473. KEYWORDS section_start_keys[] =
  474.    {
  475.     KEYWORD_ENTRY("BUG:"),
  476.     KEYWORD_ENTRY("BUGS:"),
  477.     KEYWORD_ENTRY("Desc:"),
  478.     KEYWORD_ENTRY("Index:"),
  479.     KEYWORD_ENTRY("Note:"),
  480.     KEYWORD_ENTRY("Notes:"),
  481.     KEYWORD_ENTRY("Program:"),
  482.     KEYWORD_ENTRY("Range:"),
  483.     KEYWORD_ENTRY("Return:"),
  484.     KEYWORD_ENTRY("SeeAlso:"),
  485.    } ;
  486.  
  487. KEYWORDS table_start_keys[] =
  488.    {
  489.     KEYWORD_ENTRY("Bitfields "),
  490.     KEYWORD_ENTRY("Call "),
  491.     KEYWORD_ENTRY("Format "),
  492.     KEYWORD_ENTRY("Values "),
  493.    } ;
  494.  
  495. /***********************************************/
  496.  
  497. #ifdef isxdigit
  498. #undef NEED_ISXDIGIT
  499. #endif
  500.  
  501. #ifdef NEED_STRNICMP
  502. #ifdef PROTOTYPES
  503. int strnicmp(char *s1,char *s2,unsigned int len) ;
  504. #endif
  505. int strnicmp(s1,s2,len)
  506. char *s1,*s2 ;
  507. unsigned int len ;
  508. {
  509.    char c1, c2 ;
  510.  
  511.    while (*s1 && *s2 && len > 0)
  512.       {
  513.       len-- ;
  514.       c1 = (islower(*s1) ? toupper(*s1) : *s1) ;
  515.       c2 = (islower(*s2) ? toupper(*s2) : *s2) ;
  516.       if (c1 != c2 || len == 0)     /* mismatch or substrings exhausted? */
  517.      return (c1 - c2) ;
  518.       s1++ ;
  519.       s2++ ;
  520.       }
  521.    return 0 ;  /* strings match exactly on first 'len' characters */
  522. }
  523. #endif /* NEED_STRNICMP */
  524.  
  525. #ifdef NEED_STRUPR
  526. #ifdef PROTOTYPES
  527. char *strupr(char *s) ;
  528. #endif
  529. char *strupr(s)
  530. char *s ;
  531. {
  532.    char *orig_s = s ;
  533.    char c ;
  534.    
  535.    if (s)
  536.       while (*s)
  537.      {
  538.      c = *s ;      
  539.      *s++ = (islower(c) ? toupper(c) : c) ;
  540.      }
  541.    return orig_s ;
  542. }
  543. #endif /* NEED_STRUPR */
  544.  
  545. #ifdef NEED_ISXDIGIT
  546. #ifdef PROTOTYPES
  547. int isxdigit(int c) ;
  548. #endif
  549. int isxdigit(c)
  550. int c ;
  551. {
  552.    return isdigit(c) || (memchr("ABCDEFabcdef",c,12) != NULL) ;
  553. }
  554. #endif /* NEED_ISXDIGIT */
  555.  
  556. #ifdef NEED_ITOA
  557. #ifdef PROTOTYPES
  558. char *itoa(int num,char *buf,int radix) ;
  559. #endif
  560. char *itoa(num,buf,radix)   /* not everybody has the same itoa() as TurboC */
  561. int num ;            /* minimal implementation */
  562. char *buf ;
  563. int radix ;
  564. {
  565.    int count = 0 ;
  566.    int i ; 
  567.    char tmp ;
  568.  
  569.    do {
  570.       buf[count++] = "0123456789ABCDEF"[num % radix] ;
  571.       num /= radix ;
  572.    } while (num) ;
  573.    buf[count] = '\0' ;
  574.    if (count > 1)
  575.       for (i = 0 ; i < count / 2 ; i++)
  576.      {
  577.      tmp = buf[i] ;
  578.      buf[i] = buf[count-i-1] ;
  579.      buf[count-i-1] = tmp ;
  580.      }
  581.    return buf ;
  582. }
  583. #endif /* NEED_ITOA */
  584.  
  585. #ifdef NEED_ULTOA
  586. #ifdef PROTOTYPES
  587. char *ultoa(unsigned long num,char *buf,int radix) ;
  588. #endif
  589. char *ultoa(num,buf,radix)   /* not everybody has the same ultoa() as TurboC */
  590. unsigned long num ;         /* minimal implementation */
  591. char *buf ;
  592. int radix ;
  593. {
  594.    int count = 0 ;
  595.    int i ; 
  596.    char tmp ;
  597.  
  598.    do {
  599.       buf[count++] = "0123456789ABCDEF"[num % radix] ;
  600.       num /= radix ;
  601.    } while (num) ;
  602.    buf[count] = '\0' ;
  603.    if (count > 1)
  604.       for (i = 0 ; i < count / 2 ; i++)
  605.      {
  606.      tmp = buf[i] ;
  607.      buf[i] = buf[count-i-1] ;
  608.      buf[count-i-1] = tmp ;
  609.      }
  610.    return buf ;
  611. }
  612. #endif /* NEED_ULTOA */
  613.  
  614. /***********************************************/
  615.  
  616. void usage()
  617. {
  618.    ip_putlit("\
  619. Usage: intprint [options] intlist [>|>>]output\r\n\
  620. Options:\r\n\
  621. Filtering:\t-Ffile\tprint only entries matching filtering info in 'file'\r\n\
  622. \t\t-k\tkeep original divider lines\r\n\
  623. \t\t-rN:M\tprint only pages N through M\r\n\
  624. \t\t-x\tinclude Index: lines in formatted output\r\n\
  625. Formatting:\t-b\tboldface headings\t-B\tbold with control codes\r\n\
  626. \t\t-d\t(duplex) print even/odd pages with different indents\r\n\
  627. \t\t-e\t(elite) 96 chars/line\t-tN\tselect typeface N\r\n\
  628. Pagination:\t-H\tadd page headers\t-iN\tindent N spaces\r\n\
  629. \t\t-p\tnumber pages\t\t-nN\tN pages already printed\r\n\
  630. \t\t-wN\twidow lines control\r\n\
  631. \t\t-lN\tprint length\t\t-LN\ttotal page length\r\n\
  632. \t\t\t(0 = infinite)\t(use linefeeds if > #lines printed)\r\n\
  633. Printer:\t-I\tIBM graphics characters\r\n\
  634. \t\t-Pname\tassume printer 'name'\t-P?\tlist printers\r\n\
  635. Summaries:\t-ffile\tdata structure formats\t-sfile\tINT calls\r\n\
  636. \t\t-Tfile\tlist tables\r\n\
  637. Misc:\t\t-m\tprocess multiple parts\t-V\tmake INTERVUE summary\r\n\
  638. "
  639.     ,err) ;
  640.    ip_flush(err) ;
  641.    exit(1) ;
  642. }
  643.  
  644. /***********************************************/
  645.  
  646. void fatal(msg)
  647. char *msg ;
  648. {
  649.    ip_putlit("UNRECOVERABLE ERROR:",err) ;
  650.    newline(err) ;
  651.    ip_puts(msg,err) ;
  652.    newline(err) ;
  653.    exit(1) ;
  654. }
  655.  
  656. /***********************************************/
  657.  
  658. void warning(msg)
  659. char *msg ;
  660. {
  661.    ip_putlit("Warning: ",err) ;
  662.    ip_puts(msg,err) ;
  663.    newline(err) ;
  664. }
  665.  
  666. /***********************************************/
  667.  
  668. IP_FILE *ip_fdopen(fd,buf,bufsiz,maxsiz,write)
  669. int fd ;
  670. char *buf ;
  671. int bufsiz, maxsiz, write ;
  672. {
  673.    IP_FILE *fp = (IP_FILE *)malloc(sizeof(IP_FILE)) ;
  674.    
  675.    if (fp)
  676.       {
  677.       fp->fd = fd ;
  678.       fp->buf = buf ;
  679.       fp->bufsize = bufsiz ;
  680.       fp->buf_maxsize = maxsiz ;
  681.       fp->bufpos = 0 ;
  682.       fp->bufoffset = 0 ;
  683.       fp->write = write ;
  684.       }
  685.    return fp ;
  686. }
  687.  
  688. /***********************************************/
  689.  
  690. IP_FILE *ip_open_write(name,trunc,buf,bufsiz)
  691. char *name ;
  692. char *buf ;
  693. int trunc ;
  694. int bufsiz ;
  695. {
  696.    int fd ;
  697.    
  698.    if (name && *name == '\0')
  699.       fd = 1 ;    /* open stdout */
  700.    else
  701.       {
  702. #ifdef __TURBOC__
  703.       if (trunc)
  704.      fd = _creat(name,0) ;     /* create with no attribute bits sets */
  705.       else
  706.      fd = _open(name,O_WRONLY) ;
  707. #else
  708.       if (trunc) trunc = O_TRUNC ;
  709.       fd = open(name,O_WRONLY|O_BINARY|O_CREAT|trunc,S_IREAD|S_IWRITE) ;
  710. #endif
  711.       if (fd == -1)
  712.      return 0 ;
  713.       if (!trunc)
  714.      lseek(fd,0L,SEEK_END) ;
  715.       }
  716.    return ip_fdopen(fd,buf,bufsiz,bufsiz,1) ;
  717. }
  718.  
  719. /***********************************************/
  720.  
  721. IP_FILE *ip_open_read(name,buf,bufsiz)
  722. char *name ;
  723. char *buf ;
  724. int bufsiz ;
  725. {
  726.    int fd, siz ;
  727.  
  728. #ifdef __TURBOC__
  729.    if ((fd = _open(name,O_RDONLY)) != -1)
  730. #else
  731.    if ((fd = open(name,O_RDONLY | O_BINARY,0)) != -1)
  732. #endif
  733.       {
  734.       siz = read(fd,buf,bufsiz) ;
  735.       if (siz == -1)
  736.      return 0 ;
  737.       return ip_fdopen(fd,buf,siz,bufsiz,0) ;
  738.       }
  739.    else
  740.       return 0 ;
  741. }
  742.  
  743. /***********************************************/
  744.  
  745. int ip_flush(fp)
  746. IP_FILE *fp ;
  747. {
  748.    if (fp->write && fp->bufpos)
  749.       {
  750.       if (fp->bufpos > fp->buf_maxsize)
  751.      fp->bufpos = fp->buf_maxsize ;
  752.       if (write(fp->fd,fp->buf,fp->bufpos) == -1)
  753.      return -1 ;
  754.       fp->bufpos = 0 ;
  755.       }
  756.    return 0 ;       
  757. }
  758.  
  759. /***********************************************/
  760.  
  761. int ip_close(fp)
  762. IP_FILE *fp ;
  763. {
  764.    if (ip_flush(fp) == -1 || close(fp->fd) == -1)
  765.       return -1 ;
  766.    free(fp) ;
  767.    return 0 ;
  768. }
  769.  
  770. /***********************************************/
  771.  
  772. unsigned long ip_fgets(buf, max, fp)
  773. char *buf ;
  774. int max ;
  775. IP_FILE *fp ;
  776. {
  777.    unsigned long line_offset = fp->bufoffset + fp->bufpos ;
  778.    char *end ;
  779.    int len ;
  780.    int new_bufpos ;
  781.    char *fpbuf = fp->buf ;
  782.    int bufpos = fp->bufpos ;
  783.    
  784.    --max ;
  785.    if (bufpos + max < fp->bufsize)
  786.       {
  787.       end = (char *)memchr(fpbuf+bufpos,LINE_TERMINATOR,max) ;
  788.       if (end)
  789.      {
  790.      new_bufpos = (end-fpbuf) ;
  791.      len = new_bufpos++ - bufpos ;
  792.      /* eradicate rest of multi-character line terminator */
  793.      while (len > 0 && fpbuf[bufpos+len-1] <= ' ')
  794.         len-- ;
  795.      }
  796.       else
  797.      new_bufpos = bufpos + len ;
  798.       if (len)
  799.      memcpy(buf,fpbuf+bufpos,len) ;
  800.       buf[len] = '\0' ;
  801.       bufpos = new_bufpos ;
  802.       }
  803.    else
  804.       {
  805.       for (len = 1 ; len <= max ; len++)
  806.      {
  807.      *buf = fpbuf[bufpos++] ;
  808.      if (bufpos >= fp->bufsize)
  809.         {
  810.         if (fp->bufsize < fp->buf_maxsize)
  811.            {
  812.            fp->bufsize = bufpos = 0 ;
  813.            fpbuf[0] = '\0' ;  /* dummy value to ensure empty string */
  814.            }
  815.         else
  816.            {
  817.            fp->bufoffset += fp->buf_maxsize ;
  818.            fp->bufsize = read(fp->fd,fpbuf,fp->buf_maxsize) ;
  819.            bufpos = 0 ;
  820.            }
  821.         if (fp->bufsize <= 0)
  822.            {
  823.            line_offset = (unsigned long)-1 ; /* signal end of file */
  824.            if (*buf != LINE_TERMINATOR)
  825.           *buf++ = LINE_TERMINATOR;
  826.            break ;
  827.            }
  828.         }
  829.      if (*buf == LINE_TERMINATOR)
  830.         break ;
  831.      else
  832.         buf++ ;
  833.      }
  834.       if (len > max)       /* did we overflow before hitting EOL? */
  835.      *buf = '\0' ;       /* if yes, plug in the terminator */
  836.       else
  837.      /* eradicate rest of multi-character line terminator */
  838.      while (len-- > 0 && *buf <= ' ')
  839.         *buf-- = '\0' ;
  840.       }
  841.    fp->bufpos = bufpos ;
  842.    return line_offset ;
  843. }
  844.  
  845. /***********************************************/
  846.  
  847. int ip_write(buf, count, fp)
  848. char *buf ;
  849. int count ;
  850. IP_FILE *fp ;
  851. {
  852.    if (fp->bufpos + count < fp->buf_maxsize)
  853.       {
  854.       memcpy(fp->buf+fp->bufpos,buf,count) ;
  855.       fp->bufpos += count ;
  856.       }
  857.    else
  858.       while (count > 0)
  859.      {
  860.      int partial = fp->buf_maxsize - fp->bufpos ;
  861.  
  862.      if (count < partial)
  863.         partial = count ;
  864.      memcpy(fp->buf+fp->bufpos,buf,partial) ;
  865.      buf += partial ;
  866.      fp->bufpos += partial ;
  867.      count -= partial ;
  868.      if (fp->bufpos >= fp->buf_maxsize && ip_flush(fp) == -1)
  869.            return -1 ;
  870.      }
  871.    return 0 ;
  872. }
  873.  
  874. /***********************************************/
  875.  
  876. #define indent_line(fp) if(indent_string)ip_write(indent_string,indent_len,fp)
  877.  
  878. /***********************************************/
  879.  
  880. void indent_to(where,fp)
  881. int where ;
  882. IP_FILE *fp ;
  883. {
  884.    where += indent ;
  885.    while (where >= 8)
  886.       {
  887.       ip_putc('\t',fp) ;
  888.       where -= 8 ;
  889.       }
  890.    if (where)
  891.       ip_write("        ",where,fp) ;
  892. }
  893.  
  894. /***********************************************/
  895.  
  896. void put_line(fp,len)
  897. IP_FILE *fp ;
  898. int len ;
  899. {
  900.    static char line[8] = { 196, 196, 196, 196, 196, 196, 196, 196 } ;
  901.  
  902.    if (IBM_chars)
  903.       {
  904.       while (len >= 8)
  905.      {
  906.      ip_write(line,8,fp) ;
  907.      len -= 8 ;
  908.      }
  909.       if (len)
  910.      ip_write(line,len,fp) ;
  911.       }
  912.    else
  913.       {
  914.       while (len >= 8)
  915.      {
  916.      ip_write("--------",8,fp) ;
  917.      len -= 8 ;
  918.      }
  919.       if (len)
  920.      ip_write("--------",len,fp) ;
  921.       }
  922. }
  923.  
  924. /***********************************************/
  925.  
  926. void HPPCL_put_line(fp,len)
  927. IP_FILE *fp ;
  928. int len ;
  929. {
  930.    ip_putlit(HPPCL_IBM_LN_A,fp) ;
  931.    ip_puts(itoa((len * 25), num, 10),fp) ;
  932.    ip_putlit(HPPCL_IBM_LN_B,fp) ;
  933. }
  934.  
  935. /***********************************************/
  936.  
  937. void HPPCL_set_typeface(fp,typeface)
  938. IP_FILE *fp ;
  939. char *typeface ;
  940. {
  941.    ip_putlit(HPPCL_FONT_ON_A,fp) ;
  942.    if (typeface)
  943.       ip_puts(typeface,fp) ;
  944.    else
  945.       ip_putlit("8",fp) ;
  946.    ip_putlit(HPPCL_FONT_ON_B,fp) ;
  947. }
  948.  
  949. /***********************************************/
  950.  
  951. int is_keyword(s,keys,numkeys)
  952. char *s ;
  953. KEYWORDS *keys ;
  954. unsigned int numkeys ;
  955. {
  956.    register int cmp ;
  957.    register unsigned int i ;
  958.    KEYWORDS *currkey ;
  959.    int firstchar = *s ;
  960.  
  961.    do {
  962.       i = numkeys / 2 ;
  963.       currkey = &keys[i] ;
  964.       cmp = (firstchar - currkey->name[0]) ;
  965.       if (cmp == 0)
  966.          cmp = memcmp(s,currkey->name,currkey->length) ;
  967.       if (cmp < 0)
  968.      numkeys = i ;
  969.       else if (cmp > 0)
  970.      {
  971.      keys = currkey+1 ;
  972.      numkeys -= i+1 ;
  973.      }
  974.       else
  975.      return TRUE ;
  976.       } while (numkeys) ;
  977.    return FALSE ;
  978. }
  979.  
  980. /***********************************************/
  981.  
  982. void output_line(line,fp)
  983. char *line ;
  984. IP_FILE *fp ;
  985. {
  986.    if (*line)
  987.       {
  988.       int pos = 0 ;
  989.       int len = strlen(line) ;
  990.       
  991.       indent_line(fp) ;
  992.       if (boldface)
  993.      {
  994.      if (start_of_entry(line) || start_of_table(line))
  995.         {
  996.         if (printer_bold)
  997.            {
  998.            ip_putcstr(&printer->bold_on,fp) ;
  999.            ip_write(line,len,fp) ;
  1000.            ip_putcstr(&printer->bold_off,fp) ;
  1001.            newline(fp) ;
  1002.            return ;
  1003.            }
  1004.         else
  1005.            {
  1006.            ip_write(line,len,fp) ;
  1007.            ip_putc('\r',fp) ;
  1008.            indent_line(fp) ;
  1009.            }
  1010.         }
  1011.      else if (section_start(line))
  1012.         {
  1013.         pos = (char *)memchr(line,':',len) - line ;
  1014.         if (printer_bold)
  1015.            {
  1016.            ip_putcstr(&printer->bold_on,fp) ;
  1017.            ip_write(line,pos,fp) ;
  1018.            ip_putcstr(&printer->bold_off,fp) ;
  1019.            line += pos ;       /* adjust because no longer at left edge */
  1020.            }
  1021.         else
  1022.            {
  1023.            ip_write(line,pos,fp) ;
  1024.            ip_putc('\r',fp) ;
  1025.            indent_line(fp) ;
  1026.            }
  1027.         }
  1028.      } /* boldface */
  1029.       if (indent % 8)
  1030.      {
  1031.      while (*line)
  1032.         {
  1033.         if (*line == '\t')
  1034.            {
  1035.            ip_write("        ",8-(pos%8),fp) ;
  1036.            pos = 0 ;   /* absolute column doesn't matter, only mod 8 */
  1037.            }
  1038.         else
  1039.            {
  1040.            ip_putc(*line,fp) ;
  1041.            pos++ ;
  1042.            }
  1043.         line++ ;
  1044.         }
  1045.      }
  1046.       else
  1047.      ip_write(line,len,fp) ;
  1048.       }
  1049.    newline(fp) ;
  1050. }
  1051.  
  1052. /***********************************************/
  1053.  
  1054. void get_raw_line(buf)
  1055. char *buf ;
  1056. {
  1057.    IP_FILE *in ;
  1058.    
  1059.    buf[0] = '\0' ;
  1060.    if (out_of_files)
  1061.       return ;
  1062.    current_line_offset = ip_fgets(buf,MAXLINE,infile) ;
  1063.    if (current_line_offset == (unsigned long)-1)
  1064.       if (multi_file)
  1065.      {
  1066.      offset_adjust += lseek(infile->fd,0L,SEEK_END) ;
  1067.      input_file[input_file_namelen-1]++ ;
  1068.      ip_close(infile) ;
  1069.      if ((in = ip_open_read(input_file,infile_buf,sizeof(infile_buf)))
  1070.            != NULL)
  1071.         {
  1072.         infile = in ;
  1073.         current_line_offset = ip_fgets(buf,MAXLINE,infile) ;
  1074.         }
  1075.      else
  1076.         {
  1077.         out_of_files = TRUE ;
  1078.         return ;
  1079.         }
  1080.      }
  1081.       else
  1082.      out_of_files = TRUE ;
  1083. }
  1084.  
  1085. /***********************************************/
  1086.  
  1087. int unwanted_section(buf)
  1088. char *buf ;
  1089. {
  1090.    int found ;
  1091.    char str[MAXLINE] ;
  1092.    FILT_LIST *p ;
  1093.  
  1094.    if (start_of_entry(buf)) /* is it an interrupt entry? */
  1095.       {
  1096.       strcpy(str,buf) ;
  1097.       (void) strupr(str) ;
  1098.       /* section is unwanted if *any* exclude string matches */
  1099.       for (p = excludes ; p ; p = p->next)
  1100.      {
  1101.      if (p->str && strstr(str, p->str) != NULL)
  1102.         return TRUE ;
  1103.      }
  1104.       /* if still wanted, set to TRUE if *no* include string matches */
  1105.       found = FALSE ;
  1106.       for (p = includes ; p ; p = p->next)
  1107.      {
  1108.      if (p->str && strstr(str, p->str) != NULL)
  1109.         {
  1110.         found = TRUE ;
  1111.         break ;
  1112.         }
  1113.      }
  1114.       if (!found)
  1115.      return TRUE ;
  1116.       }
  1117.    return FALSE ;
  1118. }
  1119.  
  1120. /***********************************************/
  1121.  
  1122. void get_line(buf)
  1123. char *buf ;
  1124. {
  1125.    static char next_line[MAXLINE] ;
  1126.    static int readahead = FALSE ;
  1127.  
  1128.    /* get the next line from the file, skipping unwanted entries */
  1129.    if (readahead)
  1130.       {
  1131.       strcpy(buf,next_line) ;
  1132.       readahead = FALSE ;
  1133.       }
  1134.    else
  1135.       {
  1136.       do {
  1137.      get_raw_line(buf) ;
  1138.      } while (!include_index_lines && index_line(buf)) ;
  1139.       if (section_file_start(buf))
  1140.      do {
  1141.         get_raw_line(buf) ;
  1142.         } while (buf[0] && !divider_line(buf)) ;
  1143.       if (do_filter)
  1144.      {
  1145.      /* if we read a divider line while filtering, we have to look ahead */
  1146.      strcpy(next_line,buf);
  1147.      while (next_line[0] && divider_line(next_line))
  1148.         {
  1149.         strcpy(buf,next_line) ; /* we may be returning the divider */
  1150.         get_raw_line(next_line) ;
  1151.         if (unwanted_section(next_line))
  1152.            {
  1153.            while (!divider_line(next_line))
  1154.           get_raw_line(next_line) ;
  1155.            }
  1156.         else /* section is wanted, so return divider and then next line */
  1157.            readahead = TRUE ;
  1158.         }
  1159.      }
  1160.       }
  1161. }
  1162.  
  1163. /***********************************************/
  1164.  
  1165. void fill_buffer(lines,lines_per_page)
  1166. int lines, lines_per_page ;
  1167. {
  1168.    int i ;
  1169.  
  1170.    /* copy remainder, if any, from last page to top of current page */
  1171.    if (lines)
  1172.       for (i = lines ; i < lines_per_page ; i++)
  1173.      {
  1174.      strcpy(buffer[i-lines], buffer[i]) ;
  1175.      line_offsets[i-lines] = line_offsets[i] ;
  1176.      }
  1177.    else
  1178.       lines = lines_per_page ;
  1179.    for (i = lines_per_page - lines ; i < lines_per_page ; i++)
  1180.       {
  1181.       get_line(buffer[i]) ;
  1182.       line_offsets[i] = current_line_offset + offset_adjust ;
  1183.       }
  1184. }
  1185.  
  1186. /***********************************************/
  1187.  
  1188. int find_page_break(lines)
  1189. int lines ;
  1190. {
  1191.    int i ;
  1192.    char *buf ;
  1193.  
  1194.    for (i = 0 ; i < widow_length ; i++)
  1195.       {
  1196.       buf = buffer[lines-i-1] ;
  1197.       if (buf[0] == '\0' || divider_line(buf))
  1198.      return lines - i ;
  1199.       else if (section_start(buf))
  1200.      return lines - i - 1 ;
  1201.       }
  1202.    return lines ;
  1203. }
  1204.  
  1205. /***********************************************/
  1206.  
  1207. int summarize(line, pages_printed)
  1208. int line, pages_printed ;
  1209. {
  1210.    char *s, reg ;
  1211.    int i ;
  1212.    int max_descrip ;
  1213.    int len, numlen ;
  1214.  
  1215.    s = buffer[line] ;
  1216.    if (start_of_entry(s))
  1217.       {
  1218.       memcpy(summary_line," -- -- -- ",10) ;
  1219.       summary_line[1] = s[4] ;     /* output interrupt number */
  1220.       summary_line[2] = s[5] ;
  1221.       len = 4 ;
  1222.       s = buffer[line+1] ;
  1223.       while (*s && isspace(*s))
  1224.      s++ ;
  1225.       if (*s == 'A')
  1226.      {
  1227.      reg = s[1] ;
  1228.      while (*s && *s != '=')
  1229.         s++ ;
  1230.      s++ ;        /* skip the equal sign */
  1231.      while (*s && isspace(*s))
  1232.         s++ ;    /* skip the space between equal sign and number */
  1233.      if (isxdigit(*s) && isxdigit(s[1]))
  1234.         {
  1235.         if (reg == 'L')
  1236.            len += 3 ;
  1237.         summary_line[len++] = *s++ ;
  1238.         summary_line[len++] = *s++ ;
  1239.         if (reg == 'X')
  1240.            {
  1241.            len++ ;
  1242.            summary_line[len++] = *s++ ;
  1243.            summary_line[len] = *s ;
  1244.            }
  1245.         }
  1246.      }
  1247.       len = 10 ;
  1248.       if (page_numbers)
  1249.      {
  1250.      itoa(pages_printed,num,10) ;
  1251.      numlen = strlen(num) ;
  1252.      for (i = numlen ; i < 3 ; i++)
  1253.         summary_line[len++] = ' ' ;
  1254.      memcpy(summary_line+len,num,numlen) ;
  1255.      len += numlen ;
  1256.      summary_line[len++] = ' ' ;
  1257.      }
  1258.       s = buffer[line] + 7 ;    /* find function description */
  1259.       if (*s && *s != '-')    /* does the heading contain flags? */
  1260.      {
  1261.      while (*s && !isspace(*s))
  1262.         summary_line[len++] = *s++ ;
  1263.      summary_line[len++] = '>' ;
  1264.      summary_line[len++] = ' ' ;
  1265.      while (*s && *s != '-')
  1266.         s++ ;
  1267.      }
  1268.       while (*s && !isspace(*s))
  1269.      s++ ;
  1270.       while (*s && isspace(*s))
  1271.      s++ ;
  1272.       max_descrip = (page_width > sizeof(summary_line)-1) ? 
  1273.                    sizeof(summary_line)-1 : page_width ;
  1274.       while (len < max_descrip && *s)
  1275.      summary_line[len++] = *s++ ;
  1276.       summary_line[len] = '\0' ;
  1277.       summary_line_len = len ;
  1278.       return 1 ;
  1279.       }
  1280.    else
  1281.       return 0 ;
  1282. }
  1283.  
  1284. /***********************************************/
  1285.  
  1286. void start_format(line)
  1287. char *line ;
  1288. {
  1289.    indent_line(formats) ;
  1290.    (*printer->put_line)(formats,79) ;
  1291.    newline(formats) ;
  1292.    indent_line(formats) ;
  1293.    ip_puts(summary_line,formats) ;
  1294.    newline(formats) ;
  1295.    indent_line(formats) ;
  1296.    ip_putc('\t',formats) ;
  1297.    ip_puts(line+10,formats) ;
  1298.    newline(formats) ;
  1299.    echo_format = TRUE ;
  1300. }
  1301.  
  1302. /***********************************************/
  1303.  
  1304. void show_offset(line,fp)
  1305. int line ;
  1306. IP_FILE *fp ;
  1307. {
  1308.    char offset_string[12] ;
  1309.    int len ;
  1310.    
  1311.    ultoa(line_offsets[line],offset_string,16) ;
  1312.    len = strlen(offset_string) ;
  1313.    ip_write("00000000",8-len,fp) ;
  1314.    ip_write(offset_string,len,fp) ;
  1315. }
  1316.  
  1317. /***********************************************/
  1318.  
  1319. void add_table(i)
  1320. int i ;
  1321. {
  1322.    char firstchar ;
  1323.    char num[6] ;
  1324.    char *end ;
  1325.    int len ;
  1326.    int summary_width ;
  1327.    char found = FALSE ;
  1328.    
  1329.    prev_table++ ;
  1330.    firstchar = buffer[i][0] ;
  1331.    if (firstchar == 'C' || firstchar == 'V')  /* Call.. or Values... ? */
  1332.       {
  1333.       if (i > 0 && buffer[i-1][0] == '(')
  1334.      {
  1335.      memcpy(num,buffer[i-1]+7,4) ;
  1336.      num[4] = '\0' ;
  1337.      len = 4 ;
  1338.      found = TRUE ;
  1339.      }
  1340.       }
  1341.    else if (firstchar == 'B' || firstchar == 'F') /* Bitfields.. or Format..? */
  1342.       {
  1343.       end = strrchr(buffer[i+1]+7,')') ;   /* rule out Bit(s) as only match */
  1344.       if (end)
  1345.      {
  1346.      memcpy(num,end-4,4) ;
  1347.      num[4] = '\0' ;
  1348.      len = 4 ;
  1349.      found = TRUE ;
  1350.      }
  1351.       }
  1352.    if (!found)
  1353.       {
  1354.       itoa(prev_table,num,10) ;
  1355.       len = strlen(num) ;
  1356.       }
  1357.    indent_line(tables) ;
  1358.    if (show_offsets)
  1359.       show_offset(i,tables) ;
  1360.    ip_write(" 0000",5-len,tables) ;
  1361.    ip_write(num,len,tables);
  1362.    if (page_numbers)
  1363.       {
  1364.       summary_width = 13 ;
  1365.       while (summary_line[summary_width] != ' ')
  1366.      summary_width++ ;
  1367.       summary_width++ ;    /* include the blank we found */
  1368.       }
  1369.    else
  1370.       summary_width = 10 ;
  1371.    ip_write(summary_line,summary_width,tables) ;
  1372.    len = strlen(buffer[i])-1 ;
  1373.    if (len > page_width - summary_width - 5)
  1374.       len = page_width - summary_width - 5 ;
  1375.    ip_write(buffer[i],len,tables) ;
  1376.    newline(tables) ;
  1377. }
  1378.  
  1379. /***********************************************/
  1380.  
  1381. int make_description(desc,line)
  1382. char *desc ;
  1383. int line ;
  1384. {
  1385.    char *start = desc ;
  1386.    
  1387.    summarize(line,pages_printed) ;
  1388.    memcpy(desc,"INT ", 4) ;
  1389.    desc += 4 ;
  1390.    *desc++ = summary_line[1] ;
  1391.    *desc++ = summary_line[2] ;
  1392.    if (summary_line[4] != '-')
  1393.       {
  1394.       memcpy(desc,", AH=", 5) ;
  1395.       desc += 5 ;
  1396.       *desc++ = summary_line[4] ;
  1397.       *desc++ = summary_line[5] ;
  1398.       }
  1399.    if (summary_line[7] != '-')
  1400.       {
  1401.       memcpy(desc,", AL=", 5) ;
  1402.       desc += 5 ;
  1403.       *desc++ = summary_line[7] ;
  1404.       *desc++ = summary_line[8] ;
  1405.       }
  1406.    *desc = '\0' ;
  1407.    return (desc-start)+1 ;
  1408. }
  1409.  
  1410. /***********************************************/
  1411.  
  1412. char *determine_heading(last)
  1413. int last ;
  1414. {
  1415.    int i ;
  1416.    static char heading[MAXLINE] ;
  1417.    char save[25] ;
  1418.    char num[10] ;
  1419.  
  1420.    /* ugly hack to keep the combination of -H and -T from showing wrong page */
  1421.    /* numbers for tables--copy last summary line from previous page to safe */
  1422.    /* place before processing current page, then restore it */
  1423.    memcpy(save,summary_line,sizeof(save)) ;
  1424.    if (start_of_entry(buffer[0]))
  1425.       {
  1426.       header_first.len = make_description(header_first.desc,0) ;
  1427.       header_first.part = 1 ;
  1428.       header_first.first_on_page = TRUE ;
  1429.       }
  1430.    else if (header_last.part == 0)  /* very first entry? */
  1431.       {
  1432.       for (i = 0 ; i < last ; i++)
  1433.      if (start_of_entry(buffer[i]))
  1434.         {
  1435.         header_first.len = make_description(header_first.desc,i) ;
  1436.         header_first.part = 1 ;
  1437.         header_first.first_on_page = TRUE ;
  1438.         break ;
  1439.         }
  1440.       }
  1441.    else
  1442.       {
  1443.       header_first.len = header_last.len ;
  1444.       memcpy(header_first.desc,header_last.desc,header_last.len) ;
  1445.       header_first.part = header_last.part + 1 ;
  1446.       header_first.first_on_page = FALSE ;
  1447.       }
  1448.    /* assume entry spans entire page */
  1449.    header_last.len = header_first.len ;
  1450.    memcpy(header_last.desc,header_first.desc,header_first.len) ;
  1451.    header_last.part = header_first.part ;
  1452.    header_last.first_on_page = header_first.first_on_page ;
  1453.    /* find last entry on page */
  1454.    if (header_first.part > 0)
  1455.       {
  1456.       for (i = last-1 ; i > 0 ; i--)
  1457.      if (start_of_entry(buffer[i]))
  1458.         {
  1459.         header_last.len = make_description(header_last.desc,i) ;
  1460.         header_last.part = 1 ;
  1461.         header_last.first_on_page = FALSE ;
  1462.         break ;
  1463.         }
  1464.       memcpy(heading,header_first.desc,header_first.len) ;
  1465.       if (header_first.part > 1)
  1466.      {
  1467.      strcat(heading," (Part ") ;
  1468.      strcat(heading,itoa(header_first.part,num,10)) ;
  1469.      strcat(heading,")") ;
  1470.      }
  1471.       if (memcmp(header_first.desc,header_last.desc,header_last.len) != 0 ||
  1472.       header_first.part != header_last.part)
  1473.      {
  1474.      strcat(heading," to ") ;
  1475.      strcat(heading,header_last.desc) ;
  1476.      if (header_last.part > 1)
  1477.         {
  1478.         strcat(heading," (Part ") ;
  1479.         strcat(heading,itoa(header_last.part,num,10)) ;
  1480.         strcat(heading,")") ;
  1481.         }
  1482.      }
  1483.       memcpy(summary_line,save,sizeof(save)) ;
  1484.       return heading ; 
  1485.       }
  1486.    else /* no headings yet */
  1487.       {
  1488.       memcpy(summary_line,save,sizeof(save)) ;
  1489.       return NULL ;
  1490.       }
  1491. }
  1492.  
  1493. /***********************************************/
  1494.  
  1495. void print_buffer(last,body_lines,lines_per_page,total_lines,use_FF)
  1496. int last, body_lines, lines_per_page, total_lines ;
  1497. int use_FF ;
  1498. {
  1499.    int i, len ;
  1500.    int headpos ;
  1501.    int print_this_page = (pages_printed>=first_page && pages_printed<=last_page);
  1502.    
  1503.    pages_printed++ ;
  1504.    if (do_headers)
  1505.       {
  1506.       char *heading ;
  1507.       
  1508.       if ((heading = determine_heading(last)) != NULL)
  1509.      {
  1510.      if (print_this_page)
  1511.         {
  1512.         len = strlen(heading) ;
  1513.         headpos = 40-len/2 ;
  1514.         indent_to(headpos,outfile) ;
  1515.         if (boldface)
  1516.            {
  1517.            if (printer_bold)
  1518.           {
  1519.           ip_putcstr(&printer->bold_on,outfile) ;
  1520.           ip_write(heading,len,outfile) ;
  1521.           ip_putcstr(&printer->bold_off,outfile) ;
  1522.           }
  1523.            else
  1524.           {
  1525.           ip_write(heading,len,outfile) ;
  1526.           ip_putc('\r',outfile) ;
  1527.           indent_to(headpos,outfile) ;
  1528.           ip_write(heading,len,outfile) ;
  1529.           }
  1530.            }
  1531.         else
  1532.            ip_write(heading,len,outfile) ;
  1533.         }
  1534.      }
  1535.       newline(outfile) ;
  1536.       newline(outfile) ;
  1537.       }
  1538.    for (i = 0 ; i < last ; i++)
  1539.       {
  1540.       if (print_this_page)
  1541.      {
  1542.      char *line = buffer[i] ;
  1543.      if (*line)
  1544.         {
  1545.         if (!keep_divider_lines && divider_line(line))
  1546.            {
  1547.            indent_line(outfile) ;
  1548.            (*printer->put_line)(outfile,79) ;
  1549.            newline(outfile) ;
  1550.            echo_format = FALSE ;
  1551.            }
  1552.         else
  1553.            {
  1554.            output_line(line, outfile) ;
  1555.            if (echo_format)
  1556.           output_line(line,formats) ;
  1557.            }
  1558.         }
  1559.      else
  1560.         {
  1561.         newline(outfile) ;
  1562.         echo_format = FALSE ;
  1563.         }
  1564.      }
  1565.       /* need summary lines if doing summary, formats, or table index */
  1566.       if (need_summary)
  1567.      {
  1568.      if (summarize(i,pages_printed) && do_summary && summary)
  1569.         {
  1570.         if (show_offsets)
  1571.            show_offset(i,summary) ;
  1572.         ip_write(summary_line,summary_line_len,summary) ;
  1573.         newline(summary) ;
  1574.         }
  1575.      if (do_formats && memcmp(buffer[i],"Format ",7) == 0)
  1576.         start_format(buffer[i]) ;
  1577.      if (do_tables && start_of_table(buffer[i]))
  1578.         add_table(i) ;
  1579.      }
  1580.       }
  1581.    if (print_this_page)
  1582.       {
  1583.       if (page_numbers)
  1584.      {
  1585.      for (i = last ; i <= body_lines ; i++)
  1586.         newline(outfile) ;
  1587.      itoa(pages_printed, num, 10) ;
  1588.      i = strlen(num) ;
  1589.      if (!duplex)
  1590.         indent_to(38-i/2,outfile) ;
  1591.      else if ((pages_printed % 2) == 1)
  1592.         indent_to(75-i/2,outfile) ;
  1593.      else
  1594.         indent_to(2,outfile) ;
  1595.      ip_putlit("- ", outfile) ;
  1596.      ip_write(num, i, outfile) ;
  1597.      ip_putlit(" -", outfile) ;
  1598.      newline(outfile) ;
  1599.      }
  1600.       if (use_FF)
  1601.      ip_putc('\f',outfile) ;
  1602.       else
  1603.      for (i = page_numbers?lines_per_page:last ; i<total_lines ; i++)
  1604.         newline(outfile) ;
  1605.       if (duplex)
  1606.      {
  1607.      if ((pages_printed % 2) == 1)    /* next page even or odd? */
  1608.         ip_putcstr(&printer->marginl, outfile) ;    /* even page */
  1609.      else
  1610.         ip_putcstr(&printer->marginr, outfile) ;    /* odd page */
  1611.      }
  1612.       }
  1613. }
  1614.  
  1615. /***********************************************/
  1616.  
  1617. void display_printers()
  1618. {
  1619.    int i ;
  1620.    
  1621.    ip_putlit("Valid printer names are:",err) ;
  1622.    newline(err) ;
  1623.    for (i = 0 ; i < NUM_PRINTERS ; i++)
  1624.       {
  1625.       ip_putc('\t',err) ;
  1626.       ip_puts(printers[i].name,err) ;
  1627.       newline(err) ;
  1628.       }
  1629.    ip_putlit("When entering the printer name, use either a dash or an",err) ;
  1630.    newline(err) ;
  1631.    ip_putlit("underscore in place of blanks.  Case is ignored, and the",err) ;
  1632.    newline(err) ;
  1633.    ip_putlit("name may be abbreviated to the shortest unique prefix.",err) ;
  1634.    newline(err) ;
  1635.    exit(1) ;
  1636. }
  1637.  
  1638. /***********************************************/
  1639.  
  1640. void select_printer(name)
  1641. char *name ;
  1642. {
  1643.    int i, len, prt = -1 ;
  1644.    
  1645.    len = strlen(name) ;
  1646.    for (i = 0 ; i < len ; i++)        /* convert dashes and underscores to blanks */
  1647.       if (name[i] == '-' || name[i] == '_')
  1648.      name[i] = ' ' ;
  1649.    for (i = 0 ; i < NUM_PRINTERS ; i++)
  1650.       if (strnicmp(name,printers[i].name,len) == 0)
  1651.      if (prt == -1)
  1652.         prt = i ;
  1653.      else
  1654.         fatal("Ambiguous printer name!  Use -P? to list printers.") ;
  1655.    if (prt == -1)
  1656.       fatal("Unknown printer name!  Use -P? to list printers.") ;
  1657.    else
  1658.       printer = &printers[prt] ;
  1659. }
  1660.  
  1661. /***********************************************/
  1662.  
  1663. FILT_LIST *add_filter_info(list,str)
  1664. FILT_LIST *list ;
  1665. char *str ;
  1666. {
  1667.    FILT_LIST *newfilt ;
  1668.    int len = strlen(str)+1 ;
  1669.    
  1670.    if ((newfilt = (FILT_LIST *)malloc(sizeof(struct filter_list)+len))
  1671.       != NULL)
  1672.       {
  1673.       newfilt->next = list ;
  1674.       memcpy(newfilt->str,str,len) ;
  1675.       strupr(newfilt->str) ;
  1676.       }
  1677.    else
  1678.       fatal("out of memory") ;
  1679.    return newfilt ;
  1680. }
  1681.  
  1682. /***********************************************/
  1683.  
  1684. void build_filter_lists(file)
  1685. char *file ;
  1686. {
  1687.    IP_FILE *fp ;
  1688.    char buf[MAXLINE] ;
  1689.    int len ;
  1690.    long result ;
  1691.  
  1692.    if ((fp = ip_open_read(file,filter_buf,sizeof(filter_buf))) == NULL)
  1693.       {
  1694.       warning("unable to open filtering file, will print entire list.") ;
  1695.       do_filter = FALSE ;
  1696.       }
  1697.    else /* OK, file is open, so start reading */
  1698.       {
  1699.       do {
  1700.      buf[0] = '\0' ;
  1701.      result = ip_fgets(buf, sizeof(buf), fp) ;
  1702.      len = strlen(buf) ;
  1703.      if (len > 1)
  1704.         {
  1705.         switch (buf[0])
  1706.            {
  1707.            case '+':
  1708.           includes = add_filter_info(includes,buf+1) ;
  1709.           break ;
  1710.            case '-':
  1711.           excludes = add_filter_info(excludes,buf+1) ;
  1712.           break ;
  1713.            case '#':        /* comment lines */
  1714.            default:
  1715.           break ;
  1716.            }
  1717.         }
  1718.      } while (result != -1) ;
  1719.       ip_close(fp) ;
  1720.       do_filter = TRUE ;
  1721.       }
  1722. }
  1723.  
  1724. /***********************************************/
  1725.  
  1726. void write_summary_header(fp,title,show_offsets,show_table)
  1727. IP_FILE *fp ;
  1728. char *title ;
  1729. int show_offsets, show_table ;
  1730. {
  1731.    /* set up the printer */
  1732.    ip_putcstr(&printer->init1,fp) ;
  1733.    ip_putcstr(&printer->init2,fp) ;
  1734.    ip_putcstr(&printer->marginc,fp) ;
  1735.    /* now start writing the actual header */
  1736.    indent_to(show_offsets?8:0,fp) ;
  1737.    ip_putlit("\t\t\t\t",fp) ;
  1738.    ip_puts(title,fp) ;
  1739.    newline(fp) ;
  1740.    indent_to(show_offsets?8:0,fp) ;
  1741.    ip_putlit("\t\t\t\t",fp) ;
  1742.    (*printer->put_line)(fp,strlen(title)) ;
  1743.    newline(fp) ;
  1744.    newline(fp) ;
  1745.    indent_line(fp) ;
  1746.    if (show_offsets)
  1747.       ip_putlit("Offset  ", fp) ;
  1748.    if (show_table)
  1749.       ip_putlit("Tbl# ",fp) ;
  1750.    ip_putlit("INT AH AL", fp) ;
  1751.    if (page_numbers)
  1752.       ip_putlit(" Page", fp) ;
  1753.    ip_putlit("\t\t\tDescription", fp) ;
  1754.    newline(fp) ;
  1755.    indent_line(fp) ;
  1756.    (*printer->put_line)(fp,page_width+(show_offsets?8:0)) ;
  1757.    newline(fp) ;
  1758. }
  1759.  
  1760. /***********************************************/
  1761.  
  1762. static void reset_printer_and_close(fp)
  1763. IP_FILE *fp ;
  1764. {
  1765.    ip_putcstr(&printer->term1,fp) ;
  1766.    ip_putcstr(&printer->term2,fp) ;
  1767.    ip_close(fp) ;
  1768. }
  1769.  
  1770. /***********************************************/
  1771.  
  1772. int _Cdecl main(argc,argv)
  1773. int argc ;
  1774. char *argv[] ;
  1775. {
  1776.    int lines_per_page = -1 ;
  1777.    int total_lines = -1 ;
  1778.    int use_FF = TRUE ;
  1779.    int last_line ;
  1780.    int body_lines ;
  1781.    char *typeface = NULL ;
  1782.    char *summary_file = NULL ;
  1783.    char *table_file = NULL ;
  1784.    char *formats_file = NULL ;
  1785.    char *filter_file = NULL ;
  1786.    char *last_page_num ;
  1787.  
  1788.    err = ip_fdopen(2,stderr_buf,sizeof(stderr_buf),sizeof(stderr_buf),1) ;
  1789.    ip_putlit("INTPRINT v", err) ;
  1790.    ip_putlit(VERSION, err) ;
  1791.    ip_putlit(" by Ralf Brown and others.  Donated to the Public Domain.",err) ;
  1792.    newline(err) ;
  1793.    ip_flush(err) ;
  1794.    if (argc == 1 && isatty(0))
  1795.       usage() ;      /* give help if invoked with no args and keybd input */
  1796.    while (argc >= 2 && argv[1][0] == '-')
  1797.       {
  1798.       switch (argv[1][1])
  1799.      {
  1800.      case 'B':
  1801.         printer_bold = TRUE ;
  1802.         /* fall through to -b */
  1803.      case 'b':
  1804.         boldface = TRUE ;
  1805.         break ;
  1806.      case 'd':
  1807.         duplex = TRUE ;
  1808.         break ;
  1809.      case 'e':
  1810.         indent = 8 ;
  1811.         page_width = 87 ;  /* 96 - indent - 1 right margin */
  1812.         break ;
  1813.      case 'f':
  1814.         formats_file = argv[1]+2 ;
  1815.         break ;
  1816.      case 'F':
  1817.         filter_file = argv[1]+2 ;
  1818.         break ;
  1819.      case 'H':   /* page headers */
  1820.         do_headers = TRUE ;
  1821.         break ;
  1822.      case 'i':
  1823.         indent = atoi(argv[1]+2) ;
  1824.         break ;
  1825.      case 'I':
  1826.         IBM_chars = TRUE ;
  1827.         break ;
  1828.      case 'k':
  1829.         keep_divider_lines = TRUE ;
  1830.         break ;
  1831.      case 'l':
  1832.         lines_per_page = atoi(argv[1]+2) ;
  1833.         break ;
  1834.      case 'L':
  1835.         total_lines = atoi(argv[1]+2) ;
  1836.         break ;
  1837.      case 'm':
  1838.         multi_file = TRUE ;
  1839.         break ;
  1840.      case 'n':
  1841.         pages_printed = atoi(argv[1]+2) ;
  1842.         break ;
  1843.      case 'P':
  1844.         if (argv[1][2] == '?')
  1845.            display_printers() ;
  1846.         else
  1847.            select_printer(argv[1]+2) ;
  1848.         break ;
  1849.      case 'p':
  1850.         page_numbers = TRUE ;
  1851.         break ;
  1852.      case 'r':
  1853.         first_page = atoi(argv[1]+2) ;
  1854.         last_page_num = strchr(argv[1]+2, ':') ;
  1855.         last_page = last_page_num ? atoi(last_page_num+1) : 0 ;
  1856.         if (last_page == 0)
  1857.            last_page = ~0 ;
  1858.         break ;
  1859.      case 's':
  1860.         summary_file = argv[1]+2 ;
  1861.         break ;
  1862.      case 't':
  1863.         typeface = argv[1]+2 ;
  1864.         break ;
  1865.      case 'T':
  1866.         table_file = argv[1]+2 ;
  1867.         break ;
  1868.      case 'V':
  1869.         show_offsets = IBM_chars = TRUE ;
  1870.         break ;
  1871.      case 'w':
  1872.         widow_length = atoi(argv[1]+2) ;
  1873.         break ;
  1874.      case 'x':
  1875.         include_index_lines = TRUE ;
  1876.         break ;
  1877.      default:
  1878.         usage() ;
  1879.      }
  1880.       argv++ ;
  1881.       argc-- ;
  1882.       }
  1883.    if (printer == NULL)
  1884.       select_printer("default") ;
  1885.    /* apply any necessary overrides to parameters */
  1886.    if (printer->indent != -1)
  1887.       indent = printer->indent ;
  1888.    if (lines_per_page < 0)
  1889.       lines_per_page = printer->lines_per_page ;
  1890.    if (total_lines <= 0)
  1891.       total_lines = printer->page_length ;
  1892.    if (page_width <= 0)
  1893.       page_width = printer->page_width ;
  1894.    if (show_offsets && page_width < 80)
  1895.       page_width = 80 ;
  1896.    if (printer->flag)
  1897.       *(printer->flag) = TRUE ;
  1898.    if (cstrlen(&printer->bold_on) == 0)     /* control sequences for bold? */
  1899.       printer_bold = FALSE ;        /* if not, don't try to use them */
  1900.    /* build the indent string */
  1901.    if (indent)
  1902.       {
  1903.       char *t ;
  1904.       int ind = indent ;
  1905.  
  1906.       indent_len = indent/8 + indent%8 ;
  1907.       t = indent_string = (char *)malloc(indent_len+1) ;
  1908.       while (ind >= 8)
  1909.      {
  1910.      *t++ = '\t' ;
  1911.      ind -= 8 ;
  1912.      }
  1913.       while (ind > 0)
  1914.      {
  1915.      *t++ = ' ' ;
  1916.      ind-- ;
  1917.      }
  1918.       }
  1919.    /* open the summary file, if any */
  1920.    if (summary_file && *summary_file)
  1921.       if ((summary = ip_open_write(summary_file,!pages_printed,summary_buf,
  1922.                    sizeof(summary_buf)))
  1923.         != NULL)
  1924.      do_summary = TRUE ;
  1925.       else
  1926.      warning("unable to open summary file") ;
  1927.    /* open the table index file, if any */
  1928.    if (table_file && *table_file)
  1929.       if ((tables = ip_open_write(table_file,!pages_printed,tables_buf,
  1930.                   sizeof(tables_buf)))
  1931.         != NULL)
  1932.      do_tables = TRUE ;
  1933.       else
  1934.      warning("unable to open table index file") ;
  1935.    /* open the data formats file, if any */
  1936.    if (formats_file && *formats_file)
  1937.       if ((formats = ip_open_write(formats_file,!pages_printed,formats_buf,
  1938.                    sizeof(formats_buf)))
  1939.         != NULL)
  1940.      do_formats = TRUE ;
  1941.       else
  1942.      warning("unable to open formats file") ;
  1943.    need_summary = (do_summary || do_formats || do_tables) ;
  1944.    /* initialize filtering data, if specified */
  1945.    if (filter_file && *filter_file)
  1946.       build_filter_lists(filter_file) ;
  1947.    if (total_lines <= lines_per_page)
  1948.       {
  1949.       total_lines = lines_per_page ;
  1950.       use_FF = TRUE ;
  1951.       }
  1952.    else
  1953.       use_FF = FALSE ;
  1954.    if (argc == 2 || argc == 3)
  1955.       {
  1956.       input_file = argv[1] ;
  1957.       input_file_namelen = strlen(input_file) ;
  1958.       if ((infile = ip_open_read(input_file,infile_buf,sizeof(infile_buf))) == NULL)
  1959.      fatal("unable to open input file") ;
  1960.       if (argc == 3)
  1961.      {
  1962.      outfile = ip_open_write(argv[2],!pages_printed,outfile_buf,
  1963.                  sizeof(outfile_buf)) ;
  1964.      if (outfile == NULL)
  1965.         fatal("unable to open output file") ;
  1966.      }
  1967.       else
  1968.      outfile = ip_open_write("",0,outfile_buf,sizeof(outfile_buf)) ;
  1969.       }
  1970.    else
  1971.       usage() ;
  1972.    if (lines_per_page > MAXPAGE)
  1973.       {
  1974.       ip_putlit("Surely you jest!  I can't handle pages that long.",err) ;
  1975.       newline(err) ;
  1976.       newline(err) ;
  1977.       usage() ;
  1978.       }
  1979.    else if (lines_per_page == 0) /* infinite page? */
  1980.       {
  1981.       widow_length = 0 ;
  1982.       if (total_lines <= 0)
  1983.      total_lines = MAXPAGE ;
  1984.       lines_per_page = total_lines ;
  1985.       use_FF = do_headers = page_numbers = FALSE ;
  1986.       }
  1987.    else
  1988.       {
  1989.       if (lines_per_page < 20)
  1990.      {
  1991.      ip_putlit("Surely your printer can handle at least 20 lines per page!",
  1992.            err) ;
  1993.      newline(err) ;
  1994.      ip_putlit("Adjusting page length....",err) ;
  1995.      newline(err) ;
  1996.      lines_per_page = 20 ;
  1997.      }
  1998.       if (widow_length < 3 || widow_length > lines_per_page / 2)
  1999.      {
  2000.      ip_putlit("Widow lines (-w) must be set to at least 3 and at most one-half of the",err) ;
  2001.      newline(err) ;
  2002.      ip_putlit("page length.  Using default of 8 lines.",err) ;
  2003.      newline(err) ;
  2004.      widow_length = 8 ;
  2005.      }
  2006.       }
  2007.    /* set up the printer */
  2008.    ip_putcstr(&printer->init1,outfile) ;
  2009.    ip_putcstr(&printer->init2,outfile) ;
  2010.    if (printer->set_typeface)
  2011.       (*printer->set_typeface)(outfile,typeface) ;
  2012.    if (duplex)
  2013.       {
  2014.       ip_putcstr(&printer->duplex_on,outfile) ;
  2015.       if ((pages_printed % 2) == 1)    /* next page odd or even? */
  2016.      ip_putcstr(&printer->marginl,outfile) ;    /* even */
  2017.       else
  2018.      ip_putcstr(&printer->marginr,outfile) ;    /* odd */
  2019.       }
  2020.    else
  2021.       ip_putcstr(&printer->marginc,outfile) ;    /* non-duplex, so center */
  2022.    /* start the auxiliary files if this is the first part processed */
  2023.    if (pages_printed == 0)
  2024.       {
  2025.       /* start the summary file */
  2026.       if (do_summary)
  2027.      write_summary_header(summary,"Interrupt Summary",show_offsets,FALSE) ;
  2028.       /* start the table index file */
  2029.       if (do_tables)
  2030.      write_summary_header(tables,"Table Summary",show_offsets,TRUE) ;
  2031.       /* start the data formats file */
  2032.       if (do_formats)
  2033.      write_summary_header(formats,"Data Structure Formats",FALSE,FALSE) ;
  2034.       }
  2035.    if (page_numbers)
  2036.       body_lines = lines_per_page - 2 ;
  2037.    else
  2038.       body_lines = lines_per_page ;
  2039.    if (do_headers)
  2040.       body_lines -= 2 ;
  2041.    last_line = 0 ;
  2042.    while (!out_of_files)
  2043.       {
  2044.       fill_buffer(last_line,body_lines) ;
  2045.       last_line = find_page_break(body_lines) ;
  2046.       print_buffer(last_line,body_lines,lines_per_page,total_lines,use_FF) ;
  2047.       }
  2048.    if (last_line < body_lines)
  2049.       {
  2050.       int i ;
  2051.       
  2052.       for (i = last_line ; i < body_lines ; i++)
  2053.      {
  2054.      strcpy(buffer[i-last_line], buffer[i]) ;
  2055.      line_offsets[i-last_line] = line_offsets[i] ;
  2056.      }
  2057.       print_buffer(body_lines-last_line,body_lines,lines_per_page,total_lines,
  2058.            use_FF) ;
  2059.       }
  2060.    ip_close(infile) ;
  2061.    /* reset the printer */
  2062.    reset_printer_and_close(outfile) ;
  2063.    ip_puts(itoa(pages_printed, num, 10), err) ;
  2064.    ip_putlit(" pages", err) ;
  2065.    if (do_summary)
  2066.       reset_printer_and_close(summary) ;
  2067.    if (do_tables)
  2068.       {
  2069.       ip_putlit(", ", err) ;
  2070.       ip_puts(itoa(prev_table, num, 10), err) ;
  2071.       ip_putlit(" tables", err) ;
  2072.       reset_printer_and_close(tables) ;
  2073.       }
  2074.    if (do_formats)
  2075.       reset_printer_and_close(formats) ;
  2076.    newline(err) ;
  2077.    ip_close(err) ;
  2078.    return 0 ;
  2079. }
  2080.