Nova versão do IRPF 2021.
[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
32 static int readline_support = 1;
33
34 static int fileprocess(struct declaracao *dec, int fd)
35 {
36         char *line = NULL;
37         size_t lsz = 0;
38         FILE *f;
39         int r;
40         int rc = 0;
41         int n = 1;
42         f = fdopen(fd, "r");
43         if (!f)
44                 return -errno;
45         while (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, dec->error ?: strerror(-r));
50                         rc = r;
51                 }
52                 n++;
53         }
54         free(line);
55         fclose(f);
56         return rc;
57 }
58
59 static int ttyprocess(struct declaracao *dec)
60 {
61         char *line = NULL;
62         int r;
63         using_history();
64         while ((line = readline("declara> ")) != NULL) {
65                 if (*line)
66                         add_history(line);
67                 r = cmd_run(dec, line);
68                 if (r < 0) {
69                         fprintf(stderr, "Não foi possível executar comando: %s\n",
70                                 dec->error ?: strerror(-r));
71                 }
72                 free(line);
73         }
74         return r;
75 }
76
77 static int process(char *filename)
78 {
79         int r = 0;
80         int fd;
81         struct declaracao *dec;
82         dec = declaracao_new(-1);
83         if (!dec)
84                 return -errno;
85         if (filename != NULL) {
86                 fd = open(filename, O_RDONLY);
87                 if (fd < 0) {
88                         r = -errno;
89                         goto out_open;
90                 }
91                 r = fileprocess(dec, fd);
92                 close(fd);
93         } else {
94                 if (isatty(0) && readline_support) {
95                         r = ttyprocess(dec);
96                 } else {
97                         r = fileprocess(dec, 0);
98                 }
99         }
100 out_open:
101         declaracao_free(dec);
102         return r;
103 }
104
105 static void usage(void)
106 {
107         fprintf(stderr, "declara <filename>\n");
108         exit(1);
109 }
110
111 int main(int argc, char **argv)
112 {
113         char *filename = NULL;
114         int r;
115         if (!readline_support && argc < 2)
116                 usage();
117
118         cmd_init();
119         dec_cmd_init();
120
121         if (argc > 1)
122                 filename = argv[1];
123         r = process(filename);
124         if (r)
125                 return 1;
126
127         return 0;
128 }