AC_SEARCH_LIBS([crc32], [z], [],
AC_MSG_ERROR([could not find zlib]))
+dnl Checking for readline
+AC_SEARCH_LIBS([readline], [readline], [],
+ AC_MSG_ERROR([could not find readline]))
+
AC_CONFIG_HEADERS([config.h])
AC_OUTPUT(Makefile lib/Makefile src/Makefile test/Makefile)
#include <fcntl.h>
#include <unistd.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+
#include "declaracao.h"
#include "cmd.h"
#include "base.h"
#include "calcula.h"
#include "gera.h"
-static int realprocess(struct declaracao *dec, int fd)
+static int readline_support = 1;
+
+static int fileprocess(struct declaracao *dec, int fd)
{
char *line = NULL;
size_t lsz = 0;
return r;
}
+static int ttyprocess(struct declaracao *dec)
+{
+ char *line = NULL;
+ int r;
+ using_history();
+ while ((line = readline("declara> ")) != NULL) {
+ if (*line)
+ add_history(line);
+ r = cmd_run(dec, line);
+ if (r < 0) {
+ fprintf(stderr, "Não foi possível executar comando: %s\n",
+ strerror(-r));
+ }
+ free(line);
+ }
+ return r;
+}
+
static int process(char *filename)
{
int r = 0;
dec = declaracao_new(-1);
if (!dec)
return -errno;
- fd = open(filename, O_RDONLY);
- if (fd < 0) {
- r = -errno;
- goto out_open;
+ if (filename != NULL) {
+ fd = open(filename, O_RDONLY);
+ if (fd < 0) {
+ r = -errno;
+ goto out_open;
+ }
+ r = fileprocess(dec, fd);
+ close(fd);
+ } else {
+ if (isatty(0) && readline_support) {
+ r = ttyprocess(dec);
+ } else {
+ r = fileprocess(dec, 0);
+ }
}
- realprocess(dec, fd);
- close(fd);
out_open:
declaracao_free(dec);
return r;
int main(int argc, char **argv)
{
- char *filename;
+ char *filename = NULL;
int r;
- if (argc < 2)
+ if (!readline_support && argc < 2)
usage();
cmd_init();
gera_cmd_init();
sistema_cmd_init();
- filename = argv[1];
+ if (argc > 1)
+ filename = argv[1];
r = process(filename);
if (r)
return 1;
+
return 0;
}