From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MzYaV-0007bT-G0 for qemu-devel@nongnu.org; Sun, 18 Oct 2009 12:26:31 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MzYaQ-0007af-Gi for qemu-devel@nongnu.org; Sun, 18 Oct 2009 12:26:30 -0400 Received: from [199.232.76.173] (port=35120 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MzYaQ-0007ac-4U for qemu-devel@nongnu.org; Sun, 18 Oct 2009 12:26:26 -0400 Received: from mail-yw0-f176.google.com ([209.85.211.176]:38377) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MzYaP-0001Ju-OF for qemu-devel@nongnu.org; Sun, 18 Oct 2009 12:26:25 -0400 Received: by ywh6 with SMTP id 6so3054792ywh.4 for ; Sun, 18 Oct 2009 09:26:24 -0700 (PDT) Message-ID: <4ADB41AE.8080103@codemonkey.ws> Date: Sun, 18 Oct 2009 11:26:22 -0500 From: Anthony Liguori MIME-Version: 1.0 Subject: Re: [Qemu-devel] Re: [PATCH 01/10] Introduce qmisc module References: <1255037747-3340-1-git-send-email-lcapitulino@redhat.com> <1255037747-3340-2-git-send-email-lcapitulino@redhat.com> <4AD72B88.2040107@codemonkey.ws> <20091015122622.1f93ea2d@doriath> <20091015163936.GB532@redhat.com> <20091015142837.6c90580a@doriath> <4AD76B3C.3050001@codemonkey.ws> <4AD87424.3010000@redhat.com> <4AD87901.5030705@codemonkey.ws> <4AD8AECE.9000507@redhat.com> <4AD8AFA4.4070203@codemonkey.ws> <4AD8CB31.9080809@redhat.com> <4AD8E7B5.8000509@codemonkey.ws> <4AD910BA.4090607@gnu.org> <4AD922EB.5030501@codemonkey.ws> <4AD995FD.6070202@snarc.org> <20091018120631.0ab44d80@doriath> <4ADB2172.2040501@gnu.org> <4ADB2B13.4090207@codemonkey.ws> In-Reply-To: <4ADB2B13.4090207@codemonkey.ws> Content-Type: multipart/mixed; boundary="------------060500020202080107080503" List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: qemu-devel@nongnu.org, Vincent Hanquez , Luiz Capitulino This is a multi-part message in MIME format. --------------060500020202080107080503 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Anthony Liguori wrote: > Paolo Bonzini wrote: >> On 10/18/2009 04:06 PM, Luiz Capitulino wrote: >>> Integration with QObjects is a killer feature, I think it's the >>> stronger argument against grabbing one from the internet. >> >> Yeah, I'd say let's go with Anthony's stuff. I'll rebase the encoder >> on top of it soonish (I still think it's best if JSON encoding lies >> in QObject like a kind of toString). If we'll need the asynchronous >> parsing later, we can easily replace it with mine or Vincent's. > > One thing I want to add as a feature to the 0.12 release is a nice > client API. To have this, we'll need message boundary identification > and a JSON encoder. I'll focus on the message boundary identification > today. Here's a first pass. I'll clean up this afternoon and post a proper patch. It turned out to work pretty well. Regards, Anthony Liguori --------------060500020202080107080503 Content-Type: text/x-csrc; name="json-streamer.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="json-streamer.c" #include #include #include #include #define offset_of(type, member) ((unsigned long)&(((type *)0)->member)) #define container_of(obj, type, member) (type *)((void *)(obj) - offset_of(type, member)) /* * \"([^\\\"]|(\\\"\\'\\\\\\/\\b\\f\\n\\r\\t\\u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]))*\" * '([^\\']|(\\\"\\'\\\\\\/\\b\\f\\n\\r\\t\\u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]))*' * 0|([1-9][0-9]*(.[0-9]+)?([eE]([-+])?[0-9]+)) * [{}\[\],:] * [a-z]+ * */ enum json_lexer_state { ERROR = 0, IN_DONE, IN_DQ_UCODE3, IN_DQ_UCODE2, IN_DQ_UCODE1, IN_DQ_UCODE0, IN_DQ_STRING_ESCAPE, IN_DQ_STRING, IN_SQ_UCODE3, IN_SQ_UCODE2, IN_SQ_UCODE1, IN_SQ_UCODE0, IN_SQ_STRING_ESCAPE, IN_SQ_STRING, IN_ZERO, IN_DIGITS, IN_DIGIT, IN_EXP_E, IN_MANTISSA, IN_MANTISSA_DIGITS, IN_NONZERO_NUMBER, IN_NEG_NONZERO_NUMBER, IN_KEYWORD, IN_WHITESPACE, IN_START, DONE, SKIP, }; static const uint8_t json_lexer[][256] = { [IN_DONE] = { [1 ... 0x7F] = DONE, }, /* double quote string */ [IN_DQ_UCODE3] = { ['0' ... '9'] = IN_DQ_STRING, ['a' ... 'f'] = IN_DQ_STRING, ['A' ... 'F'] = IN_DQ_STRING, }, [IN_DQ_UCODE2] = { ['0' ... '9'] = IN_DQ_UCODE3, ['a' ... 'f'] = IN_DQ_UCODE3, ['A' ... 'F'] = IN_DQ_UCODE3, }, [IN_DQ_UCODE1] = { ['0' ... '9'] = IN_DQ_UCODE2, ['a' ... 'f'] = IN_DQ_UCODE2, ['A' ... 'F'] = IN_DQ_UCODE2, }, [IN_DQ_UCODE0] = { ['0' ... '9'] = IN_DQ_UCODE1, ['a' ... 'f'] = IN_DQ_UCODE1, ['A' ... 'F'] = IN_DQ_UCODE1, }, [IN_DQ_STRING_ESCAPE] = { ['b'] = IN_DQ_STRING, ['f'] = IN_DQ_STRING, ['n'] = IN_DQ_STRING, ['r'] = IN_DQ_STRING, ['t'] = IN_DQ_STRING, ['\''] = IN_DQ_STRING, ['\"'] = IN_DQ_STRING, ['u'] = IN_DQ_UCODE0, }, [IN_DQ_STRING] = { [1 ... 0xFF] = IN_DQ_STRING, ['\\'] = IN_DQ_STRING_ESCAPE, ['"'] = IN_DONE, }, /* single quote string */ [IN_SQ_UCODE3] = { ['0' ... '9'] = IN_SQ_STRING, ['a' ... 'f'] = IN_SQ_STRING, ['A' ... 'F'] = IN_SQ_STRING, }, [IN_SQ_UCODE2] = { ['0' ... '9'] = IN_SQ_UCODE3, ['a' ... 'f'] = IN_SQ_UCODE3, ['A' ... 'F'] = IN_SQ_UCODE3, }, [IN_SQ_UCODE1] = { ['0' ... '9'] = IN_SQ_UCODE2, ['a' ... 'f'] = IN_SQ_UCODE2, ['A' ... 'F'] = IN_SQ_UCODE2, }, [IN_SQ_UCODE0] = { ['0' ... '9'] = IN_SQ_UCODE1, ['a' ... 'f'] = IN_SQ_UCODE1, ['A' ... 'F'] = IN_SQ_UCODE1, }, [IN_SQ_STRING_ESCAPE] = { ['b'] = IN_SQ_STRING, ['f'] = IN_SQ_STRING, ['n'] = IN_SQ_STRING, ['r'] = IN_SQ_STRING, ['t'] = IN_SQ_STRING, ['\''] = IN_SQ_STRING, ['\"'] = IN_SQ_STRING, ['u'] = IN_SQ_UCODE0, }, [IN_SQ_STRING] = { [1 ... 0xFF] = IN_SQ_STRING, ['\\'] = IN_SQ_STRING_ESCAPE, ['\''] = IN_DONE, }, /* Zero */ [IN_ZERO] = { [1 ... 0x7F] = DONE, ['0' ... '9'] = ERROR, }, /* Non-zero numbers */ [IN_DIGITS] = { [1 ... 0x7F] = DONE, ['0' ... '9'] = IN_DIGITS, }, [IN_DIGIT] = { ['0' ... '9'] = IN_DIGITS, }, [IN_EXP_E] = { ['-'] = IN_DIGIT, ['+'] = IN_DIGIT, ['0' ... '9'] = IN_DIGITS, }, [IN_MANTISSA_DIGITS] = { [1 ... 0x7F] = DONE, ['0' ... '9'] = IN_MANTISSA_DIGITS, ['e'] = IN_EXP_E, ['E'] = IN_EXP_E, }, [IN_MANTISSA] = { ['0' ... '9'] = IN_MANTISSA_DIGITS, }, [IN_NONZERO_NUMBER] = { [1 ... 0x7F] = DONE, ['0' ... '9'] = IN_NONZERO_NUMBER, ['e'] = IN_EXP_E, ['E'] = IN_EXP_E, ['.'] = IN_MANTISSA, }, [IN_NEG_NONZERO_NUMBER] = { ['1' ... '9'] = IN_NONZERO_NUMBER, }, /* keywords */ [IN_KEYWORD] = { [1 ... 0x7F] = DONE, ['a' ... 'z'] = IN_KEYWORD, }, /* whitespace */ [IN_WHITESPACE] = { [1 ... 0x7F] = SKIP, [' '] = IN_WHITESPACE, ['\t'] = IN_WHITESPACE, ['\r'] = IN_WHITESPACE, ['\n'] = IN_WHITESPACE, }, /* top level rule */ [IN_START] = { ['"'] = IN_DQ_STRING, ['\''] = IN_SQ_STRING, ['0'] = IN_ZERO, ['1' ... '9'] = IN_NONZERO_NUMBER, ['-'] = IN_NEG_NONZERO_NUMBER, ['{'] = IN_DONE, ['}'] = IN_DONE, ['['] = IN_DONE, [']'] = IN_DONE, [','] = IN_DONE, [':'] = IN_DONE, ['a' ... 'z'] = IN_KEYWORD, [' '] = IN_WHITESPACE, ['\t'] = IN_WHITESPACE, ['\r'] = IN_WHITESPACE, ['\n'] = IN_WHITESPACE, }, }; typedef struct JSONLexer { void (*emit)(struct JSONLexer *lexer, const char *token); enum json_lexer_state state; char token[1024]; size_t len; } JSONLexer; void json_lexer_init(JSONLexer *lexer, void (*func)(JSONLexer *, const char *)) { lexer->emit = func; lexer->state = IN_START; lexer->len = 0; lexer->token[lexer->len] = 0; } int json_lexer_feed(JSONLexer *lexer, char ch) { lexer->state = json_lexer[lexer->state][(uint8_t)ch]; if (lexer->state == DONE || lexer->state == SKIP) { if (lexer->state == DONE) { lexer->emit(lexer, lexer->token); } lexer->state = json_lexer[IN_START][(uint8_t)ch]; lexer->len = 0; } if (lexer->state == ERROR) { return -EINVAL; } if (lexer->len < (sizeof(lexer->token) - 1)) { lexer->token[lexer->len++] = ch; } lexer->token[lexer->len] = 0; return 0; } typedef struct JSONMessageParser { void (*emit)(struct JSONMessageParser *parser, const char *message); JSONLexer lexer; int brace_count; int bracket_count; char buffer[1024]; size_t len; } JSONMessageParser; static void json_message_process_token(JSONLexer *lexer, const char *token) { JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer); if (strcmp(token, "{") == 0) { parser->brace_count++; } else if (strcmp(token, "}") == 0) { parser->brace_count--; } else if (strcmp(token, "[") == 0) { parser->bracket_count++; } else if (strcmp(token, "]") == 0) { parser->bracket_count--; } if (parser->brace_count == 0 && parser->bracket_count == 0) { parser->emit(parser, parser->buffer); parser->len = 0; } } void json_message_parser_init(JSONMessageParser *parser, void (*func)(JSONMessageParser *, const char *)) { parser->emit = func; parser->brace_count = 0; parser->bracket_count = 0; parser->len = 0; parser->buffer[parser->len] = 0; json_lexer_init(&parser->lexer, json_message_process_token); } int json_message_parser_feed(JSONMessageParser *parser, const char *buffer, size_t size) { size_t i; for (i = 0; i < size; i++) { int ret; parser->buffer[parser->len++] = buffer[i]; parser->buffer[parser->len] = 0; /* FIXME overflow */ ret = json_lexer_feed(&parser->lexer, buffer[i]); if (ret < 0) { return ret; } } return 0; } static void got_message(JSONMessageParser *parser, const char *message) { printf("got message `%s'\n", message); } int main(int argc, char **argv) { JSONMessageParser parser = {}; char buf[2]; int ch; json_message_parser_init(&parser, got_message); while ((ch = getchar()) != EOF) { buf[0] = ch; buf[1] = 0; if (json_message_parser_feed(&parser, buf, 1) < 0) { fprintf(stderr, "Invalid character `%c'\n", ch); return 1; } } return 0; } --------------060500020202080107080503--