[BACK]Return to cfg.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cfg.c, Revision 1.9

1.9     ! nicm        1: /* $OpenBSD: cfg.c,v 1.8 2009/11/26 21:37:13 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20: #include <sys/stat.h>
                     21:
                     22: #include <errno.h>
                     23: #include <stdio.h>
                     24: #include <string.h>
                     25:
                     26: #include "tmux.h"
                     27:
                     28: /*
                     29:  * Config file parser. Pretty quick and simple, each line is parsed into a
                     30:  * argv array and executed as a command.
                     31:  */
                     32:
                     33: void printflike2 cfg_print(struct cmd_ctx *, const char *, ...);
                     34: void printflike2 cfg_error(struct cmd_ctx *, const char *, ...);
                     35:
                     36: char    *cfg_cause;
1.9     ! nicm       37: int       cfg_finished;
        !            38: char    **cfg_causes;
        !            39: u_int     cfg_ncauses;
1.1       nicm       40:
1.8       nicm       41: /* ARGSUSED */
1.1       nicm       42: void printflike2
                     43: cfg_print(unused struct cmd_ctx *ctx, unused const char *fmt, ...)
                     44: {
                     45: }
                     46:
1.8       nicm       47: /* ARGSUSED */
1.1       nicm       48: void printflike2
                     49: cfg_error(unused struct cmd_ctx *ctx, const char *fmt, ...)
                     50: {
                     51:        va_list ap;
                     52:
                     53:        va_start(ap, fmt);
                     54:        xvasprintf(&cfg_cause, fmt, ap);
                     55:        va_end(ap);
                     56: }
                     57:
1.9     ! nicm       58: void printflike3
        !            59: cfg_add_cause(u_int *ncauses, char ***causes, const char *fmt, ...)
        !            60: {
        !            61:        char    *cause;
        !            62:        va_list  ap;
        !            63:
        !            64:        va_start(ap, fmt);
        !            65:        xvasprintf(&cause, fmt, ap);
        !            66:        va_end(ap);
        !            67:
        !            68:        *causes = xrealloc(*causes, *ncauses + 1, sizeof **causes);
        !            69:        (*causes)[(*ncauses)++] = cause;
        !            70: }
        !            71:
        !            72: /*
        !            73:  * Load configuration file. Returns -1 for an error with a list of messages in
        !            74:  * causes. Note that causes and ncauses must be initialised by the caller!
        !            75:  */
1.1       nicm       76: int
1.9     ! nicm       77: load_cfg(
        !            78:     const char *path, struct cmd_ctx *ctxin, u_int *ncauses, char ***causes)
1.1       nicm       79: {
1.7       deraadt    80:        FILE            *f;
1.1       nicm       81:        u_int            n;
1.9     ! nicm       82:        char            *buf, *line, *cause;
1.1       nicm       83:        size_t           len;
                     84:        struct cmd_list *cmdlist;
                     85:        struct cmd_ctx   ctx;
                     86:
                     87:        if ((f = fopen(path, "rb")) == NULL) {
1.9     ! nicm       88:                cfg_add_cause(ncauses, causes, "%s: %s", path, strerror(errno));
        !            89:                return (-1);
1.1       nicm       90:        }
                     91:        n = 0;
                     92:
                     93:        line = NULL;
                     94:        while ((buf = fgetln(f, &len))) {
                     95:                if (buf[len - 1] == '\n')
                     96:                        buf[len - 1] = '\0';
                     97:                else {
                     98:                        line = xrealloc(line, 1, len + 1);
                     99:                        memcpy(line, buf, len);
                    100:                        line[len] = '\0';
                    101:                        buf = line;
                    102:                }
                    103:                n++;
                    104:
1.9     ! nicm      105:                if (cmd_string_parse(buf, &cmdlist, &cause) != 0) {
        !           106:                        if (cause == NULL)
1.1       nicm      107:                                continue;
1.9     ! nicm      108:                        cfg_add_cause(
        !           109:                            ncauses, causes, "%s: %u: %s", path, n, cause);
        !           110:                        xfree(cause);
        !           111:                        continue;
1.1       nicm      112:                }
                    113:                if (cmdlist == NULL)
                    114:                        continue;
                    115:                cfg_cause = NULL;
                    116:
1.6       nicm      117:                if (ctxin == NULL) {
                    118:                        ctx.msgdata = NULL;
                    119:                        ctx.curclient = NULL;
                    120:                        ctx.cmdclient = NULL;
                    121:                } else {
                    122:                        ctx.msgdata = ctxin->msgdata;
                    123:                        ctx.curclient = ctxin->curclient;
                    124:                        ctx.cmdclient = ctxin->cmdclient;
                    125:                }
1.1       nicm      126:
                    127:                ctx.error = cfg_error;
                    128:                ctx.print = cfg_print;
                    129:                ctx.info = cfg_print;
                    130:
                    131:                cfg_cause = NULL;
                    132:                cmd_list_exec(cmdlist, &ctx);
                    133:                cmd_list_free(cmdlist);
                    134:                if (cfg_cause != NULL) {
1.9     ! nicm      135:                        cfg_add_cause(
        !           136:                            ncauses, causes, "%s: %d: %s", path, n, cfg_cause);
        !           137:                        xfree(cfg_cause);
        !           138:                        continue;
1.1       nicm      139:                }
                    140:        }
                    141:        if (line != NULL)
                    142:                xfree(line);
                    143:        fclose(f);
                    144:
1.9     ! nicm      145:        if (*ncauses != 0)
        !           146:                return (-1);
1.1       nicm      147:        return (0);
                    148: }