home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / intprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-02  |  9.7 KB  |  355 lines

  1. /***********************************************/
  2. /* Copyright (c) 1989  Ralf Brown              */
  3. /* May be freely redistributed provided no fee */
  4. /* is charged, this notice remains intact,     */
  5. /* and any changes are clearly marked as such  */
  6. /***********************************************/
  7. /* Program History:                            */
  8. /*   v1.00  4/23/89  initial public release    */
  9. /*                   with 4/30/89 list         */
  10. /***********************************************/
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. #define VERSION "1.00"
  16.  
  17. #define MAXLINE 81   /* at most 80 chars per line (plus newline) */
  18. #define MAXPAGE 200  /* at most 200 lines per page */
  19.  
  20. #ifndef FALSE
  21. #define FALSE 0
  22. #endif
  23. #ifndef TRUE
  24. #define TRUE !FALSE
  25. #endif
  26.  
  27. #ifdef __TURBOC__
  28. #  define PROTOTYPES
  29. #  include <stdlib.h>
  30. void _setenvp(void) {} /* don't need the environment, so don't include it */
  31. int isspace(char c) { return (c == ' ' || c == '\t') ; }
  32. #else
  33. /*#define PROTOTYPES  /* uncomment if compiler supports ANSI-style prototypes */
  34. #  include <ctype.h>
  35. char *itoa(num,buf,radix)   /* not everybody has the same itoa() as TurboC */
  36. int num ;                   /* minimal implementation */
  37. char *buf ;
  38. int radix ;
  39. {
  40.    int count = 0 ;
  41.    int i ; 
  42.    char tmp ;
  43.  
  44.    do {
  45.       buf[count++] = '0' + num % radix ;
  46.       num = num / radix ;
  47.    } while (num) ;
  48.    buf[count] = '\0' ;
  49.    if (count > 1)
  50.       for (i = 0 ; i < count / 2 ; i++)
  51.          {
  52.          tmp = buf[i] ;
  53.          buf[i] = buf[count-i-1] ;
  54.          buf[count-i-1] = tmp ;
  55.          }
  56.    return buf ;
  57. }
  58. #endif __TURBOC__
  59.  
  60. #ifdef PROTOTYPES
  61. void usage(void) ;
  62. void fill_buffer(int lines, int lines_per_page) ;
  63. int find_page_break(int lines) ;
  64. void summarize(FILE *summary, int line, int pages_printed) ;
  65. void print_buffer(int first, int last, int lines_per_page, int total_lines, int use_FF) ;
  66. void main(int argc, char **argv) ;
  67. #endif PROTOTYPES
  68.  
  69. char buffer[MAXPAGE][MAXLINE] ;
  70. char num[6] ;
  71. int pages_printed = 0 ;
  72. int indent = 0 ;
  73. int page_numbers = FALSE ;
  74. int do_summary = FALSE ;
  75. FILE *summary ;
  76.  
  77. void usage()
  78. {
  79.    fputs( "INTPRINT v", stderr ) ;
  80.    fputs( VERSION, stderr ) ;
  81.    fputs( " Copyright (c) 1989  Ralf Brown.  Free for non-commercial use.\n\n", stderr ) ;
  82.    fputs( "Usage: intprint [options] [lines [page_size]] <intlist >output\n", stderr );
  83.    fputs( "\t'lines' defaults to 60\n", stderr ) ;
  84.    fputs( "\tif page_size is given, only linefeeds will be used to advance\n", stderr ) ;
  85.    fputs( "Options:\n", stderr ) ;
  86.    fputs( "\t-p\tcauses pages to be numbered\n", stderr ) ;
  87.    fputs( "\t-nN\tassume N pages have been printed from previous parts\n", stderr ) ;
  88.    fputs( "\t-e\tassume 'elite' mode (96 characters per line)\n", stderr ) ;
  89.    fputs( "\t-iN\tindent output N spaces\n", stderr ) ;
  90.    fputs( "\t-E\tspecifies that the printer is an Epson FX80 or compatible\n", stderr ) ;
  91.    fputs( "\t\t-E overrides -e and forces -i8\n", stderr ) ;
  92.    fputs( "\t-sfile\twrite a one-line-per-function summary to 'file'\n", stderr ) ;
  93.    exit(1) ;
  94. }
  95.  
  96. void fill_buffer(lines,lines_per_page)
  97. int lines, lines_per_page ;
  98. {
  99.    int i ;
  100.  
  101.    if (lines)
  102.       for (i = lines ; i < lines_per_page ; i++)
  103.          strcpy( buffer[i-lines], buffer[i] ) ;
  104.    else
  105.       lines = lines_per_page ;
  106.    for (i = lines_per_page - lines ; i < lines_per_page ; i++)
  107.       {
  108.       buffer[i][0] = '\0' ;  /* force empty line in case of EOF */
  109.       fgets( buffer[i], sizeof(buffer[i]), stdin ) ;
  110.       }
  111. }
  112.  
  113. int find_page_break(lines)
  114. int lines ;
  115. {
  116.    int i ;
  117.  
  118.    for (i = 0 ; i < 10 ; i++)
  119.       {
  120.       if (strcmp(buffer[lines-i-1],"\n") == 0 ||
  121.           strlen(buffer[lines-i-1]) == 0 ||
  122.           strncmp(buffer[lines-i-1],"--------",8) == 0)
  123.          return lines - i ;
  124.       }
  125.    return lines ;
  126. }
  127.  
  128. void summarize(summary, line, pages_printed)
  129. FILE *summary ;
  130. int line, pages_printed ;
  131. {
  132.    char *s ;
  133.    int i ;
  134.    int max_descrip ;
  135.  
  136.    max_descrip = (page_numbers ? 65 : 69) ;
  137.    s = buffer[line] ;
  138.    if (strncmp(s, "INT ", 4 ) == 0)   /* start of an entry? */
  139.       {
  140.       fputc(' ', summary) ;
  141.       fputc(s[4], summary) ;  /* output interrupt number */
  142.       fputc(s[5], summary) ;
  143.       fputc(' ', summary) ;
  144.       s = buffer[line+1] ;
  145.       while (*s && isspace(*s))
  146.          s++ ;
  147.       if (strncmp(s,"AX",2) == 0)
  148.          i = 4 ;
  149.       else if (strncmp(s,"AH",2) == 0)
  150.          i = 2 ;
  151.       else
  152.          i = 0 ;
  153.       if (i)
  154.          {
  155.          while (*s && *s != '=' )
  156.             s++ ;
  157.          s++ ;  /* skip the equal sign */
  158.          while (*s && isspace(*s))
  159.             s++ ;
  160.          if (strchr("0123456789ABCDEFabcdef",*s) != NULL)
  161.             {
  162.             fputc(*s++, summary) ;
  163.             fputc(*s++, summary) ;
  164.             fputc(' ', summary) ;
  165.             if (i == 4)
  166.                {
  167.                fputc(*s++, summary) ;
  168.                fputc(*s, summary) ;
  169.                fputc(' ', summary) ;
  170.                }
  171.             else
  172.                fputs( "-- ", summary) ;
  173.             }
  174.          else
  175.             fputs( "-- -- ", summary) ;  /* wasn't legal digit, so no numbers */
  176.          }
  177.       else
  178.          fputs("-- -- ", summary) ;
  179.       if (page_numbers)
  180.          {
  181.          itoa(pages_printed+1,num,10) ; /* pages_printed not incremented until later */
  182.          for (i = strlen(num) ; i < 3 ; i++)
  183.             fputc(' ', summary) ;
  184.          fputs(num, summary) ;
  185.          fputc(' ', summary) ;
  186.          }
  187.       s = buffer[line] + 7 ;  /* find function description */
  188.       while (*s && !isspace(*s))
  189.          s++ ;
  190.       while (*s && isspace(*s))
  191.          s++ ;
  192.       for (i = 0 ; (i < max_descrip) && *s && (*s != '\n') ; i++)
  193.          fputc(*s++, summary) ;
  194.       fputc('\n',summary) ;
  195.       }
  196. }
  197.  
  198. void print_buffer(first,last,lines_per_page,total_lines,use_FF)
  199. int first, last, lines_per_page, total_lines ;
  200. int use_FF ;
  201. {
  202.    int i, ind ;
  203.  
  204.    for (i = first ; i < last ; i++)
  205.       {
  206.       for (ind = 0 ; ind < indent ; ind++)
  207.          fputc( ' ', stdout ) ;
  208.       if (buffer[i][0])
  209.          {
  210.          fputs( buffer[i], stdout ) ;
  211.          if (do_summary)
  212.             summarize(summary,i,pages_printed) ;
  213.          }
  214.       else
  215.          fputs( "\n", stdout ) ;
  216.       }
  217.    pages_printed++ ;
  218.    if (page_numbers)
  219.       {
  220.       for (i = last - first ; i < lines_per_page - 1 ; i++)
  221.          fputc( '\n', stdout ) ;
  222.       for (ind = 0 ; ind < indent ; ind++)
  223.          fputc( ' ', stdout ) ;
  224.       fputs( "                                      - ", stdout ) ;
  225.       itoa( pages_printed, num, 10 ) ;
  226.       fputs( num, stdout ) ;
  227.       fputs( " -\n", stdout ) ;
  228.       }
  229.    if (use_FF)
  230.       fputc( '\f', stdout ) ;
  231.    else
  232.       for (i = page_numbers?lines_per_page:(last-first) ; i<total_lines ; i++)
  233.          fputc( '\n', stdout ) ;
  234. }
  235.  
  236. void main(argc,argv)
  237. int argc ;
  238. char *argv[] ;
  239. {
  240.    int lines_per_page = 60 ;
  241.    int total_lines = 66 ;
  242.    int use_FF = TRUE ;
  243.    int Epson_mode = FALSE ;
  244.    int last_line ;
  245.    int body_lines ;
  246.    int i ;
  247.    char *summary_file = NULL ;
  248.  
  249.    while (argc >= 2 && argv[1][0] == '-')
  250.       {
  251.       switch (argv[1][1])
  252.          {
  253.          case 'e':
  254.             indent = 8 ;
  255.             break ;
  256.          case 'E':
  257.             Epson_mode = TRUE ;
  258.             break ;
  259.          case 'p':
  260.             page_numbers = TRUE ;
  261.             break ;
  262.          case 'n':
  263.             pages_printed = atoi( argv[1]+2 ) ;
  264.             break ;
  265.          case 'i':
  266.             indent = atoi( argv[1]+2 ) ;
  267.             break ;
  268.          case 's':
  269.             summary_file = argv[1]+2 ;
  270.             break ;
  271.          default:
  272.             usage() ;
  273.          }
  274.       argv++ ;
  275.       argc-- ;
  276.       }
  277.    if (summary_file)
  278.       {
  279.       if ((summary = fopen( summary_file, pages_printed ? "a":"w" )) != NULL)
  280.          do_summary = TRUE ;
  281.       else
  282.          fputs( "unable to open summary file\n", stderr ) ;
  283.       }
  284.    if (argc >= 2)
  285.       lines_per_page = atoi(argv[1]) ;
  286.    if (argc == 3)
  287.       {
  288.       total_lines = atoi(argv[2]) ;
  289.       use_FF = FALSE ;
  290.       }
  291.    if (total_lines < lines_per_page)
  292.       {
  293.       total_lines = lines_per_page ;
  294.       use_FF = TRUE ;
  295.       }
  296.    if (argc > 3 || lines_per_page == 0) /* too many or non-numeric first param */
  297.       usage() ;
  298.    if (lines_per_page < 20 || lines_per_page > MAXPAGE)
  299.       {
  300.       fputs( "Surely you jest!  At least 20 and at most 200 lines per page.\n\n", stderr ) ;
  301.       usage() ;
  302.       }
  303. #ifdef __TURBOC__
  304.    setvbuf(stdin,NULL,_IOFBF,12288) ;  /* use larger disk buffers */
  305.    setvbuf(stdout,NULL,_IOFBF,12288) ; /* for better performance */
  306.    if (do_summary)
  307.       setvbuf(summary,NULL,_IOFBF,4096) ;
  308. #endif __TURBOC__
  309.    if (do_summary && pages_printed == 0)
  310.       {           /* create header, but only on first part */
  311.       fputs("\t\t\t\tInterrupt Summary\n", summary) ;
  312.       fputs("\t\t\t\t-----------------\n\n", summary) ;
  313.       fputs("INT AH AL", summary) ;
  314.       if (page_numbers)
  315.          fputs(" Page", summary) ;
  316.       fputs("\t\t\tDescription\n", summary) ;
  317.       for (i = 0 ; i < 79 ; i++)
  318.          fputc('-', summary) ;
  319.       fputc('\n', summary) ;
  320.       }
  321.    if (Epson_mode)
  322.       {
  323.       indent = 0 ;  /* -E overrides -e and -i */
  324.       fputs( "\033M\033l\007", stdout ) ;
  325.       }
  326.    last_line = 0 ;
  327.    if (page_numbers)
  328.       body_lines = lines_per_page - 2 ;
  329.    else
  330.       body_lines = lines_per_page ;
  331.    while (!feof(stdin))
  332.       {
  333.       fill_buffer(last_line,body_lines) ;
  334.       last_line = find_page_break(body_lines) ;
  335.       print_buffer(0,last_line,lines_per_page,total_lines,use_FF) ;
  336.       }
  337.    if (last_line < body_lines)
  338.       print_buffer(last_line,body_lines,lines_per_page,total_lines,use_FF) ;
  339.    if (Epson_mode)
  340.       {
  341.       fputs( "\033M\033l", stdout ) ;  /* cancel Elite mode and indent */
  342.       fputc( '\0', stdout ) ;
  343.       }
  344.    fflush(stdout) ;
  345.    itoa( pages_printed, num, 10 ) ;
  346.    fputs( num, stderr ) ;
  347.    fputs( " pages\n", stderr) ;
  348.    if (do_summary)
  349.       fclose(summary) ;
  350.    exit(0) ;
  351. }
  352. -- 
  353.  
  354.  
  355.