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. #include <fcntl.h>
  17. #if WITH_INTERNAL_GETOPT
  18. #include "libc/getopt.h"
  19. #else
  20. #ifdef HAVE_GETOPT_H
  21. #include <getopt.h>
  22. #endif
  23. #endif
  24. #include <signal.h>
  25. #if HAVE_LOCALE_H
  26. #include <locale.h>
  27. #endif
  28. #if HAVE_LANGINFO_CODESET
  29. #include <langinfo.h>
  30. #endif
  31. #include "lub/list.h"
  32. #include "lub/system.h"
  33. #include "lub/log.h"
  34. #include "clish/shell.h"
  35. #include "clish/internal.h"
  36. #define QUOTE(t) #t
  37. /* #define version(v) printf("%s\n", QUOTE(v)) */
  38. #define version(v) printf("%s\n", v)
  39. /* Hooks */
  40. static clish_shell_hooks_t my_hooks = {
  41. NULL, /* don't worry about init callback */
  42. clish_access_callback,
  43. NULL, /* don't worry about cmd_line callback */
  44. clish_script_callback,
  45. NULL, /* don't worry about fini callback */
  46. clish_config_callback,
  47. clish_log_callback,
  48. NULL /* don't register any builtin functions */
  49. };
  50. static void sighandler(int signo);
  51. static void help(int status, const char *argv0);
  52. /*--------------------------------------------------------- */
  53. int main(int argc, char **argv)
  54. {
  55. int running;
  56. int result = -1;
  57. clish_shell_t *shell = NULL;
  58. /* Command line options */
  59. const char *socket_path = KONFD_SOCKET_PATH;
  60. bool_t lockless = BOOL_FALSE;
  61. bool_t stop_on_error = BOOL_FALSE;
  62. bool_t interactive = BOOL_TRUE;
  63. bool_t quiet = BOOL_FALSE;
  64. bool_t utf8 = BOOL_FALSE;
  65. bool_t bit8 = BOOL_FALSE;
  66. bool_t log = BOOL_FALSE;
  67. int log_facility = LOG_LOCAL0;
  68. const char *xml_path = getenv("CLISH_PATH");
  69. const char *view = getenv("CLISH_VIEW");
  70. const char *viewid = getenv("CLISH_VIEWID");
  71. FILE *outfd = stdout;
  72. bool_t istimeout = BOOL_FALSE;
  73. int timeout = 0;
  74. bool_t cmd = BOOL_FALSE; /* -c option */
  75. lub_list_t *cmds; /* Commands defined by -c */
  76. lub_list_node_t *iter;
  77. const char *histfile = "~/.clish_history";
  78. char *histfile_expanded = NULL;
  79. unsigned int histsize = 50;
  80. /* Signal vars */
  81. struct sigaction sigpipe_act;
  82. sigset_t sigpipe_set;
  83. static const char *shortopts = "hvs:ledx:w:i:bqu8oO:kt:c:f:z:";
  84. #ifdef HAVE_GETOPT_LONG
  85. static const struct option longopts[] = {
  86. {"help", 0, NULL, 'h'},
  87. {"version", 0, NULL, 'v'},
  88. {"socket", 1, NULL, 's'},
  89. {"lockless", 0, NULL, 'l'},
  90. {"stop-on-error", 0, NULL, 'e'},
  91. {"dry-run", 0, NULL, 'd'},
  92. {"xml-path", 1, NULL, 'x'},
  93. {"view", 1, NULL, 'w'},
  94. {"viewid", 1, NULL, 'i'},
  95. {"background", 0, NULL, 'b'},
  96. {"quiet", 0, NULL, 'q'},
  97. {"utf8", 0, NULL, 'u'},
  98. {"8bit", 0, NULL, '8'},
  99. {"log", 0, NULL, 'o'},
  100. {"facility", 1, NULL, 'O'},
  101. {"check", 0, NULL, 'k'},
  102. {"timeout", 1, NULL, 't'},
  103. {"command", 1, NULL, 'c'},
  104. {"histfile", 1, NULL, 'f'},
  105. {"histsize", 1, NULL, 'z'},
  106. {NULL, 0, NULL, 0}
  107. };
  108. #endif
  109. /* Ignore SIGPIPE */
  110. sigemptyset(&sigpipe_set);
  111. sigaddset(&sigpipe_set, SIGPIPE);
  112. sigpipe_act.sa_flags = 0;
  113. sigpipe_act.sa_mask = sigpipe_set;
  114. sigpipe_act.sa_handler = &sighandler;
  115. sigaction(SIGPIPE, &sigpipe_act, NULL);
  116. #if HAVE_LOCALE_H
  117. /* Set current locale */
  118. setlocale(LC_ALL, "");
  119. #endif
  120. /* Var initialization */
  121. cmds = lub_list_new(NULL);
  122. /* Parse command line options */
  123. while(1) {
  124. int opt;
  125. #ifdef HAVE_GETOPT_LONG
  126. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  127. #else
  128. opt = getopt(argc, argv, shortopts);
  129. #endif
  130. if (-1 == opt)
  131. break;
  132. switch (opt) {
  133. case 's':
  134. socket_path = optarg;
  135. break;
  136. case 'l':
  137. lockless = BOOL_TRUE;
  138. break;
  139. case 'e':
  140. stop_on_error = BOOL_TRUE;
  141. break;
  142. case 'b':
  143. interactive = BOOL_FALSE;
  144. break;
  145. case 'q':
  146. quiet = BOOL_TRUE;
  147. break;
  148. case 'u':
  149. utf8 = BOOL_TRUE;
  150. break;
  151. case '8':
  152. bit8 = BOOL_TRUE;
  153. break;
  154. case 'o':
  155. log = BOOL_TRUE;
  156. break;
  157. case 'O':
  158. if (lub_log_facility(optarg, &log_facility)) {
  159. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  160. help(-1, argv[0]);
  161. goto end;
  162. }
  163. break;
  164. case 'd':
  165. my_hooks.script_fn = clish_dryrun_callback;
  166. break;
  167. case 'x':
  168. xml_path = optarg;
  169. break;
  170. case 'w':
  171. view = optarg;
  172. break;
  173. case 'i':
  174. viewid = optarg;
  175. break;
  176. case 'k':
  177. lockless = BOOL_TRUE;
  178. my_hooks.script_fn = clish_dryrun_callback;
  179. my_hooks.config_fn = NULL;
  180. break;
  181. case 't':
  182. istimeout = BOOL_TRUE;
  183. timeout = atoi(optarg);
  184. break;
  185. case 'c': {
  186. char *str;
  187. cmd = BOOL_TRUE;
  188. quiet = BOOL_TRUE;
  189. str = strdup(optarg);
  190. lub_list_add(cmds, str);
  191. }
  192. break;
  193. case 'f':
  194. if (!strcmp(optarg, "/dev/null"))
  195. histfile = NULL;
  196. else
  197. histfile = optarg;
  198. break;
  199. case 'z': {
  200. int itmp = 0;
  201. itmp = atoi(optarg);
  202. if (itmp <= 0) {
  203. fprintf(stderr, "Error: Illegal histsize option value.\n");
  204. help(-1, argv[0]);
  205. goto end;
  206. }
  207. histsize = itmp;
  208. }
  209. break;
  210. case 'h':
  211. help(0, argv[0]);
  212. exit(0);
  213. break;
  214. case 'v':
  215. version(VERSION);
  216. exit(0);
  217. break;
  218. default:
  219. help(-1, argv[0]);
  220. goto end;
  221. break;
  222. }
  223. }
  224. /* Validate command line options */
  225. if (utf8 && bit8) {
  226. fprintf(stderr, "The -u and -8 options can't be used together.\n");
  227. goto end;
  228. }
  229. /* Create shell instance */
  230. if (quiet) {
  231. FILE *tmpfd = NULL;
  232. if ((tmpfd = fopen("/dev/null", "w")))
  233. outfd = tmpfd;
  234. }
  235. shell = clish_shell_new(&my_hooks, NULL, NULL, outfd, stop_on_error);
  236. if (!shell) {
  237. fprintf(stderr, "Can't run clish.\n");
  238. goto end;
  239. }
  240. /* Load the XML files */
  241. clish_shell_load_scheme(shell, xml_path);
  242. /* Set communication to the konfd */
  243. clish_shell__set_socket(shell, socket_path);
  244. /* Set lockless mode */
  245. if (lockless)
  246. clish_shell__set_lockfile(shell, NULL);
  247. /* Set interactive mode */
  248. if (!interactive)
  249. clish_shell__set_interactive(shell, interactive);
  250. /* Set startup view */
  251. if (view)
  252. clish_shell__set_startup_view(shell, view);
  253. /* Set startup viewid */
  254. if (viewid)
  255. clish_shell__set_startup_viewid(shell, viewid);
  256. /* Set UTF-8 or 8-bit mode */
  257. if (utf8 || bit8)
  258. clish_shell__set_utf8(shell, utf8);
  259. else {
  260. #if HAVE_LANGINFO_CODESET
  261. /* Autodetect encoding */
  262. if (!strcmp(nl_langinfo(CODESET), "UTF-8"))
  263. clish_shell__set_utf8(shell, BOOL_TRUE);
  264. #else
  265. /* The default is 8-bit if locale is not supported */
  266. clish_shell__set_utf8(shell, BOOL_FALSE);
  267. #endif
  268. }
  269. /* Set logging */
  270. if (log) {
  271. clish_shell__set_log(shell, log);
  272. clish_shell__set_facility(shell, log_facility);
  273. }
  274. /* Set idle timeout */
  275. if (istimeout)
  276. clish_shell__set_timeout(shell, timeout);
  277. /* Set history settings */
  278. clish_shell__stifle_history(shell, histsize);
  279. if (histfile)
  280. histfile_expanded = lub_system_tilde_expand(histfile);
  281. if (histfile_expanded)
  282. clish_shell__restore_history(shell, histfile_expanded);
  283. /* Set source of command stream: files or interactive tty */
  284. if(optind < argc) {
  285. int i;
  286. /* Run the commands from the files */
  287. for (i = argc - 1; i >= optind; i--)
  288. clish_shell_push_file(shell, argv[i], stop_on_error);
  289. } else {
  290. /* The interactive shell */
  291. int tmpfd = dup(fileno(stdin));
  292. #ifdef FD_CLOEXEC
  293. fcntl(tmpfd, F_SETFD, fcntl(tmpfd, F_GETFD) | FD_CLOEXEC);
  294. #endif
  295. clish_shell_push_fd(shell, fdopen(tmpfd, "r"), stop_on_error);
  296. }
  297. /* Execute startup */
  298. running = clish_shell_startup(shell);
  299. if (running) {
  300. fprintf(stderr, "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 && (outfd != stdout))
  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. }