home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / msdos / viewers / showgl19 / getargs.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  8KB  |  328 lines

  1. #pragma linesize(132)
  2.  
  3. /* getargs.c     a UNIX-like switch reading routine
  4.  
  5. Copyright 1991, 1992, Robert C. Becker, Lantern Systems */
  6.  
  7. /* version 2.0
  8.  
  9. 22 July, 1992.  Added -70, -75 options for emulation of HP 7470 and 7475 
  10. plotters.  These plotters start up with relative character sizing.  The
  11. DraftPro and the HPGL/2 spec set the default char size to absolute.  Also
  12. removed istok () as dead code and modified getflags () to make better
  13. use of pointers.
  14.  
  15. /* version 1.5
  16.  
  17. Added -db flag to turn on debugging output */
  18.  
  19. /*
  20. valid option flags:
  21.  
  22. A    8.5 x 11 inch paper
  23. B    11 x 17 inch paper
  24. C    17 x 22 inch paper
  25. D    22 x 34 inch paper
  26. E    34 x 44 inch paper
  27.  
  28. A4    210 x 297 mm paper
  29. A3    297 x 420 mm paper
  30. A2    420 x 594 mm paper
  31. A1    594 x 841 mm paper
  32. A0    841 x 1189 mm paper
  33.  
  34. c    force CGA 640 x 200 mode
  35. e    force EGA 640 x 350 mode
  36. v    force VGA 640 x 480 mode
  37.  
  38. dp    assume HP DraftPro coordinate system (center of page = (0,0)
  39. db    turn on debugging output
  40.  
  41. Note that options are case sensitive.
  42. */
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <malloc.h>
  47. #include <string.h>
  48. #include "graph.h"    /* need this for video device definitions */
  49. #include "hpgl.h"
  50.  
  51.  
  52. #ifndef TRUE
  53. #define TRUE  1
  54. #endif
  55. #ifndef FALSE
  56. #define FALSE 0
  57. #endif
  58.  
  59. #define        ERR_F        0x1F
  60. #define        FNAME_LEN    128    /* length of longest MSDOS filename */
  61. #define        EXIT1        1
  62.  
  63. static void nfexit (int, char *);
  64. static void clear_flags ( void );
  65. static int setflags ( int );
  66. static void setopt_values ( int );
  67. static void getflags ( char * );
  68.  
  69. static char copyright[] = "Copyright 1991, Robert C. Becker, Lantern Systems";
  70.  
  71. static char cf, ef, vf, Af, Bf, Cf, Df, Ef, A4f, A3f, A2f, A1f, A0f, dbf, dpf,
  72.          f70, f75;
  73.  
  74. static char *progname;    /* pointer to program name */
  75.  
  76. static char **files;
  77. static char *path;    /* file found at requested directory path */
  78. static char *dpath;    /* directory pathname */
  79. static char fname_bfr [FNAME_LEN];    /* handy buffer for file names */
  80. static char tokens[] = "c e v A B C D A4 A3 A2 A1 A0 db dp 70 75";
  81.  
  82. struct _vid_x_parm_ *forced_video [] = { CGA_640x200, CGA_640x200, EGA_640x350, 
  83.             VGA_640x480 };    /* use CGA_640x200 to fill space */
  84.  
  85. #define VIDEO_AUTO    0
  86. #define CGA_mode    1
  87. #define EGA_mode    2
  88. #define    VGA_mode    3
  89.  
  90. struct option
  91.     {
  92.     char *opt;        /* option char. string */
  93.     int length;        /* length of option char string to match */
  94.     int opt_value;        /* value of option */
  95.     };
  96.  
  97. static struct option optarray[] =
  98.     {
  99.     { "c", 1, CGA_mode},        /* these values correspond to array indices in    */
  100.     { "e", 1, EGA_mode},        /* in forced_video[] */
  101.     { "v", 1, VGA_mode},
  102.     { "A4", 2, A4_paper},
  103.     { "A3", 2, A3_paper},
  104.     { "A2", 2, A2_paper},
  105.     { "A1", 2, A1_paper},
  106.     { "A0", 2, A0_paper},
  107.     { "db", 2, Debug_flag},
  108.     { "dp", 2, DRAFTPRO},
  109.     { "A", 1, A_paper},
  110.     { "B", 1, B_paper},
  111.     { "C", 1, C_paper},
  112.     { "D", 1, D_paper},
  113.     { "E", 1, E_paper},
  114.     { "70", 2, F_70},
  115.     { "75", 2, F_75},
  116.     { "", 0, 0}    /* end of struct */
  117.     };
  118.  
  119. static struct options cmd_options;
  120.  
  121. /*----------------------------------*/
  122.  
  123. static void nfexit (int val, char *opt)
  124.      {
  125.      printf ("\n%s: ", progname);
  126.     printf ("unknown option:  %s\n", opt);
  127.     printf ("usage: %s  [-%s]  <file>\n", progname, tokens);
  128.  
  129.     exit (val);
  130.     }
  131.  
  132.  
  133. /*----------------------------------*/
  134.  
  135. static void clear_flags ( void )
  136.     {
  137.     Af = Bf = Cf = Df = Ef = 0;
  138.     A4f = A3f = A2f = A1f = A0f = 0;
  139.     cf = ef = vf = 0;
  140.     dbf = 0;
  141.     dpf = 0;
  142.     f70 = f75 = 0;
  143.  
  144.     return;
  145.     }
  146.  
  147. /*----------------------------------*/
  148.  
  149. static int setflags (int flag)
  150.     {
  151.     switch(flag)
  152.         {
  153.         case  0: cf = TRUE;    /* force CGA mode */
  154.             cmd_options.video = optarray[flag].opt_value;
  155.             break;
  156.         case  1: ef = TRUE;    /* force EGA mode */
  157.             cmd_options.video = optarray[flag].opt_value;
  158.             break;
  159.         case  2: vf = TRUE;    /* force VGA mode */
  160.             cmd_options.video = optarray[flag].opt_value;
  161.             break;
  162.         case  3: A4f = TRUE;    /* A4 paper */
  163.             setopt_values (flag);
  164.             break;
  165.         case  4: A3f = TRUE;    /* A3 paper */
  166.             setopt_values (flag);
  167.             break;
  168.         case  5: A2f = TRUE;    /* A2 paper */
  169.             setopt_values (flag);
  170.             break;
  171.         case  6: A1f = TRUE;    /* A1 paper */
  172.             setopt_values (flag);
  173.             break;
  174.         case  7: A0f = TRUE;    /* A0 paper */
  175.             break;
  176.         case  8: dbf = TRUE;    /* debug mode */
  177.             cmd_options.debug = optarray[flag].opt_value;
  178.             break;
  179.         case  9: dpf = TRUE;    /* draftpro mode */
  180.             cmd_options.plotter = optarray[flag].opt_value;
  181.             break;
  182.         case 10: Af = TRUE;    /* A paper */
  183.             setopt_values (flag);
  184.             break;
  185.         case 11: Bf = TRUE;    /* B paper */
  186.             setopt_values (flag);
  187.             break;
  188.         case 12: Cf = TRUE;    /* C paper */
  189.             setopt_values (flag);
  190.             break;
  191.         case 13: Df = TRUE;    /* D paper */
  192.             setopt_values (flag);
  193.             break;    
  194.         case 14: Ef = TRUE;    /* E paper */
  195.             setopt_values (flag);
  196.             break;
  197.         case 15: f70 = TRUE;    /* 7470 flag */
  198.             cmd_options.plotter = optarray[flag].opt_value;
  199.             break;
  200.         case 16: f75 = TRUE;    /* 7475 flag */
  201.             cmd_options.plotter = optarray[flag].opt_value;
  202.             break;
  203.         default: break;
  204.         }
  205.     return (TRUE);    /* return constant value for cheap programming simplification */
  206.     }
  207.  
  208. /*----------------------------------*/
  209.  
  210. static void setopt_values (int index)
  211.     {
  212.  
  213.     cmd_options.paper = optarray[index].opt_value;
  214.  
  215.     return;
  216.     }
  217.     
  218.  
  219. /*----------------------------------*/
  220.  
  221. static void getflags (char *args)
  222.     {
  223.      int i, j, index, ind=FALSE;
  224.  
  225.     while (*args != '\0')
  226.         {
  227.         for (j = 0; ; j++)
  228.             {
  229.  
  230.             if (optarray[j].opt[0] == *args)
  231.                 {
  232.                 if (optarray[j].length == 1)
  233.                     {
  234.                     ind = setflags (j);
  235.                     ++args;
  236.                     break;
  237.                     }
  238.  
  239.                 if (optarray[j].opt[1] == *(args + 1))
  240.                     {
  241.                     ind = setflags (j);
  242.                     args += 2;    /* skip to next char */
  243.                     break;
  244.                     }
  245.                 }
  246.             if ( !optarray[j].length ) nfexit (EXIT1, args);    /* flag not found in list */
  247.             }
  248.         }
  249.  
  250.     return;
  251.     }
  252.  
  253. /*----------------------------------*/
  254.  
  255. struct options *getargs (int argc, char **argv)
  256.      {
  257.     char *s;
  258.  
  259.     path = (char *) calloc (1, 215);
  260.     dpath = (char *) calloc (1, 215);
  261.     cmd_options.paper = A_paper;    /* set default paper type to A-size */
  262.     cmd_options.video = 0;        /* initialize video and papersize to defaults */
  263.     cmd_options.plotter = 0;    /* no plotter specified: default to HPGL/2 spec */
  264.     cmd_options.debug = 0;        /* no debugging output */
  265.  
  266.     s = progname = *argv++;
  267.     --argc;
  268.     while (*s)    /* get program name */
  269.         {
  270.         if (*s == ':' || *s == '\\' || *s == '/')
  271.             progname = s + 1;
  272.         ++s;
  273.         }
  274.     strlwr (progname);
  275.  
  276.     clear_flags ();        /* zero all flags */
  277.  
  278.     if (**argv == '-')
  279.         {
  280.         while ( *(s = *argv) && (*s == '-'))    /* if we have command line flags, */
  281.             {                /*ignore environment defaults */
  282.             ++argv;
  283.             --argc;
  284.             ++s;    /* skip leading '-' */
  285.             getflags (s);
  286.             }
  287.         }
  288.     else
  289.         {
  290.         if ((s=getenv ("HPGL")) != NULL)
  291.             {
  292.             getflags (s);    /* get environment default flags */
  293.             }
  294.         }
  295.  
  296.     /* check for more than one paper size selected */
  297.     if (Af + Bf + Cf + Ef + A4f + A3f + A2f + A1f + A0f > 1)
  298.         {
  299.         printf ("%s:  more than one paper size options selected\n", progname);
  300.         exit (1);
  301.         }
  302.  
  303.     if ((dpf != 0 && (f70 != 0 || f75 != 0)) || (f70 != 0 && f75 != 0))
  304.         {
  305.         printf ("%s:  more than one plotter type selected\n", progname);
  306.         exit (1);
  307.         }
  308.  
  309.     if (cf + ef + vf > 1)
  310.         {
  311.         printf ("%s: ignoring multiple video adapter modes\n", progname);
  312.         cmd_options.video = VIDEO_AUTO;    /* use automatic mode selection */
  313.         }
  314.  
  315.     if (argc < 1)
  316.         {    /* use stdin */
  317.         cmd_options.infile = stdin;
  318.         }
  319.     else
  320.         {    /* get filename */
  321.         strcpy (path, *argv);
  322.         cmd_options.infile = fopen (path, "r");    /* returns NULL if no file */
  323.         }
  324.  
  325.     return (&cmd_options);
  326.     }
  327.  
  328.