home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / FFT142.ZIP / ARGS.C next >
Encoding:
C/C++ Source or Header  |  1991-06-06  |  3.9 KB  |  176 lines

  1. /*
  2.     args - handle command line arguments
  3.  
  4.     The calling program must supply the following two items...
  5. */
  6. extern int get_parameter();
  7. extern char *message[];
  8.  
  9. #include <stdio.h>
  10.  
  11. #define streq(a, b) (strcmp(a, b) == 0)
  12.  
  13. extern double atof();
  14.  
  15. /*---------------------------------------------------------------*/
  16. /*    write out the command line */
  17. fprint_cmd(fp, format) 
  18. FILE *fp;        /* pointer to output file */
  19. char *format;     /* desired print format, possibly including program name */
  20. {    char buf[129];
  21.     int i=0;
  22. #ifdef __DESMET__
  23.     _lmove(129, 130, _showcs()-0x10, &buf, _showds());
  24.     buf[128]=13;
  25.     while(buf[i] != 13) i++;
  26.     buf[i]=0;
  27. #else /* not __DESMET__ */
  28. #ifdef __TURBOC__
  29.     extern int _argc;
  30.     extern char **_argv;
  31.     buf[0] = 0;
  32.     for (i = 1; i < _argc; i++) {strcat(buf, _argv[i]); strcat(buf, " ");}
  33. #else /* not __TURBOC__ */
  34.     buf[0] = 0;
  35. #endif /* not __TURBOC__ */
  36. #endif /* not __DESMET__ */
  37.  
  38.     fprintf(fp, format, buf);
  39. }
  40.  
  41. /*  parse command line switches */
  42. args( argc, argv ) int argc; char **argv;
  43. {    char **av;
  44.     int ac, i;
  45.     argc--; argv++;
  46.     ac=argc; av=argv; argc=0;
  47.     while(ac>0)
  48.         {if(**av=='?') help();
  49.         else if(**av=='-' && (i=get_parameter( ac, av )) && i > 0)
  50.             {ac-=i; av+=i;
  51.             }
  52.         else {argv[argc++] = *av++; ac--;}
  53.         }
  54.     return argc+1;
  55. }
  56.  
  57. /*
  58.     get one or more double values from command line.
  59.  
  60.     note variable number of arguments...  
  61.                                 get_double(argc, argv, 3, &a, &b, &c)
  62. */
  63. int get_double(argc, argv, permitted, a)    /* returns # arguments used */
  64. int argc;                                     /* # argument strings left */
  65. char **argv;                                 /* ptr to string array */
  66. int permitted;                                 /* # pointers following */
  67. double *a;                                    /* the first of the pointers */
  68. {    int i=1;
  69.     double **dp = &a;
  70.  
  71.     while((permitted--)>0 && (argc>i) && numeric(argv[i])) 
  72.         {**dp = atof(argv[i++]); 
  73.         dp++;
  74.         }
  75.     return i;
  76. }
  77.  
  78. gripe_arg(s) char *s;
  79. {    fprintf(stderr, "argument missing for switch %s", s);
  80.     help();
  81. }
  82.  
  83. gripe(argv) char **argv;
  84. {    fprintf(stderr, *argv); fprintf(stderr, " isn\'t a legal argument \n\n");
  85.     help();
  86. }
  87.  
  88. numeric(s) char *s;
  89. {    char c;
  90.     while(c=*s++)
  91.         {if((c <= '9' && c >= '0') || 
  92.                         c == '+' || 
  93.                         c == '-' || 
  94.                         c == '.' || 
  95.                         c == 'e' || 
  96.                         c == 'E') continue;
  97.         return 0;
  98.         }
  99.     return 1;
  100. }
  101.  
  102. help()
  103. {    char **sp;
  104.  
  105.     for (sp = message; *sp; sp++) printf(*sp);
  106.     exit();
  107. }
  108.  
  109. #ifdef MAIN
  110.  
  111. double f1, f2, f3;
  112. int debugging = 0;
  113.  
  114. /* get_parameter - process one command line option
  115.         (return # parameters used) */
  116. int get_parameter(argc, argv) 
  117. int argc;             /* # valid entries in argv[] */
  118. char **argv;        /* pointer to array of pointers to argument strings */
  119. {
  120.     int i;
  121.  
  122.     if(streq(*argv, "-d")) {debugging = 1; return 1;}
  123.     else if(streq(*argv, "-f1")) 
  124.         {i = get_double(argc, argv, 1, &f1, &f2, &f3); 
  125.         return i;
  126.         }
  127.     else if(streq(*argv, "-f2")) 
  128.         {i = get_double(argc, argv, 2, &f1, &f2, &f3); 
  129.         return i;
  130.         }
  131.     else if(streq(*argv, "-f3")) 
  132.         {i = get_double(argc, argv, 3, &f1, &f2, &f3); 
  133.         return i;
  134.         }
  135.  
  136.     else gripe(argv);
  137. }
  138.  
  139.  
  140. char *message[]=
  141. {
  142. " - calculate ASAT engagements\n",
  143. "usage:  asat  [options]\n",
  144. "options:\n",
  145. "     -a <num>   target satellite altitude (km, default 400)\n",
  146. "     -i <num>   target satellite inclination (degrees, default 65)\n",
  147. "     -la <num>  ASAT site latitude (degrees, default 9)\n",
  148. "     -r         print radar horizon only\n",
  149. "     -v <num>   ASAT axial delta velocity (km/sec, default 4)\n",
  150. "     -n         northbound satellite\n",
  151. "     -vl        visibility limit\n",
  152. "     -el        energy limit (default)\n",
  153. "     -tl        timing limit\n",
  154. "     -td        delay time (sec, default 30)\n",
  155.  
  156. 0
  157. };
  158.  
  159. main(argc, argv) int argc; char **argv;
  160. {    int i;
  161.  
  162.     fprint_cmd(stdout, "; args %s\n");
  163.  
  164.     argc = args(argc, argv);
  165.  
  166.     printf("f1 = %f  f2 = %f  f3 = %f\n", f1, f2, f3);
  167.  
  168.     printf("argv = ");
  169.     for (i = 0; i < argc; i++) printf(" \"%s\"", argv[i]);
  170.  
  171. }
  172.  
  173.  
  174. #endif /* MAIN */
  175.  
  176.