example_cmd.c (4458B)
1 /* 2 * 3 * 4 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 5 * 6 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 7 */ 8 9 #include <appl/diag/system.h> 10 #include <appl/diag/parse.h> 11 12 #include <appl/applref/applrefIface.h> 13 #include <bcm/error.h> 14 15 #ifdef INCLUDE_EXAMPLES 16 /** cmd usage */ 17 char cmd_example_exec_usage[] = 18 "Usage: \n\t" 19 " example list <category>\n\t" 20 " example info cmd\n\t" 21 " example exec cmd <param ...>\n\t" 22 " example selftest cmd\n"; 23 24 typedef ApplRefFunction_t * (*_myftype)(void); 25 /** function pointer, interface with the applreflib */ 26 extern _myftype exampleCmdGetCmdList; 27 28 /** dumppest linear search for a command */ 29 static ApplRefFunction_t * findFunc(ApplRefFunction_t *pe, char *pn); 30 31 /** return 1 if match previous prefix, otherwise return 0 */ 32 static int match_str(char *pstr, char *pc, int n); 33 34 /** 35 * execute example_cmd. 36 * @ param unit chip unit 37 * @ a argument list 38 * @return stat. 39 */ 40 cmd_result_t cmd_example_exec(int unit, args_t *a) { 41 ApplRefFunction_t * pexpls; 42 char *subcmd; 43 int rc = 0; 44 45 cli_out("Invoking example cmd\n"); 46 47 if ( (subcmd = ARG_GET(a)) == NULL ) 48 return CMD_USAGE; 49 50 if ( exampleCmdGetCmdList == NULL || 51 (pexpls = (*exampleCmdGetCmdList)()) == NULL || 52 pexpls->pf == NULL ) 53 { 54 cli_out("No Examples Available\n"); 55 return CMD_OK; 56 } 57 58 if ( sal_strcasecmp(subcmd, "list") == 0 ) { 59 ApplRefFunction_t *pe; 60 char buf[128]; 61 buf[0] = 0; 62 63 for( pe = pexpls; pe->pf != NULL; pe++ ) { 64 65 if ( match_str( pe->pname, buf, sizeof(buf)-1 ) ) 66 cli_out("===================================================================================\n"); 67 68 cli_out(" %-25s : %s\n", pe->pname, pe->pbriefdescr); 69 } 70 return CMD_OK; 71 } 72 73 74 if ( sal_strcasecmp(subcmd, "alllist") == 0 ) { 75 ApplRefFunction_t *pe; 76 char buf[128]; 77 buf[0] = 0; 78 79 for( pe = pexpls; pe->pf != NULL; pe++ ) { 80 81 if ( match_str( pe->pname, buf, sizeof(buf)-1 ) ) 82 cli_out("===================================================================================\n"); 83 84 cli_out(" %-25s : %s\n", pe->pname, pe->pbriefdescr); 85 86 cli_out("\n**********************************************\n"); 87 cli_out("%s\n", pe->pdescr); 88 cli_out("\n**********************************************\n"); 89 90 } 91 return CMD_OK; 92 } 93 94 95 if ( sal_strcasecmp(subcmd, "info") == 0 ) { 96 ApplRefFunction_t *pe; 97 98 if ( (subcmd = ARG_GET(a)) == NULL ) 99 return CMD_USAGE; 100 101 pe = findFunc( pexpls, subcmd ); 102 if ( pe == NULL ) { 103 cli_out("Example %s is not found\n", subcmd); 104 return CMD_FAIL; 105 } 106 107 cli_out("\n**********************************************\n"); 108 cli_out("%s\n", pe->pdescr); 109 cli_out("\n**********************************************\n"); 110 111 return CMD_OK; 112 } 113 114 if ( sal_strcasecmp(subcmd, "exec") == 0 ) { 115 ApplRefFunction_t *pe; 116 117 if ( (subcmd = ARG_GET(a)) == NULL ) 118 return CMD_USAGE; 119 120 pe = findFunc( pexpls, subcmd ); 121 if ( pe == NULL ) { 122 cli_out("Example %s is not found\n", subcmd); 123 return CMD_FAIL; 124 } 125 126 /** invoke command */ 127 { 128 char *args[16]; /** limit to 16 args */ 129 int n = 0; 130 131 while ( (n < 16) && ( (args[n] = ARG_GET(a)) != NULL ) ) 132 n++; 133 134 if ( n == 16 ) { 135 cli_out("Excessive amount of arguments\n"); 136 return CMD_FAIL; 137 } 138 139 if ( BCM_FAILURE(rc = (*pe->dispatch)(pe->pf, n, args)) ) { 140 cli_out("Error : fail to execute %d %s\n", rc, bcm_errmsg(rc) ); 141 return rc; 142 } 143 144 } 145 146 return CMD_OK; 147 } 148 149 return CMD_USAGE; 150 151 } 152 153 154 /** change it later to better search algorithm */ 155 static ApplRefFunction_t * findFunc(ApplRefFunction_t *pe, char *pn) { 156 157 if ( pe == NULL ) 158 return NULL; 159 160 while ( pe->pf != NULL ) { 161 if ( sal_strcasecmp( pe->pname, pn ) == 0 ) 162 return pe; 163 pe++; 164 } 165 166 return NULL; 167 } 168 169 static int match_str(char *pstr, char *pc, int n) { 170 int i, stat; 171 172 char *c; 173 for( c = pstr; *c != 0; c++ ) 174 if ( *c == '_' ) 175 break; 176 177 if ( c == 0 ) { 178 pc[0] = 0; 179 return 1; 180 } 181 182 stat = 0; 183 for(i = 0; i < c - pstr && i < n; i++) { 184 if ( pc[i] != pstr[i] ) { 185 stat = 1; 186 pc[i] = pstr[i]; 187 } 188 } 189 190 if ( pc[i] != 0 || i == n ) 191 stat = 1; 192 193 pc[i] = 0; 194 195 return stat; 196 } 197 #else 198 int __no_complilation_complain________0; 199 #endif