1
0

clish.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. FILE *tmpfd = NULL;
  231. if ((tmpfd = fopen("/dev/null", "w")))
  232. outfd = tmpfd;
  233. }
  234. shell = clish_shell_new(&my_hooks, NULL, NULL, outfd, stop_on_error);
  235. if (!shell) {
  236. fprintf(stderr, "Can't run clish.\n");
  237. goto end;
  238. }
  239. /* Load the XML files */
  240. clish_shell_load_scheme(shell, xml_path);
  241. /* Set communication to the konfd */
  242. clish_shell__set_socket(shell, socket_path);
  243. /* Set lockless mode */
  244. if (lockless)
  245. clish_shell__set_lockfile(shell, NULL);
  246. /* Set interactive mode */
  247. if (!interactive)
  248. clish_shell__set_interactive(shell, interactive);
  249. /* Set startup view */
  250. if (view)
  251. clish_shell__set_startup_view(shell, view);
  252. /* Set startup viewid */
  253. if (viewid)
  254. clish_shell__set_startup_viewid(shell, viewid);
  255. /* Set UTF-8 or 8-bit mode */
  256. if (utf8 || bit8)
  257. clish_shell__set_utf8(shell, utf8);
  258. else {
  259. #if HAVE_LANGINFO_CODESET
  260. /* Autodetect encoding */
  261. if (!strcmp(nl_langinfo(CODESET), "UTF-8"))
  262. clish_shell__set_utf8(shell, BOOL_TRUE);
  263. #else
  264. /* The default is 8-bit if locale is not supported */
  265. clish_shell__set_utf8(shell, BOOL_FALSE);
  266. #endif
  267. }
  268. /* Set logging */
  269. if (log) {
  270. clish_shell__set_log(shell, log);
  271. clish_shell__set_facility(shell, log_facility);
  272. }
  273. /* Set idle timeout */
  274. if (istimeout)
  275. clish_shell__set_timeout(shell, timeout);
  276. /* Set history settings */
  277. clish_shell__stifle_history(shell, histsize);
  278. if (histfile)
  279. histfile_expanded = lub_system_tilde_expand(histfile);
  280. if (histfile_expanded)
  281. clish_shell__restore_history(shell, histfile_expanded);
  282. /* Set source of command stream: files or interactive tty */
  283. if(optind < argc) {
  284. int i;
  285. /* Run the commands from the files */
  286. for (i = argc - 1; i >= optind; i--)
  287. clish_shell_push_file(shell, argv[i], stop_on_error);
  288. } else {
  289. /* The interactive shell */
  290. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"),
  291. stop_on_error);
  292. }
  293. /* Execute startup */
  294. running = clish_shell_startup(shell);
  295. if (running) {
  296. fprintf(stderr, "Can't startup clish.\n");
  297. goto end;
  298. }
  299. if (cmd) {
  300. /* Iterate cmds */
  301. for(iter = lub_list__get_head(cmds);
  302. iter; iter = lub_list_node__get_next(iter)) {
  303. char *str = (char *)lub_list_node__get_data(iter);
  304. result = clish_shell_forceline(shell, str, NULL);
  305. if (stop_on_error && result)
  306. break;
  307. }
  308. } else {
  309. /* Main loop */
  310. result = clish_shell_loop(shell);
  311. }
  312. end:
  313. /* Cleanup */
  314. if (shell) {
  315. if (histfile_expanded) {
  316. clish_shell__save_history(shell, histfile_expanded);
  317. free(histfile_expanded);
  318. }
  319. clish_shell_delete(shell);
  320. }
  321. if (quiet && (outfd != stdout))
  322. fclose(outfd);
  323. /* Delete each cmds element */
  324. while ((iter = lub_list__get_head(cmds))) {
  325. lub_list_del(cmds, iter);
  326. free(lub_list_node__get_data(iter));
  327. lub_list_node_free(iter);
  328. }
  329. lub_list_free(cmds);
  330. return result;
  331. }
  332. /*--------------------------------------------------------- */
  333. /* Print help message */
  334. static void help(int status, const char *argv0)
  335. {
  336. const char *name = NULL;
  337. if (!argv0)
  338. return;
  339. /* Find the basename */
  340. name = strrchr(argv0, '/');
  341. if (name)
  342. name++;
  343. else
  344. name = argv0;
  345. if (status != 0) {
  346. fprintf(stderr, "Try `%s -h' for more information.\n",
  347. name);
  348. } else {
  349. printf("Usage: %s [options] [script_file] [script_file] ...\n", name);
  350. printf("CLI utility. Command line shell."
  351. "The part of the klish project.\n");
  352. printf("Options:\n");
  353. printf("\t-v, --version\tPrint version.\n");
  354. printf("\t-h, --help\tPrint this help.\n");
  355. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  356. "\n\t\tof the konfd daemon.\n");
  357. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  358. printf("\t-e, --stop-on-error\tStop script execution on error.\n");
  359. printf("\t-b, --background\tStart shell using non-interactive mode.\n");
  360. printf("\t-q, --quiet\tDisable echo while executing commands\n\t\tfrom the file stream.\n");
  361. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  362. printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
  363. printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
  364. printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
  365. printf("\t-u, --utf8\tForce UTF-8 encoding.\n");
  366. printf("\t-8, --8bit\tForce 8-bit encoding.\n");
  367. printf("\t-o, --log\tEnable command logging to syslog's.\n");
  368. printf("\t-O, --facility\tSyslog facility. Default is LOCAL0.\n");
  369. printf("\t-k, --check\tCheck input files for syntax errors only.\n");
  370. printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
  371. printf("\t-c <command>, --command=<command>\tExecute specified command(s).\n\t\tMultiple options are possible.\n");
  372. printf("\t-f <path>, --histfile=<path>\tFile to save command history.\n");
  373. printf("\t-z <num>, --histsize=<num>\tCommand history size in lines.\n");
  374. }
  375. }
  376. /*--------------------------------------------------------- */
  377. /*
  378. * Signal handler for SIGPIPE.
  379. * It's empty but it's needed to don't ignore SIGPIPE because
  380. * SIG_IGN will be inherited while ACTION execution.
  381. */
  382. static void sighandler(int signo)
  383. {
  384. signo = signo; /* Happy compiler */
  385. return;
  386. }