|
| 1 | +#include <qse/awk/StdAwk.hpp> |
| 2 | +#include <qse/cmn/opt.h> |
| 3 | +#include <qse/cmn/main.h> |
| 4 | +#include <qse/cmn/mbwc.h> |
| 5 | +#include <qse/cmn/stdio.h> |
| 6 | +#include <cstring> |
| 7 | +#include <locale.h> |
| 8 | +#include <unistd.h> |
| 9 | +#include <signal.h> |
| 10 | +#include <errno.h> |
| 11 | +#include <string> |
| 12 | +#include <iostream> |
| 13 | +#include "base_awk.h" |
| 14 | +#include "core_data.h" |
| 15 | +using namespace std; |
| 16 | + |
| 17 | + |
| 18 | +typedef QSE::StdAwk StdAwk; |
| 19 | +typedef QSE::StdAwk::Run Run; |
| 20 | +typedef QSE::StdAwk::Value Value; |
| 21 | +map<string,string> get_main_args(int c,char **argv); |
| 22 | + |
| 23 | +map<string,string> get_main_args(int c,char **argv){ |
| 24 | + map<string,string> rtn; |
| 25 | + for(int i=1;i<c;i++){ |
| 26 | + string tmp(argv[i]); |
| 27 | + string k,v; |
| 28 | + if(tmp.length()<=2){ |
| 29 | + continue; |
| 30 | + } |
| 31 | + if(tmp.substr(0,1)=="-"){ |
| 32 | + k=tmp.substr(0,2); |
| 33 | + v= tmp.substr(2); |
| 34 | + }else{ |
| 35 | + k="input_file"; |
| 36 | + v = tmp; |
| 37 | + } |
| 38 | + rtn.insert(std::make_pair<string,string>(k,v)); |
| 39 | + |
| 40 | + } |
| 41 | + return rtn; |
| 42 | +} |
| 43 | + |
| 44 | +int main(int argc,char **argv) |
| 45 | +{ |
| 46 | + base_awk awk; |
| 47 | + awk.open(); |
| 48 | + if (awk.addArgument ("apache_log_awk") <= -1){ |
| 49 | + cout<<"set flag error"<<endl; |
| 50 | + return -1; |
| 51 | + } |
| 52 | + awk.setTrait (awk.getTrait() |QSE_AWK_DEPTH_INCLUDE); |
| 53 | + int foo=awk.addGlobal(QSE_T("global_var")); |
| 54 | + Run *run = NULL; |
| 55 | + |
| 56 | + map<string,string> args_info = get_main_args(argc,argv); |
| 57 | + map<string,string>::iterator itor_end = args_info.end(); |
| 58 | + map<string,string>::iterator itor = args_info.find("input_file"); |
| 59 | + if(itor!=itor_end){ |
| 60 | + awk.addArgument(itor->second.c_str()); |
| 61 | + } |
| 62 | + itor = args_info.find("-l"); |
| 63 | + if(itor!=itor_end){ |
| 64 | + awk.loadlib(itor->second); |
| 65 | + }else{ |
| 66 | + char *env_name="AWK_LIB_PATH"; |
| 67 | + char *lib_path = getenv(env_name); |
| 68 | + if(lib_path!=NULL){ |
| 69 | + string lib_path_str(lib_path); |
| 70 | + awk.loadlib(lib_path_str); |
| 71 | + } |
| 72 | + } |
| 73 | + itor = args_info.find("-f"); |
| 74 | + if(itor!=itor_end){ |
| 75 | + base_awk::SourceFile in(itor->second.c_str()); |
| 76 | + run = awk.parse (in, StdAwk::Source::NONE); |
| 77 | + if (run == QSE_NULL){ |
| 78 | + core_data::clear(); |
| 79 | + cout<<"parse file error"<<endl; |
| 80 | + return -1; |
| 81 | + } |
| 82 | + }else{ |
| 83 | + cout<<"script file not set"<<endl; |
| 84 | + return 0; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + Value global_val(run); |
| 89 | + itor = args_info.find("-v"); |
| 90 | + if(itor!=itor_end){ |
| 91 | + char buf[1000]; |
| 92 | + bzero(buf,1000); |
| 93 | + memcpy(buf,itor->second.c_str(),itor->second.length()); |
| 94 | + char *tmp_p = strtok(buf,","); |
| 95 | + int ii=1; |
| 96 | + while(tmp_p!=NULL){ |
| 97 | + Value::IntIndex idx(ii++); |
| 98 | + global_val.setIndexedStr(idx,tmp_p,strlen(tmp_p)); |
| 99 | + tmp_p=strtok(NULL,","); |
| 100 | + } |
| 101 | + } |
| 102 | + awk.setGlobal(foo,global_val); |
| 103 | + |
| 104 | + base_awk::Value ret; |
| 105 | + if (awk.loop (&ret) <= -1) |
| 106 | + { |
| 107 | + core_data::clear(); |
| 108 | + cout<<"exec error"<<endl; |
| 109 | + return -1; |
| 110 | + } |
| 111 | + core_data::clear(); |
| 112 | + return 0; |
| 113 | +} |
0 commit comments