Adiciona implementaĆ§Ć£o de lista.
[cascardo/declara.git] / base.c
1 /*
2  *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include "cmd.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 static int set_int(char **args, int argc, int *val)
26 {
27         char *end = NULL;
28         if (argc != 2)
29                 return -EINVAL;
30         errno = 0;
31         *val = strtol(args[1], &end, 0);
32         if (end && *end)
33                 return -EINVAL;
34         if (errno == ERANGE)
35                 return -ERANGE;
36         return 0;
37 }
38
39 static int set_string(char **args, int argc, char **str)
40 {
41         if (argc != 2)
42                 return -EINVAL;
43         *str = strdup(args[1]);
44         if (!*str)
45                 return -errno;
46         return 0;
47 }
48
49 #define SET_INT(attr) \
50 static int run_##attr(struct declaracao *dec, char **args, int argc) \
51 { \
52         int val; \
53         int r = set_int(args, argc, &val); \
54         if (r) \
55                 return r; \
56         dec->attr = val; \
57         return 0; \
58 } \
59 static struct cmd cmd_##attr = { \
60         .name = #attr, \
61         .run = run_##attr, \
62 };
63
64 #define SET_STRING(attr) \
65 static int run_##attr(struct declaracao *dec, char **args, int argc) \
66 { \
67         char *val; \
68         int r = set_string(args, argc, &val); \
69         if (r) \
70                 return r; \
71         dec->attr = val; \
72         return 0; \
73 } \
74 static struct cmd cmd_##attr = { \
75         .name = #attr, \
76         .run = run_##attr, \
77 }
78
79 SET_INT(ano);
80 SET_STRING(cpf);
81 SET_STRING(nome);
82
83 static int run_dump(struct declaracao *dec, char **args, int argc)
84 {
85         printf("ano: %d\n", dec->ano);
86         printf("cpf: %s\n", dec->cpf);
87         printf("nome: %s\n", dec->nome);
88         return 0;
89 }
90
91 static struct cmd cmd_dump = {
92         .name = "dump",
93         .run = run_dump,
94 };
95
96 int base_cmd_init(void)
97 {
98         cmd_add(&cmd_dump);
99         cmd_add(&cmd_ano);
100         cmd_add(&cmd_cpf);
101         cmd_add(&cmd_nome);
102         return 0;
103 }