1
0

clish.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. optind = 0;
  109. while(1) {
  110. int opt;
  111. #ifdef HAVE_GETOPT_H
  112. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  113. #else
  114. opt = getopt(argc, argv, shortopts);
  115. #endif
  116. if (-1 == opt)
  117. break;
  118. switch (opt) {
  119. case 's':
  120. socket_path = optarg;
  121. break;
  122. case 'l':
  123. lockless = BOOL_TRUE;
  124. break;
  125. case 'e':
  126. stop_on_error = BOOL_TRUE;
  127. break;
  128. case 'b':
  129. interactive = BOOL_FALSE;
  130. break;
  131. case 'q':
  132. quiet = BOOL_TRUE;
  133. break;
  134. case 'u':
  135. utf8 = BOOL_TRUE;
  136. break;
  137. case '8':
  138. bit8 = BOOL_TRUE;
  139. break;
  140. case 'o':
  141. log = BOOL_TRUE;
  142. break;
  143. case 'd':
  144. my_hooks.script_fn = clish_dryrun_callback;
  145. break;
  146. case 'x':
  147. xml_path = optarg;
  148. break;
  149. case 'w':
  150. view = optarg;
  151. break;
  152. case 'i':
  153. viewid = optarg;
  154. break;
  155. case 'k':
  156. lockless = BOOL_TRUE;
  157. my_hooks.script_fn = clish_dryrun_callback;
  158. my_hooks.config_fn = NULL;
  159. break;
  160. case 't':
  161. istimeout = BOOL_TRUE;
  162. timeout = atoi(optarg);
  163. break;
  164. case 'c':
  165. char *str;
  166. cmd = BOOL_TRUE;
  167. quiet = BOOL_TRUE;
  168. str = strdup(optarg);
  169. lub_list_add(cmds, str);
  170. break;
  171. case 'h':
  172. help(0, argv[0]);
  173. exit(0);
  174. break;
  175. case 'v':
  176. version(VERSION);
  177. exit(0);
  178. break;
  179. default:
  180. help(-1, argv[0]);
  181. goto end;
  182. break;
  183. }
  184. }
  185. /* Validate command line options */
  186. if (utf8 && bit8) {
  187. fprintf(stderr, "The -u and -8 options can't be used together.\n");
  188. goto end;
  189. }
  190. /* Create shell instance */
  191. if (quiet)
  192. outfd = fopen("/dev/null", "w");
  193. shell = clish_shell_new(&my_hooks, NULL, NULL, outfd, stop_on_error);
  194. if (!shell) {
  195. fprintf(stderr, "Cannot run clish.\n");
  196. goto end;
  197. }
  198. /* Load the XML files */
  199. clish_shell_load_scheme(shell, xml_path);
  200. /* Set communication to the konfd */
  201. clish_shell__set_socket(shell, socket_path);
  202. /* Set lockless mode */
  203. if (lockless)
  204. clish_shell__set_lockfile(shell, NULL);
  205. /* Set interactive mode */
  206. if (!interactive)
  207. clish_shell__set_interactive(shell, interactive);
  208. /* Set startup view */
  209. if (view)
  210. clish_shell__set_startup_view(shell, view);
  211. /* Set startup viewid */
  212. if (viewid)
  213. clish_shell__set_startup_viewid(shell, viewid);
  214. /* Set UTF-8 or 8-bit mode */
  215. if (utf8 || bit8)
  216. clish_shell__set_utf8(shell, utf8);
  217. else {
  218. #if HAVE_LANGINFO_CODESET
  219. /* Autodetect encoding */
  220. if (!strcmp(nl_langinfo(CODESET), "UTF-8"))
  221. clish_shell__set_utf8(shell, BOOL_TRUE);
  222. #else
  223. /* The default is 8-bit if locale is not supported */
  224. clish_shell__set_utf8(shell, BOOL_FALSE);
  225. #endif
  226. }
  227. /* Set logging */
  228. if (log)
  229. clish_shell__set_log(shell, log);
  230. /* Set idle timeout */
  231. if (istimeout)
  232. clish_shell__set_timeout(shell, timeout);
  233. /* Set source of command stream: files or interactive tty */
  234. if(optind < argc) {
  235. int i;
  236. /* Run the commands from the files */
  237. for (i = argc - 1; i >= optind; i--)
  238. clish_shell_push_file(shell, argv[i], stop_on_error);
  239. } else {
  240. /* The interactive shell */
  241. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"),
  242. stop_on_error);
  243. }
  244. /* Execute startup */
  245. running = clish_shell_startup(shell);
  246. if (running) {
  247. fprintf(stderr, "Cannot startup clish.\n");
  248. goto end;
  249. }
  250. if (cmd) {
  251. /* Iterate cmds */
  252. for(iter = lub_list__get_head(cmds);
  253. iter; iter = lub_list_node__get_next(iter)) {
  254. char *str = (char *)lub_list_node__get_data(iter);
  255. clish_shell_forceline(shell, str, NULL);
  256. }
  257. } else {
  258. /* Main loop */
  259. result = clish_shell_loop(shell);
  260. }
  261. end:
  262. /* Cleanup */
  263. if (shell)
  264. clish_shell_delete(shell);
  265. if (quiet)
  266. fclose(outfd);
  267. /* Delete each cmds element */
  268. while ((iter = lub_list__get_head(cmds))) {
  269. lub_list_del(cmds, iter);
  270. free(lub_list_node__get_data(iter));
  271. lub_list_node_free(iter);
  272. }
  273. lub_list_free(cmds);
  274. return result;
  275. }
  276. /*--------------------------------------------------------- */
  277. /* Print help message */
  278. static void help(int status, const char *argv0)
  279. {
  280. const char *name = NULL;
  281. if (!argv0)
  282. return;
  283. /* Find the basename */
  284. name = strrchr(argv0, '/');
  285. if (name)
  286. name++;
  287. else
  288. name = argv0;
  289. if (status != 0) {
  290. fprintf(stderr, "Try `%s -h' for more information.\n",
  291. name);
  292. } else {
  293. printf("Usage: %s [options] [script_file] [script_file] ...\n", name);
  294. printf("CLI utility. Command line shell."
  295. "The part of the klish project.\n");
  296. printf("Options:\n");
  297. printf("\t-v, --version\tPrint version.\n");
  298. printf("\t-h, --help\tPrint this help.\n");
  299. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  300. "\n\t\tof the konfd daemon.\n");
  301. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  302. printf("\t-e, --stop-on-error\tStop script execution on error.\n");
  303. printf("\t-b, --background\tStart shell using non-interactive mode.\n");
  304. printf("\t-q, --quiet\tDisable echo while executing commands\n\t\tfrom the file stream.\n");
  305. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  306. printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
  307. printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
  308. printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
  309. printf("\t-u, --utf8\tForce UTF-8 encoding.\n");
  310. printf("\t-8, --8bit\tForce 8-bit encoding.\n");
  311. printf("\t-o, --log\tEnable command logging to syslog's local0.\n");
  312. printf("\t-k, --check\tCheck input files for syntax errors only.\n");
  313. printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
  314. printf("\t-c <command>, --command=<command>\tExecute specified command(s).\n\t\tMultiple options are possible.\n");
  315. }
  316. }