aboutsummaryrefslogtreecommitdiffstats
path: root/common/action_util.c
blob: dbee630d18175127e4d3aa333245ef2875d74dc9 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Copyright 2013 Jun Wako <wakojun@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "host.h"
#include "report.h"
#include "debug.h"
#include "action_util.h"
#include "timer.h"

static inline void add_key_byte(uint8_t code);
static inline void del_key_byte(uint8_t code);
#ifdef NKRO_ENABLE
static inline void add_key_bit(uint8_t code);
static inline void del_key_bit(uint8_t code);
#endif

static uint8_t real_mods = 0;
static uint8_t weak_mods = 0;

#ifdef USB_6KRO_ENABLE
#define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
#define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
#define RO_INC(a) RO_ADD(a, 1)
#define RO_DEC(a) RO_SUB(a, 1)
static int8_t cb_head = 0;
static int8_t cb_tail = 0;
static int8_t cb_count = 0;
#endif

// TODO: pointer variable is not needed
//report_keyboard_t keyboard_report = {};
report_keyboard_t *keyboard_report = &(report_keyboard_t){};

#ifndef NO_ACTION_ONESHOT
static int8_t oneshot_mods = 0;
#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
static int16_t oneshot_time = 0;
#endif
#endif


void send_keyboard_report(void) {
    keyboard_report->mods  = real_mods;
    keyboard_report->mods |= weak_mods;
#ifndef NO_ACTION_ONESHOT
    if (oneshot_mods) {
#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
        if (TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT) {
            dprintf("Oneshot: timeout\n");
            clear_oneshot_mods();
        }
#endif
        keyboard_report->mods |= oneshot_mods;
        if (has_anykey()) {
            clear_oneshot_mods();
        }
    }
#endif
    host_keyboard_send(keyboard_report);
}

/* key */
void add_key(uint8_t key)
{
#ifdef NKRO_ENABLE
    if (keyboard_nkro) {
        add_key_bit(key);
        return;
    }
#endif
    add_key_byte(key);
}

void del_key(uint8_t key)
{
#ifdef NKRO_ENABLE
    if (keyboard_nkro) {
        del_key_bit(key);
        return;
    }
#endif
    del_key_byte(key);
}

void clear_keys(void)
{
    // not clear mods
    for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
        keyboard_report->raw[i] = 0;
    }
}


/* modifier */
uint8_t get_mods(void) { return real_mods; }
void add_mods(uint8_t mods) { real_mods |= mods; }
void del_mods(uint8_t mods) { real_mods &= ~mods; }
void set_mods(uint8_t mods) { real_mods = mods; }
void clear_mods(void) { real_mods = 0; }

/* weak modifier */
uint8_t get_weak_mods(void) { return weak_mods; }
void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
void set_weak_mods(uint8_t mods) { weak_mods = mods; }
void clear_weak_mods(void) { weak_mods = 0; }

/* Oneshot modifier */
#ifndef NO_ACTION_ONESHOT
void set_oneshot_mods(uint8_t mods)
{
    oneshot_mods = mods;
#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
    oneshot_time = timer_read();
#endif
}
void clear_oneshot_mods(void)
{
    oneshot_mods = 0;
#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
    oneshot_time = 0;
#endif
}
#endif




/*
 * inspect keyboard state
 */
uint8_t has_anykey(void)
{
    uint8_t cnt = 0;
    for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
        if (keyboard_report->raw[i])
            cnt++;
    }
    return cnt;
}

uint8_t has_anymod(void)
{
    return bitpop(real_mods);
}

uint8_t get_first_key(void)
{
#ifdef NKRO_ENABLE
    if (keyboard_nkro) {
        uint8_t i = 0;
        for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
            ;
        return i<<3 | biton(keyboard_report->nkro.bits[i]);
    }
#endif
#ifdef USB_6KRO_ENABLE
    uint8_t i = cb_head;
    do {
        if (keyboard_report->keys[i] != 0) {
            break;
        }
        i = RO_INC(i);
    } while (i != cb_tail);
    return keyboard_report->keys[i];
#else
    return keyboard_report->keys[0];
#endif
}



/* local functions */
static inline void add_key_byte(uint8_t code)
{
#ifdef USB_6KRO_ENABLE
    int8_t i = cb_head;
    int8_t empty = -1;
    if (cb_count) {
        do {
            if (keyboard_report->keys[i] == code) {
                return;
            }
            if (empty == -1 && keyboard_report->keys[i] == 0) {
                empty = i;
            }
            i = RO_INC(i);
        } while (i != cb_tail);
        if (i == cb_tail) {
            if (cb_tail == cb_head) {
                // buffer is full
                if (empty == -1) {
                    // pop head when has no empty space
                    cb_head = RO_INC(cb_head);
                    cb_count--;
                }
                else {
                    // left shift when has empty space
                    uint8_t offset = 1;
                    i = RO_INC(empty);
                    do {
                        if (keyboard_report->keys[i] != 0) {
                            keyboard_report->keys[empty] = keyboard_report->keys[i];
                            keyboard_report->keys[i] = 0;
                            empty = RO_INC(empty);
                        }
                        else {
                            offset++;
                        }
                        i = RO_INC(i);
                    } while (i != cb_tail);
                    cb_tail = RO_SUB(cb_tail, offset);
                }
            }
        }
    }
    // add to tail
    keyboard_report->keys[cb_tail] = code;
    cb_tail = RO_INC(cb_tail);
    cb_count++;
#else
    int8_t i = 0;
    int8_t empty = -1;
    for (; i < KEYBOARD_REPORT_KEYS; i++) {
        if (keyboard_report->keys[i] == code) {
            break;
        }
        if (empty == -1 && keyboard_report->keys[i] == 0) {
            empty = i;
        }
    }
    if (i == KEYBOARD_REPORT_KEYS) {
        if (empty != -1) {
            keyboard_report->keys[empty] = code;
        }
    }
#endif
}

static inline void del_key_byte(uint8_t code)
{
#ifdef USB_6KRO_ENABLE
    uint8_t i = cb_head;
    if (cb_count) {
        do {
            if (keyboard_report->keys[i] == code) {
                keyboard_report->keys[i] = 0;
                cb_count--;
                if (cb_count == 0) {
                    // reset head and tail
                    cb_tail = cb_head = 0;
                }
                if (i == RO_DEC(cb_tail)) {
                    // left shift when next to tail
                    do {
                        cb_tail = RO_DEC(cb_tail);
                        if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
                            break;
                        }
                    } while (cb_tail != cb_head);
                }
                break;
            }
            i = RO_INC(i);
        } while (i != cb_tail);
    }
#else
    for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
        if (keyboard_report->keys[i] == code) {
            keyboard_report->keys[i] = 0;
        }
    }
#endif
}

#ifdef NKRO_ENABLE
static inline void add_key_bit(uint8_t code)
{
    if ((code>>3) < KEYBOARD_REPORT_BITS) {
        keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
    } else {
        dprintf("add_key_bit: can't add: %02X\n", code);
    }
}

