void cmd_if_shell_callback(struct job *job) { struct cmd_if_shell_data *cdata = job->data; struct cmd_q *cmdq = cdata->cmdq, *cmdq1; struct cmd_list *cmdlist; char *cause, *cmd; if (cmdq->flags & CMD_Q_DEAD) return; if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) cmd = cdata->cmd_else; else cmd = cdata->cmd_if; if (cmd == NULL) return; if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { if (cause != NULL) { cmdq_error(cmdq, "%s", cause); free(cause); } return; } cmdq1 = cmdq_new(cmdq->client); cmdq1->flags |= cmdq->flags & CMD_Q_NOHOOKS; cmdq1->emptyfn = cmd_if_shell_done; cmdq1->data = cdata; cdata->references++; cmdq_run(cmdq1, cmdlist, &cdata->mouse); cmd_list_free(cmdlist); }
/* Control input callback. Read lines and fire commands. */ void control_callback(struct client *c, int closed, __unused void *data) { char *line, *cause; struct cmd_list *cmdlist; struct cmd *cmd; struct cmdq_item *item; if (closed) c->flags |= CLIENT_EXIT; for (;;) { line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF); if (line == NULL) break; if (*line == '\0') { /* empty line exit */ c->flags |= CLIENT_EXIT; break; } if (cmd_string_parse(line, &cmdlist, NULL, 0, &cause) != 0) { item = cmdq_get_callback(control_error, cause); cmdq_append(c, item); } else { TAILQ_FOREACH(cmd, &cmdlist->list, qentry) cmd->flags |= CMD_CONTROL; item = cmdq_get_command(cmdlist, NULL, NULL, 0); cmdq_append(c, item); cmd_list_free(cmdlist); } free(line); } }
void cmd_if_shell_callback(struct job *job) { struct cmd_if_shell_data *cdata = job->data; struct cmd_ctx *ctx = &cdata->ctx; struct cmd_list *cmdlist; char *cause, *cmd; if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) { cmd = cdata->cmd_else; if (cmd == NULL) return; } else cmd = cdata->cmd_if; if (cmd_string_parse(cmd, &cmdlist, &cause) != 0) { if (cause != NULL) { ctx->error(ctx, "%s", cause); free(cause); } return; } cmd_list_exec(cmdlist, ctx); cmd_list_free(cmdlist); }
int cmd_confirm_before_callback(void *data, const char *s) { struct cmd_confirm_before_data *cdata = data; struct client *c = cdata->client; struct cmd_list *cmdlist; char *cause; if (c->flags & CLIENT_DEAD) return (0); if (s == NULL || *s == '\0') return (0); if (tolower((u_char) s[0]) != 'y' || s[1] != '\0') return (0); if (cmd_string_parse(cdata->cmd, &cmdlist, NULL, 0, &cause) != 0) { if (cause != NULL) { cmdq_error(c->cmdq, "%s", cause); free(cause); } return (0); } cmdq_run(c->cmdq, cmdlist); cmd_list_free(cmdlist); return (0); }
static int cmd_confirm_before_callback(void *data, const char *s, __unused int done) { struct cmd_confirm_before_data *cdata = data; struct client *c = cdata->client; struct cmd_list *cmdlist; struct cmdq_item *new_item; char *cause; if (c->flags & CLIENT_DEAD) return (0); if (s == NULL || *s == '\0') return (0); if (tolower((u_char) s[0]) != 'y' || s[1] != '\0') return (0); cmdlist = cmd_string_parse(cdata->cmd, NULL, 0, &cause); if (cmdlist == NULL) { if (cause != NULL) { new_item = cmdq_get_callback(cmd_confirm_before_error, cause); } else new_item = NULL; } else { new_item = cmdq_get_command(cmdlist, NULL, NULL, 0); cmd_list_free(cmdlist); } if (new_item != NULL) cmdq_append(c, new_item); return (0); }
int cmd_confirm_before_callback(void *data, const char *s) { struct cmd_confirm_before_data *cdata = data; struct client *c = cdata->c; struct cmd_list *cmdlist; struct cmd_ctx *ctx; char *cause; if (s == NULL || *s == '\0') return (0); if (tolower((u_char) s[0]) != 'y' || s[1] != '\0') return (0); if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) { if (cause != NULL) { *cause = toupper((u_char) *cause); status_message_set(c, "%s", cause); free(cause); } return (0); } ctx = cmd_get_ctx(); ctx->curclient = c; ctx->error = key_bindings_error; ctx->print = key_bindings_print; ctx->info = key_bindings_info; cmd_list_exec(cmdlist, ctx); cmd_list_free(cmdlist); cmd_free_ctx(ctx); return (0); }
int load_cfg(const char *path, struct cmd_q *cmdq, int quiet) { FILE *f; char delim[3] = { '\\', '\\', '\0' }; u_int found; size_t line = 0; char *buf, *cause1, *p; struct cmd_list *cmdlist; log_debug("loading %s", path); if ((f = fopen(path, "rb")) == NULL) { if (errno == ENOENT && quiet) return (0); cfg_add_cause("%s: %s", path, strerror(errno)); return (-1); } found = 0; while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) { log_debug("%s: %s", path, buf); /* Skip empty lines. */ p = buf; while (isspace((u_char) *p)) p++; if (*p == '\0') { free(buf); continue; } /* Parse and run the command. */ if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) { free(buf); if (cause1 == NULL) continue; cfg_add_cause("%s:%zu: %s", path, line, cause1); free(cause1); continue; } free(buf); if (cmdlist == NULL) continue; cmdq_append(cmdq, cmdlist, NULL); cmd_list_free(cmdlist); found++; } fclose(f); return (found); }
/* Control input callback. */ void control_read_callback(unused struct bufferevent *bufev, void *data) { struct client *c = data; struct bufferevent *out = c->stdout_event; char *line; struct cmd_ctx ctx; struct cmd_list *cmdlist; char *cause; /* Read all available input lines. */ line = evbuffer_readln(c->stdin_event->input, NULL, EVBUFFER_EOL_ANY); while (line) { /* Parse command. */ ctx.msgdata = NULL; ctx.cmdclient = NULL; ctx.curclient = c; ctx.error = control_msg_error; ctx.print = control_msg_print; ctx.info = control_msg_info; if (cmd_string_parse(line, &cmdlist, &cause) != 0) { /* Error */ if (cause) { /* cause should always be set if there's an * error. */ evbuffer_add_printf(out->output, "%%error in line \"%s\": %s", line, cause); bufferevent_write(out, "\n", 1); xfree(cause); } } else { /* Parsed ok. Run command. */ cmd_list_exec(cmdlist, &ctx); cmd_list_free(cmdlist); } xfree(line); /* Read input line. */ line = evbuffer_readln(c->stdin_event->input, NULL, EVBUFFER_EOL_ANY); } }
static void cmd_if_shell_callback(struct job *job) { struct cmd_if_shell_data *cdata = job->data; struct client *c = cdata->client; struct cmd_list *cmdlist; struct cmdq_item *new_item; char *cause, *cmd, *file = cdata->file; u_int line = cdata->line; if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) cmd = cdata->cmd_else; else cmd = cdata->cmd_if; if (cmd == NULL) goto out; cmdlist = cmd_string_parse(cmd, file, line, &cause); if (cmdlist == NULL) { if (cause != NULL) new_item = cmdq_get_callback(cmd_if_shell_error, cause); else new_item = NULL; } else { new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0); cmd_list_free(cmdlist); } if (new_item != NULL) { if (cdata->item == NULL) cmdq_append(c, new_item); else cmdq_insert_after(cdata->item, new_item); } out: if (cdata->item != NULL) cdata->item->flags &= ~CMDQ_WAITING; }
/* Control input callback. Read lines and fire commands. */ void control_callback(struct client *c, int closed, unused void *data) { char *line, *cause; struct cmd_ctx ctx; struct cmd_list *cmdlist; if (closed) c->flags |= CLIENT_EXIT; for (;;) { line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF); if (line == NULL) break; if (*line == '\0') { /* empty line exit */ c->flags |= CLIENT_EXIT; break; } ctx.msgdata = NULL; ctx.cmdclient = NULL; ctx.curclient = c; ctx.error = control_msg_error; ctx.print = control_msg_print; ctx.info = control_msg_info; if (cmd_string_parse(line, &cmdlist, &cause) != 0) { control_write(c, "%%error in line \"%s\": %s", line, cause); xfree(cause); } else { cmd_list_exec(cmdlist, &ctx); cmd_list_free(cmdlist); } xfree(line); } }
/* Control input callback. Read lines and fire commands. */ void control_callback(struct client *c, int closed, unused void *data) { char *line, *cause; struct cmd_list *cmdlist; struct cmd *cmd; if (closed) c->flags |= CLIENT_EXIT; for (;;) { line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF); if (line == NULL) break; if (*line == '\0') { /* empty line exit */ c->flags |= CLIENT_EXIT; break; } if (cmd_string_parse(line, &cmdlist, NULL, 0, &cause) != 0) { c->cmdq->time = time(NULL); c->cmdq->number++; cmdq_guard(c->cmdq, "begin", 1); control_write(c, "parse error: %s", cause); cmdq_guard(c->cmdq, "error", 1); free(cause); } else { TAILQ_FOREACH(cmd, &cmdlist->list, qentry) cmd->flags |= CMD_CONTROL; cmdq_run(c->cmdq, cmdlist); cmd_list_free(cmdlist); } free(line); } }
enum cmd_retval cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct cmd_if_shell_data *cdata; char *shellcmd, *cmd, *cause; struct cmd_list *cmdlist; struct client *c; struct session *s = NULL; struct winlink *wl = NULL; struct window_pane *wp = NULL; struct format_tree *ft; int cwd; if (args_has(args, 't')) { wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); cwd = wp->cwd; } else { c = cmd_find_client(cmdq, NULL, 1); if (c != NULL && c->session != NULL) { s = c->session; wl = s->curw; wp = wl->window->active; } if (cmdq->client != NULL && cmdq->client->session == NULL) cwd = cmdq->client->cwd; else if (s != NULL) cwd = s->cwd; else cwd = -1; } ft = format_create(); format_defaults(ft, NULL, s, wl, wp); shellcmd = format_expand(ft, args->argv[0]); format_free(ft); if (args_has(args, 'F')) { cmd = NULL; if (*shellcmd != '0' && *shellcmd != '\0') cmd = args->argv[1]; else if (args->argc == 3) cmd = args->argv[2]; if (cmd == NULL) return (CMD_RETURN_NORMAL); if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { if (cause != NULL) { cmdq_error(cmdq, "%s", cause); free(cause); } return (CMD_RETURN_ERROR); } cmdq_run(cmdq, cmdlist, &cmdq->item->mouse); cmd_list_free(cmdlist); return (CMD_RETURN_NORMAL); } cdata = xmalloc(sizeof *cdata); cdata->cmd_if = xstrdup(args->argv[1]); if (args->argc == 3) cdata->cmd_else = xstrdup(args->argv[2]); else cdata->cmd_else = NULL; cdata->bflag = args_has(args, 'b'); cdata->cmdq = cmdq; memcpy(&cdata->mouse, &cmdq->item->mouse, sizeof cdata->mouse); cmdq->references++; cdata->references = 1; job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, cdata); free(shellcmd); if (cdata->bflag) return (CMD_RETURN_NORMAL); return (CMD_RETURN_WAIT); }
static enum cmd_retval cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; struct cmd_if_shell_data *cdata; char *shellcmd, *cmd, *cause; struct cmd_list *cmdlist; struct cmdq_item *new_item; struct client *c = item->state.c; struct session *s = item->state.tflag.s; struct winlink *wl = item->state.tflag.wl; struct window_pane *wp = item->state.tflag.wp; const char *cwd; if (item->client != NULL && item->client->session == NULL) cwd = item->client->cwd; else if (s != NULL) cwd = s->cwd; else cwd = NULL; shellcmd = format_single(item, args->argv[0], c, s, wl, wp); if (args_has(args, 'F')) { cmd = NULL; if (*shellcmd != '0' && *shellcmd != '\0') cmd = args->argv[1]; else if (args->argc == 3) cmd = args->argv[2]; free(shellcmd); if (cmd == NULL) return (CMD_RETURN_NORMAL); cmdlist = cmd_string_parse(cmd, NULL, 0, &cause); if (cmdlist == NULL) { if (cause != NULL) { cmdq_error(item, "%s", cause); free(cause); } return (CMD_RETURN_ERROR); } new_item = cmdq_get_command(cmdlist, NULL, &item->mouse, 0); cmdq_insert_after(item, new_item); cmd_list_free(cmdlist); return (CMD_RETURN_NORMAL); } cdata = xcalloc(1, sizeof *cdata); if (self->file != NULL) { cdata->file = xstrdup(self->file); cdata->line = self->line; } cdata->cmd_if = xstrdup(args->argv[1]); if (args->argc == 3) cdata->cmd_else = xstrdup(args->argv[2]); else cdata->cmd_else = NULL; cdata->client = item->client; cdata->client->references++; if (!args_has(args, 'b')) cdata->item = item; else cdata->item = NULL; memcpy(&cdata->mouse, &item->mouse, sizeof cdata->mouse); job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, cdata); free(shellcmd); if (args_has(args, 'b')) return (CMD_RETURN_NORMAL); return (CMD_RETURN_WAIT); }
int load_cfg(const char *path, struct cmd_q *cmdq, char **cause) { FILE *f; u_int n, found; char *buf, *copy, *line, *cause1, *msg; size_t len, oldlen; struct cmd_list *cmdlist; log_debug("loading %s", path); if ((f = fopen(path, "rb")) == NULL) { xasprintf(cause, "%s: %s", path, strerror(errno)); return (-1); } n = found = 0; line = NULL; while ((buf = fgetln(f, &len))) { /* Trim \n. */ if (buf[len - 1] == '\n') len--; log_debug("%s: %.*s", path, (int)len, buf); /* Current line is the continuation of the previous one. */ if (line != NULL) { oldlen = strlen(line); line = xrealloc(line, 1, oldlen + len + 1); } else { oldlen = 0; line = xmalloc(len + 1); } /* Append current line to the previous. */ memcpy(line + oldlen, buf, len); line[oldlen + len] = '\0'; n++; /* Continuation: get next line? */ len = strlen(line); if (len > 0 && line[len - 1] == '\\') { line[len - 1] = '\0'; /* Ignore escaped backslash at EOL. */ if (len > 1 && line[len - 2] != '\\') continue; } copy = line; line = NULL; /* Skip empty lines. */ buf = copy; while (isspace((u_char)*buf)) buf++; if (*buf == '\0') { free(copy); continue; } /* Parse and run the command. */ if (cmd_string_parse(buf, &cmdlist, path, n, &cause1) != 0) { free(copy); if (cause1 == NULL) continue; xasprintf(&msg, "%s:%u: %s", path, n, cause1); ARRAY_ADD(&cfg_causes, msg); free(cause1); continue; } free(copy); if (cmdlist == NULL) continue; cmdq_append(cmdq, cmdlist); cmd_list_free(cmdlist); found++; } if (line != NULL) free(line); fclose(f); return (found); }
/* * Load configuration file. Returns -1 for an error with a list of messages in * causes. Note that causes must be initialised by the caller! */ int load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) { FILE *f; u_int n; char *buf, *line, *cause; size_t len; struct cmd_list *cmdlist; struct cmd_ctx ctx; int retval; if ((f = fopen(path, "rb")) == NULL) { cfg_add_cause(causes, "%s: %s", path, strerror(errno)); return (-1); } n = 0; line = NULL; retval = 0; while ((buf = fgetln(f, &len))) { if (buf[len - 1] == '\n') buf[len - 1] = '\0'; else { line = xrealloc(line, 1, len + 1); memcpy(line, buf, len); line[len] = '\0'; buf = line; } n++; if (cmd_string_parse(buf, &cmdlist, &cause) != 0) { if (cause == NULL) continue; cfg_add_cause(causes, "%s: %u: %s", path, n, cause); xfree(cause); continue; } if (cmdlist == NULL) continue; cfg_cause = NULL; if (ctxin == NULL) { ctx.msgdata = NULL; ctx.curclient = NULL; ctx.cmdclient = NULL; } else { ctx.msgdata = ctxin->msgdata; ctx.curclient = ctxin->curclient; ctx.cmdclient = ctxin->cmdclient; } ctx.error = cfg_error; ctx.print = cfg_print; ctx.info = cfg_print; cfg_cause = NULL; if (cmd_list_exec(cmdlist, &ctx) == 1) retval = 1; cmd_list_free(cmdlist); if (cfg_cause != NULL) { cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause); xfree(cfg_cause); } } if (line != NULL) xfree(line); fclose(f); return (retval); }
/* * Load configuration file. Returns -1 for an error with a list of messages in * causes. Note that causes must be initialised by the caller! */ enum cmd_retval load_cfg(const char *path, struct cmd_ctx *ctx, struct causelist *causes) { FILE *f; u_int n; char *buf, *copy, *line, *cause; size_t len, oldlen; struct cmd_list *cmdlist; enum cmd_retval retval; if ((f = fopen(path, "rb")) == NULL) { cfg_add_cause(causes, "%s: %s", path, strerror(errno)); return (CMD_RETURN_ERROR); } cfg_references++; if (ctx != NULL) cmd_ref_ctx(ctx); else { ctx = cmd_get_ctx(); ctx->error = cfg_error; ctx->print = cfg_print; ctx->info = cfg_print; } n = 0; line = NULL; retval = CMD_RETURN_NORMAL; while ((buf = fgetln(f, &len))) { /* Trim \n. */ if (buf[len - 1] == '\n') len--; log_debug("%s: %.*s", path, (int)len, buf); /* Current line is the continuation of the previous one. */ if (line != NULL) { oldlen = strlen(line); line = xrealloc(line, 1, oldlen + len + 1); } else { oldlen = 0; line = xmalloc(len + 1); } /* Append current line to the previous. */ memcpy(line + oldlen, buf, len); line[oldlen + len] = '\0'; n++; /* Continuation: get next line? */ len = strlen(line); if (len > 0 && line[len - 1] == '\\') { line[len - 1] = '\0'; /* Ignore escaped backslash at EOL. */ if (len > 1 && line[len - 2] != '\\') continue; } copy = line; line = NULL; /* Skip empty lines. */ buf = copy; while (isspace((u_char)*buf)) buf++; if (*buf == '\0') { free(copy); continue; } if (cmd_string_parse(buf, &cmdlist, &cause) != 0) { free(copy); if (cause == NULL) continue; cfg_add_cause(causes, "%s: %u: %s", path, n, cause); free(cause); continue; } free(copy); if (cmdlist == NULL) continue; cfg_cause = NULL; switch (cmd_list_exec(cmdlist, ctx)) { case CMD_RETURN_YIELD: if (retval != CMD_RETURN_ATTACH) retval = CMD_RETURN_YIELD; break; case CMD_RETURN_ATTACH: retval = CMD_RETURN_ATTACH; break; case CMD_RETURN_ERROR: case CMD_RETURN_NORMAL: break; } cmd_list_free(cmdlist); if (cfg_cause != NULL) { cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause); free(cfg_cause); } } if (line != NULL) { cfg_add_cause(causes, "%s: %d: line continuation at end of file", path, n); free(line); } fclose(f); cmd_free_ctx(ctx); cfg_references--; return (retval); }
/* * Load configuration file. Returns -1 for an error with a list of messages in * causes. Note that causes must be initialised by the caller! */ int load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) { FILE *f; u_int n; char *buf, *line, *cause; size_t len; struct cmd_list *cmdlist; struct cmd_ctx ctx; int retval; if ((f = fopen(path, "rb")) == NULL) { cfg_add_cause(causes, "%s: %s", path, strerror(errno)); return (-1); } n = 0; line = NULL; retval = 0; while ((buf = fgetln(f, &len))) { if (buf[len - 1] == '\n') len--; if (line != NULL) line = xrealloc(line, 1, strlen(line) + len + 1); else { line = xmalloc(len + 1); *line = '\0'; } /* Append buffer to line. strncat will terminate. */ strncat(line, buf, len); n++; /* Continuation: get next line? */ len = strlen(line); if (len > 0 && line[len - 1] == '\\') { line[len - 1] = '\0'; /* Ignore escaped backslash at EOL. */ if (len > 1 && line[len - 2] != '\\') continue; } buf = line; line = NULL; if (cmd_string_parse(buf, &cmdlist, &cause) != 0) { xfree(buf); if (cause == NULL) continue; cfg_add_cause(causes, "%s: %u: %s", path, n, cause); xfree(cause); continue; } else xfree(buf); if (cmdlist == NULL) continue; cfg_cause = NULL; if (ctxin == NULL) { ctx.msgdata = NULL; ctx.curclient = NULL; ctx.cmdclient = NULL; } else { ctx.msgdata = ctxin->msgdata; ctx.curclient = ctxin->curclient; ctx.cmdclient = ctxin->cmdclient; } ctx.error = cfg_error; ctx.print = cfg_print; ctx.info = cfg_print; cfg_cause = NULL; if (cmd_list_exec(cmdlist, &ctx) == 1) retval = 1; cmd_list_free(cmdlist); if (cfg_cause != NULL) { cfg_add_cause( causes, "%s: %d: %s", path, n, cfg_cause); xfree(cfg_cause); } } if (line != NULL) { cfg_add_cause(causes, "%s: %d: line continuation at end of file", path, n); xfree(line); } fclose(f); return (retval); }
static enum cmd_retval cmd_set_hook_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; struct cmd_list *cmdlist; struct hooks *hooks; struct hook *hook; char *cause, *tmp; const char *name, *cmd, *target; if (args_has(args, 'g')) hooks = global_hooks; else { if (item->state.tflag.s == NULL) { target = args_get(args, 't'); if (target != NULL) cmdq_error(item, "no such session: %s", target); else cmdq_error(item, "no current session"); return (CMD_RETURN_ERROR); } hooks = item->state.tflag.s->hooks; } if (self->entry == &cmd_show_hooks_entry) { hook = hooks_first(hooks); while (hook != NULL) { tmp = cmd_list_print(hook->cmdlist); cmdq_print(item, "%s -> %s", hook->name, tmp); free(tmp); hook = hooks_next(hook); } return (CMD_RETURN_NORMAL); } name = args->argv[0]; if (*name == '\0') { cmdq_error(item, "invalid hook name"); return (CMD_RETURN_ERROR); } if (args->argc < 2) cmd = NULL; else cmd = args->argv[1]; if (args_has(args, 'u')) { if (cmd != NULL) { cmdq_error(item, "command passed to unset hook: %s", name); return (CMD_RETURN_ERROR); } hooks_remove(hooks, name); return (CMD_RETURN_NORMAL); } if (cmd == NULL) { cmdq_error(item, "no command to set hook: %s", name); return (CMD_RETURN_ERROR); } if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { if (cause != NULL) { cmdq_error(item, "%s", cause); free(cause); } return (CMD_RETURN_ERROR); } hooks_add(hooks, name, cmdlist); cmd_list_free(cmdlist); return (CMD_RETURN_NORMAL); }
int load_cfg(const char *path, char **cause) { FILE *f; u_int n; struct stat sb; char *buf, *line, *ptr; size_t len; struct cmd_list *cmdlist; struct cmd_ctx ctx; if (stat(path, &sb) != 0) { xasprintf(cause, "%s: %s", path, strerror(errno)); return (-1); } if (!S_ISREG(sb.st_mode)) { xasprintf(cause, "%s: not a regular file", path); return (-1); } if ((f = fopen(path, "rb")) == NULL) { xasprintf(cause, "%s: %s", path, strerror(errno)); return (1); } n = 0; line = NULL; while ((buf = fgetln(f, &len))) { if (buf[len - 1] == '\n') buf[len - 1] = '\0'; else { line = xrealloc(line, 1, len + 1); memcpy(line, buf, len); line[len] = '\0'; buf = line; } n++; if (cmd_string_parse(buf, &cmdlist, cause) != 0) { if (*cause == NULL) continue; goto error; } if (cmdlist == NULL) continue; cfg_cause = NULL; ctx.msgdata = NULL; ctx.cursession = NULL; ctx.curclient = NULL; ctx.error = cfg_error; ctx.print = cfg_print; ctx.info = cfg_print; ctx.cmdclient = NULL; cfg_cause = NULL; cmd_list_exec(cmdlist, &ctx); cmd_list_free(cmdlist); if (cfg_cause != NULL) { *cause = cfg_cause; goto error; } } if (line != NULL) xfree(line); fclose(f); return (0); error: fclose(f); xasprintf(&ptr, "%s: %s at line %u", path, *cause, n); xfree(*cause); *cause = ptr; return (1); }