diff options
| author | Root THC | 2026-02-24 12:42:47 +0000 |
|---|---|---|
| committer | Root THC | 2026-02-24 12:42:47 +0000 |
| commit | c9cbeced5b3f2bdd7407e29c0811e65954132540 (patch) | |
| tree | aefc355416b561111819de159ccbd86c3004cf88 /other/gramble/input-driver.c | |
| parent | 073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff) | |
initial
Diffstat (limited to 'other/gramble/input-driver.c')
| -rw-r--r-- | other/gramble/input-driver.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/other/gramble/input-driver.c b/other/gramble/input-driver.c new file mode 100644 index 0000000..e68bdf3 --- /dev/null +++ b/other/gramble/input-driver.c | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | /* gramble - grammar ramble | ||
| 2 | * | ||
| 3 | * team teso | ||
| 4 | * | ||
| 5 | * main grammar parser driver functions | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <setjmp.h> | ||
| 9 | #include <stdio.h> | ||
| 10 | #include <stdlib.h> | ||
| 11 | #include <string.h> | ||
| 12 | #include "input.h" | ||
| 13 | |||
| 14 | |||
| 15 | extern int yydebug; | ||
| 16 | extern FILE * yyout; | ||
| 17 | extern int yyparse (void); | ||
| 18 | |||
| 19 | |||
| 20 | void * in_grammar = NULL; /* parser return */ | ||
| 21 | |||
| 22 | char * in_input_ptr; | ||
| 23 | int in_input_lim; | ||
| 24 | |||
| 25 | jmp_buf in_yy_error_jmp; /* to catch errors in parser */ | ||
| 26 | |||
| 27 | |||
| 28 | /* overridden yacc functions | ||
| 29 | */ | ||
| 30 | |||
| 31 | int | ||
| 32 | yyerror (char *str) | ||
| 33 | { | ||
| 34 | longjmp (in_yy_error_jmp, 1); | ||
| 35 | |||
| 36 | exit (EXIT_FAILURE); | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | int | ||
| 41 | yywrap (void) | ||
| 42 | { | ||
| 43 | return (1); | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | /*** driver interface | ||
| 48 | */ | ||
| 49 | |||
| 50 | /* in_parse | ||
| 51 | * | ||
| 52 | * parse a grammar from buffer pointed to by 'buf', which is 'buf_len' bytes | ||
| 53 | * long. | ||
| 54 | * | ||
| 55 | * return NULL on failure | ||
| 56 | * return pointer to grammar on success | ||
| 57 | */ | ||
| 58 | |||
| 59 | void * /* FIXME: real pointer */ | ||
| 60 | in_parse (char *buf, int buf_len) | ||
| 61 | { | ||
| 62 | in_grammar = NULL; | ||
| 63 | |||
| 64 | |||
| 65 | in_input_ptr = buf; | ||
| 66 | in_input_lim = buf_len; | ||
| 67 | |||
| 68 | if (setjmp (in_yy_error_jmp) != 0) { | ||
| 69 | if (in_grammar != NULL) | ||
| 70 | return (NULL); | ||
| 71 | |||
| 72 | /* FIXME: free all associated memory */ | ||
| 73 | /* freebla (in_grammar) */ | ||
| 74 | return (NULL); | ||
| 75 | } | ||
| 76 | |||
| 77 | yyparse (); | ||
| 78 | |||
| 79 | return (in_grammar); | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | /* in_yyinput | ||
| 84 | * | ||
| 85 | * override yyinput functionality to allow more flexible parsing. internally | ||
| 86 | * used, never called by outsiders. | ||
| 87 | * | ||
| 88 | * return number of bytes read | ||
| 89 | */ | ||
| 90 | |||
| 91 | int | ||
| 92 | in_yyinput (char *buf, int size_max) | ||
| 93 | { | ||
| 94 | int n = 0; | ||
| 95 | |||
| 96 | n = (size_max >= in_input_lim) ? in_input_lim : size_max; | ||
| 97 | if (n > 0) { | ||
| 98 | memmove (buf, in_input_ptr, n); | ||
| 99 | in_input_ptr += n; | ||
| 100 | in_input_lim -= n; | ||
| 101 | } | ||
| 102 | |||
| 103 | return (n); | ||
| 104 | } | ||
| 105 | |||
