#include #include "plantsim.h" #include "database.h" #define NARGS_MIN 2 /* minimum allowable arguments */ #define NARGS_MAX 100 /* maximum allowable arguments */ #ifdef DEBUG #define DBG(s) s #else #define DBG(s) #endif extern ObjNode *envObjs; extern char *outName; extern char *inName; extern char *scriptName; extern OutFormat outType; extern Boolean quiet; extern Boolean verbose; extern Boolean new_face; extern int lineno; extern FILE *yyin; extern int startFrame; extern int endFrame; extern int frameInc; extern Real framesPsec; char *optval(char *, int); /* Parses the command line arguments and sets appropriate variables. * Also reads in the specified object files and creates the list of * environment objects which the plant should grow around * * Params argc count of args * argv array of args * * Globals envObjs pointer to list of linked objects * outName pointer to the name of the output file * inName pointer to the name of the configuration file * scriptName pointer to the name of the script file * outType output format (showpoly or wavefront) * quiet say nothing? * verbose say a lot? * new_face point out to various functions reading in a new face * lineno for use by yyparse * yyin pointer to file to be read in * startFrame frame to start output * endFrame frame to stop at * frameInc next frame after current one * framesPsec unused at the moment * * Returns nothing */ void parseArgs(argc,argv) int argc; char *argv[]; { Tupple trans; char *opt_p; char *cmd_p; char fvalue[MAXSTRING]; char *optval(); void readSeeds(FILE *); void calcSeedNorms(); char *USAGE = " [-[hqsvw][f opts][-o ][-i ][-x script] [ ...]"; cmd_p = *argv; /* command name */ if (argc < NARGS_MIN || argc > NARGS_MAX) { fprintf(stderr,"%s: Usage: %s\n",cmd_p,USAGE); exit(EX_USAGE); } /* * skip command name */ argc--; argv++; /* * process the option arguments */ while (argc && *argv[0] == '-') { opt_p = argv[0]; /* * process the options in the arguments */ while(*(++opt_p)) { switch(*opt_p) { case 'f': switch(*(++opt_p)) { case 's': argc--; argv++; if (argc) { startFrame = abs(atoi(argv[0])); } opt_p = optval(opt_p, argc); break; case 'e': argc--; argv++; if (argc) { endFrame = abs(atoi(argv[0])); } opt_p = optval(opt_p, argc); break; case 'i': argc--; argv++; if (argc) { frameInc = abs(atoi(argv[0])); } opt_p = optval(opt_p, argc); break; case 'p': argc--; argv++; if (argc) { frameInc = abs(atoi(argv[0])); } opt_p = optval(opt_p, argc); break; default: fprintf(stderr,"%s: unrecognized option f%c\n", cmd_p, *opt_p); fprintf(stderr, "%s: Usage: %s\n", cmd_p, USAGE); exit(EX_USAGE); } break; case 'h': fprintf(stderr,"%s: Usage: %s\n\n", cmd_p, USAGE); fprintf(stderr,"-h output this help information\n"); fprintf(stderr,"-q quiet mode\n"); fprintf(stderr,"-s output in showpoly format\n"); fprintf(stderr,"-v verbose mode\n"); fprintf(stderr,"-w output in wavefront format\n"); fprintf(stderr,"\nf opts:\n"); fprintf(stderr,"-fs n where n is the starting frame\n"); fprintf(stderr,"-fe n where n is the ending frame\n"); fprintf(stderr,"-fi n where n is the frame increment\n"); fprintf(stderr,"-fp n where n is a number indicating frames"); fprintf(stderr," per second\n"); fprintf(stderr,"\nIf no output file is specified output will"); fprintf(stderr," be written to stdout.\n"); fprintf(stderr,"The input file contains the configuration "); fprintf(stderr,"of the environment, and plant growing\n"); fprintf(stderr,"characteristics\n"); fprintf(stderr,"The script is the one to be run after out"); fprintf(stderr," putting the current frame in wavefront\n"); fprintf(stderr,"object format.\n"); fprintf(stderr,"\nThe object files must be wavefront object "); fprintf(stderr,"files.\n"); exit(0); break; case 'i': DBG(fprintf(stderr,"i option\n");) /* * use the option value. */ argc--; argv++; if (argc) { /* * option value is "argv[0]" */ if((inName=(char *)calloc(MAXSTRING,sizeof(char)))==NULL) { fprintf(stderr, "Error: couldn't calloc room for "); fprintf(stderr, "input file name\n"); exit(1); } strncpy(inName,argv[0],MAXSTRING-1); } /* * check the option syntax. */ opt_p = optval(opt_p,argc); break; case 'o': DBG(fprintf(stderr,"o option\n");) /* * use the option value. */ argc--; argv++; if (argc) { /* * option value is "argv[0]" */ if((outName=(char *)calloc(MAXSTRING,sizeof(char)))==NULL) { fprintf(stderr, "Error, couldn't calloc room for output file name\n"); exit(1); } strncpy(outName,argv[0],MAXSTRING-1); } /* * check the option syntax. */ opt_p = optval(opt_p,argc); break; case 'q': DBG(fprintf(stderr, "q option\n");) quiet = True; verbose = False; break; case 's': DBG(fprintf(stderr, "s option\n");) outType = Showpoly; break; case 'v': DBG(fprintf(stderr, "v option\n");) verbose = True; quiet = False; break; case 'w': DBG(fprintf(stderr, "w option\n");) outType = Wavefront; break; case 'x': DBG(fprintf(stderr,"o option\n");) argc--; argv++; if (argc) { /* * option value is "argv[0]" */ if((scriptName=(char *)calloc(MAXSTRING,sizeof(char)))== NULL) { fprintf(stderr, "Error, couldn't calloc room for script name\n"); exit(1); } strncpy(scriptName,argv[0],MAXSTRING-1); } /* * check the option syntax. */ opt_p = optval(opt_p,argc); break; default: fprintf(stderr , "%s: unrecognized option %c\n" , cmd_p, *opt_p); fprintf(stderr,"%s: Usage: %s\n",cmd_p,USAGE); exit(EX_USAGE); } } /* * look at next argument */ argc--; argv++; } if(argc < 1) { fprintf(stderr, "%s: Need input object file(s)\n", cmd_p); fprintf(stderr, "%s: Usage: %s\n", cmd_p, USAGE); exit(EX_USAGE); } while (argc) { if (!quiet) fprintf(stderr, "Opening file %s...\n", argv[0]); if((yyin = fopen(argv[0], "r")) == NULL) { fprintf(stderr, "%s: Error couldn't open file: %s\n", cmd_p, argv[0]); exit(EX_NOINPUT); } envObjs = addEmptyObjNode(envObjs); lineno = 0; yyparse(); fclose(yyin); argc--; argv++; zeroObject(envObjs, &trans); calcObjInfo(envObjs); } if (!quiet) fprintf(stderr, "Finished reading in data\n"); } char *optval(opt_p,argc) /* * Give out error messsages and abort if the option syntax is * not adhered to. */ char *opt_p; int argc; { if (!argc) { fprintf(stderr , "missing value after `%c' option\n" , *opt_p); exit(EX_USAGE); } /* * warn about options following one that requires * a value */ if (*(opt_p+1)) { fprintf(stderr, "option(s) after `%c' ignored.\n", *opt_p); /* * skip rest of options in this arg */ while (*(opt_p + 1)) ++opt_p; } return(opt_p); }