diff options
author | Behdad Esfahbod <behdad@behdad.org> | 2009-11-22 15:24:28 -0500 |
---|---|---|
committer | Behdad Esfahbod <behdad@behdad.org> | 2009-11-22 15:24:28 -0500 |
commit | e25247345d6c075a131a90cb2c65471811a89c64 (patch) | |
tree | fd55591b86d6beb48bbab14affb229c5d4bd1328 | |
parent | 91448540f68235c7e1975fa604842b7872bc0f7a (diff) |
[interpret] Make the interpret app interactive
-rw-r--r-- | src/interpret.c | 147 |
1 files changed, 75 insertions, 72 deletions
diff --git a/src/interpret.c b/src/interpret.c index cfdbd44..6bf155b 100644 --- a/src/interpret.c +++ b/src/interpret.c @@ -26,6 +26,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <fcntl.h> #include <glib.h> #include <glib-object.h> #include "caps.h" @@ -40,13 +41,12 @@ main(int argc, char **argv) char *terminal = NULL; struct _vte_matcher *matcher = NULL; struct _vte_termcap *termcap = NULL; - VteBuffer *buffer = NULL; GArray *array; unsigned int i, j; int l; - char b; + unsigned char buf[4096]; GValue *value; - FILE *infile = NULL; + int infile; struct _vte_iso2022_state *subst; const char *tmp; GQuark quark; @@ -60,14 +60,14 @@ main(int argc, char **argv) } if ((argc > 2) && (strcmp(argv[2], "-") != 0)) { - infile = fopen(argv[2], "r"); - if (infile == NULL) { + infile = open (argv[2], O_RDONLY); + if (infile == -1) { g_print("error opening %s: %s\n", argv[2], strerror(errno)); exit(1); } } else { - infile = stdin; + infile = 1; } g_type_init(); @@ -77,90 +77,93 @@ main(int argc, char **argv) if (termcap == NULL) { termcap = _vte_termcap_new("/etc/termcap"); } - buffer = _vte_buffer_new(); array = g_array_new(FALSE, FALSE, sizeof(gunichar)); matcher = _vte_matcher_new(terminal, termcap); subst = _vte_iso2022_state_new(NULL, NULL, NULL); - while (fread(&b, 1, 1, infile) == 1) { - _vte_buffer_append(buffer, &b, 1); - } - _vte_iso2022_process(subst, buffer->data, - _vte_buffer_length(buffer), array); - - i = 0; - while (i <= array->len) { - tmp = NULL; - values = NULL; - for (j = 1; j < (array->len - i); j++) { - _vte_matcher_match(matcher, - &g_array_index(array, gunichar, i), - j, - &tmp, - NULL, - &quark, - &values); - if ((tmp == NULL) || (strlen(tmp) > 0)) { - break; - } - } - if (i + j == array->len) { - g_print("End of data.\n"); + for (;;) { + l = read (infile, buf, sizeof (buf)); + if (!l) break; + if (l == -1) { + if (errno == EAGAIN) + continue; + exit (1); } - if (tmp == NULL) { - gunichar c; - c = g_array_index(array, gunichar, i); - if (VTE_ISO2022_HAS_ENCODED_WIDTH(c)) { - c &= ~VTE_ISO2022_ENCODED_WIDTH_MASK; - } - if (c < 32) { - g_print("`^%c'\n", c + 64); - } else - if (c < 127) { - g_print("`%c'\n", c); - } else { - g_print("`0x%x'\n", c); - } - i++; - continue; - } + g_array_set_size (array, 0); + _vte_iso2022_process(subst, buf, (unsigned int) l, array); - l = j; - g_print("%s(", g_quark_to_string(quark)); - for (j = 0; (values != NULL) && (j < values->n_values); j++) { - if (j > 0) { - g_print(", "); + i = 0; + while (i <= array->len) { + tmp = NULL; + values = NULL; + for (j = 1; j < (array->len - i); j++) { + _vte_matcher_match(matcher, + &g_array_index(array, gunichar, i), + j, + &tmp, + NULL, + &quark, + &values); + if ((tmp == NULL) || (strlen(tmp) > 0)) { + break; + } + } + if (i + j == array->len) { + break; } - value = g_value_array_get_nth(values, j); - if (G_VALUE_HOLDS_LONG(value)) { - g_print("%ld", g_value_get_long(value)); + if (tmp == NULL) { + gunichar c; + c = g_array_index(array, gunichar, i); + if (VTE_ISO2022_HAS_ENCODED_WIDTH(c)) { + c &= ~VTE_ISO2022_ENCODED_WIDTH_MASK; + } + if (c < 32) { + g_print("`^%c'\n", c + 64); + } else + if (c < 127) { + g_print("`%c'\n", c); + } else { + g_print("`0x%x'\n", c); + } + i++; + continue; } - if (G_VALUE_HOLDS_STRING(value)) { - g_print("`%s'", - g_value_get_string(value)); + + l = j; + g_print("%s(", g_quark_to_string(quark)); + for (j = 0; (values != NULL) && (j < values->n_values); j++) { + if (j > 0) { + g_print(", "); + } + value = g_value_array_get_nth(values, j); + if (G_VALUE_HOLDS_LONG(value)) { + g_print("%ld", g_value_get_long(value)); + } + if (G_VALUE_HOLDS_STRING(value)) { + g_print("`%s'", + g_value_get_string(value)); + } + if (G_VALUE_HOLDS_POINTER(value)) { + g_print("`%ls'", + (wchar_t*) + g_value_get_pointer(value)); + } } - if (G_VALUE_HOLDS_POINTER(value)) { - g_print("`%ls'", - (wchar_t*) - g_value_get_pointer(value)); + if (values != NULL) { + _vte_matcher_free_params_array(matcher, values); } + g_print(")\n"); + i += l; } - if (values != NULL) { - _vte_matcher_free_params_array(matcher, values); - } - g_print(")\n"); - i += l; } + g_print("End of data.\n"); - if (infile != stdin) { - fclose(infile); - } + close (infile); _vte_iso2022_state_free(subst); - _vte_buffer_free(buffer); g_array_free(array, TRUE); _vte_termcap_free(termcap); _vte_matcher_free(matcher); |