From 3ed94b497b3a2617a28fd1b9feb08e1cb9d42b00 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sun, 26 Apr 2015 15:54:47 +0000 Subject: [PATCH] =?utf8?q?Processa=20comandos=20para=20definir=20dados=20d?= =?utf8?q?a=20declara=C3=A7=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Makefile.am | 5 ++- base.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ base.h | 24 ++++++++++++ cmd.c | 55 ++++++++++++++++++++++++++++ cmd.h | 33 +++++++++++++++++ declara.c | 23 ++++++++++++ 6 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 base.c create mode 100644 base.h create mode 100644 cmd.c create mode 100644 cmd.h diff --git a/Makefile.am b/Makefile.am index 7cc18af..59a2ab7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,2 +1,5 @@ bin_PROGRAMS = declara -declara_SOURCES = declara.c declaracao.c declaracao.h +declara_SOURCES = declara.c declaracao.c declaracao.h \ + token.c token.h \ + cmd.c cmd.h pmhash.c pmhash.h \ + base.c base.h diff --git a/base.c b/base.c new file mode 100644 index 0000000..fdc7980 --- /dev/null +++ b/base.c @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "cmd.h" +#include +#include +#include +#include + +static int set_int(char **args, int *val) +{ + char *end = NULL; + if (args[0] == NULL || args[1] == NULL) + return -EINVAL; + errno = 0; + *val = strtol(args[1], &end, 0); + if (end && *end) + return -EINVAL; + if (errno == ERANGE) + return -ERANGE; + return 0; +} + +static int set_string(char **args, char **str) +{ + if (args[0] == NULL || args[1] == NULL) + return -EINVAL; + *str = strdup(args[1]); + if (!*str) + return -errno; + return 0; +} + +#define SET_INT(attr) \ +static int run_##attr(struct declaracao *dec, char **args) \ +{ \ + int val; \ + int r = set_int(args, &val); \ + if (r) \ + return r; \ + dec->attr = val; \ + return 0; \ +} \ +static struct cmd cmd_##attr = { \ + .name = #attr, \ + .run = run_##attr, \ +}; + +#define SET_STRING(attr) \ +static int run_##attr(struct declaracao *dec, char **args) \ +{ \ + char *val; \ + int r = set_string(args, &val); \ + if (r) \ + return r; \ + dec->attr = val; \ + return 0; \ +} \ +static struct cmd cmd_##attr = { \ + .name = #attr, \ + .run = run_##attr, \ +} + +SET_INT(ano); +SET_STRING(cpf); +SET_STRING(nome); + +static int run_dump(struct declaracao *dec, char **args) +{ + printf("ano: %d\n", dec->ano); + printf("cpf: %s\n", dec->cpf); + printf("nome: %s\n", dec->nome); + return 0; +} + +static struct cmd cmd_dump = { + .name = "dump", + .run = run_dump, +}; + +int base_cmd_init(void) +{ + cmd_add(&cmd_dump); + cmd_add(&cmd_ano); + cmd_add(&cmd_cpf); + cmd_add(&cmd_nome); + return 0; +} diff --git a/base.h b/base.h new file mode 100644 index 0000000..32b2779 --- /dev/null +++ b/base.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _BASE_H +#define _BASE_H + +int base_cmd_init(void); + +#endif diff --git a/cmd.c b/cmd.c new file mode 100644 index 0000000..ec7b6dc --- /dev/null +++ b/cmd.c @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "cmd.h" +#include "pmhash.h" +#include "token.h" +#include +#include + +struct pmhash *cmds; + +int cmd_init(void) +{ + cmds = pmhash_new(); + if (!cmds) + return -errno; + return 0; +} + +int cmd_add(struct cmd *cmd) +{ + return pmhash_add(&cmds, cmd->name, cmd); +} + +int cmd_run(struct declaracao *dec, char *line) +{ + char **args = tokens_new(line); + struct cmd *cmd; + int r; + if (!args) + return -errno; + cmd = pmhash_get(cmds, args[0]); + if (!cmd || !cmd->run) { + tokens_free(args); + return -EINVAL; + } + r = cmd->run(dec, args); + tokens_free(args); + return r; +} diff --git a/cmd.h b/cmd.h new file mode 100644 index 0000000..e3f1250 --- /dev/null +++ b/cmd.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _CMD_H +#define _CMD_H + +#include "declaracao.h" + +struct cmd { + char *name; + int (*run) (struct declaracao *dec, char **args); +}; + +int cmd_init(void); +int cmd_add(struct cmd *cmd); +int cmd_run(struct declaracao *dec, char *line); + +#endif diff --git a/declara.c b/declara.c index 873335e..bcda3d5 100644 --- a/declara.c +++ b/declara.c @@ -24,6 +24,24 @@ #include #include "declaracao.h" +#include "cmd.h" +#include "base.h" + +static int realprocess(struct declaracao *dec, int fd) +{ + char *line = NULL; + size_t lsz = 0; + FILE *f; + int r; + f = fdopen(fd, "r"); + if (!f) + return -errno; + while ((r = getline(&line, &lsz, f)) > 0) { + cmd_run(dec, line); + } + free(line); + return r; +} static int process(char *filename) { @@ -38,6 +56,7 @@ static int process(char *filename) r = -errno; goto out_open; } + realprocess(dec, fd); close(fd); out_open: declaracao_free(dec); @@ -56,6 +75,10 @@ int main(int argc, char **argv) int r; if (argc < 2) usage(); + + cmd_init(); + base_cmd_init(); + filename = argv[1]; r = process(filename); if (r) -- 2.20.1