NeoMutt  2025-12-11-1039-g550ac6
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
expando.c File Reference

Parsed Expando. More...

#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include "mutt/lib.h"
#include "expando.h"
#include "node.h"
#include "node_container.h"
#include "node_padding.h"
#include "node_text.h"
#include "parse.h"
#include "render.h"
Include dependency graph for expando.c:

Go to the source code of this file.

Functions

struct Expandoexpando_new (const char *format)
 Create an Expando from a string.
void expando_free (struct Expando **ptr)
 Free an Expando object.
static const struct ExpandoNodenode_find (const struct ExpandoNode *node, int did, int uid)
 Find a Node in a tree.
const struct ExpandoNodeexpando_find_node (const struct Expando *exp, int did, int uid)
 Find a Node in an Expando tree.
struct Expandoexpando_parse (const char *str, const struct ExpandoDefinition *defs, struct Buffer *err)
 Parse an Expando string.
int expando_render (const struct Expando *exp, const struct ExpandoRenderCallback *erc, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
 Render an Expando + data into a string.
bool expando_equal (const struct Expando *a, const struct Expando *b)
 Compare two expandos.

Detailed Description

Parsed Expando.

Authors
  • Tóth János
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file expando.c.

Function Documentation

◆ expando_new()

struct Expando * expando_new ( const char * format)

Create an Expando from a string.

Parameters
formatFormat string to parse
Return values
ptrNew Expando object

Definition at line 49 of file expando.c.

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}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
struct ExpandoNode * node_container_new(void)
Create a new Container ExpandoNode.
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ expando_free()

void expando_free ( struct Expando ** ptr)

Free an Expando object.

Parameters
[out]ptrExpando to free

Definition at line 61 of file expando.c.

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}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
void node_free(struct ExpandoNode **ptr)
Free an ExpandoNode and its private data.
Definition node.c:48
Here is the call graph for this function:
Here is the caller graph for this function:

◆ node_find()

const struct ExpandoNode * node_find ( const struct ExpandoNode * node,
int did,
int uid )
static

Find a Node in a tree.

Parameters
nodeRoot Node
didDomain ID
uidUnique ID
Return values
ptrMatching Node
NULLNo matching Node

Definition at line 82 of file expando.c.

83{
84 if (!node)
85 return NULL;
86
87 if ((node->did == did) && (node->uid == uid))
88 return node;
89
90 struct ExpandoNode **np = NULL;
91 ARRAY_FOREACH(np, &node->children)
92 {
93 const struct ExpandoNode *match = node_find(*np, did, uid);
94 if (match)
95 return match;
96 }
97
98 return NULL;
99}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
static const struct ExpandoNode * node_find(const struct ExpandoNode *node, int did, int uid)
Find a Node in a tree.
Definition expando.c:82
Basic Expando Node.
Definition node.h:67
int uid
Unique ID, e.g. ED_EMA_SIZE.
Definition node.h:70
int did
Domain ID, e.g. ED_EMAIL.
Definition node.h:69
struct ExpandoNodeArray children
Children nodes.
Definition node.h:75
Here is the call graph for this function:
Here is the caller graph for this function:

◆ expando_find_node()

const struct ExpandoNode * expando_find_node ( const struct Expando * exp,
int did,
int uid )

Find a Node in an Expando tree.

Parameters
expExpando to search
didDomain ID
uidUnique ID
Return values
ptrMatching Node
NULLNo matching Node

Definition at line 109 of file expando.c.

110{
111 if (!exp)
112 return NULL;
113
114 return node_find(exp->node, did, uid);
115}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ expando_parse()

struct Expando * expando_parse ( const char * str,
const struct ExpandoDefinition * defs,
struct Buffer * err )

Parse an Expando string.

Parameters
strString to parse
defsData defining Expando
errBuffer for error messages
Return values
ptrNew Expando

Definition at line 124 of file expando.c.

126{
127 if (!str || (*str == '\0') || !defs)
128 return NULL;
129
130 struct Expando *exp = expando_new(str);
131
132 struct ExpandoParseError error = { 0 };
133
134 const char *parsed_until = NULL;
135 node_parse_many(exp->node, str, NTE_NONE, defs, &parsed_until, &error);
136
137 if (error.position)
138 {
139 buf_strcpy(err, error.message);
140 expando_free(&exp);
141 return NULL;
142 }
143
144 // Optimise the tree layout
147
148 return exp;
149}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:401
struct Expando * expando_new(const char *format)
Create an Expando from a string.
Definition expando.c:49
void expando_free(struct Expando **ptr)
Free an Expando object.
Definition expando.c:61
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:83
void node_container_collapse_all(struct ExpandoNode **ptr)
Remove unnecessary Containers.
void node_padding_repad(struct ExpandoNode **ptr)
Rearrange Padding in a tree of ExpandoNodes.
@ NTE_NONE
No flags are set.
Definition node_text.h:34
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ expando_render()

int expando_render ( const struct Expando * exp,
const struct ExpandoRenderCallback * erc,
void * data,
MuttFormatFlags flags,
int max_cols,
struct Buffer * buf )

Render an Expando + data into a string.

Parameters
[in]expExpando containing the expando tree
[in]ercExpando render callback functions
[in]dataCallback data
[in]flagsCallback flags
[in]max_colsNumber of screen columns (-1 means unlimited)
[out]bufBuffer in which to save string
Return values
numNumber of bytes written to buf and screen columns used

Definition at line 161 of file expando.c.

163{
164 if (!exp || !exp->node || !erc)
165 return 0;
166
167 // Give enough space for a long command line
168 if (max_cols == -1)
169 max_cols = 8192;
170
171 return node_render(exp->node, erc, buf, max_cols, data, flags);
172}
int node_render(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render a tree of ExpandoNodes into a string.
Definition render.c:45
Here is the call graph for this function:
Here is the caller graph for this function:

◆ expando_equal()

bool expando_equal ( const struct Expando * a,
const struct Expando * b )

Compare two expandos.

Parameters
aFirst Expando
bSecond Expando
Return values
trueThey are identical

Definition at line 180 of file expando.c.

181{
182 if (!a && !b) /* both empty */
183 return true;
184 if (!a ^ !b) /* one is empty, but not the other */
185 return false;
186
187 return mutt_str_equal(a->string, b->string);
188}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:666
Here is the call graph for this function:
Here is the caller graph for this function: