opts.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <getopt.h>
  12. #include <faux/faux.h>
  13. #include <faux/str.h>
  14. #include <faux/list.h>
  15. #include <klish/ktp_session.h>
  16. #include "private.h"
  17. /** @brief Initialize option structure by defaults
  18. */
  19. struct options *opts_init(void)
  20. {
  21. struct options *opts = NULL;
  22. opts = faux_zmalloc(sizeof(*opts));
  23. assert(opts);
  24. // Initialize
  25. opts->verbose = BOOL_FALSE;
  26. opts->unix_socket_path = faux_str_dup(KLISH_DEFAULT_UNIX_SOCKET_PATH);
  27. // Don't free command list because elements are the pointers to
  28. // command line options and don't need to be freed().
  29. opts->commands = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  30. NULL, NULL, NULL);
  31. return opts;
  32. }
  33. /** @brief Free options structure
  34. */
  35. void opts_free(struct options *opts)
  36. {
  37. if (!opts)
  38. return;
  39. faux_str_free(opts->unix_socket_path);
  40. faux_list_free(opts->commands);
  41. faux_free(opts);
  42. }
  43. /** @brief Parse command line options
  44. */
  45. int opts_parse(int argc, char *argv[], struct options *opts)
  46. {
  47. static const char *shortopts = "hvS:c:";
  48. static const struct option longopts[] = {
  49. {"socket", 1, NULL, 'S'},
  50. {"help", 0, NULL, 'h'},
  51. {"verbose", 0, NULL, 'v'},
  52. {"command", 1, NULL, 'c'},
  53. {NULL, 0, NULL, 0}
  54. };
  55. optind = 1;
  56. while(1) {
  57. int opt = 0;
  58. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  59. if (-1 == opt)
  60. break;
  61. switch (opt) {
  62. case 'S':
  63. faux_str_free(opts->unix_socket_path);
  64. opts->unix_socket_path = faux_str_dup(optarg);
  65. break;
  66. case 'v':
  67. opts->verbose = BOOL_TRUE;
  68. break;
  69. case 'h':
  70. help(0, argv[0]);
  71. _exit(0);
  72. break;
  73. case 'c':
  74. faux_list_add(opts->commands, optarg);
  75. break;
  76. default:
  77. help(-1, argv[0]);
  78. _exit(-1);
  79. break;
  80. }
  81. }
  82. return 0;
  83. }
  84. /** @brief Print help message
  85. */
  86. void help(int status, const char *argv0)
  87. {
  88. const char *name = NULL;
  89. if (!argv0)
  90. return;
  91. // Find the basename
  92. name = strrchr(argv0, '/');
  93. if (name)
  94. name++;
  95. else
  96. name = argv0;
  97. if (status != 0) {
  98. fprintf(stderr, "Try `%s -h' for more information.\n",
  99. name);
  100. } else {
  101. printf("Version : %s\n", VERSION);
  102. printf("Usage : %s [options]\n", name);
  103. printf("Klish client\n");
  104. printf("Options :\n");
  105. printf("\t-S <path>, --socket=<path> UNIX socket path.\n");
  106. printf("\t-h, --help Print this help.\n");
  107. printf("\t-v, --verbose Be verbose.\n");
  108. printf("\t-c <line>, --command=<line> Command to execute.\n");
  109. }
  110. }