Hash :
a5dc42e3
Author :
Thomas de Grivel
Date :
2024-12-03T22:15:52
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/* kc3
* Copyright 2022,2023,2024 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 <errno.h>
#include <string.h>
#include "../libkc3/kc3.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 ikc3_run (void)
{
s_tag input;
sw r;
s_tag result;
while (1) {
r = buf_ignore_spaces(g_kc3_env->in);
if (r < 0)
return 0;
r = buf_parse_comments(g_kc3_env->in);
if (r < 0)
return 0;
r = buf_parse_tag(g_kc3_env->in, &input);
if (r > 0) {
if (! eval_tag(&input, &result)) {
tag_clean(&input);
continue;
}
if (buf_inspect_tag(g_kc3_env->out, &result) < 0) {
tag_clean(&input);
tag_clean(&result);
return 0;
}
tag_clean(&input);
tag_clean(&result);
}
if (r < 0 ||
(r == 0 &&
(r = buf_ignore_character(g_kc3_env->in)) <= 0))
return 0;
if ((r = buf_write_1(g_kc3_env->out, "\n")) < 0)
return 0;
if ((r = buf_flush(g_kc3_env->out)) < 0)
return 0;
}
return 0;
}
int main (int argc, char **argv)
{
sw e = 0;
s_env *env;
s_tag *file_dir;
s_tag file_dir_save;
s_tag *file_path;
s_tag file_path_save;
FILE *fp = 0;
s_buf in_original;
sw r;
if (argc < 1)
return usage("ikc3");
if (! kc3_init(NULL, &argc, &argv))
return 1;
env = g_kc3_env;
in_original = *env->in;
while (argc) {
if (! strcmp("--load", *argv) ||
! strcmp("-l", *argv)) {
if (argc < 2) {
err_write_1(env->argv[0]);
err_write_1(": ");
err_write_1(*argv);
err_write_1(" without an argument\n");
assert(! "env_init: -l or --load without an argument");
r = 1;
goto clean;
}
fp = file_open(argv[1], "rb");
if (! fp) {
e = errno;
err_write_1("ikc3: ");
err_write_1(argv[1]);
err_write_1(": ");
err_write_1(strerror(e));
err_write_1("\n");
assert(! "ikc3: fopen");
r = 1;
goto clean;
}
if (! buf_file_open_r(env->in, fp)) {
r = -1;
goto clean;
}
file_dir = frame_get_w(env->global_frame, &g_sym___DIR__);
file_dir_save = *file_dir;
file_path = frame_get_w(env->global_frame, &g_sym___FILE__);
file_path_save = *file_path;
tag_init_str_1(file_path, NULL, argv[1]);
file_dir->type = TAG_STR;
if (! file_dirname(&file_path->data.str, &file_dir->data.str)) {
buf_file_close(env->in);
r = -1;
goto clean;
}
r = ikc3_run();
*file_dir = file_dir_save;
*file_path = file_path_save;
buf_file_close(env->in);
fclose(fp);
if (r)
goto clean;
argc -= 2;
argv += 2;
}
else if (argc == 1 && ! strcmp("--quit", *argv)) {
r = 0;
goto clean;
}
else
break;
}
#if HAVE_WINEDITLINE
buf_wineditline_open_r(env->in, "ikc3> ", ".ikc3_history");
#else
buf_linenoise_open_r(env->in, "ikc3> ", ".ikc3_history");
#endif
r = ikc3_run();
#if HAVE_WINEDITLINE
buf_wineditline_close(env->in, ".ikc3_history");
#else
buf_linenoise_close(env->in, ".ikc3_history");
#endif
clean:
*env->in = in_original;
kc3_clean(NULL);
return r;
}
int usage (char *argv0)
{
printf("Usage: %s [--load FILE] [--quit]\n", argv0);
return 1;
}