Fixed newline characters throughout the code
[com/gs-lite.git] / src / ftacmp / gen_tuple_access.cc
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
6
7      http://www.apache.org/licenses/LICENSE-2.0
8
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  ------------------------------------------- */
15
16 #include "parse_fta.h"
17 #include "parse_schema.h"
18 #include"generate_utils.h"
19
20 #include <string>
21 #include<set>
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <unistd.h>
26
27 #include<errno.h>
28
29 #define TMPSTRLEN 1000
30
31 #ifndef PATH_DELIM
32   #define PATH_DELIM '/'
33 #endif
34
35 char tmp_schema_str[10000];
36
37 //              Interface to FTA definition lexer and parser ...
38
39 extern int FtaParserparse(void);
40 extern FILE *FtaParserin;
41 extern int FtaParserdebug;
42
43 fta_parse_t *fta_parse_result;
44 var_defs_t *fta_parse_defines;
45
46
47
48 using namespace std;
49
50
51
52
53 int main(int argc, char **argv){
54         int s,t,f;
55   char tmpstr[TMPSTRLEN];
56
57
58
59 //                              set these to 1 to debug the parser
60   FtaParserdebug = 0;
61
62   FILE *table_schemas_in;               // source tables definition file
63
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";
70
71
72 //              parameters gathered from command line processing
73         string config_dir_path;
74         set<string> operator_names;
75         string schema_file_name;
76
77    char chopt;
78    while((chopt = getopt(argc,argv,optstr)) != -1){
79                 switch(chopt){
80                 case 'C':
81                                 if(optarg != NULL)
82                                  config_dir_path = string(optarg) + string("/");
83                         break;
84                 case '?':
85                         fprintf(stderr,"Error, argument %c not recognized.\n",optopt);
86                         fprintf(stderr,"%s\n", usage_str);
87                         exit(1);
88                 default:
89                         fprintf(stderr,"Invalid arguments\n");
90                         fprintf(stderr,"%s\n", usage_str);
91                         exit(1);
92                 }
93         }
94         argc -= optind;
95         argv += optind;
96         for (int i = 0; i < argc; ++i) {
97                 if(schema_file_name == ""){
98                         schema_file_name = argv[i];
99                 }else{
100                         operator_names.insert(argv[i]);
101                 }
102         }
103
104         if(schema_file_name == ""){
105                 fprintf(stderr,"%s\n", usage_str);
106                 exit(1);
107         }
108
109
110 //                      Open globally used file names.
111
112         // prepend config directory to schema file
113         schema_file_name = config_dir_path + schema_file_name;
114
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));
117                 exit(1);
118         }
119
120
121
122
123 //                      Get an initial Schema
124         table_list *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");
130                 exit(1);
131         }
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());
134                 exit(1);
135         }
136         Schema = fta_parse_result->tables;
137
138 //                      Process schema field inheritance
139         string err_str;
140         int retval;
141         retval = Schema->unroll_tables(err_str);
142         if(retval){
143                 fprintf(stderr,"Error processing schema filed inheritance:\n %s\n", err_str.c_str() );
144                 exit(1);
145         }
146
147 //                      Open header,  code files
148
149         FILE *hdr_fl = fopen("operator_tuple_pack.h", "w");
150         if(hdr_fl == NULL){
151                 fprintf(stderr,"Can't open output header file operator_tuple_pack.h\n%s\n",strerror(errno));
152                 exit(1);
153         }
154         FILE *code_fl = fopen("operator_tuple_pack.cc", "w");
155         if(code_fl == NULL){
156                 fprintf(stderr,"Can't open output code file operator_tuple_pack.c\n%s\n",strerror(errno));
157                 exit(1);
158         }
159
160
161
162
163
164 //                      Generate code for the accessing the operator tuples.
165
166         string header =
167 "#ifndef __OPERATOR_TUPLE_PACK__\n"
168 "#define __OPERATOR_TUPLE_PACK__\n"
169 "\n"
170 "#include \"host_tuple.h\"\n"
171 "#include \"vstring.h\"\n"
172 "#include \"byteswap.h\""
173 "\n"
174 "void *unpack_operator_params(void *buf, int buflen, char **param_list, int plist_len, int *params_found);\n"
175 "\n"
176 "\n";
177
178         string code =
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"
184 "\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"
189 "\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"
193 "       }\n"
194 "       if(pos == buflen) return buf;\n"
195 "\n"
196 "       pos++; \n"
197 "       param_list[pno]=c+pos;\n"
198 "       pno++;\n"
199 "       \n"
200 "       for(;pos<buflen;++pos){\n"
201 "               if(c[pos]==','){\n"
202 "                       c[pos]='\\0';\n"
203 "                       pos++;\n"
204 "                       param_list[pno]=c+pos;\n"
205 "                       pno++;\n"
206 "               }\n"
207 "               if(c[pos]=='\\n'){\n"
208 "                       c[pos]='\\0';\n"
209 "                       pos++;\n"
210 "                       *params_found = pno;\n"
211 "                       return c+pos;\n"
212 "               }\n"
213 "       }\n"
214 "       return buf;\n"
215 "}\n"
216 "\n"
217 "\n";
218
219         set<string> ufcns;
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);
229
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();
234                                 schema_str +=
235 "DEFINE{\n"
236 "\tquery_name '"+tbl_names[t]+"';\n"
237 "}\n"
238 "\n"
239 "Select";
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();
244                                 }
245                                 schema_str += "\nFrom "+tbl_names[t]+"\n}\n\n";
246                                 code += make_C_embedded_string(schema_str)+";\n\n";
247
248
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));
255                                         }
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);
261                                 }
262                         }else{
263                                 if(operator_names.size()>0)
264                                         fprintf(stderr,"WARNING: %s is not an operator, skipping.\n",tbl_names[t].c_str());
265                         }
266                 }
267         }
268
269         header += "\n#endif\n";
270
271
272         fprintf(hdr_fl,"%s\n",header.c_str());
273         fprintf(code_fl,"%s\n",code.c_str());
274
275 ///////////////////////////////////////////////////////////////////////
276
277         return(0);
278 }
279