Processa comandos para definir dados da declaração.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 26 Apr 2015 15:54:47 +0000 (15:54 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Mon, 11 May 2015 00:37:37 +0000 (00:37 +0000)
Makefile.am
base.c [new file with mode: 0644]
base.h [new file with mode: 0644]
cmd.c [new file with mode: 0644]
cmd.h [new file with mode: 0644]
declara.c

index 7cc18af..59a2ab7 100644 (file)
@@ -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 (file)
index 0000000..fdc7980
--- /dev/null
+++ b/base.c
@@ -0,0 +1,103 @@
+/*
+ *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ *  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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+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 (file)
index 0000000..32b2779
--- /dev/null
+++ b/base.h
@@ -0,0 +1,24 @@
+/*
+ *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ *  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 (file)
index 0000000..ec7b6dc
--- /dev/null
+++ b/cmd.c
@@ -0,0 +1,55 @@
+/*
+ *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ *  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 <errno.h>
+#include <stdlib.h>
+
+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 (file)
index 0000000..e3f1250
--- /dev/null
+++ b/cmd.h
@@ -0,0 +1,33 @@
+/*
+ *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ *  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
index 873335e..bcda3d5 100644 (file)
--- a/declara.c
+++ b/declara.c
 #include <unistd.h>
 
 #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)