NeoMutt  2024-04-16-36-g75b6fb
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->tree);
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->tree = 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->tree || !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 struct ExpandoNode *root = exp->tree;
120 return node_tree_render(root, rdata, buf, max_cols, data, flags);
121}
122
129bool expando_equal(const struct Expando *a, const struct Expando *b)
130{
131 if (!a && !b) /* both empty */
132 return true;
133 if (!a ^ !b) /* one is empty, but not the other */
134 return false;
135
136 return mutt_str_equal(a->string, b->string);
137}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:394
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:300
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:129
Parsed Expando.
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#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:654
void node_tree_free(struct ExpandoNode **ptr)
Free a tree of ExpandoNodes.
Definition: node.c:76
Basic Expando Node.
int node_tree_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:67
Buffer for parsing errors.
Definition: parse.h:34
char message[128]
Error message.
Definition: parse.h:35
const char * position
Position of error in original string.
Definition: parse.h:36
Parsed Expando trees.
Definition: expando.h:41
struct ExpandoNode * tree
Parsed tree.
Definition: expando.h:43
const char * string
Pointer to the parsed string.
Definition: expando.h:42