clish.cpp 8.2 KB

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