54e1abbc5c9d6b639f615cd7ee322c78713b963a
[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
42 static int readline_support = 1;
43
44 static int fileprocess(struct declaracao *dec, int fd)
45 {
46         char *line = NULL;
47         size_t lsz = 0;
48         FILE *f;
49         int r;
50         int n = 1;
51         f = fdopen(fd, "r");
52         if (!f)
53                 return -errno;
54         while ((r = getline(&line, &lsz, f)) > 0) {
55                 r = cmd_run(dec, line);
56                 if (r < 0) {
57                         fprintf(stderr, "Não foi possível executar comando na linha %d: %s\n",
58                                 n, strerror(-r));
59                 }
60                 n++;
61         }
62         free(line);
63         return r;
64 }
65
66 static int ttyprocess(struct declaracao *dec)
67 {
68         char *line = NULL;
69         int r;
70         using_history();
71         while ((line = readline("declara> ")) != NULL) {
72                 if (*line)
73                         add_history(line);
74                 r = cmd_run(dec, line);
75                 if (r < 0) {
76                         fprintf(stderr, "Não foi possível executar comando: %s\n",
77                                 strerror(-r));
78                 }
79                 free(line);
80         }
81         return r;
82 }
83
84 static int process(char *filename)
85 {
86         int r = 0;
87         int fd;
88         struct declaracao *dec;
89         dec = declaracao_new(-1);
90         if (!dec)
91                 return -errno;
92         if (filename != NULL) {
93                 fd = open(filename, O_RDONLY);
94                 if (fd < 0) {
95                         r = -errno;
96                         goto out_open;
97                 }
98                 r = fileprocess(dec, fd);
99                 close(fd);
100         } else {
101                 if (isatty(0) && readline_support) {
102                         r = ttyprocess(dec);
103                 } else {
104                         r = fileprocess(dec, 0);
105                 }
106         }
107 out_open:
108         declaracao_free(dec);
109         return r;
110 }
111
112 static void usage(void)
113 {
114         fprintf(stderr, "declara <filename>\n");
115         exit(1);
116 }
117
118 int main(int argc, char **argv)
119 {
120         char *filename = NULL;
121         int r;
122         if (!readline_support && argc < 2)
123                 usage();
124
125         cmd_init();
126         base_cmd_init();
127         contribuinte_cmd_init();
128         conjuge_cmd_init();
129         rendimento_cmd_init();
130         isento_cmd_init();
131         pagamento_cmd_init();
132         bem_cmd_init();
133         dependente_cmd_init();
134         calcula_cmd_init();
135         gera_cmd_init();
136         sistema_cmd_init();
137
138         if (argc > 1)
139                 filename = argv[1];
140         r = process(filename);
141         if (r)
142                 return 1;
143
144         return 0;
145 }