static inline void del_key_bit(uint8_t code)
{
    if ((code>>3) < KEYBOARD_REPORT_BITS) {
        keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
    } else {
        dprintf("del_key_bit: can't del: %02X\n", code);
    }
}
#endif
"p">(Parser *in){ return in->tok_begin_line; } /** Get the column number the current token started on. * * @param in parser */ static int get_tok_column(Parser *in){ return in->tok_begin_char; } /** Return the current token. * The return value points at the internal buffer, so * it must not be modified (or freed). Use copy_token() if you need a copy. * * @param p parser * @return token */ char *peek_token(Parser *p){ return p->tok; } int token_len(Parser *p){ return p->tok_end - p->tok; } /** Return a copy of the current token. * The returned value should be freed when finished with. * * @param p parser * @return copy of token */ char *copy_token(Parser *p){ int n = token_len(p); char *buf = allocate(n + 1); if(buf){ memcpy(buf, peek_token(p), n); buf[n] = '\0'; } return buf; } void new_token(Parser *p){ memset(p->buf, 0, p->buf_end - p->buf); p->tok = p->buf; p->tok_end = p->tok; p->tok_begin_line = p->line_no; p->tok_begin_char = p->char_no; } /** Report a parse error. * Does nothing if the error stream is null or there is no error. * * @param in parser */ static void report_error(Parser *in){ if(in->error_out && in->err){ char *msg = get_message(in->err); char *tok = peek_token(in); IOStream_print(in->error_out, PARSE_ERR_FMT, get_tok_line(in), get_tok_column(in), msg); if(tok && tok[0]){ IOStream_print(in->error_out, " '%s'", tok); } IOStream_print(in->error_out, "\n"); } } /** Get the error message for the current parse error code. * Does nothing if there is no error. * * @param in parser * @param buf where to place the message * @param n maximum number of characters to place in buf * @return current error code (zero for no error) */ int Parser_error_message(Parser *in, char *buf, int n){ if(in->err){ char *msg = get_message(in->err); snprintf(buf, n, PARSE_ERR_FMT, get_tok_line(in), get_tok_column(in), msg); } return in->err; } /** Flag a parse error. All subsequent reads will fail. * Does not change the parser error code if it is already set. * * @param in parser * @param id error code */ int Parser_error_id(Parser *in, ParseErrorId id){ if(!in->err){ in->err = id; report_error(in); } return -EINVAL; } /** Flag an unspecified parse error. * * @param in parser */ int Parser_error(Parser *in){ return Parser_error_id(in, PARSE_ERR_INVALID_SYNTAX); } /** Test if the parser's error flag is set. * * @param in parser * @return 1 if set, 0 otherwise */ int Parser_has_error(Parser *in){ return (in->err > 0); } /** Test if the parser is at end of input. * * @param in parser * @return 1 if at EOF, 0 otherwise */ int Parser_at_eof(Parser *p){ return p->eof; } void ParserState_free(ParserState *z){ if(!z) return; objfree(z->val); deallocate(z); } int ParserState_new(ParserStateFn *fn, char *name, ParserState *parent, ParserState **val){ int err = -ENOMEM; ParserState *z; z = ALLOCATE(ParserState); if(!z) goto exit; z->name = name; z->fn = fn; z->parent = parent; z->val = ONULL; err = 0; exit: *val = (err ? NULL : z); return err; } void Parser_pop(Parser *p){ ParserState *s = p->state; if(!s) return; dprintf("Parser_pop> %s\n", s->name); p->state = s->parent; if (p->start_state == s) { p->start_state = NULL; } ParserState_free(s); } /** Free a parser. * No-op if the parser is null. * * @param z parser */ void Parser_free(Parser *z){ if(!z) return; // Hmmm. Need to free states, but careful about double free of values. while(z->state){ objfree(z->state->val); Parser_pop(z); } if(z->buf) deallocate(z->buf); objfree(z->val); z->val = ONONE; deallocate(z); } int Parser_push(Parser *p, ParserStateFn *fn, char *name){ dprintf("Parser_push> %s\n", name); return ParserState_new(fn, name, p->state, &p->state); } int Parser_return(Parser *p){ int err = 0; Sxpr val = ONONE; if(!p->state){ err = -EINVAL; goto exit; } val = p->state->val; p->state->val = ONONE; Parser_pop(p); if(p->state){ err = cons_push(&p->state->val, val); } else { val = nrev(val); p->val = val; } exit: if(err){ objfree(val); } return err; } /** Reset the fields of a parser to initial values. * * @param z parser */ static void reset(Parser *z){ // leave flags // leave error_out while(z->state){ Parser_pop(z); } z->val = ONONE; z->eof = 0; z->err = 0; z->line_no = 1; z->char_no = 0; memset(z->buf, 0, z->buf_end - z->buf); z->tok = z->buf; z->tok_end = z->tok; z->tok_begin_line = 0; z->tok_begin_char = 0; z->start_state = NULL; } /** Create a new parser. The error stream defaults to null. */ Parser * Parser_new(void){ Parser *z = ALLOCATE(Parser); int n = PARSER_BUF_SIZE; int err = -ENOMEM; if(!z) goto exit; z->buf = allocate(n); if(!z->buf) goto exit; err = 0; z->buf_end = z->buf + n; z->begin = begin_start; reset(z); exit: if(err){ Parser_free(z); z = NULL; } return z; } /** Get the next character. * Records the character read in the parser, * and sets the line and character counts. * * @param p parser * @return error flag: 0 on success, non-zero on error */ static int input_char(Parser *p, char c){ int err = 0; if(c=='\n'){ p->line_no++; p->char_no = 0; } else { p->char_no++; } return err; } int save_char(Parser *p, char c){ int err = 0; if(p->tok_end >= p->buf_end){ int buf_n = (p->buf_end - p->buf) + PARSER_BUF_INCREMENT; char *buf = allocate(buf_n); if(!buf){ err = -ENOMEM; goto exit; } memcpy(buf, p->buf, p->tok_end - p->buf); p->buf_end = buf + buf_n; p->tok = buf + (p->tok - p->buf); p->tok_end = buf + (p->tok_end - p->buf); deallocate(p->buf); p->buf = buf; } *p->tok_end++ = c; exit: return err; } /** Determine if a character is a separator. * * @param p parser * @param c character to test * @return 1 if a separator, 0 otherwise */ static int is_separator(Parser *p, char c){ return in_sep_class(c); } int Parser_set_value(Parser *p, Sxpr obj){ int err = 0; if(NOMEMP(obj)){ err = -ENOMEM; } else { p->state->val = obj; } return err; } int Parser_intern(Parser *p){ Sxpr obj = intern(peek_token(p)); return Parser_set_value(p, obj); } int Parser_atom(Parser *p){ Sxpr obj; long v; if(Parser_flags(p, PARSE_INT) && convert_atol(peek_token(p), &v) == 0){ obj = OINT(v); } else { obj = atom_new(peek_token(p)); } return Parser_set_value(p, obj); } int Parser_string(Parser *p){ Sxpr obj = string_new_n(peek_token(p), token_len(p)); return Parser_set_value(p, obj); } int Parser_data(Parser *p){ Sxpr obj = string_new_n(peek_token(p), token_len(p)); return Parser_set_value(p, obj); } int Parser_uint(Parser *p){ unsigned int x = htonl(*(unsigned int *)peek_token(p)); return Parser_set_value(p, OINT(x)); } static int get_escape(char c, char *d){ int err = 0; switch(c){ case 'a': *d = '\a'; break; case 'b': *d = '\b'; break; case 'f': *d = '\f'; break; case 'n': *d = '\n'; break; case 'r': *d = '\r'; break; case 't': *d = '\t'; break; case 'v': *d = '\v'; break; case c_escape: *d = c_escape; break; case c_single_quote: *d = c_single_quote; break; case c_double_quote: *d = c_double_quote; break; default: err = -EINVAL; } return err; } int Parser_ready(Parser *p){ return CONSP(p->val) || (p->start_state && CONSP(p->start_state->val)); } Sxpr Parser_get_val(Parser *p){ Sxpr v = ONONE, w = ONONE; if(CONSP(p->val)){ } else if (p->start_state && CONSP(p->start_state->val)){ p->val = p->start_state->val; p->val = nrev(p->val); p->start_state->val = ONULL; } else { goto exit; } w = p->val; v = CAR(w); p->val = CDR(w); hfree(w); exit: return v; } Sxpr Parser_get_all(Parser *p){ Sxpr v = ONULL; if(CONSP(p->val)){ v = p->val; p->val = ONONE; } else if(p->start_state && CONSP(p->start_state->val)){ v = p->start_state->val; p->start_state->val = ONULL; v = nrev(v); } return v; } static int state_comment(Parser *p, char c){ int err = 0; if(c == '\n' || Parser_at_eof(p)){ Parser_pop(p); } else { err = input_char(p, c); } return err; } static int begin_comment(Parser *p, char c){ int err = 0; err = Parser_push(p, state_comment, "comment"); if(err) goto exit; err = input_char(p, c); exit: return err; } static int end_string(Parser *p){ int err = 0; err = Parser_string(p); if(err) goto exit; err = Parser_return(p); exit: return err; } static int octaldone(Parser *p){ int err = 0; char d = (char)(p->state->ival & 0xff); Parser_pop(p); err = Parser_input_char(p, d); return err; } static int octaldigit(Parser *p, int d){ int err = 0; p->state->ival *= 8; p->state->ival += d; p->state->count++; if(err) goto exit; if(p->state->ival < 0 || p->state->ival > 0xff){ err = Parser_error(p); goto exit; } if(p->state->count == 3){ err = octaldone(p); } exit: return err; } static int state_octal(Parser *p, char c){ int err = 0; if(Parser_at_eof(p)){ err = Parser_error_id(p, PARSE_ERR_UNEXPECTED_EOF); goto exit; } else if('0' <= c && c <= '7'){ err = octaldigit(p, c - '0'); } else { err = octaldone(p); if(err) goto exit; Parser_input_char(p, c); } exit: return err; } static int hexdone(Parser *p){ int err = 0; char d = (char)(p->state->ival & 0xff); Parser_pop(p); err = Parser_input_char(p, d); return err; } static int hexdigit(Parser *p, int d){ int err = 0; p->state->ival *= 16; p->state->ival += d; p->state->count++; if(err) goto exit; if(p->state->ival < 0 || p->state->ival > 0xff){ err = Parser_error(p); goto exit; } if(p->state->count == 2){ err = hexdone(p); } exit: return err; } static int state_hex(Parser *p, char c){ int err = 0; if(Parser_at_eof(p)){ err = Parser_error_id(p, PARSE_ERR_UNEXPECTED_EOF); goto exit; } else if('0' <= c && c <= '9'){ err = hexdigit(p, c - '0'); } else if('A' <= c && c <= 'F'){ err = hexdigit(p, c - 'A' + 10); } else if('a' <= c && c <= 'f'){ err = hexdigit(p, c - 'a' + 10); } else if(p->state->count){ err = hexdone(p); if(err) goto exit; Parser_input_char(p, c); } exit: return err; } static int state_escape(Parser *p, char c){ int err = 0; char d; if(Parser_at_eof(p)){ err = Parser_error_id(p, PARSE_ERR_UNEXPECTED_EOF); goto exit; } if(get_escape(c, &d) == 0){ err = save_char(p, d); if(err) goto exit; Parser_pop(p); } else if(c == 'x'){ p->state->fn = state_hex; p->state->ival = 0; p->state->count = 0; } else { p->state->fn = state_octal; p->state->ival = 0; p->state->count = 0; err = Parser_input_char(p, c); } exit: return err; } static int state_string(Parser *p, char c){ int err = 0; if(Parser_at_eof(p)){ err = Parser_error_id(p, PARSE_ERR_UNEXPECTED_EOF); } else if(c == p->state->delim){ err = end_string(p); } else if(c == '\\'){ err = Parser_push(p, state_escape, "escape"); } else { err = save_char(p, c); } return err; } static int begin_string(Parser *p, char c){ int err = 0; err = Parser_push(p, state_string, "string"); if(err) goto exit; new_token(p); p->state->delim = c; exit: return err; } static int end_atom(Parser *p){ int err = 0; err = Parser_atom(p); if(err) goto exit; err = Parser_return(p); exit: return err; } static int state_atom(Parser *p, char c){ int err = 0; if(Parser_at_eof(p)){ err = end_atom(p); } else if(is_separator(p, c) || in_space_class(c) || in_comment_class(c)){ err = end_atom(p); if(err) goto exit; err = Parser_input_char(p, c); } else { err = save_char(p, c); } exit: return err; } static int begin_atom(Parser *p, char c){ int err = 0; err = Parser_push(p, state_atom, "atom"); if(err) goto exit; new_token(p); err = save_char(p, c); exit: return err; } static int end_data(Parser *p){ int err = 0; err = Parser_data(p); if(err) goto exit; err = Parser_return(p); exit: return err; } static int counted_data(Parser *p, char c){ int err = 0; err = save_char(p, c); if(err) goto exit; if(token_len(p) == p->state->count){ err = end_data(p); } exit: return err; } static int counted_data_count(Parser *p, char c){ int err = 0; if(c == p->state->delim){ new_token(p); p->state->count = p->state->ival; p->state->fn = counted_data; } else if('0' <= c && c <= '9'){ p->state->ival *= 10; p->state->ival += c - '0'; } else { err = -EINVAL; } return err; } static int quoted_data(Parser *p, char c){ int err = 0; int count = p->state->count; err = save_char(p, c); if(err) goto exit; // Check that buf is longer than delim and // ends with delim. If so, trim delim off and return. if((token_len(p) >= count) && !memcmp(p->tok_end - count, p->buf, count)){ p->tok_end -= count; end_data(p); } exit: return err; } static int quoted_data_delim(Parser *p, char c){ // Saves the delim in the token buffer. int err = 0; err = save_char(p, c); if(err) goto exit; if(c == p->state->delim){ p->state->fn = quoted_data; p->state->count = token_len(p); // Advance the token pointer past the delim. p->tok = p->tok_end; } exit: return err; } static int state_data(Parser *p, char c){ // Quoted data: // <<delim< anything not containing delimiter<delim< // Where 'delim' is anything not containing '<'. // Counted data: // <*nnn..* N bytes // Where nnn... is N in decimal ( int err = 0; switch(c){ case c_data_count: p->state->delim = c; p->state->fn = counted_data_count; p->state->ival = 0; new_token(p); break; case c_data_quote: p->state->delim = c; p->state->fn = quoted_data_delim; new_token(p); err = save_char(p, c); break; default: err = Parser_error(p); break; } return err; } static int begin_data(Parser *p, char c){ int err = 0; err = Parser_push(p, state_data, "data"); if(err) goto exit; new_token(p); exit: return err; } static int state_list(Parser *p, char c){ int err = 0; dprintf(">\n"); if(Parser_at_eof(p)){ err = Parser_error_id(p, PARSE_ERR_UNEXPECTED_EOF); } else if(c == c_list_close){ p->state->val = nrev(p->state->val); err = Parser_return(p); } else { err = state_start(p, c); } dprintf("< err=%d\n", err); return err; } static int begin_list(Parser *p, char c){ return Parser_push(p, state_list, "list"); } static int state_start(Parser *p, char c){ int err = 0; dprintf(">\n"); if(Parser_at_eof(p)){ err = Parser_return(p); } else if(in_space_class(c)){ //skip } else if(in_comment_class(c)){ begin_comment(p, c); } else if(c == c_list_open){ begin_list(p, c); } else if(c == c_list_close){ err = Parser_error(p); } else if(in_string_quote_class(c)){ begin_string(p, c); } else if(c == c_data_open){ begin_data(p, c); } else if(in_printable_class(c)){ begin_atom(p, c); } else if(c == 0x04){ //ctrl-D, EOT: end-of-text. Parser_input_eof(p); } else { err = Parser_error(p); } dprintf("< err=%d\n", err); return err; } int begin_start(Parser *p, char c){ int err = 0; dprintf(">\n"); err = Parser_push(p, state_start, "start"); if(err) goto exit; p->start_state = p->state; exit: dprintf("< err=%d\n", err); return err; } int Parser_input_char(Parser *p, char c){ int err = 0; if(Parser_at_eof(p)){ //skip; } else { input_char(p, c); } if(!p->state){ err = p->begin(p, c); if(err) goto exit; } err = p->state->fn(p, c); exit: return err; } int Parser_input_eof(Parser *p){ int err = 0; p->eof = 1; err = Parser_input_char(p, IOSTREAM_EOF); return err; } int Parser_input(Parser *p, char *buf, int buf_n){ int err = 0; int i = 0; dprintf("> buf_n=%d\n", buf_n); if(buf_n <= 0){ buf_n = 0; err = Parser_input_eof(p); goto exit; } dprintf("> buf=|%*s|\n", buf_n, buf); for(i = 0; i < buf_n; i++){ err = Parser_input_char(p, buf[i]); if(err) goto exit; } exit: err = (err < 0 ? err : buf_n); dprintf("< err=%d\n", err); return err; } #ifdef SXPR_PARSER_MAIN /* Stuff for standalone testing. */ #include "file_stream.h" //#include "string_stream.h" /** Main program for testing. * Parses input and prints it. * * @param argc number of arguments * @param argv arguments * @return error code */ int main(int argc, char *argv[]){ Parser *pin; int err = 0; char buf[1024]; int k; Sxpr obj; int i = 0; pin = Parser_new(); Parser_set_error_stream(pin, iostdout); dprintf("> parse...\n"); while(1){ k = fread(buf, 1, 100, stdin); if(k>=0){ buf[k+1] = '\0'; } err = Parser_input(pin, buf, k); while(Parser_ready(pin)){ obj = Parser_get_val(pin); printf("obj %d\n", i++); objprint(iostdout, obj, 0); printf("\n"); } if(k <= 0) break; } dprintf("> err=%d\n", err); return 0; } #endif