clish.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //-------------------------------------
  2. // clish.cpp
  3. //
  4. // A console client for libclish
  5. //-------------------------------------
  6. #ifdef HAVE_CONFIG_H
  7. #include "config.h"
  8. #endif /* HAVE_CONFIG_H */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #ifdef HAVE_GETOPT_H
  14. #include <getopt.h>
  15. #endif
  16. #include <signal.h>
  17. #if HAVE_LOCALE_H
  18. #include <locale.h>
  19. #endif
  20. #if HAVE_LANGINFO_CODESET
  21. #include <langinfo.h>
  22. #endif
  23. #include "lub/list.h"
  24. #include "clish/shell.h"
  25. #include "clish/internal.h"
  26. #ifndef VERSION
  27. #define VERSION 1.2.2
  28. #endif
  29. #define QUOTE(t) #t
  30. /* #define version(v) printf("%s\n", QUOTE(v)) */
  31. #define version(v) printf("%s\n", v)
  32. /* Hooks */
  33. static clish_shell_hooks_t my_hooks = {
  34. NULL, /* don't worry about init callback */
  35. clish_access_callback,
  36. NULL, /* don't worry about cmd_line callback */
  37. clish_script_callback,
  38. NULL, /* don't worry about fini callback */
  39. clish_config_callback,
  40. clish_log_callback,
  41. NULL /* don't register any builtin functions */
  42. };
  43. static void help(int status, const char *argv0);
  44. /*--------------------------------------------------------- */
  45. int main(int argc, char **argv)
  46. {
  47. int running;
  48. int result = -1;
  49. clish_shell_t *shell = NULL;
  50. /* Command line options */
  51. const char *socket_path = KONFD_SOCKET_PATH;
  52. bool_t lockless = BOOL_FALSE;
  53. bool_t stop_on_error = BOOL_FALSE;
  54. bool_t interactive = BOOL_TRUE;
  55. bool_t quiet = BOOL_FALSE;
  56. bool_t utf8 = BOOL_FALSE;
  57. bool_t bit8 = BOOL_FALSE;
  58. bool_t log = BOOL_FALSE;
  59. const char *xml_path = getenv("CLISH_PATH");
  60. const char *view = getenv("CLISH_VIEW");
  61. const char *viewid = getenv("CLISH_VIEWID");
  62. FILE *outfd = stdout;
  63. bool_t istimeout = BOOL_FALSE;
  64. int timeout = 0;
  65. lub_list_t *cmds; /* Commands defined by -c */
  66. lub_list_node_t *iter;
  67. /* Signal vars */
  68. struct sigaction sigpipe_act;
  69. sigset_t sigpipe_set;
  70. static const char *shortopts = "hvs:ledx:w:i:bqu8okt:c:";
  71. #ifdef HAVE_GETOPT_H
  72. static const struct option longopts[] = {
  73. {"help", 0, NULL, 'h'},
  74. {"version", 0, NULL, 'v'},
  75. {"socket", 1, NULL, 's'},
  76. {"lockless", 0, NULL, 'l'},
  77. {"stop-on-error", 0, NULL, 'e'},
  78. {"dry-run", 0, NULL, 'd'},
  79. {"xml-path", 1, NULL, 'x'},
  80. {"view", 1, NULL, 'w'},
  81. {"viewid", 1, NULL, 'i'},
  82. {"background", 0, NULL, 'b'},
  83. {"quiet", 0, NULL, 'q'},
  84. {"utf8", 0, NULL, 'u'},
  85. {"8bit", 0, NULL, '8'},
  86. {"log", 0, NULL, 'o'},
  87. {"check", 0, NULL, 'k'},
  88. {"timeout", 1, NULL, 't'},
  89. {"command", 1, NULL, 'c'},
  90. {NULL, 0, NULL, 0}
  91. };
  92. #endif
  93. /* Ignore SIGPIPE */
  94. sigemptyset(&sigpipe_set);
  95. sigaddset(&sigpipe_set, SIGPIPE);
  96. sigpipe_act.sa_flags = 0;
  97. sigpipe_act.sa_mask = sigpipe_set;
  98. sigpipe_act.sa_handler = SIG_IGN;
  99. sigaction(SIGPIPE, &sigpipe_act, NULL);
  100. #if HAVE_LOCALE_H
  101. /* Set current locale */
  102. setlocale(LC_ALL, "");
  103. #endif
  104. /* Var initialization */
  105. cmds = lub_list_new(NULL);
  106. /* Parse command line options */
  107. optind = 0;
  108. while(1) {
  109. int opt;
  110. #ifdef HAVE_GETOPT_H
  111. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  112. #else
  113. opt = getopt(argc, argv, shortopts);
  114. #endif
  115. if (-1 == opt)
  116. break;
  117. switch (opt) {
  118. case 's':
  119. socket_path = optarg;
  120. break;
  121. case 'l':
  122. lockless = BOOL_TRUE;
  123. break;
  124. case 'e':
  125. stop_on_error = BOOL_TRUE;
  126. break;
  127. case 'b':
  128. interactive = BOOL_FALSE;
  129. break;
  130. case 'q':
  131. quiet = BOOL_TRUE;
  132. break;
  133. case 'u':
  134. utf8 = BOOL_TRUE;
  135. break;
  136. case '8':
  137. bit8 = BOOL_TRUE;
  138. break;
  139. case 'o':
  140. log = BOOL_TRUE;
  141. break;
  142. case 'd':
  143. my_hooks.script_fn = clish_dryrun_callback;
  144. break;
  145. case 'x':
  146. xml_path = optarg;
  147. break;
  148. case 'w':
  149. view = optarg;
  150. break;
  151. case 'i':
  152. viewid = optarg;
  153. break;
  154. case 'k':
  155. lockless = BOOL_TRUE;
  156. my_hooks.script_fn = clish_dryrun_callback;
  157. my_hooks.config_fn = NULL;
  158. break;
  159. case 't':
  160. istimeout = BOOL_TRUE;
  161. timeout = atoi(optarg);
  162. break;
  163. case 'h':
  164. help(0, argv[0]);
  165. exit(0);
  166. break;
  167. case 'v':
  168. version(VERSION);
  169. exit(0);
  170. break;
  171. default:
  172. help(-1, argv[0]);
  173. exit(-1);
  174. break;
  175. }
  176. }
  177. /* Validate command line options */
  178. if (utf8 && bit8) {
  179. fprintf(stderr, "The -u and -8 options can't be used together.\n");
  180. goto end;
  181. }
  182. /* Create shell instance */
  183. if (quiet)
  184. outfd = fopen("/dev/null", "w");
  185. shell = clish_shell_new(&my_hooks, NULL, NULL, outfd, stop_on_error);
  186. if (!shell) {
  187. fprintf(stderr, "Cannot run clish.\n");
  188. goto end;
  189. }
  190. /* Load the XML files */
  191. clish_shell_load_scheme(shell, xml_path);
  192. /* Set communication to the konfd */
  193. clish_shell__set_socket(shell, socket_path);
  194. /* Set lockless mode */
  195. if (lockless)
  196. clish_shell__set_lockfile(shell, NULL);
  197. /* Set interactive mode */
  198. if (!interactive)
  199. clish_shell__set_interactive(shell, interactive);
  200. /* Set startup view */
  201. if (view)
  202. clish_shell__set_startup_view(shell, view);
  203. /* Set startup viewid */
  204. if (viewid)
  205. clish_shell__set_startup_viewid(shell, viewid);
  206. /* Set UTF-8 or 8-bit mode */
  207. if (utf8 || bit8)
  208. clish_shell__set_utf8(shell, utf8);
  209. else {
  210. #if HAVE_LANGINFO_CODESET
  211. /* Autodetect encoding */
  212. if (!strcmp(nl_langinfo(CODESET), "UTF-8"))
  213. clish_shell__set_utf8(shell, BOOL_TRUE);
  214. #else
  215. /* The default is 8-bit if locale is not supported */
  216. clish_shell__set_utf8(shell, BOOL_FALSE);
  217. #endif
  218. }
  219. /* Set logging */
  220. if (log)
  221. clish_shell__set_log(shell, log);
  222. /* Set idle timeout */
  223. if (istimeout)
  224. clish_shell__set_timeout(shell, timeout);
  225. /* Set source of command stream: files or interactive tty */
  226. if(optind < argc) {
  227. int i;
  228. /* Run the commands from the files */
  229. for (i = argc - 1; i >= optind; i--)
  230. clish_shell_push_file(shell, argv[i], stop_on_error);
  231. } else {
  232. /* The interactive shell */
  233. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"),
  234. stop_on_error);
  235. }
  236. /* Execute startup */
  237. running = clish_shell_startup(shell);
  238. if (running) {
  239. fprintf(stderr, "Cannot startup clish.\n");
  240. goto end;
  241. }
  242. /* Main loop */
  243. result = clish_shell_loop(shell);
  244. end:
  245. /* Cleanup */
  246. if (shell)
  247. clish_shell_delete(shell);
  248. if (quiet)
  249. fclose(outfd);
  250. /* Delete each cmds element */
  251. while ((iter = lub_list__get_head(cmds))) {
  252. lub_list_del(cmds, iter);
  253. free(lub_list_node__get_data(iter));
  254. lub_list_node_free(iter);
  255. }
  256. lub_list_free(cmds);
  257. return result;
  258. }
  259. /*--------------------------------------------------------- */
  260. /* Print help message */
  261. static void help(int status, const char *argv0)
  262. {
  263. const char *name = NULL;
  264. if (!argv0)
  265. return;
  266. /* Find the basename */
  267. name = strrchr(argv0, '/');
  268. if (name)
  269. name++;
  270. else
  271. name = argv0;
  272. if (status != 0) {
  273. fprintf(stderr, "Try `%s -h' for more information.\n",
  274. name);
  275. } else {
  276. printf("Usage: %s [options] [script_file] [script_file] ...\n", name);
  277. printf("CLI utility. Command line shell."
  278. "The part of the klish project.\n");
  279. printf("Options:\n");
  280. printf("\t-v, --version\tPrint version.\n");
  281. printf("\t-h, --help\tPrint this help.\n");
  282. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  283. "of the konfd daemon.\n");
  284. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  285. printf("\t-e, --stop-on-error\tStop script execution on error.\n");
  286. printf("\t-b, --background\tStart shell using non-interactive mode.\n");
  287. printf("\t-q, --quiet\tDisable echo while executing commands from the file stream.\n");
  288. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  289. printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
  290. printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
  291. printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
  292. printf("\t-u, --utf8\tForce UTF-8 encoding.\n");
  293. printf("\t-8, --8bit\tForce 8-bit encoding.\n");
  294. printf("\t-o, --log\tEnable command logging to syslog's local0.\n");
  295. printf("\t-k, --check\tCheck input files for syntax errors only.\n");
  296. printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
  297. }
  298. }