core/cmdarg.c

Go to the documentation of this file.
00001 
00042 #include <stdio.h>
00043 #include <string.h>
00044 
00045 #include "globals.h"
00046 #include "cmdarg.h"
00047 #include "output.h"
00048 #include "load.h"
00049 #include "legal.h"
00050 #include "timestamp.h"
00051 #include "random.h"
00052 
00053 STATUS load_module_list(FILE *fd,int* test_mod_num)
00054 {
00055     /*
00056 
00057     sprintf(mod_test,"mod_test%d=%s",test_mod_num++,*++argv);
00058     if (global_setvar(mod_test)==SUCCESS)
00059     */
00060     char mod_test[100];
00061     char line[100];
00062     while(fscanf(fd,"%s",line) != EOF)
00063     {
00064         printf("Line: %s",line);
00065         sprintf(mod_test,"mod_test%d=%s",(*test_mod_num)++,line);
00066         if (global_setvar(mod_test)!=SUCCESS)
00067         {
00068             output_fatal("Unable to store module name");
00069             return FAILED;
00070         }
00071     }
00072 
00073     return SUCCESS;
00074 }
00075 
00087 STATUS cmdarg_load(int argc, 
00088                    char *argv[]) 
00089 {
00090     int test_mod_num = 1;
00091     unsigned int pos=0;
00092     int i;
00093     char *pd1, *pd2;
00094 
00095     /* capture the execdir */
00096     strcpy(global_execdir,argv[0]);
00097     pd1 = strrchr(global_execdir,'/');
00098     pd2 = strrchr(global_execdir,'\\');
00099     if (pd1>pd2) *pd1='\0';
00100     else if (pd2>pd1) *pd2='\0';
00101 
00102     /* capture the command line */
00103     for (i=0; i<argc; i++)
00104     {
00105         if (pos<sizeof(global_command_line)-strlen(argv[i]))
00106             pos += sprintf(global_command_line+pos,"%s%s",pos>0?" ":"",argv[i]);
00107     }
00108 
00109     while (argv++,--argc>0)
00110     {
00111         if (strcmp(*argv,"-w")==0 || strcmp(*argv,"--warn")==0)
00112             global_warn_mode=!global_warn_mode;
00113         else if (strcmp(*argv,"-c")==0 || strcmp(*argv,"--check")==0)
00114             global_runchecks=!global_runchecks;
00115         else if (strcmp(*argv,"--debug")==0)
00116             global_debug_output=!global_debug_output;
00117         else if (strcmp(*argv,"--debugger")==0)
00118             global_debug_mode=global_debug_output=1;
00119         else if (strcmp(*argv,"--dumpall")==0)
00120             global_dumpall=!global_dumpall;
00121         else if (strcmp(*argv,"-q")==0 || strcmp(*argv,"--quiet")==0)
00122             global_quiet_mode=!global_quiet_mode;
00123         else if (strcmp(*argv,"-v")==0 || strcmp(*argv,"--verbose")==0)
00124             global_verbose_mode=!global_verbose_mode;
00125         else if (strcmp(*argv,"--profile")==0)
00126             global_profiler=!global_profiler;
00127         else if (strcmp(*argv,"--pause")==0)
00128             global_pauseatexit=!global_pauseatexit;
00129         else if (strcmp(*argv,"--license")==0)
00130             legal_license();
00131         else if (strcmp(*argv, "--version")==0)
00132             legal_notice();
00133         else if (strcmp(*argv,"--dsttest")==0)
00134             timestamp_test();
00135         else if (strcmp(*argv,"--randtest")==0)
00136             random_test();
00137         else if (strcmp(*argv,"--unitstest")==0)
00138             unit_test();
00139         else if (strcmp(*argv,"--globaldump")==0)
00140         {
00141             global_dump();
00142             exit(0);
00143         }
00144         else if (strncmp(*argv,"--pidfile",9)==0)
00145         {
00146             char *filename = strchr(*argv,'=');
00147             if (filename==NULL)
00148                 strcpy(global_pidfile,"gridlabd.pid");
00149             else
00150                 strcpy(global_pidfile,filename+1);
00151         }
00152         else if (strncmp(*argv,"--kml",5)==0)
00153         {
00154             char *filename = strchr(*argv,'=');
00155             if (filename)
00156                 strcpy(global_kmlfile,filename+1);
00157             else
00158                 strcpy(global_kmlfile,"gridlabd.kml");
00159         }
00160         else if (strcmp(*argv, "--avlbalance") == 0){
00161             global_no_balance = !global_no_balance;
00162         }
00163         else if (strcmp(*argv,"--testall")==0){
00164             FILE *fd = NULL;
00165             if(*++argv != NULL)
00166                 fd = fopen(*argv,"r");
00167             else {
00168                 output_fatal("no filename for testall");
00169                 return FAILED;
00170             }
00171             argc--;
00172             global_test_mode=TRUE;
00173 
00174             if(fd == NULL)
00175             {
00176                 output_fatal("incorrect module list file name");
00177                 return FAILED;
00178             }
00179             if(load_module_list(fd,&test_mod_num) == FAILED)
00180                 return FAILED;
00181         }
00182         else if (strcmp(*argv,"--test")==0){
00183             global_test_mode=TRUE;
00184 
00185             if (argc-1>0)
00186             {
00187                 char mod_test[100];
00188                 sprintf(mod_test,"mod_test%d=%s",test_mod_num++,*++argv);
00189                 if (global_setvar(mod_test)==SUCCESS)
00190                     argc--;
00191             }
00192             else
00193             {
00194                 output_fatal("test module name is missing");
00195                 return FAILED;
00196             }
00197 
00198         }
00199         else if (strcmp(*argv,"-D")==0 || strcmp(*argv,"--define")==0)
00200         {
00201             if (argc-1>0)
00202             {
00203                 if (global_setvar(*++argv)==SUCCESS)
00204                     argc--;
00205             }
00206             else
00207             {
00208                 output_fatal("definition is missing");
00209                 return FAILED;
00210             }
00211         }
00212         else if (strcmp(*argv,"--redirect")==0)
00213         {
00214             if (argc-1>0)
00215             {
00216                 char buffer[1024]; char *p;
00217                 strcpy(buffer,*++argv); argc--;
00218                 if (strcmp(buffer,"all")==0)
00219                 {
00220                     if (output_redirect("output",NULL)==NULL ||
00221                         output_redirect("error",NULL)==NULL ||
00222                         output_redirect("warning",NULL)==NULL ||
00223                         output_redirect("debug",NULL)==NULL ||
00224                         output_redirect("verbose",NULL)==NULL ||
00225                         output_redirect("profile",NULL)==NULL ||
00226                         output_redirect("progress",NULL)==NULL)
00227                     {
00228                         output_fatal("redirection of all failed");
00229                         return FAILED;
00230                     }
00231                 }
00232                 else if ((p=strchr(buffer,':'))!=NULL)
00233                 {
00234                     *p++='\0';
00235                     if (output_redirect(buffer,p)==NULL)
00236                     {
00237                         output_fatal("redirection of %s to '%s' failed: %s",buffer,p, strerror(errno));
00238                         return FAILED;
00239                     }
00240                 }
00241                 else if (output_redirect(buffer,NULL)==NULL)
00242                 {
00243                         output_fatal("default redirection of %s failed: %s",buffer, strerror(errno));
00244                         return FAILED;
00245                 }
00246             }
00247             else
00248             {
00249                 output_fatal("redirection is missing");
00250                 return FAILED;
00251             }
00252         }
00253         else if (strcmp(*argv,"-L")==0 || strcmp(*argv,"--libinfo")==0)
00254         {
00255             if (argc-1>0)
00256             {   argc--;
00257                 module_libinfo(*++argv);
00258                 exit(0);
00259             }
00260             else
00261             {
00262                 output_fatal("missing library name");
00263                 return FAILED;
00264             }
00265         }
00266         else if (strcmp(*argv,"-T")==0 || strcmp(*argv,"--threadcount")==0)
00267         {
00268             if (argc-1>0)
00269                 global_threadcount = (argc--,atoi(*++argv));
00270             else
00271             {
00272                 output_fatal("missing thread count");
00273                 return FAILED;
00274             }
00275         }
00276         else if (strcmp(*argv,"-o")==0 || strcmp(*argv,"--output")==0)
00277         {
00278             if (argc-1>0)
00279                 strcpy(global_savefile,(argc--,*++argv));
00280             else
00281             {
00282                 output_fatal("missing output file");
00283                 return FAILED;
00284             }
00285         }
00286         else if (strcmp(*argv,"-e")==0 || strcmp(*argv,"--environment")==0)
00287         {
00288             if (argc-1>0)
00289                 strcpy(global_environment,(argc--,*++argv));
00290             else
00291             {
00292                 output_fatal("environment not specified");
00293                 return FAILED;
00294             }
00295         }
00296         else if (strcmp(*argv,"--xmlencoding")==0)
00297         {
00298             if (argc-1>0)
00299             {
00300                 global_xml_encoding = atoi(*++argv);
00301                 argc--;
00302             }
00303             else
00304             {
00305                 output_fatal("xml encoding not specified");
00306                 return FAILED;
00307             }
00308         }
00309         else if (strcmp(*argv,"--xsd")==0)
00310         {
00311             if (argc-1>0)
00312             {
00313                 argc--;
00314                 exit(output_xsd(*++argv));
00315             }
00316             else
00317             {
00318                 output_fatal("module:object not specified");
00319                 return FAILED;
00320             }
00321         }
00322         else if (strcmp(*argv,"-h")==0 || strcmp(*argv,"--help")==0)
00323         {
00324             printf("Syntax: gridlabd [OPTIONS ...] <file> ... \nOptions:\n"
00325                 "  --avlbalance              toggles AVL tree balancing\n"
00326                 "  -c|--check                toggles module checks after model loads\n"
00327                 "  -D|--define <def>         defines a macro value\n"
00328                 "  --debug                   toggles debug output (prints internal messages)\n"
00329                 "  --debugger                toggles debugger mode (generates internal messages)\n"
00330                 "  --dumpall                 toggles module data dump after run completes\n"
00331                 "  -e|--environment <name>   specifies user environment (default none)\n"
00332                 "  --license                 print license information\n"
00333                 "  -L|--libinfo <module>     print module library information\n"
00334                 "  -o|--output <file>        specifies model should be output after run\n"
00335                 "  --profile                 toggles profilers\n"
00336                 "  -q|--quiet                toggles quiet mode (suppresses startup banner)\n"
00337                 "  --test                    toggles test mode (activate testing procedures)\n"
00338                 "  -T|--threadcount <n>      specifies the number of processor threads to use\n"
00339                 "  -v|--verbose              toggles verbose mode (active verbose messages)\n"
00340                 "  --version                 prints the GridlabD version information\n"
00341                 "  -w|--warn                 toggles warning mode (generates warning messages)\n"
00342                 "  --xmlencoding <num>       set the XML encoding (8, 16, or 32)\n"
00343                 "  --xsd <module>[:<object>] prints the xsd of an object\n"
00344                 );
00345             exit(0);
00346         }
00347         else if (**argv!='-')
00348         {
00349             if (global_test_mode)
00350                 output_warning("file '%s' ignored in test mode", *argv);
00351             else {
00352                 if (!loadall(*argv))
00353                     return FAILED;
00354                 /* preserve name of first model only */
00355                 if (strcmp(global_modelname,"")==0)
00356                     strcpy(global_modelname,*argv);
00357             }
00358         }
00359         else
00360         {
00361             int n = module_cmdargs(argc,argv);
00362             if (n==0)
00363             {
00364                 output_error("command line option '%s' is not recognized",*argv);
00365                 return FAILED;
00366             }
00367         }
00368     }
00369     /*debug_traverse_tree(NULL);*/  /* for checking the name tree & getting a test file. -mh */
00370     return SUCCESS;
00371 }
00372 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy