home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d122 / pr.lha / Pr / pr.c < prev    next >
C/C++ Source or Header  |  1987-12-31  |  13KB  |  468 lines

  1. /*
  2.  * NAME: pr.c
  3.  *
  4.  * FUNCTION: Prints a list of files to the printer.
  5.  *
  6.  * COPYRIGHT: 1987 by Samuel Paolucci
  7.  *
  8.  * NOTE: This code may be freely distributed provided this notice is retained.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <exec/types.h>
  14. #include <time.h>
  15.  
  16. #define NOT !
  17. #define to_decimal(x) (x - '0')
  18.  
  19. BOOL io_error = FALSE;
  20.  
  21. FILE *in;
  22. FILE *out;
  23. FILE *fopen ();
  24.  
  25. static int page;
  26. static int linetot;
  27. static int linenum = 0;
  28. static int linesize = 80;
  29. static char output[32] = "prt:";
  30. static char linebuffer[256];
  31. static char *copyright = "Copyright 1987 by Samuel Paolucci";
  32.  
  33. int confirm = 0;
  34. int control = 0;
  35. int copies  = 1;
  36. int lines   = 58;
  37. int headers = 1;
  38. int numbers = 0;
  39. int wrap    = 0;
  40.  
  41. struct tm *localtime();
  42. char *scdir ();
  43. char *fgets ();
  44. char *gets ();
  45. char *index ();
  46. char *ctime ();
  47. time_t time ();
  48. int  fputs ();
  49.  
  50. void help ()                           /* print help page */
  51. {
  52.    fprintf (stderr, "\npr [options] file1 [file2 ...]\n");
  53.    fprintf (stderr, "   prints multiple files to the printer.\n\n");
  54.    fprintf (stderr, "The program will accept Un*x style wildcards:\n");
  55.    fprintf (stderr, "   *  matches any substring\n");
  56.    fprintf (stderr, "   ?  matches any single character\n\n");
  57.    fprintf (stderr, "The valid options are:\n");
  58.    fprintf (stderr, "  -?  - displays this page\n");
  59.    fprintf (stderr, "  -c  - confirms wild cards\n");
  60.    fprintf (stderr, "        <CR>, y, or Y - print this file\n");
  61.    fprintf (stderr, "        n or N - don't print this file\n");
  62.    fprintf (stderr, "        ! - print this and all remaining files\n");
  63.    fprintf (stderr, "  -f  - specifies wide carriage\n");
  64.    fprintf (stderr, "  -h  - removes headers\n");
  65.    fprintf (stderr, "  -l# - specifies the # of lines per page\n");
  66.    fprintf (stderr, "  -m# - specifies the # of copies\n");
  67.    fprintf (stderr, "  -n  - displays line numbers\n");
  68.    fprintf (stderr, "  -s  - diverts output to the standard output file\n");
  69.    fprintf (stderr, "  -w  - wraps lines\n");
  70. }
  71.  
  72. static short hour;
  73. static short minute;
  74. static short year;
  75. static short day;
  76. static short month;
  77.  
  78. void getdate ()
  79.  
  80. {
  81.    long clock;
  82.    struct tm *tm;
  83.  
  84.    clock = time (0L);
  85.    tm = localtime (&clock);
  86.  
  87.    hour = tm->tm_hour;
  88.    minute = tm->tm_min;
  89.    year = 1900 + tm->tm_year;
  90.    day = tm->tm_mday;
  91.    month = 1 + tm->tm_mon;
  92. }
  93.  
  94. void putheader (name)
  95.  
  96. char *name;
  97.  
  98. {
  99.    int i = 0;
  100.    int j;
  101.    int ablanks = (linesize+numbers*5)/2 - 4;
  102.    int bblanks = (linesize+numbers*5)/2 - 21;
  103.    char outfb[133];
  104.    static char headbuffer[174];
  105.  
  106.    if (out != stdout && page == 1) {      /* reset printer */
  107.       headbuffer[i++] = 0x1b;
  108.       headbuffer[i++] = 0x63;
  109.    }
  110.    for (j = 0; name[j] != NULL; j++, i++)         /* add filename */
  111.       headbuffer[i] = name[j];
  112.    for (j = 0; j < ablanks - strlen (name); j++, i++)   /* add blanks */
  113.       headbuffer[i] = ' ';
  114.    sprintf (outfb, "Page %-3d", page++);  /* add page number */
  115.    for (j = 0; outfb[j] != NULL; j++, i++)
  116.       headbuffer[i] = outfb[j];
  117.    for (j = 0; j < bblanks; j++, i++)     /* add blanks*/
  118.       headbuffer[i] = ' ';
  119.    sprintf (outfb, "%02.2d/%02.2d/%4d, %02d:%02.2d\n\n\n",
  120.             month, day, year, hour, minute);      /* add timestamp */
  121.    for (j = 0; outfb[j] != NULL; j++, i++)
  122.       headbuffer[i] = outfb[j];
  123.    headbuffer[i++]= '\0';
  124.    
  125.    (void) fputs (headbuffer, out);       /* write it out */
  126. }
  127.  
  128. int get_a_line ()
  129.  
  130. {
  131.    char *i;
  132.  
  133.    i = fgets (linebuffer, sizeof (linebuffer), in);
  134.  
  135.    return (i != NULL);
  136. }
  137.  
  138. void newpage (name)
  139.  
  140. char *name;
  141.  
  142. {
  143.    if (out == stdout)
  144.       ;
  145.    else {
  146.       (void) fputs ("\f", out);        
  147.       if (headers) 
  148.          putheader (name);
  149.       linenum = 1;             /* reset line number */
  150.    }
  151. }
  152.  
  153. int remove_substring (string, first, num_char)
  154.  
  155. char string[];
  156. int first;     /* location of the first character to remove */
  157. int num_char;  /* number of characters to remove */
  158.  
  159. {
  160.    int length;        /* length of the string */
  161.  
  162.    int index1;          /* points to the first character to remove */
  163.    int index2;          /* points to the first character remaining after
  164.                removal of the other characters */
  165.  
  166.    length = strlen (string);
  167.  
  168.    if (first >= length || first < 0)
  169.       return (-1);    /* invalid starting location */
  170.  
  171.    if (first + num_char <= length)
  172.       index2 = first + num_char;
  173.    else
  174.       index2 = length;    /* only delete to NULL */
  175.  
  176.    /* remove the characters */
  177.  
  178.    for (index1 = first; (string[index1] = string[index2]) != NULL; index1++)
  179.       index2++;
  180.       
  181.    return (first);
  182. }
  183.  
  184. int insert_string (string, substring, location)
  185.  
  186. char *string;
  187. char *substring;
  188. int location;
  189.  
  190. {
  191.    int index1;        /* index into the string */
  192.    int index2;        /* index into the substring */
  193.    int index_temp;    /* index into the temporary string */
  194.    
  195.    char temp[256];
  196.    
  197.    /* see if the location is valid */
  198.    
  199.    if (location >= strlen (string))
  200.       return (-1);
  201.       
  202.    /* copy the characters in the string prior to the
  203.       starting location to the temporary string temp */
  204.       
  205.    for (index_temp = 0, index1 = 0; index1 < location; index1++, index_temp++)
  206.       temp[index_temp] = string[index1];
  207.    
  208.    /* append the substring to the current contents of the temporary string */
  209.    
  210.    for (index2 = 0; substring[index2] != NULL; ++index2, index_temp++)
  211.       temp[index_temp] = substring[index2];
  212.    
  213.    /* append the remainder of the string to the temporary string */
  214.    
  215.    while (temp[index_temp++] = string[index1++])
  216.       ;
  217.    
  218.    /* put contents of the temporary string back into the string */
  219.    
  220.    strcpy (string, temp);
  221.       
  222.    return (location);
  223. }
  224.  
  225. void put_a_line (name)
  226.  
  227. char *name;
  228.  
  229. {
  230.    int len;
  231.    char string[144];
  232.  
  233.    
  234.    if ((len = strlen (linebuffer)) != 0) {
  235.       if (linenum == lines)
  236.          newpage (name);
  237.       if (numbers)                /* print a line number */
  238.          fprintf (out, "%4d ", linetot++);
  239.  
  240.       strncpy (string, linebuffer, linesize);
  241.       if (string[linesize-1] != NULL) {
  242.          if (string[linesize-1] != '\n') {
  243.         string[linesize] = '\n';
  244.             string[linesize+1] = '\0';
  245.      } else
  246.         string[linesize] = '\0';
  247.       }
  248.       (void) fputs (string, out);
  249.       linenum++;
  250.       len -= linesize;
  251.  
  252.       while (len > 1 && wrap) {
  253.          (void) remove_substring (linebuffer, 0, linesize);
  254.          (void) insert_string (linebuffer, "     ", 0);
  255.          strncpy (string, linebuffer, linesize);
  256.          if (string[linesize-1] != NULL) {
  257.             string[linesize] = '\n';
  258.             string[linesize+1] = '\0';
  259.          }
  260.          if (linenum == lines)
  261.             newpage (name);
  262.          (void) fputs (string, out);
  263.          linenum++;
  264.          len -= linesize;
  265.       }
  266.    }
  267. }
  268.  
  269. void printfile (name)                  /* do the actual printing of the file */
  270.  
  271. char *name;
  272.  
  273. {
  274.    int nc;
  275.    
  276.    nc = copies;
  277.  
  278.    do {
  279.       if ((in = fopen (name, "r")) != NULL) {
  280.          if (out != stdout)
  281.         fprintf (stderr, "Printing: %s\n", name);
  282.          linetot = 1;             /* reset line number */
  283.          page = 1;                /* reset page number */
  284.  
  285.          if (headers && out != stdout) {      /* need a header */
  286.             getdate ();           /* get timestamp */
  287.             putheader (name);
  288.          }
  289.  
  290.          (void) get_a_line ();    /* get a line */
  291.  
  292.          do {
  293.             put_a_line (name);
  294.          } while (NOT io_error && (BOOL) get_a_line ());
  295.    
  296.          if (io_error) {
  297.             io_error = FALSE;
  298.             fprintf (stderr, "pr: I/O error while printing %s\n", name);
  299.          }
  300.    
  301.          if (out != stdout)
  302.             (void) fputs ("\f", out);
  303.          fclose (in);
  304.                 
  305.       } else
  306.          fprintf (stderr, "pr: unable to open '%s'\n", name);
  307.    } while (--nc);
  308.  
  309. }
  310.  
  311. int main (argc, argv)
  312.  
  313. int argc;
  314. char *argv[];
  315.  
  316. {
  317.  
  318.    int i;
  319.    int accepted;
  320.    char *p;
  321.    char name[32];                 /* name of file to print */
  322.    char answer[32];
  323.    char c;
  324.  
  325.    if (argc < 2) {
  326.       fprintf (stderr, "Usage: pr [-?cfhl#m#nsw] file1 [file2 ...]\n");
  327.       fprintf (stderr, "       use pr -? for more help\n");
  328.       exit (1);
  329.    }
  330.  
  331.    if ((out = fopen (output, "w")) == NULL) {
  332.       fprintf(stderr, "pr: can't open the printer\n");
  333.       exit (1);
  334.    }
  335.  
  336.    while (argv[1] != NULL) {
  337.  
  338.       while (*argv[1] == '-') {   /* process options if any */
  339.          p = (char *) &*argv[1];
  340.  
  341.          p++;                     /* point to the option chars */
  342.          do {
  343.             switch (*p) {
  344.                case 's':          /* list to stdout */
  345.                          fclose (out);
  346.                          out = stdout;
  347.                          if (numbers)
  348.                 linesize = 72;
  349.              else
  350.                 linesize = 77;
  351.                          break;
  352.  
  353.                case 'm':          /* specify # of copies */
  354.                          copies = 0;
  355.                          p++;
  356.                          while (isdigit (*p))
  357.                             copies = copies*10 + to_decimal (*p++);
  358.                          --p;
  359.                          break;
  360.  
  361.                case 'h':          /* do not print a header */
  362.                          headers--;
  363.                          lines = 60;
  364.                          break;
  365.  
  366.                case 'w':          /* wrap lines */
  367.                          wrap++;
  368.                          break;
  369.  
  370.                case 'n':          /* print line numbers */
  371.                          numbers++;
  372.                          linesize -= 5;
  373.                          break;
  374.  
  375.                case 'f':          /* specify wide carriage printer */
  376.              linesize = 132;
  377.              if (numbers) linesize -= 5;
  378.                          break;
  379.  
  380.                case 'l':          /* specify # of lines per page */
  381.                          lines = 0;
  382.                          p++;
  383.                          while (isdigit (*p))
  384.                             lines = lines*10 + to_decimal (*p++);
  385.                          --p;
  386.                          if (lines == 0) {
  387.                             fprintf (stderr, "pr: the number of lines per page must be greater than zero\n");
  388.                             exit (1);
  389.                          }
  390.                          break;
  391.  
  392.                case 'c':          /* confirm any templates */
  393.                          confirm++;
  394.                          break;
  395.  
  396.                case '?':          /* display help page */
  397.                          help ();
  398.                          exit (0);
  399.  
  400.                default:           /* invalid option */
  401.                          fprintf(stderr, "pr: '%c' is an invalid option\n", *p);
  402.                          exit (1);
  403.             }
  404.             p++;
  405.          } while (*p);
  406.          
  407.          argc--;
  408.          argv++;
  409.          
  410.       }
  411.  
  412.       strcpy (name, argv[1]);     /* must be a file name */
  413.  
  414.       if (index (name, '*') || index (name, '?')) {
  415.  
  416.          i = 0;                   /* a template was given, expand it */
  417.  
  418.          while (((p = scdir (name)) != NULL) && ++i < 100) {
  419.  
  420.             if (confirm) {        /* if confirm ask him for each file */
  421.  
  422. again:
  423.                fprintf (stderr, "confirm '%s'? ", p);
  424.                (void) gets (answer);
  425.                c = answer[0];
  426.                switch (c) {
  427.                   case '!':        /* if he answers ! then accept all remaining */
  428.                             confirm = 0;
  429.                             accepted = 1;
  430.                             break;
  431.  
  432.                   case '\0':      /* if yes then include this name */
  433.                   case 'y':
  434.                   case 'Y':
  435.                             accepted = 1;
  436.                             break;
  437.  
  438.                   case 'n':       /* if no skip this name */
  439.                   case 'N':
  440.                             accepted = 0;
  441.                             break;
  442.  
  443.                   default:
  444.                             fprintf (stderr, "valid answers are: <CR>, y, or Y - yes do print\n");
  445.                             fprintf (stderr, "                   n or N - no don't print this one\n");
  446.                             fprintf (stderr, "                   ! - yes print this one and all remaining\n");
  447.                                 goto again;
  448.                     
  449.                }
  450.             } else                /* if not confirming then match all items */
  451.                accepted = 1;
  452.  
  453.             if (accepted)         /* go ahead and print this one */
  454.                printfile (p);     /* now print the file */
  455.         
  456.          }
  457.         
  458.       } else 
  459.          printfile (name);        /* now print the file */
  460.         
  461.       argv++;
  462.      
  463.    }
  464.    
  465.    return (0);
  466.    
  467. }   
  468.