Browse Source

Command-line options.

test
Michael Uleysky 9 years ago
parent
commit
28cb54ff70
  1. 39
      src/init.cpp
  2. 13
      src/init.h
  3. 35
      src/main.cpp

39
src/init.cpp

@ -5,6 +5,11 @@
#include "globals.h"
#include "arifmetic.h"
#include "parser/parser.h"
// We can't include lexical.h before grammatical.h, because of strange errors, but grammatical.h only needs definition of yyscan_t
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif
#include "parser/grammatical.h"
// definitions for bison-bridge
#define YYSTYPE CONFSTYPE
@ -13,6 +18,34 @@
#undef YYSTYPE
#undef YYLTYPE
int ParseOptions(int argc, char** argv, struct program_options& options)
{
int opt=0;
const char* optstr="t:hdgiq";
optind=1;
do
{
opt=getopt(argc,argv,optstr);
switch(opt)
{
case(-1): break;
case('t'):{options.threads=atoi(optarg); if(options.threads<1) return 1; else break;}
case('h'):{options.help=true; break;}
case('d'):{options.dump=true; break;}
case('g'):{options.dl=DEBUG; break;}
case('i'):{options.dl=INFO; break;}
case('q'):{options.dl=WARNING; break;}
default: return 1;
}
} while(-1!=opt);
if(optind!=argc-1) return 1;
options.config=argv[optind];
return 0;
}
int ParseConfigFile(const char* config)
{
yyscan_t scanner;
@ -112,7 +145,7 @@ int CheckFunctions()
void DumpConfig()
{
for(auto& i: G_vars) COUT(INFO)<<i.first<<"="+i.second->Dump()<<";"<<std::endl;
for(auto& i: G_tosave) COUT(INFO)<<"save"<<i->Dump()<<";"<<std::endl;
for(auto& i: G_toprint) COUT(INFO)<<"print"<<i->Dump()<<";"<<std::endl;
for(auto& i: G_vars) COUT(NORMAL)<<i.first<<"="+i.second->Dump()<<";"<<std::endl;
for(auto& i: G_tosave) COUT(NORMAL)<<"save"<<i->Dump()<<";"<<std::endl;
for(auto& i: G_toprint) COUT(NORMAL)<<"print"<<i->Dump()<<";"<<std::endl;
}

13
src/init.h

@ -1,11 +1,16 @@
#ifndef INIT_H
#define INIT_H
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif
#include "deptree.h"
struct program_options
{
unsigned int threads;
bool help,dump;
debug_level dl;
const char* config;
};
int ParseOptions(int argc, char** argv, struct program_options& options);
int ParseConfigFile(const char* config);
int RegisterArifmeticFunctions();
int BuildDepTree(DepTree* deptree, UsedType& used);

35
src/main.cpp

@ -2,16 +2,39 @@
#include "init.h"
#include "globals.h"
static void usage(const char* prg)
{
std::cout<<"Usage: "<<prg<<" [-h] [-t num] [-d] [-g] [-i] [-q] file"<<std::endl<<
"-h - this help,"<<std::endl<<
"-t - set number of threads,"<<std::endl<<
"-d - only parse config, print it and return (with -q, nothing printing),"<<std::endl<<
"-g - enable debug messages,"<<std::endl<<
"-i - print information about current actions,"<<std::endl<<
"-q - be quitet, ignore print directives."<<std::endl<<
"-g, -i and -q are mutually exclusive."<<std::endl;
}
int main(int argc, char** argv)
{
if(argc!=2) return 1;
int ret;
DepTree* DPTree;
struct program_options options;
// Set default options
options.threads=1;
options.help=options.dump=false;
options.dl=NORMAL;
options.config=0;
SetDebugLevel(INFO);
// Parse options
ret=ParseOptions(argc,argv,options);
if(ret!=0 || options.config==0) {usage(argv[0]); return 1;}
if(options.help) {usage(argv[0]); return 0;}
COUT(INFO)<<"Parse config file "<<argv[1]<<" ";
ret=ParseConfigFile(argv[1]); if(ret!=0) { ClearGlobals(); return 1;}
SetDebugLevel(options.dl);
COUT(INFO)<<"Parse config file "<<options.config<<" ";
ret=ParseConfigFile(options.config); if(ret!=0) { ClearGlobals(); return 1;}
COUT(INFO)<<"Ok"<<std::endl;
if(G_tosave.size()==0 && G_toprint.size()==0)
{
@ -41,9 +64,13 @@ int main(int argc, char** argv)
ret=CheckFunctions(); if(ret!=0) { ClearGlobals(); delete DPTree; return 1;}
COUT(INFO)<<"Ok"<<std::endl;
if(options.dump) DumpConfig();
else
{
COUT(INFO)<<"Evaluate tree ";
ret=DPTree->EvaluateTree(); if(ret!=0) { ClearGlobals(); delete DPTree; return 1;}
COUT(INFO)<<"Ok"<<std::endl;
}
ClearGlobals();
delete DPTree;

Loading…
Cancel
Save