parse.h (9240B)
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: parse.h 8 * Purpose: Defines and types relating to parsing command lines. 9 */ 10 11 #ifndef _DIAG_ARGS_H 12 #define _DIAG_ARGS_H 13 14 #include <sal/types.h> 15 16 #define ARGS_CNT 1024 /* Max argv entries */ 17 #define ARGS_BUFFER 8192 /* # bytes total for arguments */ 18 19 /* 20 * Define: PARSE_VARIABLE_PREFIX1/PREFIX2/SYSTEM 21 * Purpose: Defines the character that indicate a variable reference, 22 * and the character the indicates a system variable. 23 */ 24 #define PARSE_VARIABLE_PREFIX1 '@' /* Deprecated */ 25 #define PARSE_VARIABLE_PREFIX2 '$' 26 #define PARSE_VARIABLE_SYSTEM '_' 27 28 /* 29 * Typedef: parse_key_t 30 * Purpose: Type used as FIRST entry in structures passed to 31 * parse_lookup. 32 */ 33 typedef char *parse_key_t; 34 35 /* 36 * Define: PARSE_ENTRIES 37 * Purpose: Determine the number of elements in a parse table. 38 */ 39 #define PARSE_ENTRIES(_p) COUNTOF(_p) 40 41 /* 42 * Typedef: args_t 43 * Purpose: argc/argv (sorta) structure for parsing arguments. 44 * 45 * If the macro ARG_GET is used to consume parameters, unused parameters 46 * may be passed to lower levels by simply passing the args_t struct. 47 */ 48 typedef struct args_s { 49 parse_key_t a_cmd; /* Initial string */ 50 char *a_argv[ARGS_CNT]; /* argv pointers */ 51 char a_buffer[ARGS_BUFFER]; /* Split up buffer */ 52 int a_argc; /* Parsed arg counter */ 53 int a_arg; /* Pointer to NEXT arg */ 54 } args_t; 55 56 #define ARG_CMD(_a) (_a)->a_cmd 57 58 #define _ARG_CUR(_a) \ 59 ((_a)->a_argv[(_a)->a_arg]) 60 61 #define ARG_CUR(_a) \ 62 (((_a)->a_arg >= (_a)->a_argc) ? NULL : _ARG_CUR(_a)) 63 64 #define _ARG_GET(_a) \ 65 ((_a)->a_argv[(_a)->a_arg++]) 66 67 #define ARG_GET(_a) \ 68 (((_a)->a_arg >= (_a)->a_argc) ? NULL : _ARG_GET(_a)) 69 70 #define ARG_NEXT(_a) (_a)->a_arg++ 71 #define ARG_PREV(_a) (_a)->a_arg-- 72 #define ARG_CUR_INDEX(_a) ((_a)->a_arg) 73 74 #define ARG_DISCARD(_a) \ 75 ((_a)->a_arg = (_a)->a_argc) 76 77 #define ARG_GET_WITH_INDEX(_a, id) \ 78 ((_a)->a_argv[id]) 79 /* 80 * Macro: ARG_CNT 81 * Purpose: Return the number of unconsumed arguments. 82 */ 83 #define ARG_CNT(_a) ((_a)->a_argc - (_a)->a_arg) 84 85 /* 86 * Typedef: parse_pm_t 87 * Purpose: Defines a "mask" type for parsing the common 88 * "+foo -bar" type arguments. 89 */ 90 typedef struct parse_pm_s { 91 parse_key_t pm_s; /* string to match */ 92 uint32 pm_value; /* Value associated with string */ 93 } parse_pm_t; 94 95 /* 96 * Typedef: parse_eq_t 97 * Purpose: Generic type for parsing a series of options of the 98 * form "abc=X". 99 * 100 * Notes: 101 * Entries of type PQ_INT/PQ_HEX cause the value to be stored 102 * in *pq_value as type "int". If an entry is marked as PQ_HEX, the 103 * only difference is the value is displayed as a hex #. 104 * 105 * Entries of type PQ_STRING cause a malloc'd string pointer to be 106 * stored in *pq_value as type "char *". 107 * 108 * Entries of type PQ_BOOL cause a boolean value of 1 or 0 to be 109 * stored in *pq_value as type "int". 110 * 111 * Entries of type PQ_MAC cause a 6-byte raw mac address to be 112 * stored at *pq_value. 113 * 114 * Entries of type PQ_IP cause a 4-byte raw IP address to be stored 115 * in *pq_value as type ip_addr_t. 116 * 117 * Entries of type PQ_PBMP cause a N-word integer value to be stored 118 * in *pq_value as type "pbmp_t". 119 * 120 * Entries of type PQ_PORTMODE cause an integer value to be stored 121 * in *pq_value as type soc_port_mode_t. 122 * 123 * Entries of type PQ_PORT cause an integer value to be stored 124 * in *pq_value as type soc_port_t. 125 * 126 * Entries of type PQ_MOD_PORT cause an integer values to be stored 127 * in *pq_value as type bcm_mod_port_t. 128 * 129 * Entries with PQ_DFL set use the pq_value instead of the pq_default 130 * field. 131 * 132 * Entries with PQ_MULTI set assume XXXX is a pointer to a NULL 133 * terminated array of strings that are possible matches. 134 * 135 * Fields in this structure are modified by parse_arg_eq, and 136 * parse_arg_eq_done. After processing is complete on the parsed 137 * arguments, parse_arg_eq_done should be called to free any 138 * malloc'd buffers, and possibly clean up internal state. 139 */ 140 typedef struct parse_eq_s { 141 parse_key_t pq_s; /* String to match */ 142 # define PQ_BCM 0x40000 /* Parse as BCM API port */ 143 # define PQ_STATIC 0x20000 /* do not deallocate string; 144 overrides PQ_MALLOC */ 145 # define PQ_LAB 0x10000 /* warn if used (lab compatibility) */ 146 # define PQ_IGNORE 0x8000 /* Dynamically set if required */ 147 # define PQ_NO_EQ_OPT 0x4000 /* May or may not take = */ 148 # define PQ_SEEN 0x2000 /* Was string seen w/o =? */ 149 # define PQ_PARSED 0x1000 /* Set if seen during parse call */ 150 # define PQ_DFL 0x800 /* Use VALUE for default */ 151 # define PQ_PTR 0x400 /* PTR type ARG */ 152 # define PQ_MALLOC 0x100 /* Value is malloc'd */ 153 # define PQ_VAL 0x000 /* VALUE type ARG */ 154 # define PQ_INT (PQ_VAL|0x01) /* Normal integer */ 155 # define PQ_HEX (PQ_VAL|0x02) /* Hex integer (like int) */ 156 # define PQ_BOOL (PQ_VAL|0x03) /* Yes/No */ 157 # define PQ_STRING (PQ_VAL|0x04) /* String value */ 158 # define PQ_MAC (PQ_PTR|0x05) /* MAC Address */ 159 # define PQ_IP (PQ_VAL|0x06) /* IP Address */ 160 # define PQ_PBMP (PQ_PTR|0x07) /* Port bitmap */ 161 # define PQ_MULTI (PQ_VAL|0x08) /* Multiple choice */ 162 # define PQ_PORTMODE (PQ_VAL|0x09) /* Port mode */ 163 # define PQ_PORT (PQ_VAL|0x0a) /* Port */ 164 # define PQ_MOD_PORT (PQ_PTR|0x0b) /* Port, mod id pair */ 165 # define PQ_IP6 (PQ_PTR|0x0c) /* IPv6 Address */ 166 # define PQ_INT64 (PQ_PTR|0x0d) /* 64 bit integer */ 167 # define PQ_LR_PHYAB (PQ_VAL|0x0e) /* Longreach PHY ability */ 168 # define PQ_PORTABIL (PQ_PTR|0x0f) /* port ability */ 169 # define PQ_INT8 (PQ_VAL|0x10) /* 8bit */ 170 # define PQ_INT16 (PQ_VAL|0x20) /* 16bit */ 171 # define PQ_TYPE(_x) ((_x) & (0xff|PQ_PTR|PQ_VAL)) 172 uint32 pq_unit; /* Unit number */ 173 uint32 pq_type; /* Type after '=' */ 174 const void *pq_default; /* Default Value */ 175 void *pq_value; /* Value parsed */ 176 char **pq_fm; /* Multi */ 177 } parse_eq_t; 178 179 typedef struct parse_table_s { 180 int pt_unit; /* unit number */ 181 int pt_cnt; /* # valid entries */ 182 int pt_alloc; /* number of allocated entries */ 183 # define PT_ALLOC 32 /* entries to allocate */ 184 parse_eq_t *pt_entries; /* Actual entries */ 185 int cmd_flag; /* The on-going config cmd */ 186 # define CMD_DC 0 /* config cmd is don't care */ 187 # define CMD_ADD 1 /* config add cmd */ 188 } parse_table_t; 189 190 /* parse.c exported routines */ 191 192 extern char *(*parse_user_var_get)(char *varname); 193 194 extern void parse_args_copy(args_t *dst, args_t *src); 195 extern int parse_cmp(const char *p, const char *s, const char term); 196 extern int diag_parse_args(const char *s, char **s_ret, args_t *); 197 extern int parse_mask(const char *s, const parse_pm_t *, uint32 *mask); 198 extern void parse_mask_format(const int, const parse_pm_t *,uint32 mask); 199 extern const void *parse_lookup(const char *, const void *tbl, int size, 200 int cnt); 201 extern int parse_arg_eq(args_t *, parse_table_t *); 202 extern int parse_arg_eq_keep_index(args_t *a, parse_table_t *pq_table); 203 extern void parse_arg_eq_done(parse_table_t *); 204 extern void parse_eq_format(parse_table_t *); 205 extern int parse_default_fill(parse_table_t *pq_table); 206 extern int parse_table_add(parse_table_t *, char *key, uint32 type, 207 void *def, void *value, void *func); 208 extern void parse_table_init(int unit, parse_table_t *); 209 210 /* variable.c exported routines */ 211 212 extern void *var_push_scope(void); 213 extern void var_pop_scope(void *); 214 extern char *var_get(const char *); 215 extern int var_set(const char *v_name, char *v_value, int local, 216 int system); 217 extern int var_set_integer(const char *v_name, int v_value, int local, 218 int system); 219 extern int var_set_hex(const char *v_name, int v_value, int local, 220 int system); 221 extern int var_unset(const char *v_name, int local, int global, 222 int system); 223 224 #endif /* _DIAG_ARGS_H */