cli_inter_preter.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #include "cli_inter_preter.h"
  2. static char arg_str_list[CLI_IPT_ARG_NUM][CLI_IPT_ARG_STR_LEN];
  3. unsigned int cli_ipt_arg_list[CLI_IPT_ARG_NUM][CLI_IPT_ARG_LEN];
  4. void print_main_menu(command_entry *main_table);
  5. void array_map_input_to_byte(unsigned char *b, unsigned int *i, unsigned int len)
  6. {
  7. if (len > CLI_IPT_ARG_LEN)
  8. return;
  9. for (unsigned char index = 0; index < len; index++)
  10. {
  11. b[index] = i[index];
  12. }
  13. }
  14. int len_of_menu(command_entry *entry)
  15. {
  16. int i, len = 0;
  17. for (i = 0; i < CLI_IPT_ENTRY_NUM_MAX; i++)
  18. {
  19. if (entry->action == NULL && entry->arg_desp == NULL && entry->arg_type == NULL && entry->desp == NULL && entry->name == NULL)
  20. {
  21. return len;
  22. }
  23. else
  24. {
  25. len++;
  26. entry++;
  27. }
  28. }
  29. return 0;
  30. }
  31. int cmd_to_param(const char *cmd, char *param, int param_len_max)
  32. {
  33. int state = 0, row = 0;
  34. char *base;
  35. base = param;
  36. while (*cmd != '\0')
  37. {
  38. if (*cmd != ' ')
  39. {
  40. if (state == 2 || state == 0)
  41. {
  42. row++;
  43. if (row > CLI_IPT_ARG_NUM)
  44. return row;
  45. param = base + param_len_max * (row - 1);
  46. memset(param, 0, param_len_max);
  47. state = 1;
  48. }
  49. state = 1;
  50. *param = *cmd;
  51. param++;
  52. if (param > base + param_len_max * (row)-1)
  53. {
  54. return 0;
  55. }
  56. }
  57. else
  58. {
  59. if (state == 1)
  60. {
  61. state = 2;
  62. }
  63. }
  64. cmd++;
  65. }
  66. return row;
  67. }
  68. void print_entry(command_entry *entry)
  69. {
  70. int len, i;
  71. len = len_of_menu(entry);
  72. for (i = 0; i < len; i++)
  73. {
  74. if (entry[i].action != NULL)
  75. {
  76. CLI_PRINTF("%-30s- (%s)%s\r\n", entry[i].name, entry[i].arg_type, entry[i].desp);
  77. }
  78. else
  79. {
  80. CLI_PRINTF("%-30s- (sub_menu)%s\r\n", entry[i].name, entry[i].desp);
  81. }
  82. }
  83. }
  84. void print_arg_desp(command_entry *entry)
  85. {
  86. int len, i;
  87. char *d[] = {"<one-byte 1 or 0x01>", "<two-byte 1 or 0x01>", "<four-byte 1 or 0x01>", "<string \"abc\" >", "<array, {1,2,3}>"};
  88. char *desp;
  89. len = strlen(entry->arg_type);
  90. if (len == 0)
  91. {
  92. return;
  93. }
  94. for (i = 0; i < len; i++)
  95. {
  96. if (entry->arg_type[i] == 'u')
  97. desp = d[0];
  98. else if (entry->arg_type[i] == 'v')
  99. desp = d[1];
  100. else if (entry->arg_type[i] == 'w')
  101. desp = d[2];
  102. else if (entry->arg_type[i] == 's')
  103. desp = d[3];
  104. else if (entry->arg_type[i] == 'a')
  105. desp = d[4];
  106. else
  107. desp = "";
  108. if (entry->arg_desp == NULL)
  109. {
  110. CLI_PRINTF("arg%d - %c%s\r\n", i, entry->arg_type[i], desp);
  111. }
  112. else
  113. {
  114. CLI_PRINTF("arg%d - %c%s %s\r\n", i, entry->arg_type[i], desp, entry->arg_desp[i]);
  115. }
  116. }
  117. }
  118. static unsigned char hex_to_int(unsigned char ch)
  119. {
  120. return ch - (ch >= 'a' ? 'a' - 10 : (ch >= 'A' ? 'A' - 10 : (ch <= '9' ? '0' : 0)));
  121. }
  122. static unsigned int string_to_unsigned_int(char *str, unsigned int *array, int param_len_max)
  123. {
  124. int result = 0;
  125. int base = 10;
  126. int i;
  127. int length;
  128. int pflag = 0;
  129. length = strlen(str);
  130. if (length == 0)
  131. return 1;
  132. if (str[0] == '-')
  133. pflag = 1;
  134. for (i = 0; i < length; i++)
  135. {
  136. unsigned char next = str[i];
  137. if (i == 0 && next == '-')
  138. {
  139. // do noting
  140. }
  141. else if ((next == 'x' || next == 'X') && result == 0 && (i == 1 || i == 2))
  142. {
  143. base = 16;
  144. }
  145. else
  146. {
  147. unsigned char value = hex_to_int(next);
  148. if (value < base)
  149. {
  150. result = result * base + value;
  151. }
  152. else
  153. {
  154. return 1;
  155. }
  156. }
  157. }
  158. if (pflag == 1)
  159. {
  160. result = ~result + 1;
  161. }
  162. *array = result;
  163. return 0;
  164. }
  165. //"s" "abc"
  166. // "str\r\n"
  167. int string_to_s(char *str, unsigned int *array, int param_len_max)
  168. {
  169. int i = 0, j = 0; /*, bs=0*/
  170. char *ps = (char *)array;
  171. if (str[0] != '"')
  172. return 1;
  173. j = 0;
  174. for (i = 0; str[i + 1] != '"';)
  175. {
  176. if (str[i + 1] == '\\')
  177. {
  178. if (str[i + 1 + 2] == 'n')
  179. {
  180. ps[j] = '\n';
  181. i++;
  182. }
  183. else if (str[i + 1 + 2] == 'r')
  184. {
  185. ps[j] = '\r';
  186. i++;
  187. }
  188. else
  189. {
  190. ps[j] = '\\';
  191. }
  192. }
  193. else
  194. {
  195. ps[j] = str[i + 1];
  196. }
  197. j++;
  198. if (j + 1 > param_len_max)
  199. {
  200. return 1;
  201. }
  202. i++;
  203. }
  204. ps[j] = 0;
  205. return 0;
  206. }
  207. // "a" {1,2,3}
  208. int string_to_a(char *str, unsigned int *array, int param_len_max)
  209. {
  210. int i, index = 0, count = 0, flag = 0;
  211. char s[CLI_IPT_ARG_LEN] = {0};
  212. if (str[0] != '{')
  213. return 1;
  214. for (i = 0; str[i] != '\0'; i++)
  215. {
  216. if (str[i] == '{')
  217. {
  218. index = 0;
  219. }
  220. else if (str[i] == ',')
  221. {
  222. s[index] = 0;
  223. if (string_to_unsigned_int(s, array + count, CLI_IPT_ARG_LEN) != 0)
  224. return 1;
  225. index = 0;
  226. count++;
  227. }
  228. else if (str[i] == '}')
  229. {
  230. flag = 1;
  231. s[index] = 0;
  232. if (string_to_unsigned_int(s, array + count, CLI_IPT_ARG_LEN) != 0)
  233. return 1;
  234. }
  235. else
  236. {
  237. s[index++] = str[i];
  238. }
  239. }
  240. if (flag == 0)
  241. return 1;
  242. return 0;
  243. }
  244. int command_action_exec(command_entry *entry, char *param, int param_len_max)
  245. {
  246. int len, i, res = 0;
  247. len = strlen(entry->arg_type);
  248. // CLI_PRINTF("arg is:");
  249. for (i = 0; i < len; i++)
  250. {
  251. if (entry->arg_type[i] == 'u')
  252. res = string_to_unsigned_int(param + param_len_max * i, cli_ipt_arg_list[i], CLI_IPT_ARG_LEN);
  253. else if (entry->arg_type[i] == 'v')
  254. res = string_to_unsigned_int(param + param_len_max * i, cli_ipt_arg_list[i], CLI_IPT_ARG_LEN);
  255. else if (entry->arg_type[i] == 'w')
  256. res = string_to_unsigned_int(param + param_len_max * i, cli_ipt_arg_list[i], CLI_IPT_ARG_LEN);
  257. else if (entry->arg_type[i] == 's')
  258. res = string_to_s(param + param_len_max * i, cli_ipt_arg_list[i], CLI_IPT_ARG_LEN * sizeof(unsigned int));
  259. else if (entry->arg_type[i] == 'a')
  260. res = string_to_a(param + param_len_max * i, cli_ipt_arg_list[i], CLI_IPT_ARG_LEN);
  261. else
  262. {
  263. }
  264. if (res == 1)
  265. {
  266. return res;
  267. }
  268. // CLI_PRINTF("%d ", cli_ipt_arg_list[i][0]);
  269. }
  270. // CLI_PRINTF("\r\n");
  271. return res;
  272. }
  273. int stricmp(const char *a, const char *b)
  274. {
  275. int i, len;
  276. if (strlen(a) != strlen(b))
  277. return 1;
  278. len = strlen(a);
  279. for (i = 0; i < len; i++)
  280. {
  281. if (toupper((unsigned int)a[i]) != toupper((unsigned int)b[i]))
  282. {
  283. return 1;
  284. }
  285. }
  286. return 0;
  287. }
  288. int command_match(const char *cmd, int param_len_max, int num, command_entry **entry_in, command_entry **entry_out)
  289. {
  290. int i, len, res = 0;
  291. len = len_of_menu(*entry_in);
  292. for (i = 0; i < len; i++)
  293. {
  294. if (stricmp(cmd, (*entry_in)->name) == 0)
  295. {
  296. if ((*entry_in)->action != NULL)
  297. {
  298. *entry_out = *entry_in;
  299. res = 1;
  300. }
  301. else
  302. {
  303. *entry_out = (command_entry *)((*entry_in)->arg_type);
  304. res = 2;
  305. }
  306. break;
  307. }
  308. (*entry_in)++;
  309. }
  310. if (i == len)
  311. {
  312. res = 0;
  313. }
  314. return res;
  315. }
  316. int command_entry_match(command_entry *main_table, const char *cmd)
  317. {
  318. static command_entry *ein, *eout, *ehint;
  319. int num, res = 2;
  320. char *plist;
  321. ein = main_table;
  322. eout = (command_entry *)NULL;
  323. ehint = ein;
  324. memset(arg_str_list, 0, sizeof(arg_str_list));
  325. num = cmd_to_param(cmd, (char *)arg_str_list, CLI_IPT_ARG_STR_LEN);
  326. if (num == 0)
  327. {
  328. // print_main_menu(main_table);
  329. CLI_PRINTF("# ");
  330. return 0;
  331. }
  332. plist = (char *)arg_str_list;
  333. while (res == 2)
  334. {
  335. res = command_match((const char *)plist, CLI_IPT_ARG_STR_LEN, num, &ein, &eout);
  336. if (res == 0)
  337. {
  338. // printf("commands or params unmatched!\r\n");
  339. print_entry(ehint);
  340. CLI_PRINTF("# ");
  341. return 0;
  342. }
  343. else if (res == 1)
  344. {
  345. if (command_action_exec(eout, plist + CLI_IPT_ARG_STR_LEN, CLI_IPT_ARG_STR_LEN) == 0)
  346. {
  347. eout->action();
  348. }
  349. else
  350. {
  351. // printf("argument error!\r\n");
  352. print_arg_desp(eout);
  353. }
  354. }
  355. else
  356. {
  357. plist = plist + CLI_IPT_ARG_STR_LEN;
  358. ein = eout;
  359. ehint = ein;
  360. }
  361. }
  362. CLI_PRINTF("# ");
  363. return 1;
  364. }
  365. void print_main_menu(command_entry *main_table)
  366. {
  367. print_entry(main_table);
  368. }
  369. int case_chg(char a)
  370. {
  371. if (a >= 'A' && a <= 'Z')
  372. a = a - 'A' + 'a';
  373. return a;
  374. }
  375. int str_include(const char *s, const char *str)
  376. {
  377. int i;
  378. if (strlen(s) > strlen(str))
  379. return -1;
  380. for (i = 0; i < strlen(s); i++)
  381. {
  382. if (case_chg(s[i]) != case_chg(str[i]))
  383. return -1;
  384. }
  385. return 0;
  386. }
  387. int command_entry_hint(command_entry *main_table, const char *cmd, char *out)
  388. {
  389. static command_entry *ein, *eout;
  390. int num, res = 2, i, len, cnt;
  391. char *plist, tmp[CLI_IPT_ARG_STR_LEN];
  392. ein = main_table;
  393. eout = ein;
  394. memset(arg_str_list, 0, sizeof(arg_str_list));
  395. num = cmd_to_param(cmd, (char *)arg_str_list, CLI_IPT_ARG_STR_LEN);
  396. if (num == 0)
  397. {
  398. // print_main_menu(main_table);
  399. // CLI_PRINTF("# ");
  400. return 0;
  401. }
  402. plist = (char *)arg_str_list;
  403. while (res == 2)
  404. {
  405. res = command_match((const char *)plist, CLI_IPT_ARG_STR_LEN, num, &ein, &eout);
  406. if (res == 0)
  407. {
  408. if (strlen(plist) == 0)
  409. {
  410. if (cmd[strlen(cmd) - 1] != ' ') //"s1" "s2"
  411. {
  412. strcpy(out, cmd);
  413. strcat(out, " ");
  414. return 3;
  415. }
  416. }
  417. len = len_of_menu(eout);
  418. cnt = 0;
  419. strcpy(out, "");
  420. for (i = 0; i < len; i++)
  421. {
  422. if (str_include(plist, eout[i].name) == 0)
  423. {
  424. cnt++;
  425. strcpy(out, eout[i].name);
  426. strcat(out, " ");
  427. }
  428. }
  429. if (cnt == 1)
  430. {
  431. strcpy(tmp, out);
  432. strcpy(out, "");
  433. for (i = 0; i < (num - 1); i++)
  434. {
  435. strcat(out, arg_str_list[i]);
  436. strcat(out, " ");
  437. }
  438. strcat(out, tmp);
  439. return 2;
  440. }
  441. else
  442. {
  443. return 1;
  444. }
  445. }
  446. else if (res == 1) // action
  447. {
  448. strcpy(out, "");
  449. for (i = 0; i < num; i++)
  450. {
  451. strcat(out, arg_str_list[i]);
  452. strcat(out, " ");
  453. if (strlen(out) >= ((unsigned int)CLI_IPT_ARG_STR_LEN >> 1))
  454. {
  455. strcat(out, "...");
  456. break;
  457. }
  458. }
  459. return 3;
  460. }
  461. else // sub_menu
  462. {
  463. plist = plist + CLI_IPT_ARG_STR_LEN;
  464. ein = eout;
  465. }
  466. }
  467. return 1;
  468. }