1 /* ------------------------------------------------
2 Copyright 2014 AT&T Intellectual Property
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
7 http://www.apache.org/licenses/LICENSE-2.0
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ------------------------------------------- */
16 #include "parse_fta.h"
17 #include "parse_schema.h"
18 #include"generate_utils.h"
29 #define TMPSTRLEN 1000
32 #define PATH_DELIM '/'
35 char tmp_schema_str[10000];
37 // Interface to FTA definition lexer and parser ...
39 extern int FtaParserparse(void);
40 extern FILE *FtaParserin;
41 extern int FtaParserdebug;
43 fta_parse_t *fta_parse_result;
44 var_defs_t *fta_parse_defines;
53 int main(int argc, char **argv){
55 char tmpstr[TMPSTRLEN];
59 // set these to 1 to debug the parser
62 FILE *table_schemas_in; // source tables definition file
64 // -------------------------------
65 // Handling of Input Arguments
66 // -------------------------------
67 const char *optstr = "C:";
68 const char *usage_str = "Usage: %s [-C <config directory>] schema_file [operator name ...]\n"
69 "\t[-C] : use <config directory> for definition files\n";
72 // parameters gathered from command line processing
73 string config_dir_path;
74 set<string> operator_names;
75 string schema_file_name;
78 while((chopt = getopt(argc,argv,optstr)) != -1){
82 config_dir_path = string(optarg) + string("/");
85 fprintf(stderr,"Error, argument %c not recognized.\n",optopt);
86 fprintf(stderr,"%s\n", usage_str);
89 fprintf(stderr,"Invalid arguments\n");
90 fprintf(stderr,"%s\n", usage_str);
96 for (int i = 0; i < argc; ++i) {
97 if(schema_file_name == ""){
98 schema_file_name = argv[i];
100 operator_names.insert(argv[i]);
104 if(schema_file_name == ""){
105 fprintf(stderr,"%s\n", usage_str);
110 // Open globally used file names.
112 // prepend config directory to schema file
113 schema_file_name = config_dir_path + schema_file_name;
115 if((table_schemas_in = fopen(schema_file_name.c_str(), "r")) == NULL) {
116 fprintf(stderr,"Can't open schema file %s\n%s\n",schema_file_name.c_str(),strerror(errno));
123 // Get an initial Schema
125 // Parse the table schema definitions.
126 fta_parse_result = new fta_parse_t();
127 FtaParser_setfileinput(table_schemas_in);
128 if(FtaParserparse()){
129 fprintf(stderr,"Table schema parse failed.\n");
132 if(fta_parse_result->parse_type != TABLE_PARSE){
133 fprintf(stderr,"ERROR, file %s is not a table definition file.\n",schema_file_name.c_str());
136 Schema = fta_parse_result->tables;
138 // Process schema field inheritance
141 retval = Schema->unroll_tables(err_str);
143 fprintf(stderr,"Error processing schema filed inheritance:\n %s\n", err_str.c_str() );
147 // Open header, code files
149 FILE *hdr_fl = fopen("operator_tuple_pack.h", "w");
151 fprintf(stderr,"Can't open output header file operator_tuple_pack.h\n%s\n",strerror(errno));
154 FILE *code_fl = fopen("operator_tuple_pack.cc", "w");
156 fprintf(stderr,"Can't open output code file operator_tuple_pack.c\n%s\n",strerror(errno));
164 // Generate code for the accessing the operator tuples.
167 "#ifndef __OPERATOR_TUPLE_PACK__\n"
168 "#define __OPERATOR_TUPLE_PACK__\n"
170 "#include \"host_tuple.h\"\n"
171 "#include \"vstring.h\"\n"
172 "#include \"byteswap.h\""
174 "void *unpack_operator_params(void *buf, int buflen, char **param_list, int plist_len, int *params_found);\n"
179 "#include \"operator_tuple_pack.h\"\n"
180 "#include <sys/param.h>\n"
181 "#include \"fta.h\"\n"
182 "#include \"lapp.h\"\n"
183 "#include \"hfta_runtime_library.h\"\n"
185 "void *unpack_operator_params(void *buf, int buflen, char **param_list, int plist_len, int *params_found){\n"
186 " char *c=(char *)buf;\n"
187 " int pno=0, pos=0;\n"
188 " *params_found = 0;\n"
190 " for(pos=0;pos<buflen;++pos){\n"
191 " if(c[pos]=='\\n' || c[pos]=='\\0') return buf;\n"
192 " if(c[pos]==':') break;\n"
194 " if(pos == buflen) return buf;\n"
197 " param_list[pno]=c+pos;\n"
200 " for(;pos<buflen;++pos){\n"
201 " if(c[pos]==','){\n"
204 " param_list[pno]=c+pos;\n"
207 " if(c[pos]=='\\n'){\n"
210 " *params_found = pno;\n"
220 vector<string> tbl_names = Schema->get_table_names();
221 for(t=0;t<tbl_names.size();++t){
222 if(operator_names.size()==0 || operator_names.count(tbl_names[t])>0){
223 int tid = Schema->find_tbl(tbl_names[t]);
224 if(Schema->get_schema_type(tid) == OPERATOR_VIEW_SCHEMA){
225 table_def *optbl = Schema->get_table(tid);
226 header += generate_host_name_tuple_struct(optbl);
227 header += "int pack_"+tbl_names[t]+"_tuple(host_tuple *tup, char *buf, int len, struct "+generate_tuple_name(tbl_names[t])+" *s, unsigned char tuple_type);\n\n";
228 code += generate_host_tuple_pack(optbl);
230 header += "extern char *"+tbl_names[t]+"_Schema_def_;\n\n";
231 code += "\tchar *"+tbl_names[t]+"_Schema_def_ = \n";
232 string schema_str = "FTA{\n\n";
233 schema_str += optbl->to_stream_string();
236 "\tquery_name '"+tbl_names[t]+"';\n"
240 vector<field_entry *> flds = optbl->get_fields();
241 for(f=0;f<flds.size();++f){
242 if(f>0) schema_str+=",";
243 schema_str += " " + flds[f]->get_name();
245 schema_str += "\nFrom "+tbl_names[t]+"\n}\n\n";
246 code += make_C_embedded_string(schema_str)+";\n\n";
249 vector<subquery_spec *> subq = Schema->get_subqueryspecs(tid);
250 for(s=0;s<subq.size();++s){
251 subquery_spec *sqs = subq[s];
252 field_entry_list fel;
253 for(f=0;f<sqs->types.size();++f){
254 fel.append_field(new field_entry(sqs->types[f].c_str(), sqs->names[f].c_str(), "", sqs->modifiers[f],ufcns));
256 string subq_name = tbl_names[t]+"_subq_"+sqs->name;
257 table_def std(subq_name.c_str(),NULL,NULL,&fel, STREAM_SCHEMA);
258 header += generate_host_name_tuple_struct(&std);
259 header += "struct "+subq_name+"_tuple *unpack_"+subq_name+"_tuple(host_tuple *tup);\n\n";
260 code += generate_host_tuple_unpack(&std);
263 if(operator_names.size()>0)
264 fprintf(stderr,"WARNING: %s is not an operator, skipping.\n",tbl_names[t].c_str());
269 header += "\n#endif\n";
272 fprintf(hdr_fl,"%s\n",header.c_str());
273 fprintf(code_fl,"%s\n",code.c_str());
275 ///////////////////////////////////////////////////////////////////////