1
0

ktool.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * --------------------------------------
  3. * clish.c
  4. *
  5. * A console client for libclish
  6. * --------------------------------------
  7. */
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif /* HAVE_CONFIG_H */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <signal.h>
  17. #if WITH_INTERNAL_GETOPT
  18. #include "libc/getopt.h"
  19. #else
  20. #ifdef HAVE_GETOPT_H
  21. #include <getopt.h>
  22. #endif
  23. #endif
  24. #include "lub/list.h"
  25. #include "clish/shell.h"
  26. #define QUOTE(t) #t
  27. #define version(v) printf("%s\n", v)
  28. /*--------------------------------------------------------- */
  29. /* Print help message */
  30. static void help(int status, const char *argv0)
  31. {
  32. const char *name = NULL;
  33. if (!argv0)
  34. return;
  35. /* Find the basename */
  36. name = strrchr(argv0, '/');
  37. if (name)
  38. name++;
  39. else
  40. name = argv0;
  41. if (status != 0) {
  42. fprintf(stderr, "Try `%s -h' for more information.\n",
  43. name);
  44. } else {
  45. printf("Usage: %s [options] [script_file] [script_file] ...\n", name);
  46. printf("CLI utility. Command line shell."
  47. "The part of the klish project.\n");
  48. printf("Options:\n");
  49. printf("\t-v, --version\tPrint version.\n");
  50. printf("\t-h, --help\tPrint this help.\n");
  51. printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
  52. }
  53. }
  54. /*--------------------------------------------------------- */
  55. int main(int argc, char **argv)
  56. {
  57. int result = -1;
  58. clish_shell_t *shell = NULL;
  59. /* Command line options */
  60. const char *xml_path = getenv("CLISH_PATH");
  61. FILE *outfd = stdout;
  62. static const char *shortopts = "hvx:";
  63. #ifdef HAVE_GETOPT_LONG
  64. static const struct option longopts[] = {
  65. {"help", 0, NULL, 'h'},
  66. {"version", 0, NULL, 'v'},
  67. {"xml-path", 1, NULL, 'x'},
  68. {NULL, 0, NULL, 0}
  69. };
  70. #endif
  71. /* Parse command line options */
  72. while(1) {
  73. int opt;
  74. #ifdef HAVE_GETOPT_LONG
  75. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  76. #else
  77. opt = getopt(argc, argv, shortopts);
  78. #endif
  79. if (-1 == opt)
  80. break;
  81. switch (opt) {
  82. case 'x':
  83. xml_path = optarg;
  84. break;
  85. case 'h':
  86. help(0, argv[0]);
  87. exit(0);
  88. break;
  89. case 'v':
  90. version(VERSION);
  91. exit(0);
  92. break;
  93. default:
  94. help(-1, argv[0]);
  95. goto end;
  96. break;
  97. }
  98. }
  99. /* Create shell instance */
  100. shell = clish_shell_new(NULL, outfd, BOOL_FALSE);
  101. if (!shell) {
  102. fprintf(stderr, "Error: Can't create shell instance.\n");
  103. goto end;
  104. }
  105. /* Load the XML files */
  106. if (clish_shell_load_scheme(shell, xml_path))
  107. goto end;
  108. clish_shell_xml2c(shell);
  109. end:
  110. /* Cleanup */
  111. if (shell)
  112. clish_shell_delete(shell);
  113. return result;
  114. }