123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <getopt.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #ifdef HAVE_LOCALE_H
- #include <locale.h>
- #endif
- #ifdef HAVE_LANGINFO_CODESET
- #include <langinfo.h>
- #endif
- #include <faux/faux.h>
- #include <faux/str.h>
- #include <faux/msg.h>
- #include <faux/list.h>
- #include <faux/file.h>
- #include <faux/eloop.h>
- #include <klish/ktp.h>
- #include <klish/ktp_session.h>
- #include "private.h"
- static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
- void *user_data);
- static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
- void *user_data);
- static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
- void *associated_data, void *user_data);
- static int ktp_sync_cmd(ktp_session_t *ktp, const char *line,
- const struct options *opts);
- int main(int argc, char **argv)
- {
- int retval = -1;
- struct options *opts = NULL;
- int unix_sock = -1;
- ktp_session_t *ktp = NULL;
- int retcode = 0;
- faux_eloop_t *eloop = NULL;
- #ifdef HAVE_LOCALE_H
- // Set current locale
- setlocale(LC_ALL, "");
- #endif
- // Parse command line options
- opts = opts_init();
- if (opts_parse(argc, argv, opts)) {
- fprintf(stderr, "Error: Can't parse command line options\n");
- goto err;
- }
- // Parse config file
- if (!access(opts->cfgfile, R_OK)) {
- if (!config_parse(opts->cfgfile, opts))
- goto err;
- } else if (opts->cfgfile_userdefined) {
- // User defined config must be found
- fprintf(stderr, "Error: Can't find config file %s\n",
- opts->cfgfile);
- goto err;
- }
- // Connect to server
- unix_sock = ktp_connect_unix(opts->unix_socket_path);
- if (unix_sock < 0) {
- fprintf(stderr, "Error: Can't connect to server\n");
- goto err;
- }
- // Eloop object
- eloop = faux_eloop_new(NULL);
- faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
- faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
- faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
- // KTP session
- ktp = ktp_session_new(unix_sock, eloop);
- assert(ktp);
- if (!ktp) {
- fprintf(stderr, "Error: Can't create klish session\n");
- goto err;
- }
- // These callback functions is only for batch mode. Interactive
- // mode can reassign them.
- ktp_session_set_cb(ktp, KTP_SESSION_CB_STDOUT, stdout_cb, NULL);
- ktp_session_set_cb(ktp, KTP_SESSION_CB_STDERR, stderr_cb, NULL);
- // Ignore SIGPIPE from server
- signal(SIGPIPE, SIG_IGN);
- // Commands from cmdline
- if (faux_list_len(opts->commands) > 0) {
- const char *line = NULL;
- faux_list_node_t *iter = faux_list_head(opts->commands);
- while ((line = faux_list_each(&iter))) {
- // Request to server
- retcode = ktp_sync_cmd(ktp, line, opts);
- // Stop-on-error
- if (opts->stop_on_error && (retcode != 0))
- break;
- }
- // Commands from files
- } else if (faux_list_len(opts->files) > 0) {
- const char *filename = NULL;
- faux_list_node_t *iter = faux_list_head(opts->files);
- while ((filename = (const char *)faux_list_each(&iter))) {
- char *line = NULL;
- bool_t stop = BOOL_FALSE;
- faux_file_t *fd = faux_file_open(filename, O_RDONLY, 0);
- while ((line = faux_file_getline(fd))) {
- // Request to server
- retcode = ktp_sync_cmd(ktp, line, opts);
- faux_str_free(line);
- // Stop-on-error
- if (opts->stop_on_error && (retcode != 0)) {
- stop = BOOL_TRUE;
- break;
- }
- }
- faux_file_close(fd);
- if (stop)
- break;
- }
- // Commands from non-interactive STDIN
- } else if (!isatty(STDIN_FILENO)) {
- char *line = NULL;
- faux_file_t *fd = faux_file_fdopen(STDIN_FILENO);
- while ((line = faux_file_getline(fd))) {
- // Request to server
- retcode = ktp_sync_cmd(ktp, line, opts);
- faux_str_free(line);
- // Stop-on-error
- if (opts->stop_on_error && (retcode != 0))
- break;
- }
- faux_file_close(fd);
- // Interactive shell
- } else {
- // Interactive code is complex so move it to separate file
- retcode = klish_interactive_shell(ktp, opts);
- }
- retval = 0;
- err:
- ktp_session_free(ktp);
- faux_eloop_free(eloop);
- ktp_disconnect(unix_sock);
- opts_free(opts);
- if ((retval < 0) || (retcode != 0))
- return -1;
- return 0;
- }
- static int ktp_sync_cmd(ktp_session_t *ktp, const char *line,
- const struct options *opts)
- {
- faux_error_t *error = NULL;
- int retcode = -1;
- if (faux_str_is_empty(line))
- return 0;
- // Echo command
- if (!opts->quiet)
- fprintf(stderr, "%s\n", line);
- error = faux_error_new();
- if (!ktp_session_cmd(ktp, line, error, opts->dry_run)) {
- faux_error_free(error);
- return -1;
- }
- faux_eloop_loop(ktp_session_eloop(ktp));
- // If retcode is not available then variable will not be changed
- ktp_session_retcode(ktp, &retcode);
- if (faux_error_len(error) > 0) {
- fprintf(stderr, "Error:\n");
- faux_error_fshow(error, stderr);
- }
- faux_error_free(error);
- return retcode;
- }
- static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
- void *user_data)
- {
- if (faux_write_block(STDOUT_FILENO, line, len) < 0)
- return BOOL_FALSE;
- ktp = ktp;
- user_data = user_data;
- return BOOL_TRUE;
- }
- static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
- void *user_data)
- {
- if (faux_write_block(STDERR_FILENO, line, len) < 0)
- return BOOL_FALSE;
- ktp = ktp;
- user_data = user_data;
- return BOOL_TRUE;
- }
- static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
- void *associated_data, void *user_data)
- {
- // Happy compiler
- eloop = eloop;
- type = type;
- associated_data = associated_data;
- user_data = user_data;
- return BOOL_FALSE; // Stop Event Loop
- }
|