Use the array.h code for the causes list.

This commit is contained in:
Nicholas Marriott 2010-02-06 23:22:27 +00:00
parent bb53c20c18
commit 4651180503
6 changed files with 46 additions and 47 deletions

View File

@ -19,6 +19,8 @@
#ifndef ARRAY_H #ifndef ARRAY_H
#define ARRAY_H #define ARRAY_H
#define ARRAY_INITIALIZER { NULL, 0, 0 }
#define ARRAY_DECL(n, c) \ #define ARRAY_DECL(n, c) \
struct n { \ struct n { \
c *list; \ c *list; \

27
cfg.c
View File

@ -33,10 +33,9 @@
void printflike2 cfg_print(struct cmd_ctx *, const char *, ...); void printflike2 cfg_print(struct cmd_ctx *, const char *, ...);
void printflike2 cfg_error(struct cmd_ctx *, const char *, ...); void printflike2 cfg_error(struct cmd_ctx *, const char *, ...);
char *cfg_cause; char *cfg_cause;
int cfg_finished; int cfg_finished;
char **cfg_causes; struct causelist cfg_causes = ARRAY_INITIALIZER;
u_int cfg_ncauses;
/* ARGSUSED */ /* ARGSUSED */
void printflike2 void printflike2
@ -55,8 +54,8 @@ cfg_error(unused struct cmd_ctx *ctx, const char *fmt, ...)
va_end(ap); va_end(ap);
} }
void printflike3 void printflike2
cfg_add_cause(u_int *ncauses, char ***causes, const char *fmt, ...) cfg_add_cause(struct causelist *causes, const char *fmt, ...)
{ {
char *cause; char *cause;
va_list ap; va_list ap;
@ -65,8 +64,7 @@ cfg_add_cause(u_int *ncauses, char ***causes, const char *fmt, ...)
xvasprintf(&cause, fmt, ap); xvasprintf(&cause, fmt, ap);
va_end(ap); va_end(ap);
*causes = xrealloc(*causes, *ncauses + 1, sizeof **causes); ARRAY_ADD(causes, cause);
(*causes)[(*ncauses)++] = cause;
} }
/* /*
@ -74,8 +72,7 @@ cfg_add_cause(u_int *ncauses, char ***causes, const char *fmt, ...)
* causes. Note that causes and ncauses must be initialised by the caller! * causes. Note that causes and ncauses must be initialised by the caller!
*/ */
int int
load_cfg( load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
const char *path, struct cmd_ctx *ctxin, u_int *ncauses, char ***causes)
{ {
FILE *f; FILE *f;
u_int n; u_int n;
@ -85,7 +82,7 @@ load_cfg(
struct cmd_ctx ctx; struct cmd_ctx ctx;
if ((f = fopen(path, "rb")) == NULL) { if ((f = fopen(path, "rb")) == NULL) {
cfg_add_cause(ncauses, causes, "%s: %s", path, strerror(errno)); cfg_add_cause(causes, "%s: %s", path, strerror(errno));
return (-1); return (-1);
} }
n = 0; n = 0;
@ -105,8 +102,7 @@ load_cfg(
if (cmd_string_parse(buf, &cmdlist, &cause) != 0) { if (cmd_string_parse(buf, &cmdlist, &cause) != 0) {
if (cause == NULL) if (cause == NULL)
continue; continue;
cfg_add_cause( cfg_add_cause(causes, "%s: %u: %s", path, n, cause);
ncauses, causes, "%s: %u: %s", path, n, cause);
xfree(cause); xfree(cause);
continue; continue;
} }
@ -132,8 +128,7 @@ load_cfg(
cmd_list_exec(cmdlist, &ctx); cmd_list_exec(cmdlist, &ctx);
cmd_list_free(cmdlist); cmd_list_free(cmdlist);
if (cfg_cause != NULL) { if (cfg_cause != NULL) {
cfg_add_cause( cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause);
ncauses, causes, "%s: %d: %s", path, n, cfg_cause);
xfree(cfg_cause); xfree(cfg_cause);
continue; continue;
} }
@ -142,7 +137,7 @@ load_cfg(
xfree(line); xfree(line);
fclose(f); fclose(f);
if (*ncauses != 0) if (ARRAY_LENGTH(causes) != 0)
return (-1); return (-1);
return (0); return (0);
} }

View File

@ -285,15 +285,15 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
* If there are still configuration file errors to display, put the new * If there are still configuration file errors to display, put the new
* session's current window into more mode and display them now. * session's current window into more mode and display them now.
*/ */
if (cfg_finished && cfg_ncauses != 0) { if (cfg_finished && !ARRAY_EMPTY(&cfg_causes)) {
wp = s->curw->window->active; wp = s->curw->window->active;
window_pane_set_mode(wp, &window_more_mode); window_pane_set_mode(wp, &window_more_mode);
for (i = 0; i < cfg_ncauses; i++) { for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
window_more_add(wp, "%s", cfg_causes[i]); cause = ARRAY_ITEM(&cfg_causes, i);
xfree(cfg_causes[i]); window_more_add(wp, "%s", cause);
xfree(cause);
} }
xfree(cfg_causes); ARRAY_FREE(&cfg_causes);
cfg_ncauses = 0;
} }
return (!detached); /* 1 means don't tell command client to exit */ return (!detached); /* 1 means don't tell command client to exit */

View File

@ -89,18 +89,18 @@ int
cmd_source_file_exec(struct cmd *self, struct cmd_ctx *ctx) cmd_source_file_exec(struct cmd *self, struct cmd_ctx *ctx)
{ {
struct cmd_source_file_data *data = self->data; struct cmd_source_file_data *data = self->data;
char **causes; struct causelist causes;
u_int i, ncauses; char *cause;
u_int i;
causes = NULL; ARRAY_INIT(&causes);
ncauses = 0; if (load_cfg(data->path, ctx, &causes) != 0) {
for (i = 0; i < ARRAY_LENGTH(&causes); i++) {
if (load_cfg(data->path, ctx, &ncauses, &causes) != 0) { cause = ARRAY_ITEM(&causes, i);
for (i = 0; i < ncauses; i++) { ctx->print(ctx, "%s", cause);
ctx->print(ctx, "%s", causes[i]); xfree(cause);
xfree(causes[i]);
} }
xfree(causes); ARRAY_FREE(&causes);
} }
return (0); return (0);

View File

@ -115,7 +115,7 @@ server_start(char *path)
{ {
struct window_pane *wp; struct window_pane *wp;
int pair[2], retval; int pair[2], retval;
char rpathbuf[MAXPATHLEN]; char rpathbuf[MAXPATHLEN], *cause;
struct timeval tv; struct timeval tv;
u_int i; u_int i;
@ -169,27 +169,27 @@ server_start(char *path)
retval = 0; retval = 0;
if (access(SYSTEM_CFG, R_OK) == 0) if (access(SYSTEM_CFG, R_OK) == 0)
load_cfg(SYSTEM_CFG, NULL, &cfg_ncauses, &cfg_causes); load_cfg(SYSTEM_CFG, NULL, &cfg_causes);
else if (errno != ENOENT) { else if (errno != ENOENT) {
cfg_add_cause(&cfg_ncauses, &cfg_causes, cfg_add_cause(
"%s: %s", strerror(errno), SYSTEM_CFG); &cfg_causes, "%s: %s", strerror(errno), SYSTEM_CFG);
} }
if (cfg_file != NULL) if (cfg_file != NULL)
load_cfg(cfg_file, NULL, &cfg_ncauses, &cfg_causes); load_cfg(cfg_file, NULL, &cfg_causes);
/* /*
* If there is a session already, put the current window and pane into * If there is a session already, put the current window and pane into
* more mode. * more mode.
*/ */
if (!ARRAY_EMPTY(&sessions) && cfg_ncauses != 0) { if (!ARRAY_EMPTY(&sessions) && !ARRAY_EMPTY(&cfg_causes)) {
wp = ARRAY_FIRST(&sessions)->curw->window->active; wp = ARRAY_FIRST(&sessions)->curw->window->active;
window_pane_set_mode(wp, &window_more_mode); window_pane_set_mode(wp, &window_more_mode);
for (i = 0; i < cfg_ncauses; i++) { for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
window_more_add(wp, "%s", cfg_causes[i]); cause = ARRAY_ITEM(&cfg_causes, i);
xfree(cfg_causes[i]); window_more_add(wp, "%s", cause);
xfree(cause);
} }
xfree(cfg_causes); ARRAY_FREE(&cfg_causes);
cfg_ncauses = 0;
} }
cfg_finished = 1; cfg_finished = 1;

10
tmux.h
View File

@ -1244,6 +1244,9 @@ struct set_option_entry {
const char **choices; const char **choices;
}; };
/* List of configuration causes. */
ARRAY_DECL(causelist, char *);
/* tmux.c */ /* tmux.c */
extern struct options global_options; extern struct options global_options;
extern struct options global_s_options; extern struct options global_s_options;
@ -1262,10 +1265,9 @@ int areshell(const char *);
/* cfg.c */ /* cfg.c */
extern int cfg_finished; extern int cfg_finished;
extern char **cfg_causes; struct causelist cfg_causes;
extern u_int cfg_ncauses; void printflike2 cfg_add_cause(struct causelist *, const char *, ...);
void printflike3 cfg_add_cause(u_int *, char ***, const char *, ...); int load_cfg(const char *, struct cmd_ctx *, struct causelist *);
int load_cfg(const char *, struct cmd_ctx *, u_int *, char ***);
/* mode-key.c */ /* mode-key.c */
extern const struct mode_key_table mode_key_tables[]; extern const struct mode_key_table mode_key_tables[];