opts.c 2.3 KB

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