home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / PR.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  9KB  |  302 lines

  1. /*
  2.       This program is similar to a program of the same name found on UNIX.
  3.       It prints the files named in the command tail with headings
  4.       except as modified below.
  5.  
  6.       usage: pr [-i -ln -on -pname -tn -wn] file1[ file2 ... filen]
  7.       where:            -i    = accept files from stdin
  8.                   -ln   = set lines per page to n
  9.                   -on   = set page offset to n
  10.                   -pname      = output to file <name>
  11.                   -tn   = set tabs to n cols
  12.                   -wn   = set page width to n
  13.  
  14.       note: the expr 'PAGE(mesg)' found in col 1 will cause a formfeed
  15.                   and the 'mesg' to be included in the title line on this and
  16.                   each subsequent page until EOF or another PAGE.
  17. */
  18.  
  19. #include <stdio.h>
  20.  
  21. #define TAB_DEFAULT 4
  22. #define PAGE_LENGTH 60
  23. #define PAGE_OFFSET 0
  24. #define PAGE_WIDTH      80
  25. #define MAX_ARGS    70
  26. #define MAX_FILES 64
  27. #define PATH_LENGTH 63
  28. #define PAGE(head)
  29.  
  30. #ifndef TRUE
  31.  
  32. #define TRUE 1
  33. #define FALSE 0
  34.  
  35. #endif
  36.  
  37.  
  38. int page_length = PAGE_LENGTH;
  39. int page_offset = PAGE_OFFSET;
  40. int page_width    = PAGE_WIDTH;
  41.  
  42. int tab_width = TAB_DEFAULT;
  43.  
  44. char *xargv[ MAX_ARGS ];
  45. unsigned xargc;
  46.  
  47. char filenames [MAX_FILES] [PATH_LENGTH + 1];
  48.  
  49. char *print_name = "PRN:";
  50.  
  51. extern long atoi();
  52.  
  53. char title [80];
  54. char date [20];
  55. char time [20];
  56. int ln, pn;
  57.  
  58. PAGE (MAIN)
  59. main(argc, argv)  /* copy file to printer */
  60. int  argc;
  61. char *argv [];
  62. {
  63.       FILE *file, *lp;
  64.       int fi = 0;
  65.       int read_stdin = FALSE;
  66.       int pn;
  67.       char *cp;
  68.  
  69.       if (argc < 2) /* No args so tell 'em how it works */
  70.       {
  71.             fprintf(stderr,
  72.             "usage:\n\npr %s %s\n\n",
  73.             "[-i] [-lnn] [-onn] [-p<name>] [-tn] [-wnn]",
  74.             "[file1[ file2 ... filen]]");
  75.             fprintf(stderr,
  76.             "where: i = read 'stdin' for filenames to print\n");
  77.             fprintf(stderr,
  78.             "       l = lines-per-page and nn <= 120\n");
  79.             fprintf(stderr,
  80.             "       o = page offset    and nn <= 120\n");
  81.             fprintf(stderr,
  82.             "       p = print redirection and\n");
  83.             fprintf(stderr,
  84.             "           <name> = pathname or devicename\n");
  85.             fprintf(stderr,
  86.             "       t = spaces-per-tab and n  <= 8\n");
  87.             fprintf(stderr,
  88.             "       w = page width     and nn <= 160\n\n");
  89.             fprintf(stderr,
  90.             "Notes: PAGE(<title text of any length>) in col 1 of text file\n");
  91.             fprintf(stderr,
  92.             "       and <title text...> the title you want.\n\n");
  93.             fprintf(stderr,
  94.             "       C pgms should include the following macro:\n\n");
  95.             fprintf(stderr,
  96.             "            #define PAGE(title)\n\n");
  97.             fprintf(stderr,
  98.             "       < and > not required and should not be used\n\n");
  99.             exit(0);
  100.       }
  101.  
  102.       xargc = xargs("pr", argc, argv, xargv, MAX_ARGS);
  103.       
  104.       for (pn = 0; pn < xargc; pn++)
  105.       {
  106.             if (*xargv[pn] == '-')
  107.             {
  108.                   cp = xargv[pn] + 1;
  109.                   switch (tolower(*cp))
  110.                   {
  111.                   case 'i':/* wants help */
  112.                         read_stdin = TRUE;
  113.                         break;
  114.                   case 'l':/* page length change */
  115.                         page_length = (int) atoi(cp + 1);
  116.                         if ((page_length <= 0) || (page_length > 120))
  117.                               page_length = PAGE_LENGTH;
  118.                         break;
  119.  
  120.                   case 'p':/* direct output to file */
  121.                         print_name = cp + 1;
  122.                         break;
  123.  
  124.                   case 't':/* tab width change */
  125.                         tab_width = (int) atoi(cp + 1);
  126.                         if ((tab_width <= 0) || (tab_width > 8))
  127.                               tab_width = TAB_DEFAULT;
  128.                         break;
  129.  
  130.                   case 'o':/* page offset change */
  131.                         page_offset = (int) atoi(cp + 1);
  132.                         if ((page_offset < 0) || (page_offset > 120))
  133.                               page_offset = PAGE_OFFSET;
  134.                         break;
  135.  
  136.                   case 'w':/* page width change */
  137.                         page_width = (int) atoi(cp + 1);
  138.                         if ((page_width <= 0) || (page_width > 160))
  139.                               page_width = PAGE_WIDTH;
  140.                         break;
  141.  
  142.                   default:
  143.                         fprintf(stderr, "pr: Invalid option = %s\n",
  144.                               xargv[pn]);
  145.                   }
  146.             }
  147.             else  /* must be a path name */
  148.             {
  149.                   if (fi < MAX_FILES)
  150.                         strcpy(filenames[fi++], xargv[pn]);
  151.                   else
  152.                   {
  153.                         fprintf(stderr, "pr: "
  154.                               "Exceeded maximum file capacity\n");
  155.                         break;
  156.                   }
  157.             }
  158.       }
  159.  
  160.       if ((lp = fopen(print_name, "w")) == 0)
  161.       {
  162.             fprintf(stderr, "pr: Unable to open %s as output\n", print_name);
  163.             exit(1);
  164.       }
  165.  
  166.       if (read_stdin)
  167.       {
  168.             for(;;)
  169.             {
  170.                   if (fi == MAX_FILES)
  171.                   {
  172.                         fputs("pr: Exceeded maximum file capacity\n",
  173.                               stderr);
  174.                         break;
  175.                   }
  176.                   cp = gets(filenames [fi], PATH_LENGTH);
  177.                   if (!cp)
  178.                         break;
  179.                   else  fi++;
  180.             }
  181.       }
  182.       /* now print each file */
  183.  
  184.       for (pn = 0; pn < fi; pn++)
  185.             prt(filenames [pn], lp);  /* print the file */
  186. }
  187. PAGE (NEW PAGE)
  188.  
  189. new_page (fnp, lp)
  190. char *fnp;
  191. FILE *lp;
  192. {
  193.       if (ln < 3)
  194.             return;
  195.       ++pn;
  196.       if (pn > 1)
  197.             fputc('\f', lp);
  198.       fprintf(lp, "%s    %s %s    PAGE %d:  %s\n\n",
  199.                    fnp, date, time, pn, title);
  200.       ln = 2;
  201. }
  202.  
  203. PAGE (PRINT FILE)
  204. prt (fnp, lp)
  205. char fnp[];
  206. FILE *lp;
  207. {
  208.       FILE *inp_file;
  209.       int i, j, col;
  210.       char line [256], *st, *et, *sp;
  211.  
  212.       inp_file = fopen(fnp, "r");
  213.       if (!inp_file)
  214.       {
  215.             fprintf(stderr, "pr: unable to open %s\n", fnp);
  216.             return;
  217.       }
  218.       else
  219.             fprintf(stderr, "pr: printing %s\n", fnp);
  220.  
  221.       pn = 0;
  222.       ln = 999;
  223.       gdates(date);          /* get date */
  224.       gtimes(time);          /* and time */
  225.       *title = '\0';
  226.  
  227.       while (fgets(line, 256, inp_file))
  228.       {
  229.             if (strncmp(line, "PAGE", 4) == 0)
  230.             {
  231.                   if (st = index(line, '('))
  232.                   {
  233.                         et = index(line, ')');
  234.                         strncpy(title, st + 1, (et) ? et - st - 1 : 160);
  235.                   }
  236.                   ln = 999;
  237.             }
  238.  
  239.             if (ln > page_length)
  240.                   new_page(fnp, lp);
  241.  
  242.             if (page_offset)
  243.                   indent(lp);
  244.  
  245.             for (col = (page_offset) ? page_offset : 1, sp = &line[0];
  246.                    *sp; sp++)
  247.             {
  248.                   switch (*sp)
  249.                   {
  250.                   case '\t':  /* tab character */
  251.                         do
  252.                         {
  253.                               fputc(' ', lp);
  254.                               col++;
  255.                               if (col > page_width)
  256.                               {
  257.                                     fputc('\n', lp);
  258.                                     col = (page_offset) ? page_offset : 1;
  259.                                     ln++;
  260.                                     if (ln > page_length)
  261.                                           new_page(fnp, lp);
  262.                                     if (page_offset)
  263.                                           indent(lp);
  264.                                     break;
  265.                               }
  266.                         } while ((col - 1) % tab_width);
  267.                         break;
  268.  
  269.                   case '\f':  /* form feed character */
  270.                         new_page(fnp, lp);
  271.                         break;
  272.  
  273.                   default:
  274.                         fputc(*sp, lp);
  275.                         ++col;
  276.                         if (col > page_width)
  277.                         {
  278.                               fputc('\n', lp);
  279.                               col = (page_offset) ? page_offset - 1 : 0;
  280.                               ln++;
  281.                               if (ln > page_length)
  282.                                     new_page(fnp, lp);
  283.                               if (page_offset)
  284.                                     indent(lp);
  285.                         }
  286.                   }
  287.             } /* of line print (for) */
  288.             ++ln;
  289.       } /* of while not eof */
  290.       fclose(inp_file);
  291.       fputc(014, lp);
  292. } /* of print */
  293.  
  294. indent(lp)
  295. FILE *lp;
  296. {
  297.       int i;
  298.  
  299.       for(i = 1; i < page_offset; i++)
  300.             fputc(' ', lp);
  301. }
  302.