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
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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
#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)
{
r = -errno;
goto out_open;
}
+ realprocess(dec, fd);
close(fd);
out_open:
declaracao_free(dec);
int r;
if (argc < 2)
usage();
+
+ cmd_init();
+ base_cmd_init();
+
filename = argv[1];
r = process(filename);
if (r)