Move macros de comandos para definir inteiros e strings.
[cascardo/declara.git] / util.h
1 /*
2  *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
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 #ifndef _UTIL_H
20 #define _UTIL_H
21
22 int set_llong(char *str, long long *val);
23 int set_int(char **args, int argc, int *val);
24 int set_string(char **args, int argc, char **str);
25
26 #define SET_INT(attr) \
27 static int run_##attr(struct declaracao *dec, char **args, int argc) \
28 { \
29         int val; \
30         int r = set_int(args, argc, &val); \
31         if (r) \
32                 return r; \
33         dec->attr = val; \
34         return 0; \
35 } \
36 static struct cmd cmd_##attr = { \
37         .name = #attr, \
38         .run = run_##attr, \
39 };
40
41 #define SET_STRING(attr) \
42 static int run_##attr(struct declaracao *dec, char **args, int argc) \
43 { \
44         char *val; \
45         int r = set_string(args, argc, &val); \
46         if (r) \
47                 return r; \
48         dec->attr = val; \
49         return 0; \
50 } \
51 static struct cmd cmd_##attr = { \
52         .name = #attr, \
53         .run = run_##attr, \
54 }
55
56 #endif