prd_utils_kv.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2023 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <getopt.h>
  8. #include <prd_utils_kv.h>
  9. #define PROGRAM_NAME "prd_utils_kv"
  10. typedef struct
  11. {
  12. int op_code; /* 0: set, 1:get; 2: clean */
  13. char key[PRD_UTILS_KEY_MAX_LENGTH];
  14. char val[PRD_UTILS_VAL_MAX_LENGTH];
  15. } cmd_params_s;
  16. static const char *shortopts = "hs:g:c:";
  17. static const struct option long_options[] =
  18. {
  19. {"help", no_argument, 0, 'h'},
  20. {"SetParam", required_argument, 0, 's'},
  21. {"GetParam", required_argument, 0, 'g'},
  22. {"CleanParam", required_argument, 0, 'c'},
  23. {0, 0, 0, 0 } // Act as end of option
  24. };
  25. static void usage(char *program_name)
  26. {
  27. printf("Usage: %s [OPTION]\n", program_name);
  28. printf(" -h, --help display this help and exit\n");
  29. printf(" -s, --SetParam set param, for example: %s -s sn=123456abcde\n", program_name);
  30. printf(" -g, --GetParam get param, for example: %s -g sn\n", program_name);
  31. printf(" -c, --CleanParam clean param, for example: %s -c sn\n", program_name);
  32. }
  33. static int parse_params(int argc, char *argv[], cmd_params_s *params)
  34. {
  35. int c;
  36. int option_index = 0;
  37. int option_count = 0;
  38. char *program_name = PROGRAM_NAME;
  39. char *equal_sign;
  40. int pos;
  41. while ((c = getopt_long(argc, argv, shortopts, long_options, &option_index)) != -1)
  42. {
  43. switch (c)
  44. {
  45. case 'h':
  46. usage(program_name);
  47. exit(0);
  48. case 's':
  49. params->op_code = 0;
  50. equal_sign = strchr(optarg, '=');
  51. if (equal_sign == NULL)
  52. {
  53. printf("command is invalid, please flow:\n");
  54. usage(program_name);
  55. exit(-1);
  56. }
  57. pos = equal_sign - optarg;
  58. strncpy(params->key, optarg, pos);
  59. strncpy(params->val, optarg + pos + 1, strlen(optarg) - pos);
  60. option_count++;
  61. break;
  62. case 'g':
  63. params->op_code = 1;
  64. sscanf(optarg, "%s", params->key);
  65. option_count++;
  66. break;
  67. case 'c':
  68. params->op_code = 2;
  69. sscanf(optarg, "%s", params->key);
  70. option_count++;
  71. break;
  72. default:
  73. printf("Not supported opt:'%c'\n", c);
  74. return -1;
  75. }
  76. }
  77. return option_count;
  78. }
  79. int main(int argc, char *argv[])
  80. {
  81. int ret;
  82. char *val;
  83. cmd_params_s cmd_params;
  84. memset(&cmd_params, 0, sizeof(cmd_params));
  85. int option_count = parse_params(argc, argv, &cmd_params);
  86. if (option_count == 0)
  87. {
  88. usage(PROGRAM_NAME);
  89. exit(0);
  90. }
  91. switch (cmd_params.op_code)
  92. {
  93. case 0: // set
  94. ret = prd_utils_set_kv(cmd_params.key, cmd_params.val);
  95. printf("set '%s'='%s' %s\n", cmd_params.key, cmd_params.val, (ret == 0) ? "OK" : "failed");
  96. break;
  97. case 1: // get
  98. ret = prd_utils_get_kv(cmd_params.key, &val);
  99. if (ret != 0)
  100. {
  101. printf("get sys param '%s' failed\n", cmd_params.key);
  102. return ret;
  103. }
  104. printf("Got sys param '%s'='%s'\n", cmd_params.key, val);
  105. break;
  106. case 2: // clean;
  107. ret = prd_utils_clean_kv(cmd_params.key);
  108. printf("clean sys param '%s' %s\n", cmd_params.key, (ret == 0) ? "OK" : "failed");
  109. break;
  110. }
  111. return 0;
  112. }