You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.8 KiB

#include <inttypes.h>
9 years ago
#include "init.h"
#include "object.h"
#include "globals.h"
#include "builtin.h"
#include "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 "grammatical.h"
// definitions for bison-bridge
#define YYSTYPE CONFSTYPE
#define YYLTYPE CONFLTYPE
#include "lexical.h"
#undef YYSTYPE
#undef YYLTYPE
9 years ago
int ParseConfigFile(const char* config, ExecType& tosave, ExecType& toprint, VarType& vars)
9 years ago
{
yyscan_t scanner;
struct lexical_extra extra;
FILE* conffd;
int ret;
9 years ago
conffd=fopen(config,"r");
if(conffd==0)
{
COUT(ERROR)<<"Can't open file "<<config<<std::endl;
return 1;
}
9 years ago
extra.maxinclevel=10;
extra.retcode=0;
extra.state.inclevel=0;
extra.state.curline=1;
extra.state.curpos=extra.state.curoffset=0;
char* cwd=get_current_dir_name();
extra.state.curdir=cwd;
free(cwd);
extra.toprint=&toprint;
extra.tosave=&tosave;
extra.vars=&vars;
extra.ParsePath(config);
// Search paths begin from working directory
extra.includedirs=extra.moduledirs="./";
9 years ago
conflex_init_extra(&extra,&scanner);
confset_in(conffd,scanner);
// {YYSTYPE qqq; while(conflex(&qqq,scanner)>0);}
ret=confparse(scanner);
9 years ago
conflex_destroy(scanner);
fclose(conffd);
return ret+extra.retcode;
}
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 RegisterBuiltinFunctions()
{
RegisterFunction("ADD",Arifm2<OpAdd>);
RegisterFunction("SUB",Arifm2<OpSub>);
RegisterFunction("MUL",Arifm2<OpMul>);
RegisterFunction("DIV",Arifm2<OpDiv>);
RegisterFunction("POW",Arifm2<OpPow,false>);
RegisterFunction("NEG",Arifm1<OpNeg>);
RegisterFunction("POS",Arifm1<OpPos>);
RegisterFunction("GET",Get<ObjectList>);
RegisterFunction("GET",Get<ObjectPair>);
return 0;
9 years ago
}
debug_level SetDebugLevel(debug_level dl)
{
static debug_level DEBUG_LEVEL=INFO;
if(dl==INTERNALREQUEST) return DEBUG_LEVEL;
debug_level s=DEBUG_LEVEL;
DEBUG_LEVEL=dl;
return s;
}
std::ostream& COUT(debug_level dl)
{
if(dl==ERROR) return std::cerr;
if(dl>=SetDebugLevel(INTERNALREQUEST)) std::cout.clear();
else std::cout.setstate(std::cout.failbit);
return std::cout;
}