NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_expando.c
Go to the documentation of this file.
1
24
30
31#include "config.h"
32#include <limits.h>
33#include <stdio.h>
34#include <string.h>
35#include "mutt/lib.h"
36#include "gui/lib.h"
37#include "node_expando.h"
38#include "color/lib.h"
39#include "definition.h"
40#include "format.h"
41#include "helpers.h"
42#include "node.h"
43#include "parse.h"
44#include "render.h"
45
51{
53
54 // NOTE(g0mb4): Expando definition should contain this
55 priv->color = -1;
56
57 return priv;
58}
59
65{
66 if (!ptr || !*ptr)
67 return;
68
69 FREE(ptr);
70}
71
79struct ExpandoNode *node_expando_new(struct ExpandoFormat *fmt, int did, int uid)
80{
81 struct ExpandoNode *node = node_new();
82
83 node->type = ENT_EXPANDO;
84 node->did = did;
85 node->uid = uid;
87
88 node->format = fmt;
89
92
93 return node;
94}
95
101void node_expando_set_color(const struct ExpandoNode *node, int cid)
102{
103 if (!node || (node->type != ENT_EXPANDO) || !node->ndata)
104 return;
105
106 struct NodeExpandoPrivate *priv = node->ndata;
107
108 priv->color = cid;
109}
110
116void node_expando_set_has_tree(const struct ExpandoNode *node, bool has_tree)
117{
118 if (!node || (node->type != ENT_EXPANDO) || !node->ndata)
119 return;
120
121 struct NodeExpandoPrivate *priv = node->ndata;
122
123 priv->has_tree = has_tree;
124}
125
137struct ExpandoFormat *parse_format(const char *str, const char **parsed_until,
138 struct ExpandoParseError *err)
139{
140 if (!str || !parsed_until || !err)
141 return NULL;
142
143 const char *start = str;
144
145 struct ExpandoFormat *fmt = MUTT_MEM_CALLOC(1, struct ExpandoFormat);
146
147 fmt->leader = ' ';
149 fmt->min_cols = 0;
150 fmt->max_cols = -1;
151
152 if (*str == '-')
153 {
155 str++;
156 }
157 else if (*str == '=')
158 {
160 str++;
161 }
162
163 if (*str == '0')
164 {
165 // Ignore '0' with left-justification
166 if (fmt->justification != JUSTIFY_LEFT)
167 fmt->leader = '0';
168 str++;
169 }
170
171 // Parse the width (min_cols)
172 if (mutt_isdigit(*str))
173 {
174 unsigned short number = 0;
175 const char *end_ptr = mutt_str_atous(str, &number);
176
177 if (!end_ptr || (number == USHRT_MAX))
178 {
179 err->position = str;
180 snprintf(err->message, sizeof(err->message), _("Invalid number: %s"), str);
181 FREE(&fmt);
182 return NULL;
183 }
184
185 fmt->min_cols = number;
186 str = end_ptr;
187 }
188
189 // Parse the precision (max_cols)
190 if (*str == '.')
191 {
192 str++;
193
194 unsigned short number = 1;
195
196 if (mutt_isdigit(*str))
197 {
198 const char *end_ptr = mutt_str_atous(str, &number);
199
200 if (!end_ptr || (number == USHRT_MAX))
201 {
202 err->position = str;
203 snprintf(err->message, sizeof(err->message), _("Invalid number: %s"), str);
204 FREE(&fmt);
205 return NULL;
206 }
207
208 str = end_ptr;
209 }
210 else
211 {
212 number = 0;
213 }
214
215 fmt->leader = (number == 0) ? ' ' : '0';
216 fmt->max_cols = number;
217 }
218
219 // A modifier of '_' before the letter means force lower case
220 if (*str == '_')
221 {
222 fmt->lower = true;
223 str++;
224 }
225
226 if (str == start) // Failed to parse anything
227 FREE(&fmt);
228
229 if (fmt && (fmt->min_cols == 0) && (fmt->max_cols == -1) && !fmt->lower)
230 FREE(&fmt);
231
232 *parsed_until = str;
233 return fmt;
234}
235
246struct ExpandoNode *parse_short_name(const char *str, const struct ExpandoDefinition *defs,
247 ExpandoParserFlags flags,
248 struct ExpandoFormat *fmt, const char **parsed_until,
249 struct ExpandoParseError *err)
250{
251 if (!str || !defs)
252 return NULL;
253
254 const struct ExpandoDefinition *def = defs;
255 for (; def && (def->short_name || def->long_name); def++)
256 {
257 size_t len = mutt_str_len(def->short_name);
258 if (len == 0)
259 continue;
260
261 if (mutt_strn_equal(def->short_name, str, len))
262 {
263 if (def->parse)
264 {
265 return def->parse(str, fmt, def->did, def->uid, flags, parsed_until, err);
266 }
267 else
268 {
269 *parsed_until = str + len;
270 return node_expando_new(fmt, def->did, def->uid);
271 }
272 }
273 }
274
275 return NULL;
276}
277
288struct ExpandoNode *parse_long_name(const char *str, const struct ExpandoDefinition *defs,
289 ExpandoParserFlags flags,
290 struct ExpandoFormat *fmt, const char **parsed_until,
291 struct ExpandoParseError *err)
292{
293 if (!str || !defs)
294 return NULL;
295
296 const struct ExpandoDefinition *def = defs;
297 for (; def && (def->short_name || def->long_name); def++)
298 {
299 if (!def->long_name)
300 continue;
301
302 size_t len = mutt_str_len(def->long_name);
303
304 if (mutt_strn_equal(def->long_name, str, len))
305 {
306 *parsed_until = str + len;
307 if (def->parse)
308 {
309 struct ExpandoNode *node = def->parse(str, fmt, def->did, def->uid,
310 flags, parsed_until, err);
311 if (node || (err->message[0] != '\0'))
312 return node;
313 }
314 else
315 {
316 if (str[len] != '}') // Not an exact match
317 continue;
318
319 return node_expando_new(fmt, def->did, def->uid);
320 }
321 }
322 }
323
324 return NULL;
325}
326
336struct ExpandoNode *node_expando_parse(const char *str, const struct ExpandoDefinition *defs,
337 ExpandoParserFlags flags, const char **parsed_until,
338 struct ExpandoParseError *err)
339{
340 ASSERT(str[0] == '%');
341 str++;
342
343 struct ExpandoFormat *fmt = parse_format(str, parsed_until, err);
344 if (err->position)
345 {
346 FREE(&fmt);
347 return NULL;
348 }
349
350 str = *parsed_until;
351
352 struct ExpandoNode *node = parse_short_name(str, defs, flags, fmt, parsed_until, err);
353 if (node)
354 return node;
355
356 if (!err->position)
357 {
358 err->position = *parsed_until;
359 // L10N: e.g. "Unknown expando: %Q"
360 snprintf(err->message, sizeof(err->message), _("Unknown expando: %%%.1s"), *parsed_until);
361 }
362
363 FREE(&fmt);
364 return NULL;
365}
366
376struct ExpandoNode *node_expando_parse_name(const char *str,
377 const struct ExpandoDefinition *defs,
378 ExpandoParserFlags flags, const char **parsed_until,
379 struct ExpandoParseError *err)
380{
381 ASSERT(str[0] == '%');
382 str++;
383
384 struct ExpandoFormat *fmt = parse_format(str, parsed_until, err);
385 if (err->position)
386 goto fail;
387
388 str = *parsed_until;
389
390 if (str[0] != '{')
391 goto fail;
392
393 str++;
394
395 struct ExpandoNode *node = parse_long_name(str, defs, flags, fmt, parsed_until, err);
396 if (!node)
397 {
398 if (!err->position)
399 {
400 const char *end = str + strspn(str, "abcdefghijklmnopqrstuvwxyz0123456789-");
401
402 // Only report an error if the content looks like a long name (i.e. starts
403 // with at least one valid character). If it doesn't, the '{' is probably
404 // a short-name expando like %{%b %d} — let node_expando_parse() handle it.
405 if (end != str)
406 {
407 err->position = str;
408 if (*end != '}')
409 {
410 snprintf(err->message, sizeof(err->message), _("Expando is missing closing '}'"));
411 }
412 else
413 {
414 // L10N: e.g. "Unknown expando: %{bad}"
415 snprintf(err->message, sizeof(err->message),
416 _("Unknown expando: %%{%.*s}"), (int) (end - str), str);
417 }
418 }
419 }
420 goto fail;
421 }
422
423 fmt = NULL; // owned by the node, now
424
425 if ((*parsed_until)[0] == '}')
426 {
427 (*parsed_until)++;
428 return node;
429 }
430
431 node_free(&node);
432
433fail:
434 FREE(&fmt);
435 return NULL;
436}
437
444const char *skip_until_ch(const char *str, char terminator)
445{
446 while (str[0] != '\0')
447 {
448 if (*str == terminator)
449 break;
450
451 if (str[0] == '\\') // Literal character
452 {
453 if (str[1] == '\0')
454 return str + 1;
455
456 str++;
457 }
458
459 str++;
460 }
461
462 return str;
463}
464
476struct ExpandoNode *node_expando_parse_enclosure(const char *str, int did,
477 int uid, char terminator,
478 struct ExpandoFormat *fmt,
479 const char **parsed_until,
480 struct ExpandoParseError *err)
481
482{
483 str++; // skip opening char
484
485 const char *expando_end = skip_until_ch(str, terminator);
486
487 if (*expando_end != terminator)
488 {
489 err->position = expando_end;
490 snprintf(err->message, sizeof(err->message),
491 // L10N: Expando is missing a terminator character
492 // e.g. "%[..." is missing the final ']'
493 _("Expando is missing terminator: '%c'"), terminator);
494 return NULL;
495 }
496
497 *parsed_until = expando_end + 1;
498
499 struct ExpandoNode *node = node_expando_new(fmt, did, uid);
500
501 struct Buffer *buf = buf_pool_get();
502 for (; str < expando_end; str++)
503 {
504 if (str[0] == '\\')
505 continue;
506 buf_addch(buf, str[0]);
507 }
508
509 node->text = buf_strdup(buf);
510 buf_pool_release(&buf);
511
512 return node;
513}
514
520void add_color(struct Buffer *buf, enum ColorId cid)
521{
522 ASSERT(cid < MT_COLOR_MAX);
523
525 buf_addch(buf, cid);
526}
527
531int node_expando_render(const struct ExpandoNode *node,
532 const struct ExpandoRenderCallback *erc, struct Buffer *buf,
533 int max_cols, void *data, MuttFormatFlags flags)
534{
535 ASSERT(node->type == ENT_EXPANDO);
536
537 struct Buffer *buf_expando = buf_pool_get();
538 struct Buffer *buf_format = buf_pool_get();
539
540 const struct ExpandoFormat *fmt = node->format;
541 const struct NodeExpandoPrivate *priv = node->ndata;
542
543 // ---------------------------------------------------------------------------
544 // Numbers and strings get treated slightly differently. We prefer strings.
545 // This allows dates to be stored as 1729850182, but displayed as "2024-10-25".
546
547 const struct ExpandoRenderCallback *erc_match = find_get_string(erc, node->did, node->uid);
548 if (erc_match)
549 {
550 erc_match->get_string(node, data, flags, buf_expando);
551
552 if (fmt && fmt->lower)
553 buf_lower_special(buf_expando);
554 }
555 else
556 {
557 erc_match = find_get_number(erc, node->did, node->uid);
558 ASSERT(erc_match && "Unknown UID");
559
560 const long num = erc_match->get_number(node, data, flags);
561
562 int precision = 1;
563
564 if (fmt)
565 {
566 precision = fmt->max_cols;
567 if ((precision < 0) && (fmt->leader == '0'))
568 precision = fmt->min_cols;
569 }
570
571 if (num < 0)
572 precision--; // Allow space for the '-' sign
573
574 buf_printf(buf_expando, "%.*ld", precision, num);
575 }
576
577 // ---------------------------------------------------------------------------
578
579 int max = max_cols;
580 int min = 0;
581
582 if (fmt)
583 {
584 min = fmt->min_cols;
585 if (fmt->max_cols > 0)
586 max = MIN(max_cols, fmt->max_cols);
587 }
588
589 const enum FormatJustify just = fmt ? fmt->justification : JUSTIFY_LEFT;
590
591 int total_cols = format_string(buf_format, min, max, just, ' ', buf_string(buf_expando),
592 buf_len(buf_expando), priv->has_tree);
593
594 if (!buf_is_empty(buf_format))
595 {
596 if (priv->color > -1)
597 add_color(buf, priv->color);
598
599 buf_addstr(buf, buf_string(buf_format));
600
601 if (priv->color > -1)
603 }
604
605 buf_pool_release(&buf_format);
606 buf_pool_release(&buf_expando);
607
608 return total_cols;
609}
const char * mutt_str_atous(const char *str, unsigned short *dst)
Convert ASCII string to an unsigned short.
Definition atoi.c:269
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:491
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:291
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
Color and attribute parsing.
ColorId
List of all coloured objects.
Definition color.h:35
@ MT_COLOR_MAX
Definition color.h:97
@ MT_COLOR_INDEX
Index: default colour.
Definition color.h:86
bool mutt_isdigit(int arg)
Wrapper for isdigit(3)
Definition ctype.c:66
Define an Expando format string.
uint8_t ExpandoParserFlags
Definition definition.h:41
const struct ExpandoRenderCallback * find_get_string(const struct ExpandoRenderCallback *erc, int did, int uid)
Find a get_string() callback function.
Definition helpers.c:69
void buf_lower_special(struct Buffer *buf)
Convert to lowercase, excluding special characters.
Definition helpers.c:92
const struct ExpandoRenderCallback * find_get_number(const struct ExpandoRenderCallback *erc, int did, int uid)
Find a get_number() callback function.
Definition helpers.c:45
Shared code.
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.
FormatJustify
Alignment for format_string()
Definition format.h:33
@ JUSTIFY_RIGHT
Right justify the text.
Definition format.h:36
@ JUSTIFY_LEFT
Left justify the text.
Definition format.h:34
@ JUSTIFY_CENTER
Centre the text.
Definition format.h:35
int node_expando_render(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render an Expando Node - Implements ExpandoNode::render() -.
Convenience wrapper for the gui headers.
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition thread.h:72
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MIN(a, b)
Return the minimum of two values.
Definition memory.h:40
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition string.c:429
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition node.c:39
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
struct ExpandoNode * node_expando_parse(const char *str, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, const char **parsed_until, struct ExpandoParseError *err)
Parse an Expando format string.
void node_expando_private_free(void **ptr)
Free Expando private data - Implements ExpandoNode::ndata_free()
void node_expando_set_color(const struct ExpandoNode *node, int cid)
Set the colour for an Expando.
void node_expando_set_has_tree(const struct ExpandoNode *node, bool has_tree)
Set the has_tree flag for an Expando.
struct ExpandoNode * parse_long_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 long name.
void add_color(struct Buffer *buf, enum ColorId cid)
Add a colour code to a buffer.
struct NodeExpandoPrivate * node_expando_private_new(void)
Create new Expando private data.
struct ExpandoNode * node_expando_new(struct ExpandoFormat *fmt, int did, int uid)
Create a new Expando ExpandoNode.
struct ExpandoNode * node_expando_parse_enclosure(const char *str, int did, int uid, char terminator, struct ExpandoFormat *fmt, const char **parsed_until, struct ExpandoParseError *err)
Parse an enclosed Expando.
struct ExpandoNode * node_expando_parse_name(const char *str, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, const char **parsed_until, struct ExpandoParseError *err)
Parse an Expando format string.
const char * skip_until_ch(const char *str, char terminator)
Search a string for a terminator character.
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.
struct ExpandoFormat * parse_format(const char *str, const char **parsed_until, struct ExpandoParseError *err)
Parse a format string.
Expando Node for an Expando.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
Render Expandos using Data.
uint8_t MuttFormatFlags
Definition render.h:45
#define ASSERT(COND)
Definition signal2.h:59
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
Definition of a format string.
Definition definition.h:49
short uid
Unique ID in domain.
Definition definition.h:53
struct ExpandoNode *(* parse)(const char *str, struct ExpandoFormat *fmt, int did, int uid, ExpandoParserFlags flags, const char **parsed_until, struct ExpandoParseError *err)
Definition definition.h:68
const char * long_name
Long Expando name, e.g. "name".
Definition definition.h:51
short did
Domain ID.
Definition definition.h:52
const char * short_name
Short Expando name, e.g. "n".
Definition definition.h:50
Formatting information for an Expando.
Definition node.h:53
char leader
Leader character, 0 or space.
Definition node.h:57
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
int uid
Unique ID, e.g. ED_EMA_SIZE.
Definition node.h:70
void * ndata
Private node data.
Definition node.h:77
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
int did
Domain ID, e.g. ED_EMAIL.
Definition node.h:69
const char * text
Node-specific text.
Definition node.h:73
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition node.h:68
void(* ndata_free)(void **ptr)
Function to free the private node data.
Definition node.h:78
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
get_string_t get_string
Callback function to get a string.
Definition render.h:86
get_number_t get_number
Callback function to get a number.
Definition render.h:87
Private data for an Expando -.
int color
ColorId to use.
bool has_tree
Contains tree characters, used in $index_format's s.