1
0

clish.c 10 KB

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