NeoMutt  2024-10-02-37-gfa9146
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 "node_condition.h"
36#include "node_container.h"
37#include "node_padding.h"
38#include "parse.h"
39#include "render.h"
40
46struct Expando *expando_new(const char *format)
47{
48 struct Expando *exp = mutt_mem_calloc(1, sizeof(struct Expando));
49 exp->string = mutt_str_dup(format);
50 exp->node = node_container_new();
51 return exp;
52}
53
58void expando_free(struct Expando **ptr)
59{
60 if (!ptr || !*ptr)
61 return;
62
63 struct Expando *exp = *ptr;
64
65 node_free(&exp->node);
66 FREE(&exp->string);
67
68 FREE(ptr);
69}
70
78struct Expando *expando_parse(const char *str, const struct ExpandoDefinition *defs,
79 struct Buffer *err)
80{
81 if (!str || (*str == '\0') || !defs)
82 return NULL;
83
84 struct Expando *exp = expando_new(str);
85
86 struct ExpandoParseError error = { 0 };
87 const char *end = NULL;
88 const char *start = exp->string;
89
90 while (*start)
91 {
92 struct ExpandoNode *node = node_parse(start, NULL, CON_NO_CONDITION, &end, defs, &error);
93 if (!node)
94 break;
95
96 node_add_child(exp->node, node);
97 start = end;
98 }
99
100 if (error.position)
101 {
102 buf_strcpy(err, error.message);
103 expando_free(&exp);
104 return NULL;
105 }
106
107 // Optimise the tree layout
110
111 return exp;
112}
113
124int expando_render(const struct Expando *exp, const struct ExpandoRenderData *rdata,
125 void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
126{
127 if (!exp || !exp->node || !rdata)
128 return 0;
129
130 // Give enough space for a long command line
131 if (max_cols == -1)
132 max_cols = 8192;
133
134 return node_render(exp->node, rdata, buf, max_cols, data, flags);
135}
136
143bool expando_equal(const struct Expando *a, const struct Expando *b)
144{
145 if (!a && !b) /* both empty */
146 return true;
147 if (!a ^ !b) /* one is empty, but not the other */
148 return false;
149
150 return mutt_str_equal(a->string, b->string);
151}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
struct ExpandoNode * node_parse(const char *str, const char *end, enum ExpandoConditionStart condition_start, const char **parsed_until, const struct ExpandoDefinition *defs, struct ExpandoParseError *err)
Parse a format string into ExpandoNodes.
Definition: parse.c:124
Expando Parsing.
struct Expando * expando_new(const char *format)
Create an Expando from a string.
Definition: expando.c:46
struct Expando * expando_parse(const char *str, const struct ExpandoDefinition *defs, struct Buffer *err)
Parse an Expando string.
Definition: expando.c:78
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:124
void expando_free(struct Expando **ptr)
Free an Expando object.
Definition: expando.c:58
bool expando_equal(const struct Expando *a, const struct Expando *b)
Compare two expandos.
Definition: expando.c:143
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_add_child(struct ExpandoNode *node, struct ExpandoNode *child)
Add a child to an ExpandoNode.
Definition: node.c:76
void node_free(struct ExpandoNode **ptr)
Free an ExpandoNode and its private data.
Definition: node.c:48
Basic Expando Node.
Expando Node for a Condition.
@ CON_NO_CONDITION
Parser is not currently in a condition.
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:282
Expando Node for Padding.
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:67
Buffer for parsing errors.
Definition: parse.h:35
const char * position
Position of error in original string.
Definition: parse.h:37
char message[256]
Error message.
Definition: parse.h:36
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