Adiciona suporte a linha de dependentes.
[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 "declaracao.h"
27 #include "cmd.h"
28 #include "base.h"
29 #include "contribuinte.h"
30 #include "rendimento.h"
31 #include "isento.h"
32 #include "pagamento.h"
33 #include "bem.h"
34 #include "dependente.h"
35 #include "calcula.h"
36 #include "gera.h"
37
38 static int realprocess(struct declaracao *dec, int fd)
39 {
40         char *line = NULL;
41         size_t lsz = 0;
42         FILE *f;
43         int r;
44         int n = 1;
45         f = fdopen(fd, "r");
46         if (!f)
47                 return -errno;
48         while ((r = getline(&line, &lsz, f)) > 0) {
49                 r = cmd_run(dec, line);
50                 if (r < 0) {
51                         fprintf(stderr, "Não foi possível executar comando na linha %d: %s\n",
52                                 n, strerror(-r));
53                 }
54                 n++;
55         }
56         free(line);
57         return r;
58 }
59
60 static int process(char *filename)
61 {
62         int r = 0;
63         int fd;
64         struct declaracao *dec;
65         dec = declaracao_new(-1);
66         if (!dec)
67                 return -errno;
68         fd = open(filename, O_RDONLY);
69         if (fd < 0) {
70                 r = -errno;
71                 goto out_open;
72         }
73         realprocess(dec, fd);
74         close(fd);
75 out_open:
76         declaracao_free(dec);
77         return r;
78 }
79
80 static void usage(void)
81 {
82         fprintf(stderr, "declara <filename>\n");
83         exit(1);
84 }
85
86 int main(int argc, char **argv)
87 {
88         char *filename;
89         int r;
90         if (argc < 2)
91                 usage();
92
93         cmd_init();
94         base_cmd_init();
95         contribuinte_cmd_init();
96         rendimento_cmd_init();
97         isento_cmd_init();
98         pagamento_cmd_init();
99         bem_cmd_init();
100         dependente_cmd_init();
101         calcula_cmd_init();
102         gera_cmd_init();
103         sistema_cmd_init();
104
105         filename = argv[1];
106         r = process(filename);
107         if (r)
108                 return 1;
109         return 0;
110 }