1
0

clish.cpp 9.3 KB

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