clish.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. bool_t cmd = BOOL_FALSE; /* -c option */
  66. lub_list_t *cmds; /* Commands defined by -c */
  67. lub_list_node_t *iter;
  68. /* Signal vars */
  69. struct sigaction sigpipe_act;
  70. sigset_t sigpipe_set;
  71. static const char *shortopts = "hvs:ledx:w:i:bqu8okt:c:";
  72. #ifdef HAVE_GETOPT_H
  73. static const struct option longopts[] = {
  74. {"help", 0, NULL, 'h'},
  75. {"version", 0, NULL, 'v'},
  76. {"socket", 1, NULL, 's'},
  77. {"lockless", 0, NULL, 'l'},
  78. {"stop-on-error", 0, NULL, 'e'},
  79. {"dry-run", 0, NULL, 'd'},
  80. {"xml-path", 1, NULL, 'x'},
  81. {"view", 1, NULL, 'w'},
  82. {"viewid", 1, NULL, 'i'},
  83. {"background", 0, NULL, 'b'},
  84. {"quiet", 0, NULL, 'q'},
  85. {"utf8", 0, NULL, 'u'},
  86. {"8bit", 0, NULL, '8'},
  87. {"log", 0, NULL, 'o'},
  88. {"check", 0, NULL, 'k'},
  89. {"timeout", 1, NULL, 't'},
  90. {"command", 1, NULL, 'c'},
  91. {NULL, 0, NULL, 0}
  92. };
  93. #endif
  94. /* Ignore SIGPIPE */
  95. sigemptyset(&sigpipe_set);
  96. sigaddset(&sigpipe_set, SIGPIPE);
  97. sigpipe_act.sa_flags = 0;
  98. sigpipe_act.sa_mask = sigpipe_set;
  99. sigpipe_act.sa_handler = SIG_IGN;
  100. sigaction(SIGPIPE, &sigpipe_act, NULL);
  101. #if HAVE_LOCALE_H
  102. /* Set current locale */
  103. setlocale(LC_ALL, "");
  104. #endif
  105. /* Var initialization */
  106. cmds = lub_list_new(NULL);
  107. /* Parse command line options */
  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 'c':
  164. char *str;
  165. cmd = BOOL_TRUE;
  166. quiet = BOOL_TRUE;
  167. str = strdup(optarg);
  168. lub_list_add(cmds, str);
  169. break;
  170. case 'h':
  171. help(0, argv[0]);
  172. exit(0);
  173. break;
  174. case 'v':
  175. version(VERSION);
  176. exit(0);
  177. break;
  178. default:
  179. help(-1, argv[0]);
  180. goto end;
  181. break;
  182. }
  183. }
  184. /* Validate command line options */
  185. if (utf8 && bit8) {
  186. fprintf(stderr, "The -u and -8 options can't be used together.\n");
  187. goto end;
  188. }
  189. /* Create shell instance */
  190. if (quiet)
  191. outfd = fopen("/dev/null", "w");
  192. shell = clish_shell_new(&my_hooks, NULL, NULL, outfd, stop_on_error);
  193. if (!shell) {
  194. fprintf(stderr, "Cannot run clish.\n");
  195. goto end;
  196. }
  197. /* Load the XML files */
  198. clish_shell_load_scheme(shell, xml_path);
  199. /* Set communication to the konfd */
  200. clish_shell__set_socket(shell, socket_path);
  201. /* Set lockless mode */
  202. if (lockless)
  203. clish_shell__set_lockfile(shell, NULL);
  204. /* Set interactive mode */
  205. if (!interactive)
  206. clish_shell__set_interactive(shell, interactive);
  207. /* Set startup view */
  208. if (view)
  209. clish_shell__set_startup_view(shell, view);
  210. /* Set startup viewid */
  211. if (viewid)
  212. clish_shell__set_startup_viewid(shell, viewid);
  213. /* Set UTF-8 or 8-bit mode */
  214. if (utf8 || bit8)
  215. clish_shell__set_utf8(shell, utf8);
  216. else {
  217. #if HAVE_LANGINFO_CODESET
  218. /* Autodetect encoding */
  219. if (!strcmp(nl_langinfo(CODESET), "UTF-8"))
  220. clish_shell__set_utf8(shell, BOOL_TRUE);
  221. #else
  222. /* The default is 8-bit if locale is not supported */
  223. clish_shell__set_utf8(shell, BOOL_FALSE);
  224. #endif
  225. }
  226. /* Set logging */
  227. if (log)
  228. clish_shell__set_log(shell, log);
  229. /* Set idle timeout */
  230. if (istimeout)
  231. clish_shell__set_timeout(shell, timeout);
  232. /* Set source of command stream: files or interactive tty */
  233. if(optind < argc) {
  234. int i;
  235. /* Run the commands from the files */
  236. for (i = argc - 1; i >= optind; i--)
  237. clish_shell_push_file(shell, argv[i], stop_on_error);
  238. } else {
  239. /* The interactive shell */
  240. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"),
  241. stop_on_error);
  242. }
  243. /* Execute startup */
  244. running = clish_shell_startup(shell);
  245. if (running) {
  246. fprintf(stderr, "Cannot startup clish.\n");
  247. goto end;
  248. }
  249. if (cmd) {
  250. /* Iterate cmds */
  251. for(iter = lub_list__get_head(cmds);
  252. iter; iter = lub_list_node__get_next(iter)) {
  253. char *str = (char *)lub_list_node__get_data(iter);
  254. clish_shell_forceline(shell, str, NULL);
  255. }
  256. } else {
  257. /* Main loop */
  258. result = clish_shell_loop(shell);
  259. }
  260. end:
  261. /* Cleanup */
  262. if (shell)
  263. clish_shell_delete(shell);
  264. if (quiet)
  265. fclose(outfd);
  266. /* Delete each cmds element */
  267. while ((iter = lub_list__get_head(cmds))) {
  268. lub_list_del(cmds, iter);
  269. free(lub_list_node__get_data(iter));
  270. lub_list_node_free(iter);
  271. }
  272. lub_list_free(cmds);
  273. return result;
  274. }
  275. /*--------------------------------------------------------- */
  276. /* Print help message */
  277. static void help(int status, const char *argv0)
  278. {
  279. const char *name = NULL;
  280. if (!argv0)
  281. return;
  282. /* Find the basename */
  283. name = strrchr(argv0, '/');
  284. if (name)
  285. name++;
  286. else
  287. name = argv0;
  288. if (status != 0) {
  289. fprintf(stderr, "Try `%s -h' for more information.\n",
  290. name);
  291. } else {
  292. printf("Usage: %s [options] [script_file] [script_file] ...\n", name);
  293. printf("CLI utility. Command line shell."
  294. "The part of the klish project.\n");
  295. printf("Options:\n");
  296. printf("\t-v, --version\tPrint version.\n");
  297. printf("\t-h, --help\tPrint this help.\n");
  298. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  299. "\n\t\tof the konfd daemon.\n");
  300. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  301. printf("\t-e, --stop-on-error\tStop script execution on error.\n");
  302. printf("\t-b, --background\tStart shell using non-interactive mode.\n");
  303. printf("\t-q, --quiet\tDisable echo while executing commands\n\t\tfrom the file stream.\n");
  304. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  305. printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
  306. printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
  307. printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
  308. printf("\t-u, --utf8\tForce UTF-8 encoding.\n");
  309. printf("\t-8, --8bit\tForce 8-bit encoding.\n");
  310. printf("\t-o, --log\tEnable command logging to syslog's local0.\n");
  311. printf("\t-k, --check\tCheck input files for syntax errors only.\n");
  312. printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
  313. printf("\t-c <command>, --command=<command>\tExecute specified command(s).\n\t\tMultiple options are possible.\n");
  314. }
  315. }