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