main.c (4424B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: main.c 8 * Purpose: Standalone CINT application 9 */ 10 11 #include "cint_config.h" 12 13 #if CINT_CONFIG_INCLUDE_MAIN == 1 14 15 #include "cint_interpreter.h" 16 #include "cint_ast.h" 17 #include "cint_parser.h" 18 #include "cint_variables.h" 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <limits.h> 22 #include <getopt.h> 23 24 #if CINT_CONFIG_INCLUDE_TEST_DATA == 1 25 extern int cint_test_data_init(void); 26 extern int cint_test_data_deinit(void); 27 #endif 28 29 /* 30 * Program Options 31 */ 32 33 static int _opt_ast = 0; 34 static int _opt_str = 0; 35 static int _opt_prompt = 1; 36 37 #ifdef YYDEBUG 38 extern int cint_c_debug; 39 #endif 40 41 static struct option long_options[] = 42 { 43 { "atrace", no_argument, &interp.debug.atrace, 1 }, 44 { "ftrace", no_argument, &interp.debug.ftrace, 1 }, 45 { "dtrace", no_argument, &interp.debug.dtrace, 1 }, 46 { "print", no_argument, &interp.print_expr, 1 }, 47 { "ast", no_argument, &_opt_ast, 1 }, 48 { "str", no_argument, &_opt_str, 1 }, 49 { "noprompt", no_argument, &_opt_prompt, 0 }, 50 #ifdef YYDEBUG 51 { "yydebug", no_argument, &cint_c_debug, 1 }, 52 #endif 53 { 0, 0, 0, 0 }, 54 }; 55 56 57 static char * 58 santize_filename(char *s) 59 { 60 return s ? ((CINT_STRLEN(s) > PATH_MAX) ? NULL : s) : NULL; 61 } 62 63 #if CINT_CONFIG_INCLUDE_XINCLUDE == 1 64 static void 65 init_include_path(void) 66 { 67 char *path; 68 69 path = getenv("CINT_INCLUDE_PATH"); 70 if (path) { 71 cint_interpreter_include_set(path); 72 } 73 } 74 #endif 75 76 int 77 main(int argc, char* argv[]) 78 { 79 FILE* fp = NULL; 80 char *file_arg; 81 82 while(1) { 83 84 int opt_index; 85 int c; 86 87 c = getopt_long(argc, argv, "", long_options, &opt_index); 88 89 if(c == -1) { 90 break; 91 } 92 } 93 file_arg = santize_filename(argv[optind]); 94 95 /* 96 * What actions? 97 */ 98 if(_opt_ast) { 99 100 /* Do not interpret. Just parse and dump the ASTs */ 101 cint_cparser_t* cp = cint_cparser_create(); 102 cint_ast_t* ast; 103 104 if(cp == NULL) { 105 CINT_PRINTF("** error creating parser object\n"); 106 return -1; 107 } 108 109 if(file_arg) { 110 #if CINT_CONFIG_FILE_IO == 1 111 /* Assume file argument */ 112 fp = CINT_FOPEN(file_arg, "r"); 113 114 if(fp) { 115 cint_cparser_start_handle(cp, fp); 116 } 117 else { 118 /* Assume immediate string argument */ 119 cint_cparser_start_string(cp, file_arg); 120 } 121 #else 122 CINT_PRINTF("File IO not supported\n"); 123 return 1; 124 #endif 125 } 126 else { 127 /* Assume stdin */ 128 cint_cparser_start_handle(cp, NULL); 129 } 130 131 /* 132 * Parse translation units until EOF or error 133 */ 134 while((ast = cint_cparser_parse(cp))) { 135 cint_ast_dump(ast, 0); 136 } 137 138 cint_cparser_destroy(cp); 139 140 #if CINT_CONFIG_FILE_IO == 1 141 if(fp) { 142 CINT_FCLOSE(fp); 143 } 144 #endif 145 146 return 0; 147 } else if (_opt_str) { 148 int rv = -1; 149 cint_ast_t* ast; 150 151 /* interpret using string interface */ 152 if (optind < argc) { 153 cint_interpreter_init(); 154 ast = cint_interpreter_parse_string(argv[optind]); 155 if (ast) { 156 rv = cint_interpreter_evaluate(ast); 157 } 158 } 159 return (rv != 0); 160 } 161 162 #if CINT_CONFIG_INCLUDE_TEST_DATA == 1 163 if (cint_test_data_init() != CINT_E_NONE) { 164 return (1); 165 } 166 #endif 167 cint_interpreter_init(); 168 #if CINT_CONFIG_INCLUDE_XINCLUDE == 1 169 init_include_path(); 170 #endif 171 #if CINT_CONFIG_FILE_IO == 1 172 if(file_arg) { 173 fp = CINT_FOPEN(file_arg, "r"); 174 } 175 #endif 176 177 cint_interpreter_parse(fp, (_opt_prompt) ? "cint> " : NULL, 0, NULL); 178 179 #if CINT_CONFIG_FILE_IO == 1 180 if(fp) { 181 CINT_FCLOSE(fp); 182 } 183 #endif 184 185 cint_datatype_clear(); 186 cint_variable_clear(); 187 #if CINT_CONFIG_INCLUDE_TEST_DATA == 1 188 if (cint_test_data_deinit() != CINT_E_NONE) { 189 return (1); 190 } 191 #endif 192 193 return 0; 194 } 195 196 #else /* CINT_CONFIG_INCLUDE_MAIN */ 197 int cint_main_c_not_empty; 198 #endif