NeoMutt  2024-10-02-7-gd3e66a
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 <stddef.h>
32#include "mutt/lib.h"
33#include "expando.h"
34#include "node.h"
35#include "parse.h"
36#include "render.h"
37
43struct Expando *expando_new(const char *format)
44{
45 struct Expando *exp = mutt_mem_calloc(1, sizeof(struct Expando));
46 exp->string = mutt_str_dup(format);
47 return exp;
48}
49
54void expando_free(struct Expando **ptr)
55{
56 if (!ptr || !*ptr)
57 return;
58
59 struct Expando *exp = *ptr;
60
61 node_tree_free(&exp->node);
62 FREE(&exp->string);
63
64 FREE(ptr);
65}
66
74struct Expando *expando_parse(const char *str, const struct ExpandoDefinition *defs,
75 struct Buffer *err)
76{
77 if (!str || !defs)
78 return NULL;
79
80 struct Expando *exp = expando_new(str);
81
82 struct ExpandoParseError error = { 0 };
83 struct ExpandoNode *root = NULL;
84
85 node_tree_parse(&root, exp->string, defs, &error);
86
87 if (error.position)
88 {
89 buf_strcpy(err, error.message);
90 node_tree_free(&root);
91 expando_free(&exp);
92 return NULL;
93 }
94
95 exp->node = root;
96 return exp;
97}
98
109int expando_render(const struct Expando *exp, const struct ExpandoRenderData *rdata,
110 void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
111{
112 if (!exp || !exp->node || !rdata)
113 return 0;
114
115 // Give enough space for a long command line
116 if (max_cols == -1)
117 max_cols = 8192;
118
119 return node_render(exp->node, rdata, buf, max_cols, data, flags);
120}
121
128bool expando_equal(const struct Expando *a, const struct Expando *b)
129{
130 if (!a && !b) /* both empty */
131 return true;
132 if (!a ^ !b) /* one is empty, but not the other */
133 return false;
134
135 return mutt_str_equal(a->string, b->string);
136}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
void node_tree_parse(struct ExpandoNode **root, const char *string, const struct ExpandoDefinition *defs, struct ExpandoParseError *error)
Parse a format string into ExpandoNodes.
Definition: parse.c:301
Expando Parsing.
struct Expando * expando_new(const char *format)
Create an Expando from a string.
Definition: expando.c:43
struct Expando * expando_parse(const char *str, const struct ExpandoDefinition *defs, struct Buffer *err)
Parse an Expando string.
Definition: expando.c:74
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:109
void expando_free(struct Expando **ptr)
Free an Expando object.
Definition: expando.c:54
bool expando_equal(const struct Expando *a, const struct Expando *b)
Compare two expandos.
Definition: expando.c:128
Parsed Expando.
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
#define FREE(x)
Definition: memory.h:45
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_tree_free(struct ExpandoNode **ptr)
Free a tree of ExpandoNodes.
Definition: node.c:76
Basic Expando Node.
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:52
Basic Expando Node.
Definition: node.h:69
Buffer for parsing errors.
Definition: parse.h:34
const char * position
Position of error in original string.
Definition: parse.h:36
char message[256]
Error message.
Definition: parse.h:35
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