Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 1CD36431FAF for ; Sun, 22 Jul 2012 09:08:50 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XINKmzhsxIti for ; Sun, 22 Jul 2012 09:08:49 -0700 (PDT) Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU [18.9.25.15]) by olra.theworths.org (Postfix) with ESMTP id D2AA5431FAE for ; Sun, 22 Jul 2012 09:08:48 -0700 (PDT) X-AuditID: 1209190f-b7f306d0000008b4-b8-500c258f283e Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP id B3.D5.02228.F852C005; Sun, 22 Jul 2012 12:08:47 -0400 (EDT) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id q6MG8lvG010878; Sun, 22 Jul 2012 12:08:47 -0400 Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q6MG8iZl004254 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Sun, 22 Jul 2012 12:08:46 -0400 (EDT) Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77) (envelope-from ) id 1Ssyi4-0007E5-EH; Sun, 22 Jul 2012 12:08:44 -0400 Date: Sun, 22 Jul 2012 12:08:43 -0400 From: Austin Clements To: craven@gmx.net Subject: Re: [PATCH v7 2/3] Add structured output formatter for JSON and plain text (but don't use them yet). Message-ID: <20120722160843.GC31834@mit.edu> References: <20120718194819.GP31670@mit.edu> <1342766173-1344-1-git-send-email-craven@gmx.net> <1342766173-1344-3-git-send-email-craven@gmx.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1342766173-1344-3-git-send-email-craven@gmx.net> User-Agent: Mutt/1.5.21 (2010-09-15) X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpmleLIzCtJLcpLzFFi42IR4hRV1u1X5QkwOPPN0mJvQzujxfWbM5kd mDwWb9rP5vFs1S3mAKYoLpuU1JzMstQifbsEroybBx8xFewIrtjy4ANzA+M3uy5GDg4JAROJ PzuKuxg5gUwxiQv31rOB2EIC+xgljjxK7GLkArI3MErs6DnPDOGcZJK4vesflLOEUeLhkudg LSwCqhIf+jsYQWw2AQ2JbfuXg9kiAkISk768YgGxmQWkJb79bmYCsYUF8iVWruhlBbF5BXQk jj15wQYxdAqjROupVkaIhKDEyZlPoJq1JG78e8kEcjbIoOX/OEDCnAJ2Ek83PGYHsUUFVCSm nNzGNoFRaBaS7llIumchdC9gZF7FKJuSW6Wbm5iZU5yarFucnJiXl1qka6KXm1mil5pSuokR HNaS/DsYvx1UOsQowMGoxMNr6M4dIMSaWFZcmXuIUZKDSUmUd4U0T4AQX1J+SmVGYnFGfFFp TmrxIUYJDmYlEd7L14HKeVMSK6tSi/JhUtIcLErivFdTbvoLCaQnlqRmp6YWpBbBZGU4OJQk eG+oAA0VLEpNT61Iy8wpQUgzcXCCDOcBGr4BpIa3uCAxtzgzHSJ/ilFRSpx3BkhCACSRUZoH 1wtLO68YxYFeEebdCVLFA0xZcN2vgAYzAQ2WzuICGVySiJCSamBkSH1aeEfxUIQOe0BAtNFh loSOwkecu4U1nJjvi70QfJ742tFVUv0Rq+MnlVWNR51PXN7Vtu7kLul7k4+Uzl2wuLN/6o38 CKNDnSKMawKztglu3jjLV8/W/qF+VsDjxF+KZ2oVzv3JSP8k+dP2W67kCjYDU/fN/8rVZUOL /xn3f7boex/73UqJpTgj0VCLuag4EQDaJVlnFgMAAA== Cc: notmuch@notmuchmail.org X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 22 Jul 2012 16:08:50 -0000 Quoth craven@gmx.net on Jul 20 at 8:36 am: > From: > > Using the new structured printer support in sprinter.h, implement > sprinter_json_create, which returns a new JSON structured output > formatter. The formatter prints output similar to the existing JSON, but > with differences in whitespace (mostly newlines, --output=summary prints > the entire message summary on one line, not split across multiple lines). > > Also implement a "structured" formatter for plain text that prints > prefixed strings, to be used with notmuch-search.c plain text output. > --- > Makefile.local | 2 + > sprinter-json.c | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > sprinter-text.c | 126 ++++++++++++++++++++++++++++++++++++++ > sprinter.h | 10 +++ > 4 files changed, 323 insertions(+) > create mode 100644 sprinter-json.c > create mode 100644 sprinter-text.c > > diff --git a/Makefile.local b/Makefile.local > index a890df2..296995d 100644 > --- a/Makefile.local > +++ b/Makefile.local > @@ -290,6 +290,8 @@ notmuch_client_srcs = \ > notmuch-show.c \ > notmuch-tag.c \ > notmuch-time.c \ > + sprinter-json.c \ > + sprinter-text.c \ > query-string.c \ > mime-node.c \ > crypto.c \ > diff --git a/sprinter-json.c b/sprinter-json.c > new file mode 100644 > index 0000000..32daa5a > --- /dev/null > +++ b/sprinter-json.c > @@ -0,0 +1,185 @@ > +#include > +#include > +#include > +#include "sprinter.h" > + > +struct sprinter_json { > + struct sprinter vtable; > + FILE *stream; > + /* Top of the state stack, or NULL if the printer is not currently > + * inside any aggregate types. */ > + struct json_state *state; > + > + /* A flag to signify that a separator should be inserted in the > + * output as soon as possible. > + */ > + notmuch_bool_t insert_separator; > +}; > + > +struct json_state { > + struct json_state *parent; > + /* True if nothing has been printed in this aggregate yet. > + * Suppresses the comma before a value. */ > + notmuch_bool_t first; > + /* The character that closes the current aggregate. */ > + char close; > +}; > + > +/* Helper function to set up the stream to print a value. If this > + * value follows another value, prints a comma. */ > +static struct sprinter_json * > +json_begin_value (struct sprinter *sp) > +{ > + struct sprinter_json *spj = (struct sprinter_json *) sp; > + > + if (spj->state) { > + if (! spj->state->first) { > + fputc (',', spj->stream); > + if (spj->insert_separator) { > + fputc ('\n', spj->stream); > + spj->insert_separator = FALSE; > + } else > + fputc (' ', spj->stream); > + } else > + spj->state->first = FALSE; Just a nit: if you roll a v8, it would be nice to have braces around both of these else parts to match the braces on the then parts. Obviously this doesn't warrant a v8 on its own, though. > + } > + return spj; > +} > + > +/* Helper function to begin an aggregate type. Prints the open > + * character and pushes a new state frame. */ > +static void > +json_begin_aggregate (struct sprinter *sp, char open, char close) > +{ > + struct sprinter_json *spj = json_begin_value (sp); > + struct json_state *state = talloc (spj, struct json_state); > + > + fputc (open, spj->stream); > + state->parent = spj->state; > + state->first = TRUE; > + state->close = close; > + spj->state = state; > +} > + > +static void > +json_begin_map (struct sprinter *sp) > +{ > + json_begin_aggregate (sp, '{', '}'); > +} > + > +static void > +json_begin_list (struct sprinter *sp) > +{ > + json_begin_aggregate (sp, '[', ']'); > +} > + > +static void > +json_end (struct sprinter *sp) > +{ > + struct sprinter_json *spj = (struct sprinter_json *) sp; > + struct json_state *state = spj->state; > + > + fputc (spj->state->close, spj->stream); > + spj->state = state->parent; > + talloc_free (state); > + if (spj->state == NULL) > + fputc ('\n', spj->stream); > +} > + > +static void > +json_string (struct sprinter *sp, const char *val) > +{ > + static const char *const escapes[] = { > + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b", > + ['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t" > + }; > + struct sprinter_json *spj = json_begin_value (sp); > + > + fputc ('"', spj->stream); > + for (; *val; ++val) { > + unsigned char ch = *val; > + if (ch < ARRAY_SIZE (escapes) && escapes[ch]) > + fputs (escapes[ch], spj->stream); > + else if (ch >= 32) > + fputc (ch, spj->stream); > + else > + fprintf (spj->stream, "\\u%04x", ch); > + } > + fputc ('"', spj->stream); > +} > + > +static void > +json_integer (struct sprinter *sp, int val) > +{ > + struct sprinter_json *spj = json_begin_value (sp); > + > + fprintf (spj->stream, "%d", val); > +} > + > +static void > +json_boolean (struct sprinter *sp, notmuch_bool_t val) > +{ > + struct sprinter_json *spj = json_begin_value (sp); > + > + fputs (val ? "true" : "false", spj->stream); > +} > + > +static void > +json_null (struct sprinter *sp) > +{ > + struct sprinter_json *spj = json_begin_value (sp); > + > + fputs ("null", spj->stream); > +} > + > +static void > +json_map_key (struct sprinter *sp, const char *key) > +{ > + struct sprinter_json *spj = (struct sprinter_json *) sp; > + > + json_string (sp, key); > + fputs (": ", spj->stream); > + spj->state->first = TRUE; > +} > + > +static void > +json_set_prefix (unused (struct sprinter *sp), unused (const char *name)) > +{ > +} > + > +static void > +json_separator (struct sprinter *sp) > +{ > + struct sprinter_json *spj = (struct sprinter_json *) sp; > + > + spj->insert_separator = TRUE; > +} > + > +struct sprinter * > +sprinter_json_create (const void *ctx, FILE *stream) > +{ > + static const struct sprinter_json template = { > + .vtable = { > + .begin_map = json_begin_map, > + .begin_list = json_begin_list, > + .end = json_end, > + .string = json_string, > + .integer = json_integer, > + .boolean = json_boolean, > + .null = json_null, > + .map_key = json_map_key, > + .separator = json_separator, > + .set_prefix = json_set_prefix, > + .is_text_printer = FALSE, > + } > + }; > + struct sprinter_json *res; > + > + res = talloc (ctx, struct sprinter_json); > + if (! res) > + return NULL; > + > + *res = template; > + res->stream = stream; > + return &res->vtable; > +} > diff --git a/sprinter-text.c b/sprinter-text.c > new file mode 100644 > index 0000000..b208840 > --- /dev/null > +++ b/sprinter-text.c > @@ -0,0 +1,126 @@ > +#include > +#include > +#include > +#include "sprinter.h" > + > +/* "Structured printer" interface for unstructured text printing. > + * Note that --output=summary is dispatched and formatted in > + * notmuch-search.c, the code in this file is only used for all other > + * output types. > + */ > + > +struct sprinter_text { > + struct sprinter vtable; > + FILE *stream; > + > + /* The current prefix to be printed with string/integer/boolean > + * data. > + */ > + const char *current_prefix; > + > + /* A flag to indicate if this is the first tag. Used in list of tags > + * for summary. > + */ > + notmuch_bool_t first_tag; > +}; > + > +static void > +text_string (struct sprinter *sp, const char *val) > +{ > + struct sprinter_text *sptxt = (struct sprinter_text *) sp; > + > + if (sptxt->current_prefix != NULL) > + fprintf (sptxt->stream, "%s:", sptxt->current_prefix); > + > + fputs(val, sptxt->stream); > +} > + > +static void > +text_integer (struct sprinter *sp, int val) > +{ > + struct sprinter_text *sptxt = (struct sprinter_text *) sp; > + > + fprintf (sptxt->stream, "%d", val); > +} > + > +static void > +text_boolean (struct sprinter *sp, notmuch_bool_t val) > +{ > + struct sprinter_text *sptxt = (struct sprinter_text *) sp; > + > + fputs (val ? "true" : "false", sptxt->stream); > +} > + > +static void > +text_separator (struct sprinter *sp) > +{ > + struct sprinter_text *sptxt = (struct sprinter_text *) sp; > + > + fputc ('\n', sptxt->stream); > +} > + > +static void > +text_set_prefix (struct sprinter *sp, const char *prefix) > +{ > + struct sprinter_text *sptxt = (struct sprinter_text *) sp; > + > + sptxt->current_prefix = prefix; > +} > + > +/* The structure functions begin_map, begin_list, end and map_key > + * don't do anything in the text formatter. > + */ > + > +static void > +text_begin_map (unused (struct sprinter *sp)) > +{ > +} > + > +static void > +text_begin_list (unused (struct sprinter *sp)) > +{ > +} > + > +static void > +text_end (unused (struct sprinter *sp)) > +{ > +} > + > +static void > +text_null (unused (struct sprinter *sp)) > +{ > +} > + > +static void > +text_map_key (unused (struct sprinter *sp), unused (const char *key)) > +{ > +} > + > +struct sprinter * > +sprinter_text_create (const void *ctx, FILE *stream) > +{ > + static const struct sprinter_text template = { > + .vtable = { > + .begin_map = text_begin_map, > + .begin_list = text_begin_list, > + .end = text_end, > + .string = text_string, > + .integer = text_integer, > + .boolean = text_boolean, > + .null = text_null, > + .map_key = text_map_key, > + .separator = text_separator, > + .set_prefix = text_set_prefix, > + .is_text_printer = TRUE, > + }, > + }; > + struct sprinter_text *res; > + > + res = talloc (ctx, struct sprinter_text); > + if (! res) > + return NULL; > + > + *res = template; > + res->stream = stream; > + return &res->vtable; > +} > diff --git a/sprinter.h b/sprinter.h > index 77dc26f..6680d41 100644 > --- a/sprinter.h > +++ b/sprinter.h > @@ -55,4 +55,14 @@ typedef struct sprinter { > notmuch_bool_t is_text_printer; > } sprinter_t; > > + > +/* Create a new unstructured printer that emits the default text format > + * for "notmuch search". */ > +struct sprinter * > +sprinter_text_create (const void *ctx, FILE *stream); > + > +/* Create a new structure printer that emits JSON. */ > +struct sprinter * > +sprinter_json_create (const void *ctx, FILE *stream); > + > #endif // NOTMUCH_SPRINTER_H