Mercurial > projects > pwm
view pwmrc.c @ 29:00d782cb45fa
Read configuration file on startup
author | Guido Berhoerster <guido+pwm@berhoerster.name> |
---|---|
date | Thu, 28 Sep 2017 10:06:59 +0200 |
parents | |
children | 2552eec9b913 |
line wrap: on
line source
/* * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "compat.h" #include <errno.h> #include <fcntl.h> #ifdef HAVE_ERR_H #include <err.h> #endif /* HAVE_ERR_H */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include "pwm.h" #include "cmd.h" #include "io.h" #include "macro.h" #include "tok.h" #include "util.h" static const char *config_cmds[] = { "set", "define" }; int pwmrc_read(struct pwm_ctx *ctx) { int retval = -1; char *filename = NULL; FILE *fp = NULL; int lineno; char buf[PWM_LINE_MAX] = { '\0' }; size_t tokenc = 0; union tok **tokenv = NULL; int argc = 0; char **argv = NULL; struct cmd *cmd; size_t i; int is_config_cmd; int j; xasprintf(&filename, "%s/pwmrc", ctx->dirname); fp = fopen(filename, "r"); if (fp == NULL) { if (errno == ENOENT) { retval = 0; } else { warn("failed to open configuration file \"%s\"", filename); } goto out; } for (lineno = 1;; lineno++) { /* read next line */ is_config_cmd = 0; if (fgets(buf, sizeof (buf), fp) == NULL) { if (feof(fp)) { goto done; } warn("failed to read from configuration file \"%s\"", filename); goto out; } if (strchr(buf, '\n') == NULL) { fprintf(stderr, "line %d too long\n", lineno); goto out; } /* tokenize line */ switch (tok_tokenize(buf, &tokenc, &tokenv)) { case TOK_ERR_UNTERMINATED_QUOTE: fprintf(stderr, "unterminated quote in line %d\n", lineno); if (!ctx->is_interactive) { goto out; } goto next; case TOK_ERR_TRAILING_BACKSLASH: fprintf(stderr, "trailing backslash in line %d\n", lineno); if (!ctx->is_interactive) { goto out; } goto next; case TOK_ERR_INVALID_MACRO_NAME: fprintf(stderr, "invalid macro name in line %d\n", lineno); if (!ctx->is_interactive) { goto out; } goto next; } /* expand macros */ if (macro_expand_macros(ctx->macro_head, tokenc, tokenv, &argc, &argv) != 0) { fprintf(stderr, "undefined macro in line %d\n", lineno); if (!ctx->is_interactive) { goto out; } goto next; } if (argc == 0) { /* empty line */ goto next; } /* find and execute the command */ cmd = cmd_match(argv[0]); for (i = 0; (cmd != NULL) && (i < COUNTOF(config_cmds)); i++) { if (strcmp(cmd->full_cmd, config_cmds[i]) == 0) { is_config_cmd = 1; break; } } if (!is_config_cmd) { fprintf(stderr, "unknown configuration command \"%s\"" " in line %d\n", argv[0], lineno); goto out; } if (cmd->cmd_func(ctx, argc, argv) != CMD_OK) { fprintf(stderr, "syntax error in line %d\n", lineno); goto out; } next: for (j = 0; j < argc; j++) { free(argv[j]); } free(argv); argc = 0; argv = NULL; tok_free(tokenv); tokenc = 0; tokenv = NULL; } done: retval = 0; out: for (j = 0; j < argc; j++) { free(argv[j]); } free(argv); tok_free(tokenv); if (fp != NULL) { fclose(fp); } free(filename); return (retval); }