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