home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / mpage.zip / mp_args.c < prev    next >
Text File  |  1997-09-25  |  5KB  |  241 lines

  1. # include <stdio.h>
  2. # include <sys/types.h>
  3.  
  4. # ifndef lint
  5. static char *rcs_id =
  6.     "@(#) $Header: mp_args.c,v 2.6 89/05/25 15:43:07 mark Exp $";
  7. # endif
  8.  
  9. # include "mp_head.h"
  10.  
  11. /*
  12.  * mpage:    a program to reduce pages of print so that several pages
  13.  *           of output appear on one printed page.
  14.  *
  15.  * Written by:
  16.  *   ...!uunet!\                       Mark Hahn, Sr Systems Engineer
  17.  *              >pyrdc!mark            Pyramid Technology Corporation
  18.  * ...!pyramid!/                       Vienna, Va    (703)848-2050
  19.  *
  20.  *
  21.  * Copyright (c) 1988 Mark P. Hahn, Herndon, Virginia
  22.  *  
  23.  *     Permission is granted to anyone to make or distribute verbatim
  24.  *     copies of this document as received, in any medium, provided
  25.  *     that this copyright notice notice is preserved, and that the
  26.  *     distributor grants the recipient permission for further
  27.  *     redistribution as permitted by this notice.
  28.  *
  29.  */
  30.  
  31. /* $Log:    mp_args.c,v $
  32.  * Revision 2.6  89/05/25  15:43:07  mark
  33.  * added the -b options as a converse to the -o option.
  34.  * 
  35.  * Revision 2.5  89/05/25  10:42:39  mark
  36.  * changes to print a page count on stderr after the print job is queued.
  37.  * 
  38.  * Revision 2.4  89/05/25  08:57:47  mark
  39.  * rearranged the rcs header keywords for better readability.
  40.  * 
  41.  * Revision 2.3  89/05/22  14:41:03  mark
  42.  * Fixed the type-o in the rcs identification string
  43.  * 
  44.  * Revision 2.2  89/05/22  14:38:06  mark
  45.  * Added rcs identification usable with the "what" program
  46.  * 
  47.  * Revision 2.1  89/05/22  14:31:29  mark
  48.  * New Major Revision
  49.  * 
  50.  * Revision 1.1  89/05/22  14:25:29  mark
  51.  * Initial revision
  52.  *  */
  53.  
  54. do_args(argc, argv, envflag)
  55.  int argc;
  56.  char **argv;
  57.  int envflag;
  58. {
  59.     char *optstr;
  60.     int consumed;
  61.     int currarg;
  62.     int opterrors;
  63.  
  64.     opterrors = 0;
  65.     for (currarg = 1; currarg < argc; currarg++) {
  66.         if (*argv[currarg] != '-') {
  67.             if (envflag) {
  68.                 opterrors++;
  69.             }
  70.             break;
  71.         }
  72.         optstr = argv[currarg];
  73.         consumed = 0;
  74.         while (*++optstr && ! consumed) {
  75.             switch (*optstr) {
  76.             default:
  77.                 fprintf(stderr,"%s: unknown option -%c\n",
  78.                        MPAGE, *optstr);
  79.                 opterrors++;
  80.                 break;
  81.             case '1':
  82.                 sheetindex = 0;
  83.                 break;
  84.             case '2':
  85.                 sheetindex = 1;
  86.                 break;
  87.             case '4':
  88.                 sheetindex = 2;
  89.                 break;
  90.             case '8':
  91.                 sheetindex = 3;
  92.                 break;
  93.             case 'A':    /* A4 sized, european paper */
  94.                 opt_a4 = 1;
  95.                 set_page();
  96.                 break;
  97.             case 'a':    /* across */
  98.                 sheetorder = LEFTRIGHT;
  99.                 break;
  100.             case 'h':
  101.                 ++optstr;
  102.                 opt_doheader = 1;
  103.                 (void)strcpy(opt_header, optstr);
  104.                 consumed = 1;
  105.                 break;
  106.             case 'L':
  107.                 ++optstr;
  108.                 opt_lines = atoi(optstr);
  109.                 consumed = 1;
  110.                 break;
  111.             case 'l':    /* landscape */
  112.                 sheetaspect = LANDSCAPE;
  113.                 break;
  114.             case 'n':    /* normal */
  115.                 sheetaspect = NORMAL;
  116.                 break;
  117.             case 'o':    /* print outlines */
  118.                 opt_outline = 1;
  119.                 break;
  120.             case 'b':    /* don't print outlines */
  121.                 opt_outline = 0;
  122.                 break;
  123.             case 'P':    /* Printer */
  124.                 ++optstr;
  125.                 (void) strcpy(printer, optstr);
  126.                 consumed = 1;
  127.                 break;
  128.             case 'p':    /* pr */
  129.                 opt_pr++;
  130.                 break;
  131.             case 's':    /* silent (don't print page count) */
  132.                 opt_verbose = 0;
  133.                 break;
  134.             case 'u':    /* updown */
  135.                 sheetorder = UPDOWN;
  136.                 break;
  137.             case 'v':    /* verbose (print page count) */
  138.                 opt_verbose = 1;
  139.                 break;
  140.             case 'W':
  141.                 ++optstr;
  142.                 opt_width = atoi(optstr);
  143.                 consumed = 1;
  144.                 break;
  145.             }
  146.         }
  147.     }
  148.     if (opterrors) {
  149.         return -1;
  150.     }
  151.     return currarg;
  152. }
  153.  
  154. do_env()
  155. {
  156.     int argc;
  157.     char **argv, **slice();
  158.     char copy[LINESIZE];
  159.     char *env, *getenv();
  160.  
  161.     env = getenv("PRINTER");
  162.     if (env) {
  163.         strcpy(printer, env);
  164.     }
  165.  
  166.     env = getenv("MPAGE");
  167.     if (env) {
  168.         strcpy(copy, env);
  169.         argv = slice(copy, &argc);
  170.         if (do_args(argc, argv, 1) < 0) {
  171.             fprintf(stderr, "%s: error in envronment \"%s\"\n",
  172.                 MPAGE, env);
  173.             return 0;
  174.         }
  175.     }
  176.     return 1;
  177. }
  178.  
  179. char *slc_argv[20];
  180.  
  181. char **slice(string, cntp)
  182.  char *string;
  183.  int *cntp;
  184. {
  185.     int count;
  186.  
  187.     /*
  188.      * mimic the shell for conformity
  189.      */
  190.     slc_argv[0] = MPAGE;
  191.     count = 1;
  192.     /*
  193.      * while there are still characters to be processed
  194.      */
  195.     while (*string) {
  196.         /*
  197.          * skip any leading or leftover white space
  198.          */
  199.         while (*string == ' ') {
  200.             string++;
  201.         }
  202.         /*
  203.          * make sure we had more than just white space before
  204.          * we believe we actually have an argument
  205.          */
  206.         if (*string) {
  207.             /*
  208.              * point the next slot in argv to this string
  209.              */
  210.             slc_argv[count] = string;
  211.             count += 1;
  212.             /*
  213.              * and go looking for the end of this string
  214.              * which is delienated by a space or NULL
  215.              */
  216.             while (*string && *string != ' ') {
  217.                 string++;
  218.             }
  219.             /*
  220.              * if this not the end of the string, then convert
  221.              * the space into a NULL and move forward one byte.
  222.              * if this is the end of the string, we already have
  223.              * a suitable NULL byte for the string and it also
  224.              * drops us out of all the loops
  225.              */
  226.             if (*string) {
  227.                 *string = 0;
  228.                 string++;
  229.             }
  230.         }
  231.     }
  232.     /*
  233.      * return the count via the integer pointer we were given
  234.      * and put a null pointer into the argv array for conformity
  235.      */
  236.     slc_argv[count] = 0;
  237.     *cntp = count;
  238.     return slc_argv;
  239. }
  240.  
  241.