Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2013, WIDE Project and NICT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 9 | * permitted provided that the following conditions are met: * |
| 10 | * * |
| 11 | * * Redistributions of source code must retain the above * |
| 12 | * copyright notice, this list of conditions and the * |
| 13 | * following disclaimer. * |
| 14 | * * |
| 15 | * * Redistributions in binary form must reproduce the above * |
| 16 | * copyright notice, this list of conditions and the * |
| 17 | * following disclaimer in the documentation and/or other * |
| 18 | * materials provided with the distribution. * |
| 19 | * * |
| 20 | * * Neither the name of the WIDE Project or NICT nor the * |
| 21 | * names of its contributors may be used to endorse or * |
| 22 | * promote products derived from this software without * |
| 23 | * specific prior written permission of WIDE Project and * |
| 24 | * NICT. * |
| 25 | * * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 34 | *********************************************************************************************************/ |
| 35 | |
| 36 | /* Yacc extension's configuration parser. |
| 37 | * See doc/app_test.conf.sample for configuration file format |
| 38 | */ |
| 39 | |
| 40 | /* For development only : */ |
| 41 | %debug |
| 42 | %error-verbose |
| 43 | |
| 44 | /* The parser receives the configuration file filename as parameter */ |
| 45 | %parse-param {char * conffile} |
| 46 | |
| 47 | /* Keep track of location */ |
| 48 | %locations |
| 49 | %pure-parser |
| 50 | |
| 51 | %{ |
| 52 | #include "test_app.h" |
| 53 | #include "ta_conf.tab.h" /* bison is not smart enough to define the YYLTYPE before including this code, so... */ |
| 54 | |
| 55 | #include <string.h> |
| 56 | #include <errno.h> |
| 57 | |
| 58 | /* Forward declaration */ |
| 59 | int yyparse(char * conffile); |
| 60 | |
| 61 | /* Parse the configuration file */ |
| 62 | int ta_conf_handle(char * conffile) |
| 63 | { |
| 64 | extern FILE * ta_confin; |
| 65 | int ret; |
| 66 | |
| 67 | TRACE_ENTRY("%p", conffile); |
| 68 | |
| 69 | TRACE_DEBUG (FULL, "Parsing configuration file: %s...", conffile); |
| 70 | |
| 71 | ta_confin = fopen(conffile, "r"); |
| 72 | if (ta_confin == NULL) { |
| 73 | ret = errno; |
| 74 | fd_log_debug("Unable to open extension configuration file %s for reading: %s", conffile, strerror(ret)); |
| 75 | TRACE_DEBUG (INFO, "Error occurred, message logged -- configuration file."); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | ret = yyparse(conffile); |
| 80 | |
| 81 | fclose(ta_confin); |
| 82 | |
| 83 | if (ret != 0) { |
| 84 | TRACE_DEBUG (INFO, "Unable to parse the configuration file."); |
| 85 | return EINVAL; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* The Lex parser prototype */ |
| 92 | int ta_conflex(YYSTYPE *lvalp, YYLTYPE *llocp); |
| 93 | |
| 94 | /* Function to report the errors */ |
| 95 | void yyerror (YYLTYPE *ploc, char * conffile, char const *s) |
| 96 | { |
| 97 | TRACE_DEBUG(INFO, "Error in configuration parsing"); |
| 98 | |
| 99 | if (ploc->first_line != ploc->last_line) |
| 100 | fd_log_debug("%s:%d.%d-%d.%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_line, ploc->last_column, s); |
| 101 | else if (ploc->first_column != ploc->last_column) |
| 102 | fd_log_debug("%s:%d.%d-%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s); |
| 103 | else |
| 104 | fd_log_debug("%s:%d.%d : %s", conffile, ploc->first_line, ploc->first_column, s); |
| 105 | } |
| 106 | |
| 107 | %} |
| 108 | |
| 109 | /* Values returned by lex for token */ |
| 110 | %union { |
| 111 | char *string; /* The string is allocated by strdup in lex.*/ |
| 112 | int integer; /* Store integer values */ |
| 113 | } |
| 114 | |
| 115 | /* In case of error in the lexical analysis */ |
| 116 | %token LEX_ERROR |
| 117 | |
| 118 | /* Key words */ |
| 119 | %token VENDOR_ID |
| 120 | %token APPLI_ID |
| 121 | %token CMD_ID |
| 122 | %token AVP_ID |
| 123 | %token LONG_AVP_ID |
| 124 | %token LONG_AVP_LEN |
| 125 | %token MODE |
| 126 | %token DEST_REALM |
| 127 | %token DEST_HOST |
| 128 | %token USER_NAME |
| 129 | %token SIGNAL |
| 130 | %token BENCH |
| 131 | |
| 132 | /* Tokens and types for routing table definition */ |
| 133 | /* A (de)quoted string (malloc'd in lex parser; it must be freed after use) */ |
| 134 | %token <string> QSTRING |
| 135 | |
| 136 | /* An integer value */ |
| 137 | %token <integer> INTEGER |
| 138 | |
| 139 | |
| 140 | |
| 141 | /* -------------------------------------- */ |
| 142 | %% |
| 143 | |
| 144 | /* The grammar definition */ |
| 145 | conffile: /* empty grammar is OK */ |
| 146 | | conffile vendor |
| 147 | | conffile appli |
| 148 | | conffile cmd |
| 149 | | conffile avp |
| 150 | | conffile long_avp_id |
| 151 | | conffile long_avp_len |
| 152 | | conffile mode |
| 153 | | conffile dstrealm |
| 154 | | conffile dsthost |
| 155 | | conffile usrname |
| 156 | | conffile signal |
| 157 | | conffile bench |
| 158 | ; |
| 159 | |
| 160 | vendor: VENDOR_ID '=' INTEGER ';' |
| 161 | { |
| 162 | ta_conf->vendor_id = $3; |
| 163 | } |
| 164 | ; |
| 165 | |
| 166 | appli: APPLI_ID '=' INTEGER ';' |
| 167 | { |
| 168 | ta_conf->appli_id = $3; |
| 169 | } |
| 170 | ; |
| 171 | |
| 172 | cmd: CMD_ID '=' INTEGER ';' |
| 173 | { |
| 174 | ta_conf->cmd_id = $3; |
| 175 | } |
| 176 | ; |
| 177 | |
| 178 | avp: AVP_ID '=' INTEGER ';' |
| 179 | { |
| 180 | ta_conf->avp_id = $3; |
| 181 | } |
| 182 | ; |
| 183 | |
| 184 | long_avp_id: LONG_AVP_ID '=' INTEGER ';' |
| 185 | { |
| 186 | ta_conf->long_avp_id = $3; |
| 187 | } |
| 188 | ; |
| 189 | |
| 190 | long_avp_len: LONG_AVP_LEN '=' INTEGER ';' |
| 191 | { |
| 192 | ta_conf->long_avp_len = $3; |
| 193 | } |
| 194 | ; |
| 195 | |
| 196 | mode: MODE '=' INTEGER ';' |
| 197 | { |
| 198 | ta_conf->mode = $3 | (ta_conf->mode & ~3); /* overwrite the 2 lsb */ |
| 199 | } |
| 200 | ; |
| 201 | |
| 202 | bench: BENCH ';' |
| 203 | { |
| 204 | ta_conf->mode |= MODE_BENCH; |
| 205 | } |
| 206 | | BENCH INTEGER INTEGER ';' |
| 207 | { |
| 208 | ta_conf->mode |= MODE_BENCH; |
| 209 | ta_conf->bench_duration = $2; |
| 210 | ta_conf->bench_concur = $3; |
| 211 | } |
| 212 | ; |
| 213 | |
| 214 | dstrealm: DEST_REALM '=' QSTRING ';' |
| 215 | { |
| 216 | free(ta_conf->dest_realm); |
| 217 | ta_conf->dest_realm = $3; |
| 218 | } |
| 219 | ; |
| 220 | |
| 221 | dsthost: DEST_HOST '=' QSTRING ';' |
| 222 | { |
| 223 | free(ta_conf->dest_host); |
| 224 | ta_conf->dest_host = $3; |
| 225 | } |
| 226 | ; |
| 227 | |
| 228 | usrname: USER_NAME '=' QSTRING ';' |
| 229 | { |
| 230 | free(ta_conf->user_name); |
| 231 | ta_conf->user_name = $3; |
| 232 | } |
| 233 | ; |
| 234 | |
| 235 | signal: SIGNAL '=' INTEGER ';' |
| 236 | { |
| 237 | ta_conf->signal = $3; |
| 238 | } |
| 239 | ; |