home *** CD-ROM | disk | FTP | other *** search
/ gdead.berkeley.edu / gdead.berkeley.edu.tar / gdead.berkeley.edu / pub / cad-tools / m36gen.tar / m36gen_dir / get_args.c < prev    next >
C/C++ Source or Header  |  1996-01-08  |  4KB  |  212 lines

  1. #include <stdio.h>
  2.  
  3. extern int debug;
  4. extern int center;
  5. extern int silent;
  6. extern int one_pass;
  7.  
  8. extern int x_trans;
  9. extern int y_trans;
  10. extern int x_center;
  11. extern int y_center;
  12.  
  13. extern char *name;
  14. extern char *dir_file;
  15.  
  16. extern FILE *infile;
  17.  
  18. FILE *fopen();
  19.  
  20. #define USAGE "Usage : m36gen [-s] [-n name] [-d dir_file] [-c x_center y_center] [-t x_offset y_offset] input_file\n"
  21.  
  22.     /* get_args parses the command line arguments */
  23.  
  24. get_args(argc,argv)
  25. int argc;
  26. char **argv;
  27. {
  28.     char *ptr;
  29.     char    c;
  30.     int t_used = 0;
  31.  
  32.     if (argc == 1) {
  33.     fprintf(stderr,USAGE);
  34.     exit(1);
  35.     }
  36.  
  37.     while ( argc-- > 1 ) {
  38.  
  39.     if (*(++argv)[0] == '-' && argv[0][1] != '\0') {
  40.  
  41.         ptr = argv[0];
  42.  
  43.         switch (ptr[1]) {
  44.  
  45.         case 't' :
  46.  
  47.             if ( ptr[2] != '\0' || argc < 3 ) {
  48.             fprintf(stderr,USAGE);
  49.             exit(1);
  50.             }
  51.  
  52.             t_used++;
  53.             
  54.             if ( center ) {
  55.             fprintf(stderr,"t and c options may not be used at the same time\n");
  56.             exit(1);
  57.             }
  58.  
  59.             if ( sscanf(*++argv,"%d",&x_trans) != 1 || 
  60.                 sscanf(*++argv,"%d",&y_trans) != 1 ) {
  61.  
  62.             fprintf(stderr,"Offsets must be integers\n");
  63.             fprintf(stderr,USAGE);
  64.             exit(1);
  65.             }
  66.  
  67.             argc -= 2;
  68.             break;
  69.  
  70.  
  71.         case 'c' :
  72.  
  73.             if ( ptr[2] != '\0' || argc < 3 ) {
  74.             fprintf(stderr,USAGE);
  75.             exit(1);
  76.             }
  77.             
  78.             fprintf(stderr,"Warning:  Using the centering option will individually center each layer\n\t\tand change the coordinates of your alignment mark(s).\n");
  79.  
  80.             printf("Abort this execution of m36gen \(n\)? :");
  81.             scanf("%c", &c);
  82.             if((c == 'y') || (c == 'Y')) {
  83.               fprintf(stderr, "exiting\n");
  84.               return(-1);
  85.             }
  86.             
  87.  
  88.             center++;
  89.             
  90.             if ( t_used ) {
  91.             fprintf(stderr,"t and c options may not be used at the same time\n");
  92.             exit(1);
  93.             }
  94.  
  95.             if ( sscanf(*++argv,"%d",&x_center) != 1 || 
  96.                 sscanf(*++argv,"%d",&y_center) != 1 ) {
  97.  
  98.             fprintf(stderr,"center coordinates must be integers\n");
  99.             fprintf(stderr,USAGE);
  100.             exit(1);
  101.             }
  102.  
  103.             argc -= 2;
  104.             break;
  105.  
  106.         case 'n' :
  107.             
  108.             if ( argc < 2 ) {
  109.             fprintf("Name missing in '-n name'\n");
  110.             fprintf(stderr,USAGE);
  111.             exit(1);
  112.             }
  113.             name = *++argv;
  114.             argc--;
  115.             break;
  116.  
  117.         case 'd' :
  118.             
  119.             if ( argc < 2 ) {
  120.             fprintf("File name missing in '-d dir_file'\n");
  121.             fprintf(stderr,USAGE);
  122.             exit(1);
  123.             }
  124.             dir_file = *++argv;
  125.             one_pass = 1;
  126.             argc--;
  127.             break;
  128.  
  129.         default :
  130.  
  131.             while ( *(++ptr) != '\0' ) {
  132.             
  133.             switch (*ptr) {
  134.                 
  135.                 case 'D' :
  136.                 
  137.                 debug++;
  138.                 break;
  139.                 
  140.                 case 's' :
  141.  
  142.                 silent++;
  143.                 break;
  144.  
  145.                 default :
  146.                 
  147.                 fprintf(stderr,"Unknown flag %c\n",*ptr);
  148.                 fprintf(stderr,USAGE);
  149.                 exit(1);
  150.             }
  151.             }
  152.         }
  153.     } else {
  154.         
  155.         char *infile_name = argv[0];
  156.  
  157.         if (strcmp(infile_name,"-") == 0) {
  158.         if (!one_pass) {
  159.             fprintf(stderr, "You can only read from standard input if you use the -d dir_file option\n");
  160.             fprintf(stderr, USAGE);
  161.             exit(1);
  162.         } else {
  163.             infile = stdin;
  164.         }
  165.         } else if ( (infile = fopen(infile_name,"r")) == NULL ) {
  166.         fprintf(stderr,"Cannot open the input file %s\n",
  167.             argv[0]);
  168.         fprintf(stderr,USAGE);
  169.         exit(1);
  170.         }
  171.  
  172.         if ( *(++argv) != 0 ) {
  173.         fprintf(stderr,"Extra characters after the input file\n");
  174.         fprintf(stderr,USAGE);
  175.         exit(1);
  176.         }
  177.  
  178.         return;
  179.     }
  180.     }
  181.     
  182.     fprintf(stderr,"No input_file given\n");
  183.     fprintf(stderr,USAGE);
  184.     exit(1);
  185. }
  186.  
  187. #define IS_DIGIT(c) ( (c) >='0' && (c) <='9' )
  188.  
  189. atoi(string,int_ptr)
  190. char *string;
  191. int *int_ptr;
  192. {
  193.     *int_ptr = 0;
  194.  
  195.     if ( (string == (char *) 0) || *string == '\0' ) {
  196.     return(0);
  197.     }
  198.  
  199.     while ( *string != '\0' ) {
  200.  
  201.     if ( IS_DIGIT(*string) ) {
  202.         *int_ptr += *string - '0';
  203.     } else {
  204.         return(0);
  205.     }
  206.  
  207.     string++;
  208.     }
  209.  
  210.     return(1);
  211. }
  212.