2 * Copyright 2008 Search Solution Corporation
3 * Copyright 2016 CUBRID Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * esql_grammar.y : esql grammar file
25 %{/*%CODE_REQUIRES_START%*/
26 #include "esql_host_variable.h"
29 %{/*%CODE_PROVIDES_START%*/
30 #include "esql_scanner_support.h"
32 #if defined (SUPPRESS_STRLEN_WARNING)
33 #define strlen(s1) ((int) strlen(s1))
34 #endif /* defined (SUPPRESS_STRLEN_WARNING) */
37 #define ECHO_mode START
44 #define COMMENT_mode 7
46 static KEYWORD_REC c_keywords[] = {
50 {"break", GENERIC_TOKEN, 0},
51 {"case", GENERIC_TOKEN, 0},
54 {"continue", GENERIC_TOKEN, 0},
55 {"default", GENERIC_TOKEN, 0},
56 {"do", GENERIC_TOKEN, 0},
57 {"double", DOUBLE_, 0},
58 {"else", GENERIC_TOKEN, 0},
60 {"extern", EXTERN_, 0},
62 {"for", GENERIC_TOKEN, 0},
63 {"go", GENERIC_TOKEN, 0},
64 {"goto", GENERIC_TOKEN, 0},
65 {"if", GENERIC_TOKEN, 0},
68 {"register", REGISTER_, 0},
69 {"return", GENERIC_TOKEN, 0},
71 {"signed", SIGNED_, 0},
72 {"sizeof", GENERIC_TOKEN, 0},
73 {"static", STATIC_, 0},
74 {"struct", STRUCT_, 0},
75 {"switch", GENERIC_TOKEN, 0},
76 {"typedef", TYPEDEF_, 0},
78 {"unsigned", UNSIGNED_, 0},
79 {"VARCHAR", VARCHAR_, 1},
80 {"varchar", VARCHAR_, 1},
81 {"VARYING", VARYING, 0},
82 {"varying", VARYING, 0},
84 {"volatile", VOLATILE_, 0},
85 {"while", GENERIC_TOKEN, 0},
88 static KEYWORD_REC csql_keywords[] = {
89 /* Make sure that they are in alphabetical order */
96 {"ATTACH", ATTACH, 0},
97 {"ATTRIBUTE", ATTRIBUTE, 0},
100 {"BETWEEN", BETWEEN, 0},
103 {"CHANGE", CHANGE, 0},
105 {"CHARACTER", CHARACTER, 0},
106 {"CHECK", CHECK_, 0},
109 {"COMMIT", COMMIT, 0},
110 {"CONNECT", CONNECT, 1},
111 {"CONTINUE", CONTINUE_, 1},
113 {"CREATE", CREATE, 0},
114 {"CURRENT", CURRENT, 1},
115 {"CURSOR", CURSOR_, 1},
118 {"DECIMAL", NUMERIC, 0},
119 {"DECLARE", DECLARE, 1},
120 {"DEFAULT", DEFAULT, 0},
121 {"DELETE", DELETE_, 0},
123 {"DESCRIBE", DESCRIBE, 1},
124 {"DESCRIPTOR", DESCRIPTOR, 1},
125 {"DIFFERENCE", DIFFERENCE_, 0},
126 {"DISCONNECT", DISCONNECT, 1},
127 {"DISTINCT", DISTINCT, 0},
128 {"DOUBLE", DOUBLE_, 0},
131 {"ESCAPE", ESCAPE, 0},
132 {"EVALUATE", EVALUATE, 0},
133 {"EXCEPT", EXCEPT, 0},
134 {"EXCLUDE", EXCLUDE, 0},
135 {"EXECUTE", EXECUTE, 1},
136 {"EXISTS", EXISTS, 0},
139 {"FLOAT", FLOAT_, 0},
143 {"FUNCTION", FUNCTION_, 0},
148 {"GROUP", GROUP_, 0},
149 {"HAVING", HAVING, 0},
150 {"IDENTIFIED", IDENTIFIED, 1},
151 {"IMMEDIATE", IMMEDIATE, 1},
153 {"INCLUDE", INCLUDE, 1},
155 {"INDICATOR", INDICATOR, 1},
156 {"INHERIT", INHERIT, 0},
157 {"INSERT", INSERT, 0},
159 {"INTEGER", INTEGER, 0},
160 {"INTERSECTION", INTERSECTION, 0},
165 {"METHOD", METHOD, 0},
167 {"MULTISET_OF", MULTISET_OF, 0},
170 {"NUMBER", NUMERIC, 0},
171 {"NUMERIC", NUMERIC, 0},
172 {"OBJECT", OBJECT, 0},
178 {"OPTION", OPTION, 0},
181 {"PRECISION", PRECISION, 0},
182 {"PREPARE", PREPARE, 1},
183 {"PRIVILEGES", PRIVILEGES, 0},
184 {"PUBLIC", PUBLIC_, 0},
187 {"REGISTER", REGISTER_, 0},
188 {"RENAME", RENAME, 0},
189 {"REPEATED", REPEATED, 1},
190 {"REVOKE", REVOKE, 0},
191 {"ROLLBACK", ROLLBACK, 0},
192 {"SECTION", SECTION, 1},
193 {"SELECT", SELECT, 0},
194 {"SEQUENCE_OF", SEQUENCE_OF, 0},
197 {"SETNEQ", SETNEQ, 0},
198 {"SET_OF", SET_OF, 0},
199 {"SHARED", SHARED, 0},
200 {"SMALLINT", SMALLINT, 0},
204 {"SQLERROR", SQLERROR_, 1},
206 {"SQLWARNING", SQLWARNING_, 1},
207 {"STATISTICS", STATISTICS, 0},
209 {"STRING", STRING, 0},
210 {"SUBCLASS", SUBCLASS, 0},
211 {"SUBSET", SUBSET, 0},
212 {"SUBSETEQ", SUBSETEQ, 0},
214 {"SUPERCLASS", SUPERCLASS, 0},
215 {"SUPERSET", SUPERSET, 0},
216 {"SUPERSETEQ", SUPERSETEQ, 0},
219 {"TIMESTAMP", TIMESTAMP, 0},
221 {"TRIGGER", TRIGGER, 0},
222 {"UNION", UNION_, 0},
223 {"UNIQUE", UNIQUE, 0},
224 {"UPDATE", UPDATE, 0},
229 {"VALUES", VALUES, 0},
231 {"WHENEVER", WHENEVER, 1},
237 static KEYWORD_REC preprocessor_keywords[] = {
238 /* Make sure that they are in alphabetical order */
240 {"IDENTIFIED", IDENTIFIED, 0},
241 {"INDICATOR", INDICATOR, 1},
244 {"SELECT", SELECT, 0},
246 {"VALUES", VALUES, 0},
250 static KEYWORD_TABLE csql_table = { csql_keywords, DIM (csql_keywords) };
251 static KEYWORD_TABLE preprocessor_table = { preprocessor_keywords,
252 DIM (preprocessor_keywords) };
254 extern char g_delay[];
255 extern bool g_indicator;
256 extern bool need_line_directive;
258 enum scanner_mode esql_yy_mode (void);
259 char *mm_malloc (int size);
260 char *mm_strdup (char *str);
269 /* #define PARSER_DEBUG */
273 #define DBG_PRINT printf("rule: %d\n", __LINE__);
274 #define PARSER_NEW_NODE(a, b) __make_node(a, b, __LINE__)
275 #define PARSER_FREE_NODE(a, b) parser_free_node(a, b)
276 #define PRINT_(a) printf(a)
277 #define PRINT_1(a, b) printf(a, b)
282 #define PARSER_NEW_NODE(a, b) parser_new_node(a, b)
283 #define PARSER_FREE_NODE(a, b) parser_free_node(a, b)
285 #define PRINT_1(a, b)
289 #if !defined(yytext_ptr)
290 extern char *esql_yytext;
291 extern int esql_yylineno;
301 #include "cubrid_getopt.h"
302 #include "language_support.h"
303 #include "message_catalog.h"
304 #include "variable_string.h"
306 #include "esql_translate.h"
307 #include "memory_alloc.h"
308 #include "misc_string.h"
309 #include "esql_misc.h"
310 #include "environment_variable.h"
312 #include "esql_scanner_support.h"
313 #define ESQLMSG(x) pp_get_msg(EX_ESQLM_SET, (x))
316 #define CURSOR_DELETE 1
317 #define CURSOR_UPDATE 2
319 static SYMBOL *g_identifier_symbol;
320 static STRUCTDEF *g_sdef2;
322 static SYMBOL *g_psym;
323 static SYMBOL *g_struct_declaration_sym;
324 static SYMBOL *g_last;
325 static HOST_VAR *g_href;
326 static int g_cursor_type;
328 static bool repeat_option;
329 static PTR_VEC id_list;
330 static bool already_translated;
331 static bool structs_allowed;
332 static bool hvs_allowed;
336 static void reset_globals (void);
337 static void ignore_repeat (void);
338 static char *ofile_name (char *fname);
339 #if defined (ENABLE_UNUSED_FUNCTION)
340 static int validate_input_file (void *fname, FILE * outfp);
342 void pt_print_errors (PARSER_CONTEXT * parser_in);
344 extern PARSER_CONTEXT *parser;
346 varstring *esql_yy_get_buf (void);
347 void esql_yy_set_buf (varstring * vstr);
348 int esql_yylex(void);
351 extern varstring *g_varstring;
352 extern varstring *g_subscript;
358 %define parse.error verbose
372 %token<ptr> ATTRIBUTE
376 %token<ptr> BAD_TOKEN
377 %token<ptr> BOGUS_ECHO
385 %token<ptr> CHARACTER
392 %token<ptr> CONTINUE_
404 %token<ptr> DESCRIPTOR
405 %token<ptr> DIFFERENCE_
406 %token<ptr> DISCONNECT
423 %token<ptr> FILE_PATH_LIT
428 %token<ptr> FUNCTION_
429 %token<ptr> GENERIC_TOKEN
438 %token<ptr> IDENTIFIED
439 %token<ptr> IMMEDIATE
443 %token<ptr> INDICATOR
447 %token<ptr> INTERSECTION
452 %token<ptr> LDBVCLASS
461 %token<ptr> MONEY_LIT
462 %token<ptr> MULTISET_OF
477 %token<ptr> PRECISION
479 %token<ptr> PRIVILEGES
483 %token<ptr> READ_ONLY_
486 %token<ptr> REGISTER_
493 %token<ptr> SEQUENCE_OF
506 %token<ptr> SQLERROR_
507 %token<ptr> SQLWARNING_
509 %token<ptr> STATISTICS
517 %token<ptr> SUPERCLASS
519 %token<ptr> SUPERSETEQ
522 %token<ptr> TIMESTAMP
529 %token<ptr> UNSIGNED_
542 %token<ptr> VOLATILE_
548 %token<ptr> ENUMERATION_CONSTANT
549 %token<ptr> TYPEDEF_NAME
550 %token<ptr> STRING_LIT
551 %token<ptr> IDENTIFIER
558 %type<number> of_whenever_state
559 %type<number> opt_for_read_only
560 %type<number> csql_statement_head
561 %type<number> opt_varying
562 %type<number> of_struct_union
564 %type<ptr> opt_id_by_quasi
565 %type<ptr> opt_with_quasi
566 %type<ptr> dynamic_statement
567 %type<ptr> quasi_string_const
568 %type<ptr> host_variable_wo_indicator
570 %type<ptr> opt_specifier_list
571 %type<ptr> specifier_list
572 %type<ptr> optional_declarators
573 %type<ptr> struct_specifier
574 %type<ptr> opt_struct_specifier_body
575 %type<ptr> struct_specifier_body
576 %type<ptr> specifier_qualifier_list
577 %type<ptr> optional_struct_declarator_list
578 %type<ptr> struct_declarator_list
579 %type<ptr> struct_declarator
580 %type<ptr> init_declarator_list
581 %type<ptr> init_declarator
582 %type<ptr> declarator_
583 %type<ptr> direct_declarator
584 %type<ptr> param_spec
585 %type<ptr> param_type_list
586 %type<ptr> parameter_list
587 %type<ptr> trailing_params
588 %type<ptr> param_decl_tail_list
589 %type<ptr> parameter_declaration
590 %type<ptr> opt_abstract_declarator
591 %type<ptr> abstract_declarator
592 %type<ptr> direct_abstract_declarator
593 %type<ptr> opt_param_type_list
594 %type<ptr> identifier
595 %type<ptr> c_subscript
596 %type<ptr> indicator_var
597 %type<ptr> host_var_w_opt_indicator
600 %type<ptr> opt_host_ref_tail
601 %type<ptr> host_ref_tail
602 %type<ptr> host_var_subscript
603 %type<ptr> host_var_subscript_body
604 %type<ptr> cursor_query_statement
606 %type<ptr> descriptor_name
607 %type<ptr> struct_tag
608 %type<ptr> struct_declaration_list
609 %type<ptr> struct_declaration
610 %type<ptr> host_variable
617 opt_embedded_chunk_list
621 vs_free (&pt_statement_buf);
622 /* verify push/pop modes were matched. */
623 esql_yy_check_mode ();
628 opt_embedded_chunk_list
631 | embedded_chunk_list
636 : embedded_chunk_list embedded_chunk
650 pp_push_name_scope ();
653 opt_embedded_chunk_list
656 pp_pop_name_scope ();
669 : declare_section ';'
672 esql_yy_enter (ECHO_mode);
673 pp_suppress_echo (false);
679 esql_yy_enter (ECHO_mode);
680 pp_suppress_echo (false);
689 esql_yy_enter (CSQL_mode);
702 : BEGIN_ DECLARE SECTION ';'
705 esql_yy_enter (C_mode);
706 pp_suppress_echo (false);
711 exec sql END DECLARE SECTION
716 : opt_repeated embedded_statement
720 esql_yy_sync_lineno ();
721 esql_yy_set_buf (NULL);
724 | opt_repeated declare_cursor_statement
733 vs_clear (&pt_statement_buf);
734 esql_yy_set_buf (&pt_statement_buf);
745 repeat_option = false;
751 repeat_option = true;
761 | disconnect_statement
786 : WHENEVER whenever_action
812 : of_whenever_state CONTINUE_
815 pp_add_whenever_to_scope ($1, CONTINUE, NULL);
818 | of_whenever_state STOP_
821 pp_add_whenever_to_scope ($1, STOP, NULL);
824 | of_whenever_state GOTO_ opt_COLON IDENTIFIER
827 pp_add_whenever_to_scope ($1, GOTO, $4);
830 | of_whenever_state GO TO opt_COLON IDENTIFIER
833 pp_add_whenever_to_scope ($1, GOTO, $5);
836 | of_whenever_state CALL_ IDENTIFIER
839 pp_add_whenever_to_scope ($1, CALL, $3);
852 : CONNECT quasi_string_const opt_id_by_quasi
861 hvars = pp_input_refs ();
862 esql_Translate_table.tr_connect (CHECK_HOST_REF (hvars, 0),
863 CHECK_HOST_REF (hvars, 1),
864 CHECK_HOST_REF (hvars, 2));
865 already_translated = true;
877 | IDENTIFIED BY quasi_string_const opt_with_quasi
899 | WITH quasi_string_const
912 esql_Translate_table.tr_disconnect ();
913 already_translated = true;
918 declare_cursor_statement
919 : DECLARE IDENTIFIER CURSOR_ FOR cursor_query_statement
923 char *c_nam = strdup($2);
924 pp_new_cursor (c_nam, cp[0], (int) (long) cp[1], NULL,
925 pp_detach_host_refs ());
926 free_and_init(c_nam);
929 | DECLARE IDENTIFIER CURSOR_ FOR dynamic_statement
932 char *c_nam = strdup($2);
933 pp_new_cursor (c_nam, NULL, 0, (STMT *) $5, pp_detach_host_refs ());
934 free_and_init(c_nam);
935 esql_yy_sync_lineno ();
936 esql_yy_set_buf (NULL);
946 $$ = pp_new_stmt ($1);
953 : OPEN cursor opt_for_read_only using_part
960 if (cur->static_stmt)
962 if (pp_input_refs ())
963 esql_yyverror (pp_get_msg (EX_ESQLM_SET, MSG_USING_NOT_PERMITTED),
966 esql_Translate_table.tr_open_cs (cur->cid,
967 (const char *) cur->static_stmt,
971 HOST_N_REFS (cur->host_refs),
972 HOST_REFS (cur->host_refs),
973 (const char *) HOST_DESC (cur->
976 else if (cur->dynamic_stmt)
978 usg = pp_input_refs ();
979 esql_Translate_table.tr_open_cs (cur->cid,
982 cur->dynamic_stmt->sid,
984 HOST_N_REFS (usg), HOST_REFS (usg),
985 (const char *) HOST_DESC (usg));
989 esql_yyverror (pp_get_msg (EX_ESQLM_SET, MSG_CURSOR_UNDEFINED),
994 already_translated = true;
1017 | USING host_variable_lod
1022 : host_variable_wo_indicator
1025 HOST_REF *href = $1;
1027 if (pp_check_type (href,
1028 NEWSET (C_TYPE_SQLDA),
1029 pp_get_msg (EX_ESQLM_SET, MSG_PTR_TO_DESCR)))
1030 nam = pp_switch_to_descriptor ();
1043 esql_Translate_table.tr_close_cs (cur->cid);
1044 already_translated = true;
1054 if ((cur = pp_lookup_cursor ($1)) == NULL)
1055 esql_yyverror (pp_get_msg (EX_ESQLM_SET, MSG_CURSOR_UNDEFINED), $1);
1063 : DESCRIBE dynamic_statement INTO descriptor_name
1066 STMT *d_statement = $2;
1069 if (d_statement && nam)
1070 esql_Translate_table.tr_describe (d_statement->sid, nam);
1071 already_translated = true;
1074 | DESCRIBE OBJECT host_variable_wo_indicator ON_ id_list_ INTO descriptor_name
1077 HOST_REF *href = $3;
1081 pp_copy_host_refs ();
1083 if (href && pvec && nam &&
1084 pp_check_type (href,
1085 NEWSET (C_TYPE_OBJECTID),
1086 pp_get_msg (EX_ESQLM_SET, MSG_PTR_TO_DB_OBJECT)))
1088 esql_Translate_table.tr_object_describe (href,
1089 pp_ptr_vec_n_elems (pvec),
1091 pp_ptr_vec_elems (pvec), nam);
1094 pp_free_ptr_vec (pvec);
1095 already_translated = true;
1101 : PREPARE dynamic_statement FROM quasi_string_const
1104 HOST_REF *href = $4;
1105 STMT *d_statement = $2;
1107 if (d_statement && href)
1108 esql_Translate_table.tr_prepare_esql (d_statement->sid, href);
1110 already_translated = true;
1119 pp_gather_output_refs ();
1120 esql_yy_erase_last_token ();
1121 esql_yy_set_buf (NULL);
1127 pp_gather_input_refs ();
1138 : host_variable_list
1140 | DESCRIPTOR descriptor_name
1148 $$ = pp_add_host_str ($1);
1151 | host_variable_wo_indicator
1154 HOST_REF *href = $1;
1155 $$ = pp_check_type (href,
1156 NEWSET (C_TYPE_CHAR_ARRAY) |
1157 NEWSET (C_TYPE_CHAR_POINTER) |
1158 NEWSET (C_TYPE_STRING_CONST) |
1159 NEWSET (C_TYPE_VARCHAR) |
1160 NEWSET (C_TYPE_NCHAR) |
1161 NEWSET (C_TYPE_VARNCHAR),
1162 pp_get_msg (EX_ESQLM_SET, MSG_CHAR_STRING));
1171 esql_yy_erase_last_token ();
1172 structs_allowed = true;
1175 host_variable_list_tail
1178 pp_check_host_var_list ();
1184 host_variable_wo_indicator
1188 esql_yy_erase_last_token ();
1191 host_var_w_opt_indicator
1197 if (href2 && href2->ind)
1199 esql_yyverror (pp_get_msg (EX_ESQLM_SET, MSG_INDICATOR_NOT_ALLOWED),
1200 pp_get_expr (href2));
1201 pp_free_host_var (href2->ind);
1211 : id_list_ ',' IDENTIFIER
1214 PTR_VEC *pvec = NULL;
1215 pvec = pp_add_ptr (pvec, strdup ($3));
1223 pvec = pp_new_ptr_vec (&id_list);
1224 pp_add_ptr (&id_list, strdup ($1));
1232 : EXECUTE dynamic_statement using_part opt_into_result
1235 STMT *d_statement = $2;
1238 HOST_LOD *inputs, *outputs;
1239 inputs = pp_input_refs ();
1240 outputs = pp_output_refs ();
1241 esql_Translate_table.tr_execute (d_statement->sid,
1242 HOST_N_REFS (inputs),
1244 (const char *) HOST_DESC (inputs),
1245 HOST_N_REFS (outputs),
1246 HOST_REFS (outputs),
1247 (const char *) HOST_DESC (outputs));
1248 already_translated = true;
1252 | EXECUTE IMMEDIATE quasi_string_const
1255 HOST_REF *href = $3;
1258 esql_Translate_table.tr_execute_immediate (href);
1259 already_translated = true;
1270 : FETCH cursor into_result
1276 HOST_LOD *hlod = pp_output_refs ();
1277 esql_Translate_table.tr_fetch_cs (cur->cid,
1278 HOST_N_REFS (hlod), HOST_REFS (hlod),
1279 (char *) HOST_DESC (hlod));
1282 already_translated = true;
1285 | FETCH OBJECT host_variable_wo_indicator ON_ id_list_ into_result
1288 HOST_REF *href = $3;
1294 && (hlod = pp_output_refs ())
1295 && pp_check_type (href,
1298 pp_get_msg (EX_ESQLM_SET, MSG_PTR_TO_DB_OBJECT)))
1300 esql_Translate_table.tr_object_fetch (href,
1301 pp_ptr_vec_n_elems (pvec),
1303 pp_ptr_vec_elems (pvec),
1306 (const char *) HOST_DESC (hlod));
1309 pp_free_ptr_vec (pvec);
1310 already_translated = true;
1319 esql_Translate_table.tr_commit ();
1320 already_translated = true;
1334 esql_Translate_table.tr_rollback ();
1335 already_translated = true;
1344 ECHO_STR ($1, strlen ($1));
1351 ECHO_STR ($1, strlen ($1));
1358 ECHO_STR ($1, strlen ($1));
1365 ECHO_STR ($1, strlen ($1));
1372 ECHO_STR ($1, strlen ($1));
1379 ECHO_STR ($1, strlen ($1));
1386 ECHO_STR ($1, strlen ($1));
1393 ECHO_STR ($1, strlen ($1));
1400 ECHO_STR ($1, strlen ($1));
1407 ECHO_STR ($1, strlen ($1));
1414 ECHO_STR ($1, strlen ($1));
1421 ECHO_STR ($1, strlen ($1));
1428 ECHO_STR ($1, strlen ($1));
1435 ECHO_STR ($1, strlen ($1));
1442 ECHO_STR ($1, strlen ($1));
1449 ECHO_STR ($1, strlen ($1));
1456 ECHO_STR ($1, strlen ($1));
1463 ECHO_STR ($1, strlen ($1));
1470 ECHO_STR ($1, strlen ($1));
1477 ECHO_STR ($1, strlen ($1));
1484 ECHO_STR ($1, strlen ($1));
1491 ECHO_STR ($1, strlen ($1));
1501 esql_yy_set_buf (&pt_statement_buf);
1502 esql_yy_push_mode (BUFFER_mode);
1509 //esql_yy_push_mode(BUFFER_mode);
1519 declare_section_body
1533 : ext_decl_list external_declaration
1535 | external_declaration
1539 external_declaration
1543 pp_reset_current_type_spec ();
1546 opt_specifier_list optional_declarators
1554 pp_add_spec_to_decl (plink, psym);
1556 * If we have a VARCHAR declaration that didn't
1557 * come from a typedef, it has been suppressed
1558 * (i.e., it hasn't yet been echoed to the output
1559 * stream because it needs to be rewritten). Now
1560 * is the time to echo the rewritten decl out.
1562 * If the decl came from a typedef, it's already
1563 * been echoed. That's fine, because it doesn't
1564 * need to be rewritten in that case.
1566 if (IS_PSEUDO_TYPE (plink) && !plink->from_tdef)
1568 pp_print_decls (psym, true);
1569 esql_yy_sync_lineno ();
1572 * Don't do this until after printing, because
1573 * it's going to jack with the 'next' links in
1574 * the symbols, which will cause the printer to
1575 * repeat declarations.
1577 pp_add_symbols_to_table (psym);
1581 /* pp_print_specs(plink); */
1583 if (plink && !plink->tdef)
1584 pp_discard_link_chain (plink);
1594 $$ = pp_current_type_spec ();
1605 optional_declarators
1612 | init_declarator_list
1625 * The specifier for this declarator list may have
1626 * been a VARCHAR (or maybe not); if it was, echoing
1627 * has been suppressed since we saw that keyword.
1628 * Either way, we want to turn echoing back on.
1630 pp_suppress_echo (false);
1636 : specifier_list specifier_
1639 $$ = pp_current_type_spec ();
1645 $$ = pp_current_type_spec ();
1651 : storage_class_specifier
1659 storage_class_specifier
1663 pp_add_storage_class (TYPEDEF_);
1669 pp_add_storage_class (EXTERN_);
1675 pp_add_storage_class (STATIC_);
1681 pp_add_storage_class (AUTO_);
1687 pp_add_storage_class (REGISTER_);
1693 : nS_ntd base_type_specifier
1700 pp_add_struct_spec ($1);
1706 pp_add_type_noun (INT_);
1707 pp_add_type_adj (INT_);
1716 pp_add_type_noun (VOID_);
1722 pp_add_type_noun (CHAR_);
1728 pp_add_type_noun (INT_);
1734 pp_add_type_noun (FLOAT_);
1740 pp_add_type_noun (DOUBLE_);
1746 pp_add_typedefed_spec (g_identifier_symbol->type);
1753 * The VARCHAR_ token turns OFF echoing (the magic
1754 * happens in check_c_identifier()) because we're
1755 * going to have to rewrite the declaration. This is
1756 * slimy, but doing the right thing (parsing
1757 * everything and then regurgitating it) is too
1758 * expensive because we allow arbitrary initializers,
1759 * etc. Echoing will be re-enabled again when we see
1760 * the end of the declarator list following the
1761 * current specifier.
1763 pp_add_type_noun (VARCHAR_);
1769 pp_add_type_noun ($2);
1793 pp_add_type_adj (SHORT_);
1799 pp_add_type_adj (LONG_);
1805 pp_add_type_adj (SIGNED_);
1811 pp_add_type_adj (UNSIGNED_);
1820 pp_add_type_adj (CONST_);
1826 pp_add_type_adj (VOLATILE_);
1840 struct_specifier_body
1843 STRUCTDEF *sdef = $4;
1856 opt_struct_specifier_body
1859 STRUCTDEF *sdef = $5;
1861 sdef->by_name = true;
1882 opt_struct_specifier_body
1889 | struct_specifier_body
1897 struct_specifier_body
1902 pp_push_spec_scope ();
1905 struct_declaration_list
1908 pp_pop_spec_scope ();
1916 if (g_sdef2 == NULL)
1918 g_sdef2 = pp_new_structdef (NULL);
1919 pp_Struct_table->add_symbol (pp_Struct_table, g_sdef2);
1923 pp_Struct_table->remove_symbol (pp_Struct_table, g_sdef2);
1924 pp_discard_structdef (g_sdef2);
1926 else if (g_sdef2->fields)
1930 vs_sprintf (&gack, "%s %s", g_sdef2->type_string, g_sdef2->tag);
1931 esql_yyredef (vs_str (&gack));
1933 pp_discard_symbol_chain (psym);
1937 g_sdef2->type = g_su;
1938 g_sdef2->type_string = (unsigned char *) (g_su ? "struct" : "union");
1939 g_sdef2->fields = psym;
1957 (STRUCTDEF *) pp_Struct_table->find_symbol (pp_Struct_table, &dummy)))
1959 sdef = pp_new_structdef (strdup($1));
1960 pp_Struct_table->add_symbol (pp_Struct_table, sdef);
1968 struct_declaration_list
1969 : struct_declaration_list struct_declaration
1972 SYMBOL *last_field = $1;
1975 SYMBOL *tmp = last_field->next;
1976 last_field->next = $2;
1977 last_field->next->next = tmp;
1983 | struct_declaration
1992 : specifier_qualifier_list optional_struct_declarator_list
2001 pp_add_spec_to_decl (plink, psym);
2003 * Same deal as with an upper-level decl: this
2004 * may have been a VARCHAR decl that needs to be
2005 * rewritten. If so, it has not yet been echoed;
2008 if (IS_PSEUDO_TYPE (plink) && !plink->from_tdef)
2010 pp_print_decls (psym, true);
2011 esql_yy_sync_lineno ();
2019 pp_discard_link_chain (plink);
2021 g_struct_declaration_sym = sym;
2027 $$ = g_struct_declaration_sym;
2032 specifier_qualifier_list
2036 pp_reset_current_type_spec ();
2037 pp_disallow_storage_classes ();
2048 optional_struct_declarator_list
2055 | struct_declarator_list
2063 struct_declarator_list
2064 : struct_declarator_list ',' struct_declarator
2088 esql_yy_push_mode (EXPR_mode);
2094 SYMBOL *psym = NULL;
2102 esql_yy_push_mode (EXPR_mode);
2120 : ENUM '{' enumerator_list '}'
2121 | ENUM identifier optional_enumerator_list
2127 esql_yyredef ((char *) dl->name);
2131 pp_discard_symbol (dl);
2137 optional_enumerator_list
2140 | '{' enumerator_list '}'
2145 : enumerator_list ',' enumerator
2163 esql_yy_push_mode (EXPR_mode);
2175 init_declarator_list
2176 : init_declarator_list ',' init_declarator
2213 esql_yy_push_mode (EXPR_mode);
2220 pp_add_initializer (psym);
2234 | pointer declarator_
2239 pp_add_declarator (psym, D_POINTER);
2249 SYMBOL *psym = pp_new_symbol ($1, pp_nesting_level);
2259 | '(' declarator_ ')'
2262 SYMBOL *psym = pp_new_symbol ($2, pp_nesting_level);
2282 : param_spec_list param_spec
2288 pp_add_declarator (g_psym, D_FUNCTION);
2289 g_psym->etype->decl.d.args = args;
2293 | param_spec_list c_subscript
2296 const char *sub = $2;
2299 pp_add_declarator (g_psym, D_ARRAY);
2300 g_psym->etype->decl.d.num_ele = (char *) sub;
2310 pp_add_declarator (g_psym, D_FUNCTION);
2311 g_psym->etype->decl.d.args = args;
2318 const char *sub = $1;
2321 pp_add_declarator (g_psym, D_ARRAY);
2322 g_psym->etype->decl.d.num_ele = (char *) sub;
2329 : '*' opt_type_qualifier_list
2333 opt_type_qualifier_list
2336 | type_qualifier_list
2341 : type_qualifier_list type_qualifier
2354 | '(' push_name_scope param_type_list pop_name_scope ')'
2360 | '(' push_name_scope id_list_ pop_name_scope ')'
2372 pp_push_name_scope ();
2381 pp_pop_name_scope ();
2390 pp_push_spec_scope ();
2399 pp_pop_spec_scope ();
2405 : push_spec_scope parameter_list pop_spec_scope
2414 : parameter_declaration trailing_params
2422 | parameter_declaration
2431 : param_decl_tail_list
2445 param_decl_tail_list
2446 : param_decl_tail_list ',' parameter_declaration
2457 | ',' parameter_declaration
2465 parameter_declaration
2469 pp_reset_current_type_spec ();
2473 opt_abstract_declarator
2481 psym = pp_new_symbol (NULL, pp_nesting_level);
2483 pp_add_spec_to_decl (plink, psym);
2484 if (plink && !plink->tdef)
2485 pp_discard_link_chain (plink);
2492 opt_abstract_declarator
2499 | abstract_declarator
2508 : pointer opt_abstract_declarator
2514 psym = pp_new_symbol (NULL, pp_nesting_level);
2516 pp_add_declarator (psym, D_POINTER);
2519 | direct_abstract_declarator
2527 direct_abstract_declarator
2528 : '(' abstract_declarator ')'
2534 opt_direct_adecl_tail
2540 | '(' opt_param_type_list ')'
2544 g_psym = pp_new_symbol (NULL, pp_nesting_level);
2545 pp_add_declarator (g_psym, D_FUNCTION);
2546 g_psym->etype->decl.d.args = args;
2549 opt_direct_adecl_tail
2558 g_psym = pp_new_symbol ($1, pp_nesting_level);
2561 opt_direct_adecl_tail
2570 const char *sub = $1;
2571 g_psym = pp_new_symbol (NULL, pp_nesting_level);
2572 pp_add_declarator (g_psym, D_ARRAY);
2573 g_psym->etype->decl.d.num_ele = (char *) sub;
2576 opt_direct_adecl_tail
2584 opt_direct_adecl_tail
2590 : direct_adecl_tail c_subscript
2593 const char *sub = $2;
2594 pp_add_declarator (g_psym, D_ARRAY);
2595 g_psym->etype->decl.d.num_ele = (char *) sub;
2598 | direct_adecl_tail '(' opt_param_type_list ')'
2602 pp_add_declarator (g_psym, D_FUNCTION);
2603 g_psym->etype->decl.d.args = args;
2609 const char *sub = $1;
2610 pp_add_declarator (g_psym, D_ARRAY);
2611 g_psym->etype->decl.d.num_ele = (char *) sub;
2614 | '(' opt_param_type_list ')'
2618 pp_add_declarator (g_psym, D_FUNCTION);
2619 g_psym->etype->decl.d.args = args;
2643 pp_make_typedef_names_visible (0);
2652 pp_make_typedef_names_visible (1);
2658 : nS_ntd TYPEDEF_NAME nS_td
2661 SYMBOL *result = pp_new_symbol ($2, pp_nesting_level);
2669 if (!g_identifier_symbol || g_identifier_symbol->level != pp_nesting_level)
2670 result = pp_new_symbol ($1, pp_nesting_level);
2672 result = g_identifier_symbol;
2677 | ENUMERATION_CONSTANT
2680 SYMBOL *result = pp_new_symbol ($1, pp_nesting_level);
2690 varstring *subscript_buf = malloc (sizeof (varstring));
2692 esql_yy_push_mode (EXPR_mode);
2693 if (IS_PSEUDO_TYPE (pp_current_type_spec ()))
2696 * There is no need to store the old values of the
2697 * echo function and echo suppression here because
2698 * we're going to pop right ought of this mode and
2699 * clobber them anyway.
2701 vs_new (subscript_buf);
2702 (void) esql_yy_set_buf (subscript_buf);
2703 (void) pp_set_echo (&echo_vstr);
2704 pp_suppress_echo (false);
2707 g_subscript = subscript_buf;
2714 char *subscript = NULL;
2715 varstring *subscript_buf = g_subscript;
2717 if (IS_PSEUDO_TYPE (pp_current_type_spec ()))
2720 * Have to clobber the RB here, since the lookahead
2721 * has already put it in our buffer. Fortunately,
2722 * we're in a mode where we can do that.
2724 esql_yy_erase_last_token ();
2725 esql_yy_set_buf (NULL);
2726 subscript = pp_strdup (vs_str (subscript_buf));
2727 vs_free (subscript_buf);
2730 esql_yy_pop_mode ();
2738 //#lexclass EXPR_mode
2752 esql_yy_pop_mode ();
2777 : acs_list of_any_comma_semi
2804 //#lexclass VAR_mode
2806 host_variable_list_tail
2807 : host_variable_list_tail ',' host_var_w_opt_indicator
2810 ECHO_STR (", ", strlen (", "));
2813 | host_var_w_opt_indicator
2822 g_indicator = false;
2829 g_indicator = false;
2832 | INDICATOR '#' hostvar
2836 g_indicator = false;
2843 g_indicator = false;
2849 host_var_w_opt_indicator
2853 HOST_REF *href = NULL;
2855 g_varstring = esql_yy_get_buf ();
2857 esql_yy_push_mode (VAR_mode);
2858 esql_yy_set_buf (NULL);
2859 structs_allowed = true;
2862 host_variable indicator_var
2866 HOST_VAR *hvar1, *hvar2;
2870 esql_yy_pop_mode ();
2871 esql_yy_set_buf (g_varstring);
2873 href = pp_add_host_ref (hvar1, hvar2, structs_allowed, &n_refs);
2874 ECHO_STR ("? ", strlen ("? "));
2875 for (i = 1; i < n_refs; i++)
2876 ECHO_STR (", ? ", strlen (", ? "));
2878 ECHO_STR (g_delay, strlen (g_delay));
2907 | '*' CONST_ hostvar
2910 HOST_VAR *hvar = NULL;
2911 HOST_VAR *hvar2 = $3;
2912 if ((hvar = pp_ptr_deref (hvar2, 0)) == NULL)
2913 pp_free_host_var (hvar2);
2915 vs_prepend (&hvar->expr, "*");
2920 | '*' VOLATILE_ hostvar
2923 HOST_VAR *hvar = NULL;
2924 HOST_VAR *hvar2 = $3;
2925 if ((hvar = pp_ptr_deref (hvar2, 0)) == NULL)
2926 pp_free_host_var (hvar2);
2928 vs_prepend (&hvar->expr, "*");
2936 HOST_VAR *hvar = NULL;
2937 HOST_VAR *hvar2 = $2;
2938 if ((hvar = pp_ptr_deref (hvar2, 0)) == NULL)
2939 pp_free_host_var (hvar2);
2941 vs_prepend (&hvar->expr, "*");
2950 HOST_VAR *href2 = $2;
2952 if ((hvar = pp_addr_of (href2)) == NULL)
2953 pp_free_host_var (href2);
2955 vs_prepend (&hvar->expr, "&");
2966 HOST_VAR *href = NULL;
2967 SYMBOL *sym = pp_findsym (pp_Symbol_table, $1);
2970 esql_yyverror (pp_get_msg (EX_ESQLM_SET, MSG_NOT_DECLARED), $1);
2974 href = pp_new_host_var (NULL, sym);
2982 HOST_VAR *href = $3;
2991 HOST_VAR *href = $2;
2994 vs_prepend (&href->expr, "(");
2995 vs_strcat (&href->expr, ")");
3003 HOST_VAR *href = $5;
3019 | host_ref_tail %dprec 2
3028 : host_ref_tail host_var_subscript
3034 | host_ref_tail '.' IDENTIFIER
3038 if ((href = pp_struct_deref (g_href, $3, 0)) == NULL)
3039 pp_free_host_var (g_href);
3041 vs_sprintf (&href->expr, ".%s", $3);
3046 | host_ref_tail PTR_OP IDENTIFIER
3050 if ((href = pp_struct_deref (g_href, $3, 1)) == NULL)
3051 pp_free_host_var (g_href);
3053 vs_sprintf (&href->expr, "->%s", $3);
3058 | host_var_subscript
3068 if ((href = pp_struct_deref (g_href, $2, 0)) == NULL)
3069 pp_free_host_var (g_href);
3071 vs_sprintf (&href->expr, ".%s", $2);
3080 if ((href = pp_struct_deref (g_href, $2, 1)) == NULL)
3081 pp_free_host_var (g_href);
3083 vs_sprintf (&href->expr, "->%s", $2);
3094 vs_clear (&pp_subscript_buf);
3095 esql_yy_push_mode (HV_mode);
3098 host_var_subscript_body
3107 host_var_subscript_body
3108 : opt_hv_sub_list ']'
3113 esql_yy_pop_mode ();
3114 if ((href = pp_ptr_deref (g_href, 1)) == NULL)
3115 pp_free_host_var (g_href);
3117 vs_sprintf (&href->expr, "[%s%s", vs_str (&pp_subscript_buf), "]");
3129 vs_strcat (&pp_subscript_buf, "(");
3136 vs_strcat (&pp_subscript_buf, ")");
3142 vs_strcat (&pp_subscript_buf, "[");
3149 vs_strcat (&pp_subscript_buf, "]");
3155 vs_strcat (&pp_subscript_buf, "{");
3162 vs_strcat (&pp_subscript_buf, "}");
3175 : hv_sub_list hv_sub
3187 : opt_buffer_sql_list
3190 PT_NODE **pt, *cursr;
3191 char *statement, *text;
3192 PT_HOST_VARS *result;
3194 HOST_LOD *inputs, *outputs;
3196 PARSER_VARCHAR *varPtr;
3197 esql_yy_pop_mode ();
3198 statement = vs_str (&pt_statement_buf);
3199 length = (int) vs_strlen (&pt_statement_buf);
3201 PRINT_1 ("statement: %s\n", statement);
3202 pt = parser_parse_string (parser, statement);
3206 pt_print_errors (parser);
3211 is_upd_obj = pt_is_update_object (pt[0]);
3212 result = pt_host_info (parser, pt[0]);
3213 inputs = pp_input_refs ();
3214 outputs = pp_output_refs ();
3218 if (HOST_N_REFS (inputs) > 0
3219 && pp_check_type (&(HOST_REFS (inputs))[0],
3220 NEWSET (C_TYPE_OBJECTID),
3221 pp_get_msg (EX_ESQLM_SET,
3222 MSG_PTR_TO_DB_OBJECT)))
3223 esql_Translate_table.tr_object_update (text,
3226 HOST_N_REFS (inputs),
3227 HOST_REFS (inputs));
3229 else if ((cursr = pt_get_cursor (result)) != NULL)
3233 c_nam = pt_get_name (cursr);
3234 if ((cur = pp_lookup_cursor ((char *) c_nam)) == NULL)
3236 esql_yyverror (pp_get_msg (EX_ESQLM_SET, MSG_CURSOR_UNDEFINED),
3241 switch (g_cursor_type)
3244 esql_Translate_table.tr_delete_cs (cur->cid);
3247 pt_set_update_object (parser, pt[0]);
3248 varPtr = pt_print_bytes (parser, pt[0]);
3249 esql_Translate_table.tr_update_cs (cur->cid,
3251 pt_get_varchar_bytes
3254 pt_get_varchar_length
3255 (varPtr), repeat_option,
3256 HOST_N_REFS (inputs),
3257 HOST_REFS (inputs));
3266 repeat_option = (pt[0]->node_type == PT_INSERT && !pt[0]->info.insert.do_replace && pt[0]->info.insert.odku_assignments == NULL
3267 && pt[0]->info.insert.value_clauses->info.node_list.list_type != PT_IS_SUBQUERY
3268 && pt[0]->info.insert.value_clauses->info.node_list.list->next == NULL);
3269 esql_Translate_table.tr_static (text,
3272 HOST_N_REFS (inputs),
3274 (const char *) HOST_DESC (inputs),
3275 HOST_N_REFS (outputs),
3276 HOST_REFS (outputs),
3277 (const char *) HOST_DESC (outputs));
3279 pt_free_host_info (result);
3282 esql_yy_sync_lineno ();
3283 esql_yy_set_buf (NULL);
3296 : buffer_sql_list buffer_sql
3303 : host_var_w_opt_indicator
3305 | DESCRIPTOR host_variable_wo_indicator
3308 HOST_REF *href = $2;
3309 if (pp_check_type (href,
3310 NEWSET (C_TYPE_SQLDA),
3311 pp_get_msg (EX_ESQLM_SET, MSG_PTR_TO_DESCR)))
3312 pp_switch_to_descriptor ();
3315 | var_mode_follow_set
3339 pp_gather_output_refs ();
3345 pp_gather_output_refs ();
3351 pp_gather_input_refs ();
3357 pp_gather_input_refs ();
3363 pp_gather_input_refs ();
3372 cursor_query_statement
3376 vs_clear (&pt_statement_buf);
3377 esql_yy_set_buf (&pt_statement_buf);
3378 esql_yy_push_mode (BUFFER_mode);
3381 of_select_lp opt_buffer_sql_list
3388 char **cp = malloc (sizeof (char *) * 2);
3390 esql_yy_pop_mode ();
3391 statement = vs_str (&pt_statement_buf);
3392 length = vs_strlen (&pt_statement_buf);
3394 PRINT_1 ("statement: %s\n", statement);
3395 pt = parser_parse_string (parser, statement);
3398 pt_print_errors (parser);
3404 cp[1] = (char *) length;
3406 esql_yy_sync_lineno ();
3407 esql_yy_set_buf (NULL);
3419 ECHO_STR ($1, strlen ($1));
3425 ECHO_STR ("(", strlen ("("));
3431 //#lexclass COMMENT_mode
3437 #define UCI_OPT_UNSAFE_NULL 0x0001
3438 #define makestring1(x) #x
3439 #define makestring(x) makestring1(x)
3440 #if !defined(PRODUCT_STRING)
3441 #define PRODUCT_STRING makestring(RELEASE_STRING)
3444 #define ESQL_MSG_CATALOG "esql.cat"
3446 char exec_echo[10] = "";
3447 static char *outfile_name = NULL;
3448 bool need_line_directive;
3449 FILE *esql_yyin, *esql_yyout;
3450 char *esql_yyfilename;
3451 int esql_yyendcol = 0;
3453 varstring *current_buf; /* remain PUBLIC for debugging ease */
3454 ECHO_FN echo_fn = &echo_stream;
3455 char g_delay[1024] = { 0, };
3456 bool g_indicator = false;
3457 varstring *g_varstring = NULL;
3458 varstring *g_subscript = NULL;
3460 typedef struct pt_host_refs
3462 unsigned char *descr[2]; /* at most 2 descriptors: 1 in, 1 out */
3463 int descr_occupied; /* occupied slots in descr */
3464 HOST_REF **host_refs; /* array of pointers to HOST_REF */
3465 int occupied; /* occupied slots in host_refs */
3466 int allocated; /* allocated slots in host_refs */
3467 unsigned char *in_descriptor; /* NULL or input descriptor name */
3468 unsigned char *out_descriptor; /* NULL or output descriptor name */
3469 HOST_REF **in_refs; /* array of input HOST_REF addresses */
3470 int in_refs_occupied;
3471 int in_refs_allocated;
3472 HOST_REF **out_refs; /* array of output HOST_REF addresses */
3473 int out_refs_occupied;
3474 int out_refs_allocated;
3479 MSG_REPEATED_IGNORED = 1,
3483 const char *VARCHAR_ARRAY_NAME = "array";
3484 const char *VARCHAR_LENGTH_NAME = "length";
3485 const char *pp_include_path;
3486 char *pp_include_file = NULL;
3487 const char *prog_name;
3489 unsigned int pp_uci_opt = 0;
3490 int pp_emit_line_directives = 1;
3491 int pp_dump_scope_info = 0;
3492 int pp_dump_malloc_info = 0;
3493 int pp_announce_version = 0;
3494 int pp_enable_uci_trace = 0;
3495 int pp_disable_varchar_length = 0;
3496 int pp_varchar2 = 0;
3497 int pp_unsafe_null = 0;
3498 int pp_internal_ind = 0;
3499 PARSER_CONTEXT *parser;
3501 varstring pt_statement_buf;
3526 check_w_file (int cond, const char *filename)
3530 char buf[2 * PATH_MAX];
3531 sprintf (buf, "%s: %s", prog_name, filename);
3542 reset_globals (void)
3546 repeat_option = false;
3547 already_translated = false;
3548 structs_allowed = false;
3551 esql_yy_set_buf (NULL);
3552 pp_clear_host_refs ();
3553 pp_gather_input_refs ();
3561 ignore_repeat (void)
3565 esql_yyvwarn (pp_get_msg (EX_ESQLMMAIN_SET, MSG_REPEATED_IGNORED));
3570 #if defined (ENABLE_UNUSED_FUNCTION)
3572 validate_input_file (void *fname, FILE * outfp)
3574 return (fname == NULL) && isatty (fileno (stdin));
3576 #endif /* ENABLE_UNUSED_FUNCTION */
3580 ofile_name (char *fname)
3582 static char buf[PATH_MAX + 1]; /* reserve buffer for '\0' */
3585 /* Get the last pathname component into buf. */
3586 p = strrchr (fname, '/');
3587 strncpy (buf, p ? (p + 1) : fname, sizeof(buf));
3589 /* Now add the .c suffix, copying over the .ec suffix if present. */
3590 p = strrchr (buf, '.');
3591 if (p && !STREQ (p, ".c"))
3597 strncat (buf, ".c", PATH_MAX - strnlen(buf, sizeof(buf)));
3610 copy (FILE * fp, const char *fname)
3619 ofd = open (fname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
3620 check_w_file (ofd >= 0, fname);
3622 /* Now copy the contents; use read() and write() to try to cut down
3623 on excess buffering. */
3626 while ((rbytes = read (ifd, buf, sizeof (buf))) > 0)
3628 wbytes = write (ofd, buf, rbytes);
3629 if (wbytes != rbytes)
3634 check (rbytes >= 0 && wbytes >= 0);
3641 pp_unsafe_get_msg (int msg_set, int msg_num)
3643 static int complained = 0;
3644 static MSG_CATD msg_cat = NULL;
3648 if (msg_cat == NULL)
3655 msg_cat = msgcat_open (ESQL_MSG_CATALOG);
3656 if (msg_cat == NULL)
3659 fprintf (stderr, "%s: %s: %s\n",
3660 prog_name, ESQL_MSG_CATALOG, strerror (ENOENT));
3665 msg = msgcat_gets (msg_cat, msg_set, msg_num, NULL);
3667 return (msg == NULL || msg[0] == '\0') ? NULL : msg;
3677 pp_get_msg (int msg_set, int msg_num)
3679 static const char no_msg[] = "No message available.";
3682 msg = pp_unsafe_get_msg (msg_set, msg_num);
3683 return msg ? msg : no_msg;
3691 "usage: cubrid_esql [OPTION] input-file\n\n"
3693 " -l, --suppress-line-directive suppress #line directives in output file; default: off\n"
3694 " -o, --output-file output file name; default: stdout\n"
3695 " -h, --include-file include file containing uci_ prototypes; default: cubrid_esql.h\n"
3696 " -s, --dump-scope-info dump scope debugging information; default: off\n"
3697 " -m, --dump-malloc-info dump final malloc debugging information; default: off\n"
3698 " -t, --enable-uci-trace enable UCI function trace; default: off\n"
3699 " -d, --disable-varchar-length disable length initialization of VARCHAR host variable; default; off\n"
3700 " -2, --varchar2-style use different VARCHAR style; default: off\n"
3701 " -u, --unsafe-null ignore -462(null indicator needed) error: default; off\n"
3702 " -e, --internal-indicator use internal NULL indicator to prevent -462(null indicator needed) error, default off\n");
3708 return fgetc (esql_yyin);
3718 main (int argc, char **argv)
3720 struct option esql_option[] = {
3721 {"suppress-line-directive", 0, 0, 'l'},
3722 {"output-file", 1, 0, 'o'},
3723 {"include-file", 1, 0, 'h'},
3724 {"dump-scope-info", 0, 0, 's'},
3725 {"dump-malloc-info", 0, 0, 'm'},
3726 {"version", 0, 0, 'v'},
3727 {"enable-uci-trace", 0, 0, 't'},
3728 {"disable-varchar-length", 0, 0, 'd'},
3729 {"varchar2-style", 0, 0, '2'},
3730 {"unsafe-null", 0, 0, 'u'},
3731 {"internal-indicator", 0, 0, 'e'},
3737 int option_index = 0;
3740 option_key = getopt_long (argc, argv, "lo:h:smvtd2ue",
3741 esql_option, &option_index);
3742 if (option_key == -1)
3750 pp_emit_line_directives = true;
3753 if (outfile_name != NULL)
3755 free_and_init(outfile_name);
3757 outfile_name = strdup (optarg);
3760 if (pp_include_file != NULL)
3762 free_and_init(pp_include_file);
3764 pp_include_file = strdup (optarg);
3767 pp_dump_scope_info = true;
3770 pp_dump_malloc_info = true;
3773 pp_announce_version = true;
3776 pp_enable_uci_trace = true;
3779 pp_disable_varchar_length = true;
3785 pp_unsafe_null = true;
3788 pp_internal_ind = true;
3798 esql_yyfilename = argv[optind];
3806 prog_name = argv[0];
3807 if (pp_announce_version)
3809 printf (pp_get_msg (EX_ESQLMMAIN_SET, MSG_VERSION), argv[0], PRODUCT_STRING);
3814 VARCHAR_ARRAY_NAME = "arr";
3815 VARCHAR_LENGTH_NAME = "len";
3820 pp_uci_opt |= UCI_OPT_UNSAFE_NULL;
3823 pp_include_path = envvar_root ();
3824 if (pp_include_path == NULL)
3829 if (esql_yyfilename == NULL)
3831 /* No input filename was supplied; use stdin for input and stdout
3837 esql_yyin = fopen (esql_yyfilename, "r");
3838 check_w_file (esql_yyin != NULL, esql_yyfilename);
3841 if (esql_yyfilename && !outfile_name)
3843 outfile_name = ofile_name (esql_yyfilename);
3848 esql_yyout = tmpfile ();
3849 check (esql_yyout != NULL);
3853 esql_yyout = stdout;
3858 vs_new (&pt_statement_buf);
3859 emit_line_directive ();
3861 if (utility_initialize () != NO_ERROR)
3866 if (lang_init () != NO_ERROR)
3871 lang_set_charset_lang ("en_US.iso88591");
3873 parser = parser_create_parser ();
3874 pt_buffer = pt_append_string (parser, NULL, NULL);
3878 if (esql_yyout != stdout)
3883 * We want to keep the file, so rewind the temp file and copy
3884 * it to the named file. It really would be nice if we could
3885 * just associate the name with the file resources we've
3886 * already created, but there doesn't seem to be a way to do
3889 copy (esql_yyout, outfile_name);
3893 if (esql_yyin != NULL)
3898 if (esql_yyout != NULL)
3900 fclose (esql_yyout);
3906 free_and_init (outfile_name);
3907 free_and_init (pp_include_file);
3913 * pt_print_errors() - print to stderr all parsing error messages associated
3914 * with current statement
3916 * parser_in(in) : the parser context
3919 pt_print_errors (PARSER_CONTEXT * parser_in)
3921 int statement_no, line_no, col_no;
3922 const char *msg = NULL;
3923 PT_NODE *err = NULL;
3925 err = pt_get_errors (parser);
3928 err = pt_get_next_error (err, &statement_no, &line_no, &col_no, &msg);
3930 esql_yyverror (msg);
3934 /* sleazy way to clear errors */
3935 parser = parser_create_parser ();
3940 * pp_suppress_echo() -
3945 pp_suppress_echo (int suppress)
3947 suppress_echo = suppress;
3957 echo_stream (const char *str, int length)
3963 fputs (exec_echo, esql_yyout);
3966 fwrite ((char *) str, length, 1, esql_yyout);
3967 //YY_FLUSH_ON_DEBUG;
3978 echo_vstr (const char *str, int length)
3985 vs_strcatn (current_buf, str, length);
3990 #if defined (ENABLE_UNUSED_FUNCTION)
3998 echo_devnull (const char *str, int length)
4002 #endif /* ENABLE_UNUSED_FUNCTION */
4010 pp_set_echo (ECHO_FN new_echo)
4028 esql_yyvmsg (const char *fmt, va_list args)
4030 /* Flush esql_yyout just in case stderr and esql_yyout are the same. */
4031 fflush (esql_yyout);
4033 if (esql_yyfilename)
4035 fprintf (stderr, "%s:", esql_yyfilename);
4038 fprintf (stderr, "%d: ", esql_yylineno);
4039 vfprintf (stderr, fmt, args);
4040 fputs ("\n", stderr);
4050 esql_yyerror (const char *s)
4052 esql_yyverror ("%s before \"%s\"", s, esql_yytext);
4061 esql_yyverror (const char *fmt, ...)
4067 va_start (args, fmt);
4068 esql_yyvmsg (fmt, args);
4078 esql_yyvwarn (const char *fmt, ...)
4082 va_start (args, fmt);
4083 esql_yyvmsg (fmt, args);
4093 esql_yyredef (char *name)
4095 /* Eventually we will probably want to turn this off, but it's
4096 interesting to find out about perceived redefinitions right now. */
4097 esql_yyverror ("redefinition of symbol \"%s\"", name);
4110 keyword_cmp (const void *a, const void *b)
4112 return strcmp (((const KEYWORD_REC *) a)->keyword,
4113 ((const KEYWORD_REC *) b)->keyword);
4117 * keyword_case_cmp() -
4123 keyword_case_cmp (const void *a, const void *b)
4125 return intl_mbs_casecmp (((const KEYWORD_REC *) a)->keyword,
4126 ((const KEYWORD_REC *) b)->keyword);
4134 * check_c_identifier() -
4138 check_c_identifier (char *name)
4145 printf ("check c id: %s\n", name);
4149 * Check first to see if we have a keyword.
4151 dummy.keyword = name;
4152 p = (KEYWORD_REC *) bsearch (&dummy,
4154 sizeof (c_keywords) / sizeof (c_keywords[0]),
4155 sizeof (KEYWORD_REC), &keyword_cmp);
4160 * Notic that this is "sticky", unlike in check_identifier().
4161 * Once we've seen a keyword that initiates suppression, we don't
4162 * want anything echoed until the upper level grammar productions
4163 * say so. In particular, we don't want echoing to be re-enabled
4164 * the very next time we see an ordinary C keyword.
4166 suppress_echo = suppress_echo || p->suppress_echo;
4171 * If not, see if this is an already-encountered identifier.
4173 g_identifier_symbol = sym =
4174 pp_findsym (pp_Symbol_table, (unsigned char *) name);
4178 * Something by this name lives in the symbol table.
4180 * This needs to be made sensitive to whether typedef names are
4181 * being recognized or not.
4183 return sym->type->tdef && pp_recognizing_typedef_names ? TYPEDEF_NAME :
4184 IS_INT_CONSTANT (sym->type) ? ENUMERATION_CONSTANT : IDENTIFIER;
4189 * Symbol wasn't recognized in the symbol table, so this must be
4199 * check_identifier() -
4204 check_identifier (KEYWORD_TABLE * keywords, char *name)
4210 * Check first to see if we have a keyword.
4212 dummy.keyword = name;
4213 p = (KEYWORD_REC *) bsearch (&dummy,
4214 keywords->keyword_array,
4216 sizeof (KEYWORD_REC), &keyword_case_cmp);
4220 suppress_echo = p->suppress_echo;
4225 suppress_echo = false;
4231 * esql_yy_push_mode() -
4236 esql_yy_push_mode (enum scanner_mode new_mode)
4238 SCANNER_MODE_RECORD *next = malloc (sizeof (SCANNER_MODE_RECORD));
4241 esql_yyverror (pp_get_msg (EX_MISC_SET, MSG_OUT_OF_MEMORY));
4246 next->previous_mode = mode;
4247 next->recognize_keywords = recognize_keywords;
4248 next->previous_record = mode_stack;
4249 next->suppress_echo = suppress_echo;
4253 * Set up so that the first id-like token that we see in VAR_MODE
4254 * will be reported as an IDENTIFIER, regardless of whether it
4255 * strcasecmps the same as some CSQL keyword.
4257 recognize_keywords = (new_mode != VAR_MODE);
4259 esql_yy_enter (new_mode);
4263 * esql_yy_check_mode() -
4267 esql_yy_check_mode (void)
4269 if (mode_stack != NULL)
4272 ("(esql_yy_check_mode) internal error: mode stack still full");
4278 * esql_yy_pop_mode() -
4282 esql_yy_pop_mode (void)
4284 SCANNER_MODE_RECORD *prev;
4285 enum scanner_mode new_mode;
4287 if (mode_stack == NULL)
4289 esql_yyverror (pp_get_msg (EX_ESQLMSCANSUPPORT_SET, MSG_EMPTY_STACK));
4293 prev = mode_stack->previous_record;
4294 new_mode = mode_stack->previous_mode;
4295 recognize_keywords = mode_stack->recognize_keywords;
4296 suppress_echo = mode_stack->suppress_echo;
4298 free_and_init (mode_stack);
4301 esql_yy_enter (new_mode);
4320 esql_yy_enter (enum scanner_mode new_mode)
4326 void (*echo_fn) (const char *, int);
4331 "echo", START, &echo_stream}, /* ECHO_MODE 0 */
4333 "csql", CSQL_mode, &echo_vstr}, /* CSQL_MODE 1 */
4335 "C", C_mode, &echo_stream}, /* C_MODE 2 */
4337 "expr", EXPR_mode, &echo_stream}, /* EXPR_MODE 3 */
4339 "var", VAR_mode, &echo_vstr}, /* VAR_MODE 4 */
4341 "hostvar", HV_mode, &echo_vstr}, /* HV_MODE 5 */
4343 "buffer", BUFFER_mode, &echo_vstr}, /* BUFFER_MODE */
4345 "comment", COMMENT_mode, NULL} /* COMMENT */
4349 PRINT_1 ("#set mode : %s\n", mode_info[new_mode].name);
4351 if (mode_info[new_mode].echo_fn)
4352 echo_fn = mode_info[new_mode].echo_fn;
4356 * emit_line_directive() -
4360 emit_line_directive (void)
4362 if (pp_emit_line_directives)
4364 if (esql_yyfilename)
4365 fprintf (esql_yyout, "#line %d \"%s\"\n", esql_yylineno,
4368 fprintf (esql_yyout, "#line %d\n", esql_yylineno);
4371 need_line_directive = false;
4374 #if defined (ENABLE_UNUSED_FUNCTION)
4382 esql_yyverror (pp_get_msg (EX_ESQLMSCANSUPPORT_SET, MSG_NOT_PERMITTED),
4387 * count_embedded_newlines() -
4391 count_embedded_newlines (void)
4393 const char *p = esql_yytext;
4408 * echo_string_constant() -
4413 * TODO(M2) : This may be incorrect now!
4414 * according to ansi, there is no escape syntax for newlines!
4417 echo_string_constant (const char *str, int length)
4421 int charsLeft = length;
4423 p = memchr (str, '\n', length);
4427 ECHO_STR (str, length);
4432 * Bad luck; there's at least one embedded newline, so we have to do
4433 * something to get rid of it (if it's escaped). If it's unescaped
4434 * then this is a pretty weird string, but we have to let it go.
4436 * Because we come in here with the opening quote mark still intact,
4437 * we're always guaranteed that there is a valid character in front
4438 * of the result of strchr().
4441 result = q = malloc (length + 1);
4444 esql_yyverror (pp_get_msg (EX_MISC_SET, MSG_OUT_OF_MEMORY));
4450 * Copy the spans between escaped newlines to the new buffer. If we
4451 * encounter an unescaped newline we just include it.
4456 if (*(p - 1) == '\\')
4460 span = p - str - 1; /* exclude size of escape char */
4461 memcpy (q, str, span);
4465 /* Exclude size of skipped escape and newline chars from string */
4466 charsLeft -= (span + 2);
4469 p = memchr (p + 1, '\n', charsLeft);
4472 memcpy (q, str, charsLeft);
4474 ECHO_STR (result, length);
4475 free_and_init (result);
4477 #endif /* ENABLE_UNUSED_FUNCTION */
4480 * esql_yy_sync_lineno() -
4484 esql_yy_sync_lineno (void)
4486 need_line_directive = true;
4490 * esql_yy_set_buf() -
4495 esql_yy_set_buf (varstring * vstr)
4501 * esql_yy_get_buf() -
4505 esql_yy_get_buf (void)
4511 * esql_yy_erase_last_token() -
4515 esql_yy_erase_last_token (void)
4517 if (current_buf && esql_yytext)
4518 current_buf->end -= strlen (esql_yytext);
4529 * We've been burned too many times by people who can't alphabetize;
4530 * just sort the bloody thing once to protect ourselves from bozos.
4533 sizeof (c_keywords) / sizeof (c_keywords[0]),
4534 sizeof (c_keywords[0]), &keyword_cmp);
4535 qsort (csql_keywords,
4536 sizeof (csql_keywords) / sizeof (csql_keywords[0]),
4537 sizeof (csql_keywords[0]), &keyword_case_cmp);
4540 recognize_keywords = true;
4541 esql_yy_enter (START);
4545 char* mm_buff = NULL;
4555 mm_malloc (int size)
4561 mm_size = 1024*1024;
4562 mm_buff = pp_malloc(mm_size);
4565 if (mm_idx + size > mm_size)
4568 ret = pp_malloc(mm_size);
4573 ret = &mm_buff[mm_idx];
4586 mm_strdup (char* str)
4588 const int max_len = 1024 * 1024 * 10;
4590 int len = strnlen(str, max_len);
4594 esql_yyverror("(mm_strdup) internal error: string size is too large");
4598 buff = mm_malloc(len + 1);
4599 strncpy(buff, str, len);