Adiciona informação de taxas à declaração.
[cascardo/declara.git] / 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 "rendimento.h"
30
31 static int realprocess(struct declaracao *dec, int fd)
32 {
33         char *line = NULL;
34         size_t lsz = 0;
35         FILE *f;
36         int r;
37         f = fdopen(fd, "r");
38         if (!f)
39                 return -errno;
40         while ((r = getline(&line, &lsz, f)) > 0) {
41                 cmd_run(dec, line);
42         }
43         free(line);
44         return r;
45 }
46
47 static int process(char *filename)
48 {
49         int r = 0;
50         int fd;
51         struct declaracao *dec;
52         dec = declaracao_new(-1);
53         if (!dec)
54                 return -errno;
55         fd = open(filename, O_RDONLY);
56         if (fd < 0) {
57                 r = -errno;
58                 goto out_open;
59         }
60         realprocess(dec, fd);
61         close(fd);
62 out_open:
63         declaracao_free(dec);
64         return r;
65 }
66
67 static void usage(void)
68 {
69         fprintf(stderr, "declara <filename>\n");
70         exit(1);
71 }
72
73 int main(int argc, char **argv)
74 {
75         char *filename;
76         int r;
77         if (argc < 2)
78                 usage();
79
80         cmd_init();
81         base_cmd_init();
82         rendimento_cmd_init();
83
84         filename = argv[1];
85         r = process(filename);
86         if (r)
87                 return 1;
88         return 0;
89 }