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