• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/main.c

  1 /*
  2  * Copyright (C) 2020-2021 Jo-Philipp Wich <jo@mein.io>
  3  *
  4  * Permission to use, copy, modify, and/or distribute this software for any
  5  * purpose with or without fee is hereby granted, provided that the above
  6  * copyright notice and this permission notice appear in all copies.
  7  *
  8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15  */
 16 
 17 #include <stdio.h>
 18 #include <stdbool.h>
 19 #include <stdint.h>
 20 #include <unistd.h>
 21 #include <errno.h>
 22 #include <ctype.h>
 23 #include <fcntl.h>
 24 #include <sys/stat.h>
 25 #include <sys/types.h>
 26 
 27 #include "json-c-compat.h"
 28 
 29 #include "ucode/compiler.h"
 30 #include "ucode/lexer.h"
 31 #include "ucode/lib.h"
 32 #include "ucode/vm.h"
 33 #include "ucode/source.h"
 34 #include "ucode/program.h"
 35 
 36 static FILE *stdin_unused;
 37 
 38 static void
 39 print_usage(const char *app)
 40 {
 41         printf(
 42         "Usage:\n"
 43         "  %1$s -h\n"
 44         "  %1$s -e \"expression\"\n"
 45         "  %1$s input.uc [input2.uc ...]\n"
 46         "  %1$s -c [-s] [-o output.uc] input.uc [input2.uc ...]\n\n"
 47 
 48         "-h\n"
 49         "  Help display this help.\n\n"
 50 
 51         "-e \"expression\"\n"
 52         "  Execute the given expression as ucode program.\n\n"
 53 
 54         "-p \"expression\"\n"
 55         "  Like `-e` but print the result of expression.\n\n"
 56 
 57         "-t\n"
 58         "  Enable VM execution tracing.\n\n"
 59 
 60         "-g interval\n"
 61         "  Perform periodic garbage collection every `interval` object\n"
 62         "  allocations.\n\n"
 63 
 64         "-S\n"
 65         "  Enable strict mode.\n\n"
 66 
 67         "-R\n"
 68         "  Process source file(s) as raw script code (default).\n\n"
 69 
 70         "-T[flag,flag,...]\n"
 71         "  Process the source file(s) as templates, not as raw script code.\n"
 72         "  Supported flags: no-lstrip (don't strip leading whitespace before\n"
 73         "  block tags), no-rtrim (don't strip trailing newline after block tags).\n\n"
 74 
 75         "-D [name=]value\n"
 76         "  Define global variable. If `name` is omitted, a JSON dictionary is\n"
 77         "  expected with each property becoming a global variable set to the\n"
 78         "  corresponding value. If `name` is specified, it is defined as global\n"
 79         "  variable set to `value` parsed as JSON (or the literal `value` string\n"
 80         "  if JSON parsing fails).\n\n"
 81 
 82         "-F [name=]path\n"
 83         "  Like `-D` but reading the value from the file in `path`. The given\n"
 84         "  file must contain a single, well-formed JSON dictionary.\n\n"
 85 
 86         "-U name\n"
 87         "  Undefine the given global variable name.\n\n"
 88 
 89         "-l [name=]library\n"
 90         "  Preload the given `library`, optionally aliased to `name`.\n\n"
 91 
 92         "-L pattern\n"
 93         "  Prepend given `pattern` to default library search paths. If the pattern\n"
 94         "  contains no `*`, it is added twice, once with `/*.so` and once with\n"
 95         "  `/*.uc` appended to it.\n\n"
 96 
 97         "-c[flag,flag,...]\n"
 98         "  Compile the given source file(s) to bytecode instead of executing them.\n"
 99         "  Supported flags: no-interp (omit interpreter line), interp=... (over-\n"
100         "  ride interpreter line with ...), dynlink=... (force import from ... to\n"
101         "  be treated as shared extensions loaded at runtime).\n\n"
102 
103         "-o path\n"
104         "  Output file path when compiling. If omitted, the compiled byte code\n"
105         "  is written to `./uc.out`. Only meaningful in conjunction with `-c`.\n\n"
106 
107         "-s\n"
108         "  Omit (strip) debug information when compiling files.\n"
109         "  Only meaningful in conjunction with `-c`.\n\n",
110                 app);
111 }
112 
113 
114 static int
115 compile(uc_vm_t *vm, uc_source_t *src, FILE *precompile, bool strip, char *interp, bool print_result)
116 {
117         uc_value_t *res = NULL;
118         uc_program_t *program;
119         int rc = 0;
120         char *err;
121 
122         program = uc_compile(vm->config, src, &err);
123 
124         if (!program) {
125                 fprintf(stderr, "%s", err);
126                 free(err);
127                 rc = -1;
128                 goto out;
129         }
130 
131         if (precompile) {
132                 if (interp)
133                         fprintf(precompile, "#!%s\n", interp);
134 
135                 uc_program_write(program, precompile, !strip);
136                 fclose(precompile);
137                 goto out;
138         }
139 
140         if (vm->gc_interval)
141                 uc_vm_gc_start(vm, vm->gc_interval);
142 
143         rc = uc_vm_execute(vm, program, &res);
144 
145         switch (rc) {
146         case STATUS_OK:
147                 if (print_result) {
148                         uc_vm_stack_push(vm, res);
149                         uc_vm_stack_push(vm, ucv_string_new("\n"));
150                         uc_stdlib_function("print")(vm, 2);
151                         uc_vm_stack_pop(vm);
152                         uc_vm_stack_pop(vm);
153                 }
154 
155                 rc = 0;
156                 break;
157 
158         case STATUS_EXIT:
159                 rc = (int)ucv_int64_get(res);
160                 break;
161 
162         case ERROR_COMPILE:
163                 rc = -1;
164                 break;
165 
166         case ERROR_RUNTIME:
167                 rc = -2;
168                 break;
169         }
170 
171 out:
172         uc_program_put(program);
173         ucv_put(res);
174 
175         return rc;
176 }
177 
178 static uc_source_t *
179 read_stdin(void)
180 {
181         size_t rlen = 0, tlen = 0;
182         char buf[128], *p = NULL;
183 
184         if (!stdin_unused) {
185                 fprintf(stderr, "The stdin can only be read once\n");
186                 errno = EINVAL;
187 
188                 return NULL;
189         }
190 
191         while (true) {
192                 rlen = fread(buf, 1, sizeof(buf), stdin_unused);
193 
194                 if (rlen == 0)
195                         break;
196 
197                 p = xrealloc(p, tlen + rlen);
198                 memcpy(p + tlen, buf, rlen);
199                 tlen += rlen;
200         }
201 
202         stdin_unused = NULL;
203 
204         return uc_source_new_buffer("[stdin]", p, tlen);
205 }
206 
207 static void
208 parse_template_modeflags(char *opt, uc_parse_config_t *config)
209 {
210         char *p;
211 
212         if (!opt)
213                 return;
214 
215         for (p = strtok(opt, ", "); p; p = strtok(NULL, ", ")) {
216                 if (!strcmp(p, "no-lstrip"))
217                         config->lstrip_blocks = false;
218                 else if (!strcmp(p, "no-rtrim"))
219                         config->trim_blocks = false;
220                 else
221                         fprintf(stderr, "Unrecognized -T flag \"%s\", ignoring\n", p);
222         }
223 }
224 
225 static void
226 parse_compile_flags(char *opt, char **interp, uc_search_path_t *dynlink_list)
227 {
228         char *p, *k, *v;
229 
230         if (!opt)
231                 return;
232 
233         for (p = strtok(opt, ","); p; p = strtok(NULL, ",")) {
234                 k = p;
235                 v = strchr(p, '=');
236 
237                 if (v)
238                         *v++ = 0;
239 
240                 if (!strcmp(k, "no-interp")) {
241                         if (v)
242                                 fprintf(stderr, "Compile flag \"%s\" takes no value, ignoring\n", k);
243 
244                         *interp = NULL;
245                 }
246                 else if (!strcmp(k, "interp")) {
247                         if (!v)
248                                 fprintf(stderr, "Compile flag \"%s\" requires a value, ignoring\n", k);
249                         else
250                                 *interp = v;
251                 }
252                 else if (!strcmp(k, "dynlink")) {
253                         if (!v)
254                                 fprintf(stderr, "Compile flag \"%s\" requires a value, ignoring\n", k);
255                         else
256                                 uc_vector_push(dynlink_list, v);
257                 }
258                 else {
259                         fprintf(stderr, "Unrecognized -c flag \"%s\", ignoring\n", k);
260                 }
261         }
262 }
263 
264 static bool
265 parse_define_file(char *opt, uc_value_t *globals)
266 {
267         enum json_tokener_error err = json_tokener_continue;
268         char buf[128], *name = NULL, *p;
269         struct json_tokener *tok;
270         json_object *jso = NULL;
271         size_t rlen;
272         FILE *fp;
273 
274         p = strchr(opt, '=');
275 
276         if (p) {
277                 name = opt;
278                 *p++ = 0;
279         }
280         else {
281                 p = opt;
282         }
283 
284         if (!strcmp(p, "-")) {
285                 if (!stdin_unused) {
286                         fprintf(stderr, "The stdin can only be read once\n");
287 
288                         return false;
289                 }
290 
291                 fp = stdin_unused;
292                 stdin_unused = NULL;
293         }
294         else
295                 fp = fopen(p, "r");
296 
297         if (!fp) {
298                 fprintf(stderr, "Unable to open definition file \"%s\": %s\n",
299                         p, strerror(errno));
300 
301                 return true;
302         }
303 
304         tok = xjs_new_tokener();
305 
306         while (true) {
307                 rlen = fread(buf, 1, sizeof(buf), fp);
308 
309                 if (rlen == 0)
310                         break;
311 
312                 jso = json_tokener_parse_ex(tok, buf, rlen);
313                 err = json_tokener_get_error(tok);
314 
315                 if (err != json_tokener_continue)
316                         break;
317         }
318 
319         json_tokener_free(tok);
320         fclose(fp);
321 
322         if (err != json_tokener_success || !json_object_is_type(jso, json_type_object)) {
323                 json_object_put(jso);
324 
325                 fprintf(stderr, "Invalid definition file \"%s\": %s\n",
326                         p, (err != json_tokener_success)
327                              ? "JSON parse failure" : "Not a valid JSON object");
328 
329                 return false;
330         }
331 
332         if (name && *name) {
333                 ucv_object_add(globals, name, ucv_from_json(NULL, jso));
334         }
335         else {
336                 json_object_object_foreach(jso, key, val)
337                         ucv_object_add(globals, key, ucv_from_json(NULL, val));
338         }
339 
340         json_object_put(jso);
341 
342         return true;
343 }
344 
345 static bool
346 parse_define_string(char *opt, uc_value_t *globals)
347 {
348         enum json_tokener_error err;
349         struct json_tokener *tok;
350         json_object *jso = NULL;
351         char *name = NULL, *p;
352         bool rv = false;
353         size_t len;
354 
355         p = strchr(opt, '=');
356 
357         if (p) {
358                 name = opt;
359                 *p++ = 0;
360         }
361         else {
362                 p = opt;
363         }
364 
365         len = strlen(p);
366         tok = xjs_new_tokener();
367 
368         /* NB: the len + 1 here is intentional to pass the terminating \0 byte
369          * to the json-c parser. This is required to work-around upstream
370          * issue #681 <https://github.com/json-c/json-c/issues/681> */
371         jso = json_tokener_parse_ex(tok, p, len + 1);
372 
373         err = json_tokener_get_error(tok);
374 
375         /* Treat trailing bytes after a parsed value as error */
376         if (err == json_tokener_success && json_tokener_get_parse_end(tok) < len)
377                 err = json_tokener_error_parse_unexpected;
378 
379         json_tokener_free(tok);
380 
381         if (err != json_tokener_success) {
382                 json_object_put(jso);
383 
384                 if (!name || !*name) {
385                         fprintf(stderr, "Invalid -D option value \"%s\": %s\n",
386                                 p, json_tokener_error_desc(err));
387 
388                         return false;
389                 }
390 
391                 ucv_object_add(globals, name, ucv_string_new(p));
392 
393                 return true;
394         }
395 
396         if (name && *name) {
397                 ucv_object_add(globals, name, ucv_from_json(NULL, jso));
398                 rv = true;
399         }
400         else if (json_object_is_type(jso, json_type_object)) {
401                 json_object_object_foreach(jso, key, val)
402                         ucv_object_add(globals, key, ucv_from_json(NULL, val));
403                 rv = true;
404         }
405         else {
406                 fprintf(stderr, "Invalid -D option value \"%s\": Not a valid JSON object\n", p);
407         }
408 
409         json_object_put(jso);
410 
411         return rv;
412 }
413 
414 static void
415 parse_search_path(char *pattern, uc_parse_config_t *config)
416 {
417         size_t len;
418         char *p;
419 
420         if (strchr(pattern, '*')) {
421                 uc_search_path_add(&config->module_search_path, pattern);
422                 return;
423         }
424 
425         len = strlen(pattern);
426 
427         if (!len)
428                 return;
429 
430         while (pattern[len-1] == '/')
431                 pattern[--len] = 0;
432 
433         xasprintf(&p, "%s/*.so", pattern);
434         uc_search_path_add(&config->module_search_path, p);
435         free(p);
436 
437         xasprintf(&p, "%s/*.uc", pattern);
438         uc_search_path_add(&config->module_search_path, p);
439         free(p);
440 }
441 
442 static bool
443 parse_library_load(char *opt, uc_vm_t *vm)
444 {
445         char *name = NULL, *p;
446         uc_value_t *lib, *ctx;
447 
448         p = strchr(opt, '=');
449 
450         if (p) {
451                 name = opt;
452                 *p++ = 0;
453         }
454         else {
455                 p = opt;
456         }
457 
458         lib = ucv_string_new(p);
459         ctx = uc_vm_invoke(vm, "require", 1, lib);
460         ucv_put(lib);
461 
462         if (!ctx)
463                 return vm->exception.type == EXCEPTION_NONE;
464 
465         ucv_object_add(uc_vm_scope_get(vm), name ? name : p, ctx);
466 
467         return true;
468 }
469 
470 static const char *
471 appname(const char *argv0)
472 {
473         const char *p;
474 
475         if (!argv0)
476                 return "ucode";
477 
478         p = strrchr(argv0, '/');
479 
480         if (p)
481                 return p + 1;
482 
483         return argv0;
484 }
485 
486 int
487 main(int argc, char **argv)
488 {
489         const char *optspec = "he:p:tg:ST::RD:F:U:l:L:c::o:s";
490         bool strip = false, print_result = false;
491         char *interp = "/usr/bin/env ucode";
492         uc_source_t *source = NULL;
493         FILE *precompile = NULL;
494         char *outfile = NULL;
495         uc_vm_t vm = { 0 };
496         int opt, rv = 0;
497         const char *app;
498         uc_value_t *o;
499         int fd;
500 
501         uc_parse_config_t config = {
502                 .strict_declarations = false,
503                 .lstrip_blocks = true,
504                 .trim_blocks = true,
505                 .raw_mode = true,
506                 .setup_signal_handlers = true
507         };
508 
509         app = appname(argv[0]);
510 
511         if (argc == 1) {
512                 print_usage(app);
513                 goto out;
514         }
515 
516         if (!strcmp(app, "utpl"))
517                 config.raw_mode = false;
518         else if (!strcmp(app, "ucc"))
519                 outfile = "./uc.out";
520 
521         stdin_unused = stdin;
522 
523         /* parse options iteration 1: parse config related options */
524         while ((opt = getopt(argc, argv, optspec)) != -1)
525         {
526                 switch (opt) {
527                 case 'L':
528                         parse_search_path(optarg, &config);
529                         break;
530 
531                 case 'S':
532                         config.strict_declarations = true;
533                         break;
534 
535                 case 'R':
536                         config.raw_mode = true;
537                         break;
538 
539                 case 'T':
540                         config.raw_mode = false;
541                         parse_template_modeflags(optarg, &config);
542                         break;
543                 }
544         }
545 
546         uc_search_path_init(&config.module_search_path);
547 
548         optind = 1;
549 
550         uc_vm_init(&vm, &config);
551 
552         /* load std functions into global scope */
553         uc_stdlib_load(uc_vm_scope_get(&vm));
554 
555         /* register ARGV array but populate it later (to allow for -U ARGV) */
556         o = ucv_array_new(&vm);
557 
558         ucv_object_add(uc_vm_scope_get(&vm), "ARGV", ucv_get(o));
559 
560         /* parse options iteration 2: process remaining options */
561         while ((opt = getopt(argc, argv, optspec)) != -1)
562         {
563                 switch (opt) {
564                 case 'h':
565                         print_usage(argv[0]);
566                         goto out;
567 
568                 case 'e':
569                         source = uc_source_new_buffer("[-e argument]", xstrdup(optarg), strlen(optarg));
570                         break;
571 
572                 case 'p':
573                         source = uc_source_new_buffer("[-p argument]", xstrdup(optarg), strlen(optarg));
574                         print_result = true;
575                         break;
576 
577                 case 't':
578                         uc_vm_trace_set(&vm, 1);
579                         break;
580 
581                 case 'g':
582                         vm.gc_interval = atoi(optarg);
583                         break;
584 
585                 case 'D':
586                         if (!parse_define_string(optarg, uc_vm_scope_get(&vm))) {
587                                 rv = 1;
588                                 goto out;
589                         }
590 
591                         break;
592 
593                 case 'F':
594                         if (!parse_define_file(optarg, uc_vm_scope_get(&vm))) {
595                                 rv = 1;
596                                 goto out;
597                         }
598 
599                         break;
600 
601                 case 'U':
602                         ucv_object_delete(uc_vm_scope_get(&vm), optarg);
603                         break;
604 
605                 case 'l':
606                         if (!parse_library_load(optarg, &vm)) {
607                                 rv = 1;
608                                 goto out;
609                         }
610 
611                         break;
612 
613                 case 'c':
614                         outfile = "./uc.out";
615                         parse_compile_flags(optarg, &interp, &config.force_dynlink_list);
616                         break;
617 
618                 case 's':
619                         strip = true;
620                         break;
621 
622                 case 'o':
623                         outfile = optarg;
624                         break;
625                 }
626         }
627 
628         if (!source && argv[optind] != NULL) {
629                 if (!strcmp(argv[optind], "-"))
630                         source = read_stdin();
631                 else
632                         source = uc_source_new_file(argv[optind]);
633 
634                 if (!source) {
635                         fprintf(stderr, "Failed to open \"%s\": %s\n", argv[optind], strerror(errno));
636                         rv = 1;
637                         goto out;
638                 }
639 
640                 optind++;
641         }
642 
643         if (!source) {
644                 fprintf(stderr, "Require either -e/-p expression or source file\n");
645                 rv = 1;
646                 goto out;
647         }
648 
649         if (outfile) {
650                 if (!strcmp(outfile, "-")) {
651                         precompile = stdout;
652                 }
653                 else {
654                         fd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0777);
655 
656                         if (fd == -1) {
657                                 fprintf(stderr, "Unable to open output file %s: %s\n",
658                                         outfile, strerror(errno));
659 
660                                 rv = 1;
661                                 goto out;
662                         }
663 
664                         precompile = fdopen(fd, "wb");
665                 }
666         }
667 
668         /* populate ARGV array */
669         for (; optind < argc; optind++)
670                 ucv_array_push(o, ucv_string_new(argv[optind]));
671 
672         ucv_put(o);
673 
674         rv = compile(&vm, source, precompile, strip, interp, print_result);
675 
676 out:
677         uc_search_path_free(&config.module_search_path);
678         uc_vector_clear(&config.force_dynlink_list);
679 
680         uc_source_put(source);
681 
682         uc_vm_free(&vm);
683 
684         return rv;
685 }
686 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt