openbcm

Git mirror of https://github.com/Broadcom-Network-Switching-Software/OpenBCM
git clone git://git.finwo.net/mirror/broadcom/openbcm
Log | Files | Refs | README

config.c (10636B)


      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  * System configuration command.
      8  */
      9 
     10 #ifndef NO_SAL_APPL
     11 
     12 #include <appl/diag/shell.h>
     13 #include <appl/diag/system.h>
     14 #include <sal/appl/config.h>
     15 #include <shared/bsl.h>
     16 
     17 #define ENV_CONFIG_VAR_VAL "config_var_val"
     18 #define ENV_CONFIG_VAR_NOT_SET "config_var_not_set"
     19 
     20 char sh_config_usage[] =
     21     "Parameters: show <substring> | dump | refresh |  \n\t"
     22     "                 save [filename=<filename>] [append=yes] [pattern=<substring>] | \n\t"
     23     "                 add [<var>=<value>] | \n\t"
     24     "                 delete [pattern=<substring>] |<var>\n\t"
     25     "If no parameters are given, displays the current config vars.\n\t"
     26 #ifndef COMPILER_STRING_CONST_LIMIT
     27     "  show                display current config vars. Next parameter (optional) maybe a substring to match\n\t"
     28     "  dump                display current config vars imported from config.bcm and used(hit) by SDK.\n\t"
     29     "                      e.g., Hash, Line #, Flag(Compiled/Flushed/Hit), Vars Name=Value \n\t"
     30     "  refresh             reload config vars from non-volatile storage\n\t"
     31     "  save                save config vars to non-volatile storage.\n\t"
     32     "                      it can optionally save current config to any given file \n\t"
     33     "                      providing the optional <pattern> will only save variables matching the pattern\n\t"
     34     "  append              append config vars to the end of the file.\n\t"
     35     "                      don\'t replace the old one.\n\t"
     36     "  <var>=<value>       change the value of a config var\n\t"
     37     "  add <var>=<value>   create a new config var\n\t"
     38     "  get <var>           print the value of a config var\n\t"
     39     "  delete <var>        deletes a config var\n\t"
     40     "  clear               deletes all config vars\n\t"
     41     "  =                   prompt for new value for each config var\n\t"
     42 #endif
     43     "NOTE: changes are not retained permanently unless saved\n";
     44 
     45 cmd_result_t
     46 sh_config(int u, args_t *a)
     47 /*
     48  * Function: 	sh_config
     49  * Purpose:	Configure switch management information.
     50  * Parameters:
     51  * Returns:
     52  */
     53 {
     54     parse_table_t    pt;
     55     char        *name, *value;
     56     char        **parsed_value;
     57     int         i, rv, nconfig, add, get = 0;
     58     char        *c = ARG_CUR(a);
     59     char        *substr;
     60     char        *new_fname = NULL;
     61     char        *pattern = NULL;
     62     char        *append = NULL;
     63     char        *config_var = NULL;
     64     char        *var_val = NULL;
     65 
     66 
     67     rv = CMD_OK;
     68 
     69     if (c == NULL || !sal_strcasecmp(c, "show")) {
     70         name = NULL;
     71         ARG_NEXT(a);
     72         substr = ARG_CUR(a);
     73         while (sal_config_get_next(&name, &value) >= 0) {
     74             if ((substr == NULL) || (sal_strstr(name, substr) != NULL)) {
     75                 cli_out("    %s=%s\n", name, value);
     76             }
     77         }
     78         if (NULL != c) {
     79             ARG_NEXT(a);
     80         }
     81         return CMD_OK;
     82     }
     83 
     84     if (!sal_strcasecmp(c, "dump")) {
     85         ARG_NEXT(a);
     86         sal_config_dump();
     87         return CMD_OK;
     88     }
     89 
     90     if (!sal_strcasecmp(c, "refresh")) {
     91         ARG_NEXT(a);
     92         cli_out("%s: Refreshing configuration database\n", ARG_CMD(a));
     93         if (sal_config_refresh()) {
     94             cli_out("%s: Failed to refresh configuration database\n",
     95                     ARG_CMD(a));
     96             return CMD_FAIL;
     97         }
     98         return CMD_OK;
     99     }
    100 
    101     if (!sal_strcasecmp(c, "save")) {
    102         parse_table_t    pt_save;
    103         parse_table_init(u, &pt_save);
    104         parse_table_add(&pt_save, "filename", PQ_STRING, NULL,
    105                                 (void *)&new_fname, NULL);
    106         parse_table_add(&pt_save, "append", PQ_STRING, NULL,
    107                                 (void *)&append, NULL);
    108         parse_table_add(&pt_save, "pattern", PQ_STRING, NULL,
    109                                 (void *)&pattern, NULL);
    110         ARG_NEXT(a);
    111         if (0 > parse_arg_eq(a, &pt_save)) {
    112             parse_arg_eq_done(&pt_save);
    113             return CMD_USAGE;
    114         }
    115         if (ARG_CNT(a) > 0) {
    116             cli_out("%s: Invalid option: %s\n", ARG_CMD(a), ARG_CUR(a));
    117             parse_arg_eq_done(&pt_save);
    118             return CMD_USAGE;
    119         }
    120         if (sal_strlen(new_fname)) {
    121             rv = sal_config_save(new_fname, pattern, sal_strcasecmp(append, "yes") ? 0 : 1);
    122         } else {
    123             rv = sal_config_flush();
    124         }
    125         parse_arg_eq_done(&pt_save);
    126 
    127         if (rv) {
    128             cli_out("%s: Warning: sal_config_flush failed\n", ARG_CMD(a));
    129             return CMD_FAIL;
    130         }
    131         return CMD_OK;
    132     }
    133 
    134     if (!sal_strcasecmp(c, "delete")) {
    135         parse_table_t    pt_delete;
    136         parse_table_init(u, &pt_delete);
    137         parse_table_add(&pt_delete, "pattern", PQ_STRING, NULL,
    138                                 (void *)&pattern, NULL);
    139         ARG_NEXT(a);
    140         if (0 > parse_arg_eq(a, &pt_delete)) {
    141             parse_arg_eq_done(&pt_delete);
    142             return CMD_USAGE;
    143         }
    144 
    145         if (sal_strlen(pattern)) {
    146             name = NULL;
    147             for(;;) {
    148                  if (sal_config_get_next(&name, &value) < 0) {
    149                      break; 
    150                  }
    151                  if ((sal_strstr(name, pattern) != NULL)) {
    152                     LOG_VERBOSE(BSL_LS_APPL_SHELL, (BSL_META_U(u,
    153                                 "Pattern(%s) Deleting %s=%s\n"),
    154                                 pattern, name, value));
    155                     sal_config_set(name, 0);
    156                     name = NULL;
    157                  }
    158             }
    159         } 
    160         parse_arg_eq_done(&pt_delete);
    161 
    162         while ((c = ARG_GET(a)) != NULL) {
    163             if (sal_config_set(c, 0) != 0) {
    164                 cli_out("%s: Variable not found: %s\n", ARG_CMD(a), c);
    165                 rv = CMD_FAIL;
    166             }
    167         }
    168         return rv;
    169     }
    170 
    171     if (!sal_strcasecmp(c, "clear")) {
    172         ARG_NEXT(a);
    173         for (;;) {
    174             name = NULL;
    175             if (sal_config_get_next(&name, &value) < 0) {
    176                 break;
    177             }
    178             if (sal_config_set(name, 0) < 0) {
    179                 cli_out("%s: Variable not found: %s\n", ARG_CMD(a), name);
    180                 rv = CMD_FAIL;
    181                 break;
    182             }
    183         }
    184         return rv;
    185     }
    186 
    187     if (!sal_strcasecmp(c, "add")) {
    188         ARG_NEXT(a);
    189         add = 1;
    190     } else {
    191         add = 0;
    192     }
    193 
    194     if (!sal_strcasecmp(c, "get")) {
    195         ARG_NEXT(a);
    196         config_var = ARG_CUR(a);
    197         if (config_var == NULL) {
    198             cli_out("config variable cannot be empty!!\n");
    199             return CMD_USAGE;
    200         }
    201         get = 1;
    202     } else {
    203         get = 0;
    204     }
    205 
    206     /* count config variables first, to allocate parsed_value array */
    207     name = NULL;
    208     nconfig = 0;
    209     while (sal_config_get_next(&name, &value) >= 0) {
    210         nconfig += 1;
    211         /*
    212          * "config get <config_var> was issued.
    213          */
    214         if (get) {
    215             if (!sal_strcasecmp(config_var, name)) {
    216                 if ((var_val = sal_config_get(config_var)) != NULL) {
    217                     /*
    218                      *  <config_var> found, et the env variables
    219                      *  "config_var_val" to the value in the config.
    220                      */
    221                     var_set_integer(ENV_CONFIG_VAR_VAL, 
    222                                     parse_integer(var_val), TRUE, FALSE);
    223                     var_set_integer(ENV_CONFIG_VAR_NOT_SET, 0, TRUE, FALSE);
    224                     cli_out("Environment variable (%s) was set\n", 
    225                            ENV_CONFIG_VAR_VAL);
    226                     ARG_DISCARD(a);
    227                     return  CMD_OK;
    228                 }
    229             }
    230         }
    231     }
    232 
    233     /*
    234      *  if the shell command "config get <config_var>" was
    235      *  issued and code execution has reached here, means the
    236      *  the config_var is not set. Set the env variables
    237      *  "config_var_val" to 0 and "config_var_not_set to 0
    238      */
    239     if (get) {
    240         var_set_integer(ENV_CONFIG_VAR_VAL, 0, TRUE, FALSE);
    241         var_set_integer(ENV_CONFIG_VAR_NOT_SET, 1, TRUE, FALSE);
    242         cli_out("Environment variables (%s) (%s) was set\n", 
    243                ENV_CONFIG_VAR_VAL, ENV_CONFIG_VAR_NOT_SET);
    244         ARG_DISCARD(a);
    245         return CMD_OK;
    246     }
    247 
    248     if (nconfig) {
    249         parsed_value = sal_alloc(sizeof(*parsed_value) * nconfig,
    250                                  "config values");
    251         if (parsed_value == NULL) {
    252             cli_out("%s: cannot allocate memory for config values\n",
    253                     ARG_CMD(a));
    254             return CMD_FAIL;
    255         }
    256         sal_memset(parsed_value, 0, sizeof(*parsed_value) * nconfig);
    257 
    258         name = NULL;
    259         parse_table_init(u, &pt);
    260         i = 0;
    261         while (sal_config_get_next(&name, &value) >= 0) {
    262             if (parse_table_add(&pt, name, PQ_STRING, value,
    263                                 (void *)&parsed_value[i], NULL) < 0) {
    264                 cli_out("Internal error in parsing\n");
    265                 sal_free(parsed_value);
    266                 return CMD_FAIL;
    267             }
    268             i += 1;
    269         }
    270 
    271         /* Set cmd_flag before calling parse_arg_eq() */
    272         pt.cmd_flag = (add == 1) ? CMD_ADD : CMD_DC;
    273 
    274         if (0 > parse_arg_eq(a, &pt)) {
    275             cli_out("%s: Invalid option: %s\n", ARG_CMD(a), ARG_CUR(a));
    276             parse_arg_eq_done(&pt);
    277             sal_free(parsed_value);
    278             return CMD_USAGE;
    279         }
    280 
    281         /* For each that is modified, set new value */
    282 
    283         for (i = 0; i < pt.pt_cnt; i++) {
    284             if (pt.pt_entries[i].pq_type & PQ_PARSED) {
    285                 if (sal_config_set(pt.pt_entries[i].pq_s,
    286                                    parsed_value[i]) < 0) {
    287                     rv = CMD_FAIL;
    288                 }
    289             }
    290         }
    291 
    292         parse_arg_eq_done(&pt);
    293         if (parsed_value) {
    294             sal_free(parsed_value);
    295         }
    296     }
    297 
    298     if (ARG_CNT(a)) {
    299         while ((c = ARG_GET(a)) != NULL) {
    300             char *s;
    301             s = sal_strchr(c, '=');
    302             if (s == NULL) {
    303                 cli_out("%s: Invalid assignment: %s\n", ARG_CMD(a), c);
    304                 rv = CMD_FAIL;
    305             } else if (! add) {
    306                 cli_out("%s: Must use 'add' to create new variable: %s\n",
    307                         ARG_CMD(a), c);
    308                 rv = CMD_FAIL;
    309             } else {
    310                 *s++ = 0;
    311                 if (sal_config_set(c, s) < 0) {
    312                     rv = CMD_FAIL;
    313                 }
    314             }
    315         }
    316     }
    317 
    318     return rv;
    319 }
    320 
    321 #endif /* NO_SAL_APPL */