home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / cmdparse.c < prev    next >
C/C++ Source or Header  |  1991-08-03  |  10KB  |  284 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    cmdparse.c (Command Parse)
  6.  * Purpose:    Set options from command line
  7.  * Subroutine:    parse_cmdline()            returns: int
  8.  * Subroutine:    string_cmdline()        returns: void
  9.  * Subroutine:    usage()                returns: int
  10.  * Xlib calls:    none
  11.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  12.  *        You may do anything you like with this file except remove
  13.  *        this copyright.  The Smithsonian Astrophysical Observatory
  14.  *        makes no representations about the suitability of this
  15.  *        software for any purpose.  It is provided "as is" without
  16.  *        express or implied warranty.
  17.  * Modified:    {0} Michael VanHilst    initial version           5 January 1989
  18.  *              {1} MVH BSDonly strings.h compatability           19 Feb 1990
  19.  *        {2} Rick Schumeyer (Naval Research Lab)           8 May 1991
  20.  *        Added -button option.
  21.  *          The -button option get parsed in parse_etc().
  22.  *          I added the extern variable bad_buttons, which is defined
  23.  *          in btnlib/draw.c.
  24.  *        {3} MVH added + type switches, new error message  21 Jun 1991
  25.  *        {n} <who> -- <does what> -- <when>
  26.  */
  27.  
  28. #include <stdio.h>        /* stderr, NULL, etc. */
  29. #include <ctype.h>
  30.  
  31. #ifndef VMS
  32. #ifdef SYSV
  33. #include <string.h>
  34. #else
  35. #include <strings.h>        /* strlen, etc. for unenlightened BSD's */
  36. #endif
  37. #else
  38. #include <string.h>
  39. #endif
  40.  
  41. #include <X11/Xlib.h>        /* X window stuff */
  42. #include <X11/Xutil.h>        /* X window manager stuff */
  43. #include "hfiles/constant.h"    /* define codes */
  44. #include "hfiles/struct.h"    /* declare structure types */
  45. #include "hfiles/extern.h"    /* extern main SAOimage parameter structures */
  46. #include "hfiles/cmdparse.h"    /* define parse status bits */
  47.  
  48. extern int init_done;    /* flag set at end of initialization indicates redo */
  49. #ifdef ALLIANT
  50. extern int bad_buttons;
  51. #endif
  52.  
  53. /*
  54.  * Subroutine:    parse_cmdline
  55.  * Purpose:    Process options to set filename and change defaults
  56.  * Returns:    -1 on error, 0 OK but no filename, 1 OK and new filename given
  57.  */
  58. int parse_cmdline ( argc, argv, displayname )
  59.      int argc;
  60.      char *argv[];
  61.      char **displayname;    /* i/o: gets server name only at init time */
  62. {
  63.   int i, status;
  64.   int got, init;
  65.   int parse_display(), parse_filename(), parse_filetype(), parse_connection();
  66.   int parse_rotate(), parse_scale(), parse_color(), parse_fileread(), usage();
  67.   void init_cmdline();
  68.   static int parse_etc();
  69.  
  70.   if( displayname != NULL ) {
  71.     /* if initial program command line, strip off program name, store line */
  72.     argc--;
  73.     argv = &(argv[1]);
  74.     /* store the initial command string */
  75.     init_cmdline(argc, argv);
  76.     init = 1;
  77.   } else
  78.     init = 0;
  79.   /* initialize parse history */
  80.   got = 0;
  81.   status = 1;
  82.   /* check command line arguments for option requests */
  83.   for( i = 0; (status == 1) && (i < argc); i++ ) {
  84.     if( (status =
  85.      parse_display(argc, argv, &i, &got, displayname)) ) {
  86.       /* check for server or window dressing specifications */
  87.       ;
  88.     } else if( ((got & CMD_FTYPE) == 0) &&
  89.            ((status = parse_filetype(argc, argv, &i, &got)) != 0) ) {
  90.       /* check for the image file type */
  91.       ;
  92.     } else if( (status = parse_connection(argc, argv, &i, &got, init)) != 0 ) {
  93.       /* check for change in remote connection status */
  94.       ;
  95.     } else if( (status = parse_fileread(argc, argv, &i, &got)) != 0 ) {
  96.       /* check for image file reading parameters */
  97.       ;
  98.     } else if( (status = parse_rotate(argc, argv, &i, &got)) != 0 ) {
  99.       /* check for image rotation */
  100.       ;
  101.     } else if( (status = parse_scale(argc, argv, &i, &got)) != 0 ) {
  102.       /* check for scaling specifications */
  103.       ;
  104.     } else if( (status = parse_color(argc, argv, &i, &got)) != 0 ) {
  105.       /* check for color map selection */
  106.       ;
  107.     } else if( (status = parse_etc(argc, argv, i)) != 0 ) {
  108.       /* check for miscelaneous environment switches */
  109.       ;
  110.     } else if( ((got & CMD_FNAME) == 0) &&
  111.            ((status = parse_filename(argc, argv, &i, &got)) != 0) ) {
  112.       /* check for a possible image file name */
  113.       ;
  114.     } else {
  115.       if( argv[i][0] == '-' )
  116.     status = usage("unexpected switch", argc, argv, i, i);
  117.       else
  118.     status = usage("unrecognized token", argc, argv, i, i);
  119.     }
  120.   }
  121.   if( status == 1 ) {
  122.     return( got );
  123.   } else
  124.     return( -1 );
  125. }
  126.  
  127. /*
  128.  * Subroutine:    parse_etc
  129.  * Purpose:    Parse command line for things settings from the etc menu
  130.  */
  131. static int parse_etc ( argc, argv, i )
  132.      int argc;        /* total number of arg tokens */
  133.      char *argv[];    /* array of arg tokens */
  134.      int i;        /* arg to check */
  135. {
  136.   void set_submenu_toggle();
  137.  
  138.   if( (strcmp(argv[i], "-ct") == 0) ||
  139.       (strcmp(argv[i], "-coord") == 0) ) {
  140.     if( control.coord_track == 1 ) {
  141.       control.coord_track = 0;
  142.       if( init_done )
  143.     set_submenu_toggle(EOP, EOP_TextTrack, 0);
  144.     }
  145.   } else if( (strcmp(argv[i], "+ct") == 0) ||
  146.          (strcmp(argv[i], "+coord") == 0) ) {
  147.     if( control.coord_track != 1 ) {
  148.       control.coord_track = 1;
  149.       if( init_done )
  150.     set_submenu_toggle(EOP, EOP_TextTrack, 1);
  151.     }
  152.   } else if( (strcmp(argv[i], "-mt") == 0) ||
  153.          (strcmp(argv[i], "-magnifier") == 0) ) {
  154.     if( control.magni_track != 1 ) {
  155.       /* track updates windows as mouse moves */
  156.       control.magni_track = 1;
  157.       if( init_done )
  158.     set_submenu_toggle(EOP, EOP_Track, 1);
  159.     }
  160.   } else if( (strcmp(argv[i], "+mt") == 0) ||
  161.          (strcmp(argv[i], "+magnifier") == 0) ) {
  162.     if( control.magni_track == 1 ) {
  163.       /* track updates windows as mouse moves */
  164.       control.magni_track = 0;
  165.       if( init_done )
  166.     set_submenu_toggle(EOP, EOP_Track, 0);
  167.     }
  168.   } else if( (strcmp(argv[i], "-q") == 0) ||
  169.          (strcmp(argv[i], "-quiet") == 0) ||
  170.          (strcmp(argv[i], "-v") == 0) ||
  171.          (strcmp(argv[i], "-verbose") == 0) ) {
  172.     /* verbosity to control reporting of positions and measurements */
  173.     control.verbose = 0;
  174.     if( init_done )
  175.       set_submenu_toggle(EOP, EOP_Verbose, 0);
  176.   } else if( (strcmp(argv[i], "+v") == 0) ||
  177.          (strcmp(argv[i], "+verbose") == 0) ) {
  178.     control.verbose = 1;
  179.     if( init_done )
  180.       set_submenu_toggle(EOP, EOP_Verbose, 1);
  181. #ifdef ALLIANT
  182.   } else if (strcmp(argv[i], "-alliant")==0) {
  183.       bad_buttons = 1;
  184. #endif
  185.   } else {
  186.     return( 0 );
  187.   }
  188.   return( 1 );
  189. }
  190.  
  191. /*
  192.  * Subroutine:    string_cmdline
  193.  * Purpose:    Print tokens on one line to be like original command line
  194.  */
  195. void string_cmdline ( argc, argv, cmdline, linemax )
  196.      int argc;
  197.      char *argv[];
  198.      char *cmdline;
  199.      int linemax;
  200. {
  201.   int i;
  202.  
  203.   if( argc <= 0 ) {
  204.     cmdline[0] = '\0';
  205.   } else {
  206.     (void)strcpy(cmdline, argv[0]);
  207.     for( i=1; i<argc; i++ ) {
  208.       (void)strncat(cmdline, " ", linemax);
  209.       (void)strncat(cmdline, argv[i], linemax);
  210.     }
  211.   }
  212. }
  213.  
  214. #define CMDMAX 200
  215. /*
  216.  * Subroutine:    usage
  217.  * Purpose:    Print error mesage and list of command line switches
  218.  * returns:    -1
  219.  * Use:        Use to print msg and return -1 to indicate an error:
  220.  * Example:    if(error) return( usage(errmess,argc,argv,i-?,i) );
  221.  */
  222. int usage ( what, argc, argv, first, last )
  223.      char *what;
  224.      int argc;
  225.      char *argv[];
  226.      int first, last;
  227. {
  228.   int i;
  229.   char cmdline[CMDMAX];
  230.   void string_cmdline();
  231.  
  232.   /* print the entire commandline */
  233.   string_cmdline (argc, argv, cmdline, CMDMAX);
  234.   (void)fprintf(stderr, "%s\n", cmdline);
  235.   /* if wanted args beyond end of line, say so */
  236.   if( last >= argc ) {
  237.     (void)fprintf(stderr, "Missing arg(s) to switch\n");
  238.     last = argc - 1;
  239.   }
  240.   /* print message and offending tokens */
  241.   (void)fprintf(stderr,"Command line %s error:", what);
  242.   for( i=first; i<=last; i++ ) {
  243.     (void)fprintf(stderr, " %s", argv[i]);
  244.   }
  245.   (void)fprintf(stderr, "\n");
  246.   /* list switch options */
  247.   (void)fprintf(stderr,"usage: saoimage [ options ] [ - filename ]\n");
  248.   (void)fprintf(stderr,"     where options are one or more of:\n");
  249.   (void)fprintf(stderr,"  [(-display) (-d) <display>]");
  250.   (void)fprintf(stderr," [[(-geometry) (-g) <geometry>] [-gd <geometry>]]\n");
  251.   (void)fprintf(stderr,"  [[(-fits) (-oif)] [(-u1) (-u2)");
  252.   (void)fprintf(stderr," (-i2) (-i4) (-r4) (-r8) <w> <h>]]\n");
  253.   (void)fprintf(stderr,"  [(-skip) (-sk) <bytes>] [(-byteswap) (-bswap)]\n");
  254.   (void)fprintf(stderr,"  [[-zero] [-one]]");
  255.   (void)fprintf(stderr," [[(-lowerleft) (-ll)] [(-upperleft) (-ul)]]\n");
  256.   (void)fprintf(stderr,"  [(-rotate) (-rot) <1,2,3>]\n");
  257.   (void)fprintf(stderr,"  [-rmin <input clip val>]");
  258.   (void)fprintf(stderr," [-rmax <input clip val>]\n");
  259.   (void)fprintf(stderr,"  [-linear] [-wrap <count>] [-histeq]");
  260.   (void)fprintf(stderr," [-sqrt <power>] [-log <exponent>]\n");
  261.   (void)fprintf(stderr,"  [-min <scale clip val>] [-max <scale clip val>]\n");
  262.   (void)fprintf(stderr,"  [(-palette) (-p) <number of color cells>]");
  263.   (void)fprintf(stderr," [(-neg) (-n)]\n");
  264.   (void)fprintf(stderr,"  [(-vertgraph) (-vg)] [(-horizgraph) (-hg)]\n");
  265.   (void)fprintf(stderr,"  [(+/-verbose) (+/-v) (-quiet) (-q)]");
  266.   (void)fprintf(stderr," [(+/-coord) (+/-ct)]\n");
  267.   (void)fprintf(stderr,"  [(+/-magnifier) (+/-mt)]");
  268.   (void)fprintf(stderr," [-mag <magnifier magnification>]\n");
  269.   (void)fprintf(stderr,"  [(+/-imtool) (-pros)]\n");
  270.   (void)fprintf(stderr,"  [-idev <pipe name>] [-odev <pipe name>]");
  271.   (void)fprintf(stderr," [-fbconfig <file name>]\n");
  272.   (void)fprintf(stderr,"  [(-red) (-green) (-blue)]\n");
  273.   (void)fprintf(stderr,"  [(-panboxav) (-panboxsamp) (-panboxsum)");
  274.   (void)fprintf(stderr," (-panboxmax)]\n");
  275.   (void)fprintf(stderr,"  [-lprbuttons] [-mtf]");
  276.   (void)fprintf(stderr," [(-bordercolor) (-bc) <color>]\n");
  277. #ifdef ALLIANT
  278.   (void)fprintf(stderr,"  [-alliant]");
  279. #endif
  280.   (void)fprintf(stderr,"In most cases, the image filename needs no switch");
  281.   (void)fprintf(stderr," and can appear anywhere\nin the options list\n");
  282.   return( -1 );
  283. }
  284.