Adiciona comando help.
[cascardo/declara.git] / src / declara.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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 #include <readline/readline.h>
27 #include <readline/history.h>
28
29 #include "declaracao.h"
30 #include "cmd.h"
31 #include "base.h"
32 #include "contribuinte.h"
33 #include "conjuge.h"
34 #include "rendimento.h"
35 #include "isento.h"
36 #include "pagamento.h"
37 #include "bem.h"
38 #include "dependente.h"
39 #include "calcula.h"
40 #include "gera.h"
41 #include "help.h"
42
43 static int readline_support = 1;
44
45 static int fileprocess(struct declaracao *dec, int fd)
46 {
47         char *line = NULL;
48         size_t lsz = 0;
49         FILE *f;
50         int r;
51         int n = 1;
52         f = fdopen(fd, "r");
53         if (!f)
54                 return -errno;
55         while ((r = getline(&line, &lsz, f)) > 0) {
56                 r = cmd_run(dec, line);
57                 if (r < 0) {
58                         fprintf(stderr, "Não foi possível executar comando na linha %d: %s\n",
59                                 n, strerror(-r));
60                 }
61                 n++;
62         }
63         free(line);
64         return r;
65 }
66
67 static int ttyprocess(struct declaracao *dec)
68 {
69         char *line = NULL;
70         int r;
71         using_history();
72         while ((line = readline("declara> ")) != NULL) {
73                 if (*line)
74                         add_history(line);
75                 r = cmd_run(dec, line);
76                 if (r < 0) {
77                         fprintf(stderr, "Não foi possível executar comando: %s\n",
78                                 strerror(-r));
79                 }
80                 free(line);
81         }
82         return r;
83 }
84
85 static int process(char *filename)
86 {
87         int r = 0;
88         int fd;
89         struct declaracao *dec;
90         dec = declaracao_new(-1);
91         if (!dec)
92                 return -errno;
93         if (filename != NULL) {
94                 fd = open(filename, O_RDONLY);
95                 if (fd < 0) {
96                         r = -errno;
97                         goto out_open;
98                 }
99                 r = fileprocess(dec, fd);
100                 close(fd);
101         } else {
102                 if (isatty(0) && readline_support) {
103                         r = ttyprocess(dec);
104                 } else {
105                         r = fileprocess(dec, 0);
106                 }
107         }
108 out_open:
109         declaracao_free(dec);
110         return r;
111 }
112
113 static void usage(void)
114 {
115         fprintf(stderr, "declara <filename>\n");
116         exit(1);
117 }
118
119 int main(int argc, char **argv)
120 {
121         char *filename = NULL;
122         int r;
123         if (!readline_support && argc < 2)
124                 usage();
125
126         cmd_init();
127         base_cmd_init();
128         contribuinte_cmd_init();
129         conjuge_cmd_init();
130         rendimento_cmd_init();
131         isento_cmd_init();
132         pagamento_cmd_init();
133         bem_cmd_init();
134         dependente_cmd_init();
135         calcula_cmd_init();
136         gera_cmd_init();
137         sistema_cmd_init();
138         help_cmd_init();
139
140         if (argc > 1)
141                 filename = argv[1];
142         r = process(filename);
143         if (r)
144                 return 1;
145
146         return 0;
147 }