NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_condition.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stdio.h>
33#include "mutt/lib.h"
34#include "node_condition.h"
35#include "definition.h"
36#include "format.h"
37#include "helpers.h"
38#include "node.h"
39#include "node_condbool.h"
40#include "node_container.h"
41#include "node_expando.h"
42#include "node_text.h"
43#include "parse.h"
44#include "render.h"
45
49static int node_condition_render(const struct ExpandoNode *node,
50 const struct ExpandoRenderCallback *erc,
51 struct Buffer *buf, int max_cols, void *data,
52 MuttFormatFlags flags)
53{
54 ASSERT(node->type == ENT_CONDITION);
55
56 const struct ExpandoNode *node_cond = node_get_child(node, ENC_CONDITION);
57
58 // Discard any text returned, just use the return value as a bool
59 struct Buffer *buf_cond = buf_pool_get();
60 int rc_cond = node_cond->render(node_cond, erc, buf_cond, max_cols, data, flags);
61
62 int rc = 0;
63 buf_reset(buf_cond);
64
65 if (rc_cond == true)
66 {
67 const struct ExpandoNode *node_true = node_get_child(node, ENC_TRUE);
68 rc = node_render(node_true, erc, buf_cond, max_cols, data, flags);
69 }
70 else
71 {
72 const struct ExpandoNode *node_false = node_get_child(node, ENC_FALSE);
73 rc = node_render(node_false, erc, buf_cond, max_cols, data, flags);
74 }
75
76 const struct ExpandoFormat *fmt = node->format;
77 if (!fmt)
78 {
79 buf_addstr(buf, buf_string(buf_cond));
80 buf_pool_release(&buf_cond);
81 return rc;
82 }
83
84 struct Buffer *tmp = buf_pool_get();
85
86 int min_cols = MAX(fmt->min_cols, fmt->max_cols);
87 min_cols = MIN(min_cols, max_cols);
88 if (fmt->max_cols >= 0)
89 max_cols = MIN(max_cols, fmt->max_cols);
90 rc = format_string(tmp, min_cols, max_cols, fmt->justification, ' ',
91 buf_string(buf_cond), buf_len(buf_cond), true);
92 if (fmt->lower)
94
95 buf_addstr(buf, buf_string(tmp));
96 buf_pool_release(&tmp);
97 buf_pool_release(&buf_cond);
98
99 return rc;
100}
101
111 struct ExpandoNode *node_true,
112 struct ExpandoNode *node_false,
113 struct ExpandoFormat *fmt)
114{
115 ASSERT(node_cond);
116
117 struct ExpandoNode *node = node_new();
118
119 node->type = ENT_CONDITION;
121
122 ARRAY_SET(&node->children, ENC_CONDITION, node_cond);
123 ARRAY_SET(&node->children, ENC_TRUE, node_true);
124 ARRAY_SET(&node->children, ENC_FALSE, node_false);
125
126 node->format = fmt;
127
128 return node;
129}
130
140struct ExpandoNode *node_condition_parse(const char *str, NodeTextTermFlags term_chars,
141 const struct ExpandoDefinition *defs,
142 const char **parsed_until,
143 struct ExpandoParseError *err)
144{
145 if (!str || (str[0] != '%'))
146 return NULL;
147
148 str++; // Skip %
149
150 struct ExpandoFormat *fmt = NULL;
151 struct ExpandoNode *node_cond = NULL;
152 struct ExpandoNode *node_true = NULL;
153 struct ExpandoNode *node_false = NULL;
154
155 //----------------------------------------------------------------------------
156 // Parse the format (optional)
157 fmt = parse_format(str, parsed_until, err);
158 if (err->position)
159 goto fail;
160
161 str = *parsed_until;
162
163 if ((str[0] != '<') && (str[0] != '?'))
164 goto fail;
165
166 const bool old_style = (str[0] == '?'); // %?X?...&...?
167 str++;
168
169 //----------------------------------------------------------------------------
170 // Parse the condition
171 node_cond = parse_short_name(str, defs, EP_CONDITIONAL, NULL, parsed_until, err);
172 if (!node_cond)
173 goto fail;
174
175 if (node_cond->type == ENT_EXPANDO)
176 {
177 node_cond->type = ENT_CONDBOOL;
178 node_cond->render = node_condbool_render;
179 }
180
181 str = *parsed_until; // Skip the expando
182 if (str[0] != '?')
183 {
184 err->position = str;
185 snprintf(err->message, sizeof(err->message),
186 // L10N: Expando is missing a terminator character
187 // e.g. "%[..." is missing the final ']'
188 _("Conditional expando is missing '%c'"), '?');
189 goto fail;
190 }
191 str++; // Skip the '?'
192
193 //----------------------------------------------------------------------------
194 // Parse the 'true' clause (optional)
195 const NodeTextTermFlags term_true = term_chars | NTE_AMPERSAND |
196 (old_style ? NTE_QUESTION : NTE_GREATER);
197
198 node_true = node_container_new();
199 node_parse_many(node_true, str, term_true, defs, parsed_until, err);
200 if (err->position)
201 goto fail;
202
203 str = *parsed_until;
204
205 //----------------------------------------------------------------------------
206 // Parse the 'false' clause (optional)
207
208 node_false = NULL;
209 if (str[0] == '&')
210 {
211 str++;
212 const NodeTextTermFlags term_false = term_chars | (old_style ? NTE_QUESTION : NTE_GREATER);
213
214 node_false = node_container_new();
215 node_parse_many(node_false, str, term_false, defs, parsed_until, err);
216 if (err->position)
217 goto fail;
218
219 str = *parsed_until;
220 }
221
222 //----------------------------------------------------------------------------
223 // Check for the terminator character
224 const char terminator = old_style ? '?' : '>';
225
226 if (str[0] != terminator)
227 {
228 err->position = str;
229 snprintf(err->message, sizeof(err->message),
230 // L10N: Expando is missing a terminator character
231 // e.g. "%[..." is missing the final ']'
232 _("Conditional expando is missing '%c'"), '?');
233 goto fail;
234 }
235
236 *parsed_until = str + 1;
237
238 return node_condition_new(node_cond, node_true, node_false, fmt);
239
240fail:
241 FREE(&fmt);
242 node_free(&node_cond);
243 node_free(&node_true);
244 node_free(&node_false);
245 return NULL;
246}
#define ARRAY_SET(head, idx, elem)
Set an element in the array.
Definition: array.h:123
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:491
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
Define an Expando format string.
#define EP_CONDITIONAL
Expando is being used as a condition.
Definition: definition.h:35
void buf_lower_special(struct Buffer *buf)
Convert to lowercase, excluding special characters.
Definition: helpers.c:92
Shared code.
bool node_parse_many(struct ExpandoNode *node_cont, const char *str, NodeTextTermFlags term_chars, const struct ExpandoDefinition *defs, const char **parsed_until, struct ExpandoParseError *err)
Parse a format string.
Definition: parse.c:81
Expando Parsing.
int format_string(struct Buffer *buf, int min_cols, int max_cols, enum FormatJustify justify, char pad_char, const char *str, size_t n, bool arboreal)
Format a string, like snprintf()
Definition: format.c:108
Simple string formatting.
static int node_condition_render(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render a Conditional Node - Implements ExpandoNode::render() -.
int node_condbool_render(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Callback for every bool node - Implements ExpandoNode::render() -.
Definition: node_condbool.c:41
#define FREE(x)
Definition: memory.h:55
#define MIN(a, b)
Definition: memory.h:32
#define MAX(a, b)
Definition: memory.h:31
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
struct ExpandoNode * node_get_child(const struct ExpandoNode *node, int index)
Get a child of an ExpandoNode.
Definition: node.c:91
void node_free(struct ExpandoNode **ptr)
Free an ExpandoNode and its private data.
Definition: node.c:48
Basic Expando Node.
@ ENT_EXPANDO
Expando, e.g. 'n'.
Definition: node.h:39
@ ENT_CONDITION
True/False condition.
Definition: node.h:41
@ ENT_CONDBOOL
True/False boolean condition.
Definition: node.h:42
Expando Node for a Conditional Boolean.
struct ExpandoNode * node_condition_new(struct ExpandoNode *node_cond, struct ExpandoNode *node_true, struct ExpandoNode *node_false, struct ExpandoFormat *fmt)
Create a new Condition Expando Node.
struct ExpandoNode * node_condition_parse(const char *str, NodeTextTermFlags term_chars, const struct ExpandoDefinition *defs, const char **parsed_until, struct ExpandoParseError *err)
Parse a conditional Expando.
Expando Node for a Condition.
@ ENC_CONDITION
Index of Condition Node.
@ ENC_FALSE
Index of False Node.
@ ENC_TRUE
Index of True Node.
struct ExpandoNode * node_container_new(void)
Create a new Container ExpandoNode.
Expando Node for a Container.
struct ExpandoNode * parse_short_name(const char *str, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, struct ExpandoFormat *fmt, const char **parsed_until, struct ExpandoParseError *err)
Create an expando by its short name.
Definition: node_expando.c:245
struct ExpandoFormat * parse_format(const char *str, const char **parsed_until, struct ExpandoParseError *err)
Parse a format string.
Definition: node_expando.c:136
Expando Node for an Expando.
Expando Node for Text.
#define NTE_QUESTION
'?' Question mark
Definition: node_text.h:36
#define NTE_GREATER
'>' Greater than
Definition: node_text.h:35
uint8_t NodeTextTermFlags
Special characters that end a text string.
Definition: node_text.h:32
#define NTE_AMPERSAND
'&' Ampersand
Definition: node_text.h:34
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
int node_render(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render a tree of ExpandoNodes into a string.
Definition: render.c:45
Render Expandos using Data.
uint8_t MuttFormatFlags
Flags for expando_render(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: render.h:32
#define ASSERT(COND)
Definition: signal2.h:58
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
Definition of a format string.
Definition: definition.h:44
Formatting information for an Expando.
Definition: node.h:53
enum FormatJustify justification
Justification: left, centre, right.
Definition: node.h:56
int min_cols
Minimum number of screen columns.
Definition: node.h:54
int max_cols
Maximum number of screen columns.
Definition: node.h:55
bool lower
Display in lower case.
Definition: node.h:58
Basic Expando Node.
Definition: node.h:67
struct ExpandoFormat * format
Formatting info.
Definition: node.h:72
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:92
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:75
Buffer for parsing errors.
Definition: parse.h:37
char message[1024]
Error message.
Definition: parse.h:38
const char * position
Position of error in original string.
Definition: parse.h:39