1
0

clish.c 10 KB

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