home *** CD-ROM | disk | FTP | other *** search
/ Adventures in Heaven 2 / adventuresinheaven2powergamesfordosandwindows.iso / windows / arcade / cbzone / c_parseo.c < prev    next >
C/C++ Source or Header  |  1992-05-27  |  6KB  |  241 lines

  1. #include "c_includ.h"
  2. /*
  3.  * cbzone_parseopts.c
  4.  *  -- Todd W Mummert, December 1990, CMU
  5.  *
  6.  * RCS Info
  7.  *  $Header: c_parseopts.c,v 1.1 91/01/12 02:03:36 mummert Locked $
  8.  *
  9.  * Parse the options, read the MOTD, etc...
  10.  *
  11.  * The prints in these routines will never go the game window, as it
  12.  * does not exist.  cbzone cannot be backgrounded from the beginning
  13.  * since the execl will block on tty output.  To allow this we create
  14.  * yet another flag that specifies that motd is not to be read.
  15.  */
  16.  
  17. int pager(file)
  18.      char* file;
  19. {
  20.   char buf[100], *pager, *getenv();
  21.   static char defaultpager[] = PAGER;
  22.   FILE *f;
  23.  
  24.   if ((pager = getenv("PAGER")) == NULL)
  25.     pager = defaultpager;
  26.   sprintf(buf,"%s%s",TANKDIR,file);
  27.   if ((f=fopen(buf,"r")) != NULL) {
  28.     fclose(f);
  29.     switch (fork()) {
  30.     case 0:
  31. #ifndef WIN31
  32.       execl(pager,pager,buf,0);
  33. #endif
  34.       fprintf(stderr,"Exec of %s failed\n", pager);
  35.       return 1;
  36.     case -1:
  37.       fprintf(stderr,"Unable to fork process\n");
  38.       return 1;
  39.     default:
  40. #ifndef WIN31
  41.       wait(0);
  42. #endif
  43.       break;
  44.     }
  45.   }
  46.   else {
  47.     fprintf(stderr,"File %s not found or unreadable.\n", buf);
  48.     return 1;
  49.   }
  50.   return 0;
  51. }
  52.  
  53. int getoptionint(s)
  54.      char *s;
  55. {
  56.   char rest[100];
  57.   int num;
  58.  
  59.   if (sscanf(s, "%d%s", &num, rest) != 1) {
  60.     printf("Error in optional argument %s; use -help for help.\n",
  61.            s);
  62.     exit(0);
  63.   }
  64.   return(num);
  65. }
  66.  
  67. /*
  68.  * the following routine may be called in one of two ways...
  69.  *  either w/ the display set or without...if without, then we
  70.  *  will need to parse all the options...otherwise the resources should
  71.  *  have taken care of most of them for us.
  72.  *
  73.  *  now even if the display is set, we may get options that were
  74.  *  ambiguous.
  75.  *
  76.  *  since options are more than one letter, we can't use getopt.
  77.  */
  78. #define MAXOPTIONS 16
  79. #define OPTIONINT 7
  80. void parseopt(argc, argv, status)
  81.      int argc;
  82.      char* argv[];
  83.      Bool status;
  84. {
  85.   int i;
  86.   Bool early_exit = False;
  87.  
  88.   static char* optionnames[] = {
  89.     "-xrm", "-delay", "-blocks", "-landers", "-tanks", "-missiles",
  90.     "-salvos", "-coptersonly", "-quiet", "-scores", "-original",
  91.     "-version", "-help", "-nooutput", "-mono", "-cursor",
  92.     "-defaultcolormap", "-nofullscreen"};
  93.  
  94. #ifdef WIN32
  95.   // Eric Fogelin: Fixed options (original game, in mono color, quiet)
  96.   opt->mono = True;
  97.   opt->loud = False;
  98.  
  99.   opt->mblocks = 8;
  100.   opt->mlanders = 1;
  101.   opt->mtanks = 1;
  102.   opt->practice = True;
  103.  
  104. #else //X11
  105.   for (argc--, argv++; argc>0; argc--, argv++) {
  106.     for (i=0; i<MAXOPTIONS; i++)
  107.       if (!strncmp(*argv,optionnames[i],strlen(*argv)))
  108.         break;
  109.     if (i < OPTIONINT) {
  110.       argc--; argv++;
  111.     }
  112.     switch(i) {
  113.     case 0:                     /* xrm */
  114.       break;
  115.     case 1:                     /* delay */
  116.       opt->delay = getoptionint(*argv);
  117.       break;
  118.     case 2:                     /* blocks*/
  119.       opt->mblocks = getoptionint(*argv);
  120.       break;
  121.     case 3:                     /* landers */
  122.       opt->mlanders = getoptionint(*argv);
  123.       break;
  124.     case 4:                     /* tanks */
  125.       opt->mtanks = getoptionint(*argv);
  126.       break;
  127.     case 5:                     /* missiles */
  128.       opt->mmissiles = getoptionint(*argv);
  129.       break;
  130.     case 6:                     /* salvos */
  131.       opt->msalvos = getoptionint(*argv);
  132.       break;
  133.     case 7:                     /* copter practice */
  134.       opt->copters = True;
  135.       break;
  136.     case 8:                     /* quiet mode */
  137.       opt->loud = False;
  138.       break;
  139.     case 9:                     /* scores only */
  140.       opt->scores = True;
  141.       break;
  142.     case 10:                    /* original */
  143.       opt->original = True;
  144.       break;
  145.     case 11:                    /* version */
  146.       opt->version = True;
  147.       break;
  148.     case 12:                    /* help */
  149.       opt->help = True;
  150.       break;
  151.     case 13:                    /* nooutput */
  152.       opt->output = False;
  153.       break;
  154.     case 14:                    /* monocolor */
  155.       opt->mono = True;
  156.       break;
  157.     case 15:                    /* cursor */
  158.       opt->cursor = True;
  159.       break;
  160.     case 16:                    /* default colormap */
  161.       opt->defaultcolormap = True;
  162.       break;
  163.     case 17:                    /* fullscreen */
  164.       opt->fullscreen = False;
  165.       break;
  166.     }
  167.   }
  168. #endif //X11
  169.  
  170.   if (opt->scores || opt->help || opt->version)
  171.     early_exit = True;
  172.  
  173.   if (opt->output) {
  174.     pager("cbzone.motd");
  175.  
  176.     if (opt->scores)
  177.       scores(-1);
  178.  
  179.     if (opt->version)
  180.       printf("\nVersion \"%s\"\n", VERSION);
  181.  
  182.     if (opt->help && pager("cbzone.help"))
  183.       printf("Sorry help information not available.\n");
  184.   }
  185.  
  186.   if (early_exit)
  187. #ifdef WIN32
  188.      return;
  189. #else //X11
  190.     exit(0);
  191. #endif
  192.  
  193.   if (!status)
  194.     return;
  195.  
  196. #if 0 // Eric Fogelin: Allow copters and tanks
  197.   if (opt->copters)
  198.     opt->mtanks = 0;
  199.  
  200.   if (opt->original) {
  201.     opt->mblocks = 8;
  202.     opt->copters = False;
  203.     opt->mlanders = 1;
  204.     opt->mmissiles = 1;
  205.     opt->mtanks = 1;
  206.     opt->practice = True;
  207.     opt->msalvos = 1;
  208.   }
  209. #endif
  210.  
  211.   opt->menemies = (opt->mtanks > opt->mmissiles ?
  212.                    opt->mtanks : opt->mmissiles);
  213.   if (!opt->menemies) {
  214.     printf("Must have at least one missile or tank.\n");
  215. #ifdef WIN32
  216.      return;
  217. #else //X11
  218.      exit(1);
  219. #endif
  220.   }
  221.  
  222.   if (opt->msalvos == -1)
  223.     opt->msalvos = opt->menemies;
  224.   opt->mobjects = opt->mblocks + opt->mlanders + 2*opt->menemies +
  225.     opt->msalvos + 1;
  226.   opt->estart = 1;
  227.   opt->lstart = opt->estart + opt->menemies;
  228.   opt->sstart = opt->lstart + opt->mlanders;
  229.   opt->bstart = opt->sstart + opt->menemies + opt->msalvos;
  230.  
  231.   if (opt->mmissiles == MMISSILES &&
  232.       opt->mtanks == MTANKS &&
  233.       opt->mlanders == MLANDERS &&
  234.       opt->mblocks == MBLOCKS &&
  235.       opt->delay <= DELAY &&
  236.       opt->msalvos == opt->menemies)
  237.     opt->practice = False;
  238.   else
  239.     opt->practice = True;
  240. }
  241.