]> git.notmuchmail.org Git - notmuch/blob - sprinter-text.c
cli: fix notmuch top level argument parsing
[notmuch] / sprinter-text.c
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <talloc.h>
4 #include "sprinter.h"
5
6 /* "Structured printer" interface for unstructured text printing.
7  * Note that --output=summary is dispatched and formatted in
8  * notmuch-search.c, the code in this file is only used for all other
9  * output types.
10  */
11
12 struct sprinter_text {
13     struct sprinter vtable;
14     FILE *stream;
15
16     /* The current prefix to be printed with string/integer/boolean
17      * data.
18      */
19     const char *current_prefix;
20
21     /* A flag to indicate if this is the first tag. Used in list of tags
22      * for summary.
23      */
24     notmuch_bool_t first_tag;
25 };
26
27 static void
28 text_string_len (struct sprinter *sp, const char *val, size_t len)
29 {
30     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
31
32     if (sptxt->current_prefix != NULL)
33         fprintf (sptxt->stream, "%s:", sptxt->current_prefix);
34
35     fwrite (val, len, 1, sptxt->stream);
36 }
37
38 static void
39 text_string (struct sprinter *sp, const char *val)
40 {
41     if (val == NULL)
42         val = "";
43     text_string_len (sp, val, strlen (val));
44 }
45
46 static void
47 text_integer (struct sprinter *sp, int val)
48 {
49     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
50
51     fprintf (sptxt->stream, "%d", val);
52 }
53
54 static void
55 text_boolean (struct sprinter *sp, notmuch_bool_t val)
56 {
57     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
58
59     fputs (val ? "true" : "false", sptxt->stream);
60 }
61
62 static void
63 text_separator (struct sprinter *sp)
64 {
65     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
66
67     fputc ('\n', sptxt->stream);
68 }
69
70 static void
71 text_set_prefix (struct sprinter *sp, const char *prefix)
72 {
73     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
74
75     sptxt->current_prefix = prefix;
76 }
77
78 /* The structure functions begin_map, begin_list, end and map_key
79  * don't do anything in the text formatter.
80  */
81
82 static void
83 text_begin_map (unused (struct sprinter *sp))
84 {
85 }
86
87 static void
88 text_begin_list (unused (struct sprinter *sp))
89 {
90 }
91
92 static void
93 text_end (unused (struct sprinter *sp))
94 {
95 }
96
97 static void
98 text_null (unused (struct sprinter *sp))
99 {
100 }
101
102 static void
103 text_map_key (unused (struct sprinter *sp), unused (const char *key))
104 {
105 }
106
107 struct sprinter *
108 sprinter_text_create (const void *ctx, FILE *stream)
109 {
110     static const struct sprinter_text template = {
111         .vtable = {
112             .begin_map = text_begin_map,
113             .begin_list = text_begin_list,
114             .end = text_end,
115             .string = text_string,
116             .string_len = text_string_len,
117             .integer = text_integer,
118             .boolean = text_boolean,
119             .null = text_null,
120             .map_key = text_map_key,
121             .separator = text_separator,
122             .set_prefix = text_set_prefix,
123             .is_text_printer = TRUE,
124         },
125     };
126     struct sprinter_text *res;
127
128     res = talloc (ctx, struct sprinter_text);
129     if (! res)
130         return NULL;
131
132     *res = template;
133     res->stream = stream;
134     return &res->vtable;
135 }