Hash :
ae709b76
Author :
Thomas de Grivel
Date :
2023-12-13T21:53:07
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "../libc3/c3.h"
#include "config.h"
#if HAVE_WINEDITLINE
# include "buf_wineditline.h"
#else
# include "buf_linenoise.h"
#endif
#define BUFSZ 0x10000
int usage (char *argv0);
sw buf_ignore_character (s_buf *buf)
{
u8 b;
character c = 0;
sw csize;
sw r;
if ((r = buf_peek_character_utf8(buf, &c)) > 0)
csize = r;
else if ((r = buf_peek_u8(buf, &b)) > 0)
csize = 1;
else
return r;
if ((r = buf_ignore(buf, csize)) < 0)
return r;
return csize;
}
sw ic3_buf_ignore_spaces (s_buf *out, s_buf *in)
{
character c;
sw csize;
sw r;
sw result = 0;
assert(in);
assert(out);
while ((r = buf_peek_character_utf8(in, &c)) > 0 &&
c >= 0 &&
c < UCD_MAX &&
g_ucd[c].flags & (UCD_OTHER_CONTROL | UCD_SEPARATOR_SPACE)) {
csize = r;
if ((r = buf_ignore(in, csize)) != csize)
return -1;
result += csize;
if ((r = buf_flush(out)) < 0)
return -1;
}
if (r < 0)
return r;
return result;
}
sw buf_xfer_spaces (s_buf *out, s_buf *in)
{
character c;
sw csize;
sw r;
sw size = 0;
assert(in);
assert(out);
while ((r = buf_peek_character_utf8(in, &c)) > 0 &&
c >= 0 &&
c < UCD_MAX &&
g_ucd[c].flags & (UCD_OTHER_CONTROL | UCD_SEPARATOR_SPACE)) {
csize = r;
if ((r = buf_xfer(out, in, csize)) != csize)
return -1;
size += csize;
if ((r = buf_flush(out)) < 0)
return -1;
}
if (r < 0)
return r;
return size;
}
int main (int argc, char **argv)
{
s8 i[BUF_SIZE];
s_buf in;
s_tag input;
s8 o[BUF_SIZE];
s_buf out;
sw r;
s_tag result;
if (! c3_init(NULL, argc, argv))
return 1;
if (argc < 1)
return usage(argv[0]);
buf_init(&in, false, sizeof(i), i);
#if HAVE_WINEDITLINE
buf_wineditline_open_r(&in, "ic3> ", ".ic3_history");
#else
buf_linenoise_open_r(&in, "ic3> ", ".ic3_history");
#endif
in.line = 0;
buf_init(&out, false, sizeof(o), o);
buf_file_open_w(&out, stdout);
while ((r = ic3_buf_ignore_spaces(&out, &in)) >= 0) {
if ((r = buf_parse_tag(&in, &input)) > 0) {
if (! eval_tag(&input, &result)) {
tag_clean(&input);
continue;
}
if (buf_inspect_tag(&out, &result) < 0) {
tag_clean(&input);
tag_clean(&result);
break;
}
tag_clean(&input);
tag_clean(&result);
buf_write_u8(&out, '\n');
if ((r = buf_flush(&out)) < 0)
break;
}
if (r < 0 ||
(r == 0 &&
(r = buf_ignore_character(&in)) <= 0))
break;
}
#if HAVE_WINEDITLINE
buf_wineditline_close(&in, ".ic3_history");
#else
buf_linenoise_close(&in, ".ic3_history");
#endif
buf_file_close(&out);
c3_clean(NULL);
return 0;
}
int usage (char *argv0)
{
printf("Usage: %s\n", argv0);
return 1;
}