NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
expando.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stddef.h>
33#include "mutt/lib.h"
34#include "expando.h"
35#include "node.h"
36#include "node_container.h"
37#include "node_padding.h"
38#include "node_text.h"
39#include "parse.h"
40#include "render.h"
41
43
49struct Expando *expando_new(const char *format)
50{
51 struct Expando *exp = MUTT_MEM_CALLOC(1, struct Expando);
52 exp->string = mutt_str_dup(format);
53 exp->node = node_container_new();
54 return exp;
55}
56
61void expando_free(struct Expando **ptr)
62{
63 if (!ptr || !*ptr)
64 return;
65
66 struct Expando *exp = *ptr;
67
68 node_free(&exp->node);
69 FREE(&exp->string);
70
71 FREE(ptr);
72}
73
81struct Expando *expando_parse(const char *str, const struct ExpandoDefinition *defs,
82 struct Buffer *err)
83{
84 if (!str || (*str == '\0') || !defs)
85 return NULL;
86
87 struct Expando *exp = expando_new(str);
88
89 struct ExpandoParseError error = { 0 };
90
91 const char *parsed_until = NULL;
92 node_parse_many(exp->node, str, NTE_NO_FLAGS, defs, &parsed_until, &error);
93
94 if (error.position)
95 {
96 buf_strcpy(err, error.message);
97 expando_free(&exp);
98 return NULL;
99 }
100
101 // Optimise the tree layout
104
105 return exp;
106}
107
118int expando_render(const struct Expando *exp, const struct ExpandoRenderData *rdata,
119 void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
120{
121 if (!exp || !exp->node || !rdata)
122 return 0;
123
124 // Give enough space for a long command line
125 if (max_cols == -1)
126 max_cols = 8192;
127
128 return node_render(exp->node, rdata, buf, max_cols, data, flags);
129}
130
137bool expando_equal(const struct Expando *a, const struct Expando *b)
138{
139 if (!a && !b) /* both empty */
140 return true;
141 if (!a ^ !b) /* one is empty, but not the other */
142 return false;
143
144 return mutt_str_equal(a->string, b->string);
145}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
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.
struct Expando * expando_new(const char *format)
Create an Expando from a string.
Definition: expando.c:49
struct Expando * expando_parse(const char *str, const struct ExpandoDefinition *defs, struct Buffer *err)
Parse an Expando string.
Definition: expando.c:81
int expando_render(const struct Expando *exp, const struct ExpandoRenderData *rdata, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando + data into a string.
Definition: expando.c:118
void expando_free(struct Expando **ptr)
Free an Expando object.
Definition: expando.c:61
bool expando_equal(const struct Expando *a, const struct Expando *b)
Compare two expandos.
Definition: expando.c:137
Parsed Expando.
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
void node_free(struct ExpandoNode **ptr)
Free an ExpandoNode and its private data.
Definition: node.c:48
Basic Expando Node.
struct ExpandoNode * node_container_new(void)
Create a new Container ExpandoNode.
void node_container_collapse_all(struct ExpandoNode **ptr)
Remove unnecessary Containers.
Expando Node for a Container.
void node_padding_repad(struct ExpandoNode **ptr)
Rearrange Padding in a tree of ExpandoNodes.
Definition: node_padding.c:291
Expando Node for Padding.
Expando Node for Text.
#define NTE_NO_FLAGS
No flags are set.
Definition: node_text.h:33
int node_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, 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
String manipulation buffer.
Definition: buffer.h:36
Definition of a format string.
Definition: definition.h:44
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
Parsed Expando trees.
Definition: expando.h:41
struct ExpandoNode * node
Parsed tree.
Definition: expando.h:43
const char * string
Pointer to the parsed string.
Definition: expando.h:42