static void op_backspace(qw_edit_t *e) { if (e->ac > 0) { op_left(e); op_del(e); } }
void db_op(struct mg_connection *nc, const struct http_message *hm, const struct mg_str *key, void *db, int op) { switch (op) { case API_OP_GET: op_get(nc, hm, key, db); break; case API_OP_SET: op_set(nc, hm, key, db); break; case API_OP_DEL: op_del(nc, hm, key, db); break; default: mg_printf(nc, "%s", "HTTP/1.0 501 Not Implemented\r\n" "Content-Length: 0\r\n\r\n"); break; } }
qw_op_t qw_edit_op(qw_edit_t *e, qw_event_t *ev) { qw_op_t r = QW_OP_NOP; qw_core_t *c = ev->c; switch (ev->o) { case QW_OP_LEFT: op_left(e); break; case QW_OP_RIGHT: op_right(e); break; case QW_OP_UP: op_up(e, ev->f); break; case QW_OP_DOWN: op_down(e, ev->f); break; case QW_OP_PGUP: op_pgup(e, ev->f); break; case QW_OP_PGDN: op_pgdn(e, ev->f); break; case QW_OP_BOL: op_bol(e); break; case QW_OP_EOL: op_eol(e); break; case QW_OP_BOR: op_bor(e, ev->f); break; case QW_OP_EOR: op_eor(e, ev->f); break; case QW_OP_BOF: op_bof(e); break; case QW_OP_EOF: op_eof(e); break; case QW_OP_CHAR: op_char(e, ev->w); break; case QW_OP_DEL: op_del(e); break; case QW_OP_BACKSPACE: op_backspace(e); break; case QW_OP_NEWLINE: op_char(e, L'\n'); break; case QW_OP_TAB: op_tab(e, ev->f, c); break; case QW_OP_HARD_TAB: op_char(e, L'\t'); break; case QW_OP_DEL_ROW: op_del_row(e, ev->f); break; case QW_OP_UNDO: op_undo(e); break; case QW_OP_MARK: op_mark(e); break; case QW_OP_UNMARK: op_unmark(e); break; case QW_OP_COPY: op_copy(e, c); op_unmark(e); break; case QW_OP_PASTE: op_paste(e, c); break; case QW_OP_CUT: op_cut(e, c); break; case QW_OP_SAVE: op_save(e); break; case QW_OP_SHOW_CODES: ev->f->l = L" \xb6"[!!(ev->f->l == L' ')]; break; case QW_OP_SEARCH: op_search(e, c); break; case QW_OP_CLOSE: r = QW_OP_DESTROY; break; case QW_OP_DESTROY: r = QW_OP_DESTROY; break; case QW_OP_NOP: case QW_OP_COUNT: break; } return r; }
int parse_json(uint16_t json_sz) { int ret = SUCCESS; char *cursor = json; char *op = NULL; char *line_start = NULL, *line_end = NULL, *delim = NULL; uint16_t remaining = json_sz; uint16_t line_remaining = 0; uint16_t consumed = 0; // There's nothing to do; just return. if (0 == json_sz) { #ifdef DEBUG fprintf(stderr, "[D] parse_json | json_sz = 0x%04x; nothing to do\n", json_sz); #endif goto bail; } // For each INSN (1 per line)... while (NULL != (line_start = strnchr(cursor, '\n', (uint32_t)remaining))) { #ifdef DEBUG fprintf(stderr, "[D] parse_json | top of loop; line_start = '%s'\n", line_start); #endif // Consume the newline. line_start += 1; remaining -= 1; // If there's nothing left, we've parse it all. if (0 == remaining) { #ifdef DEBUG fprintf(stderr, "[D] parse_json | consumed all JSON\n", remaining); #endif goto bail; } // Find next newline (so we have bounds to this line). if (NULL == (line_end = strnchr(line_start, '\n', (uint32_t)remaining))) { #ifdef DEBUG fprintf(stderr, "[E] parse_json | could not find next line ending; line_start is: '%s'\n", line_start); #endif ret = ERRNO_MALFORMED_JSON; goto bail; } line_remaining = line_end - line_start; cursor = line_start; #ifdef DEBUG fprintf(stderr, "[D] parse_json | cursor = line_start = '%s'\n", cursor); #endif // Decrement from global remaining now, since we'll be consuming this line. remaining -= line_remaining; //// // Get OP //// if (NULL == (delim = strnchr(cursor, ' ', (uint32_t)(line_remaining)))) { #ifdef DEBUG fprintf(stderr, "[E] parse_json | cannot find OP delim\n"); #endif ret = ERRNO_MALFORMED_JSON; goto bail; } consumed = delim - cursor; line_remaining -= consumed; if (SZ_INSN_OP != consumed) { #ifdef DEBUG fprintf(stderr, "[E] parse_json | invalid OP length: %d\n", consumed); #endif ret = ERRNO_MALFORMED_JSON; goto bail; } //// // Dispatch OP //// op = cursor; cursor += SZ_INSN_OP+1; if (0 == memcmp("NEW", op, SZ_INSN_OP)) { if (SUCCESS != (ret = op_new(&cursor, &line_remaining))) { #ifdef DEBUG fprintf(stderr, "[E] parse_json | NEW | non-SUCCESS from op_new()\n"); #endif goto bail; } } else if (0 == memcmp("SET", op, SZ_INSN_OP)) { if (SUCCESS != (ret = op_set(&cursor, &line_remaining))) { #ifdef DEBUG fprintf(stderr, "[E] parse_json | NEW | non-SUCCESS from op_set()\n"); #endif goto bail; } } else if (0 == memcmp("DEL", op, SZ_INSN_OP)) { if (SUCCESS != (ret = op_del(&cursor, &line_remaining))) { #ifdef DEBUG fprintf(stderr, "[E] parse_json | NEW | non-SUCCESS from op_del()\n"); #endif goto bail; } } else { #ifdef DEBUG fprintf(stderr, "[E] parse_json | unknown OP: '%.3s'\n", cursor); #endif ret = ERRNO_MALFORMED_JSON; goto bail; } } bail: return ret; }