NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_condition.c File Reference

Expando Node for a Condition. More...

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "node_condition.h"
#include "definition.h"
#include "format.h"
#include "helpers.h"
#include "node.h"
#include "node_condbool.h"
#include "node_container.h"
#include "node_expando.h"
#include "node_text.h"
#include "parse.h"
#include "render.h"
+ Include dependency graph for node_condition.c:

Go to the source code of this file.

Functions

static int node_condition_render (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render a Conditional Node - Implements ExpandoNode::render() -.
 
struct ExpandoNodenode_condition_new (struct ExpandoNode *node_cond, struct ExpandoNode *node_true, struct ExpandoNode *node_false, struct ExpandoFormat *fmt)
 Create a new Condition Expando Node.
 
struct ExpandoNodenode_condition_parse (const char *str, NodeTextTermFlags term_chars, const struct ExpandoDefinition *defs, const char **parsed_until, struct ExpandoParseError *err)
 Parse a conditional Expando.
 

Detailed Description

Expando Node for a Condition.

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 node_condition.c.

Function Documentation

◆ node_condition_new()

struct ExpandoNode * node_condition_new ( struct ExpandoNode node_cond,
struct ExpandoNode node_true,
struct ExpandoNode node_false,
struct ExpandoFormat fmt 
)

Create a new Condition Expando Node.

Parameters
node_condExpando Node that will be tested
node_trueNode tree for the 'true' case
node_falseNode tree for the 'false' case
fmtFormatting info
Return values
ptrNew Condition Expando Node

Definition at line 109 of file node_condition.c.

113{
114 ASSERT(node_cond);
115
116 struct ExpandoNode *node = node_new();
117
118 node->type = ENT_CONDITION;
120
121 ARRAY_SET(&node->children, ENC_CONDITION, node_cond);
122 ARRAY_SET(&node->children, ENC_TRUE, node_true);
123 ARRAY_SET(&node->children, ENC_FALSE, node_false);
124
125 node->format = fmt;
126
127 return node;
128}
#define ARRAY_SET(head, idx, elem)
Set an element in the array.
Definition: array.h:123
static int node_condition_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render a Conditional Node - Implements ExpandoNode::render() -.
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
@ ENT_CONDITION
True/False condition.
Definition: node.h:41
@ ENC_CONDITION
Index of Condition Node.
@ ENC_FALSE
Index of False Node.
@ ENC_TRUE
Index of True Node.
#define ASSERT(COND)
Definition: signal2.h:58
Basic Expando Node.
Definition: node.h:67
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:91
struct ExpandoFormat * format
Formatting info.
Definition: node.h:72
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68
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:

◆ node_condition_parse()

struct ExpandoNode * node_condition_parse ( const char *  str,
NodeTextTermFlags  term_chars,
const struct ExpandoDefinition defs,
const char **  parsed_until,
struct ExpandoParseError err 
)

Parse a conditional Expando.

Parameters
[in]strString to parse
[in]term_charsTerminator characters, e.g. NTE_GREATER
[in]defsExpando definitions
[out]parsed_untilFirst character after parsed string
[out]errBuffer for errors
Return values
ptrExpando Node

Definition at line 139 of file node_condition.c.

143{
144 if (!str || (str[0] != '%'))
145 return NULL;
146
147 str++; // Skip %
148
149 struct ExpandoFormat *fmt = NULL;
150 struct ExpandoNode *node_cond = NULL;
151 struct ExpandoNode *node_true = NULL;
152 struct ExpandoNode *node_false = NULL;
153
154 //----------------------------------------------------------------------------
155 // Parse the format (optional)
156 fmt = parse_format(str, parsed_until, err);
157 if (err->position)
158 goto fail;
159
160 str = *parsed_until;
161
162 if ((str[0] != '<') && (str[0] != '?'))
163 goto fail;
164
165 const bool old_style = (str[0] == '?'); // %?X?...&...?
166 str++;
167
168 //----------------------------------------------------------------------------
169 // Parse the condition
170 node_cond = parse_short_name(str, defs, EP_CONDITIONAL, NULL, parsed_until, err);
171 if (!node_cond)
172 goto fail;
173
174 if (node_cond->type == ENT_EXPANDO)
175 {
176 node_cond->type = ENT_CONDBOOL;
177 node_cond->render = node_condbool_render;
178 }
179
180 str = *parsed_until; // Skip the expando
181 if (str[0] != '?')
182 {
183 err->position = str;
184 snprintf(err->message, sizeof(err->message),
185 // L10N: Expando is missing a terminator character
186 // e.g. "%[..." is missing the final ']'
187 _("Conditional expando is missing '%c'"), '?');
188 goto fail;
189 }
190 str++; // Skip the '?'
191
192 //----------------------------------------------------------------------------
193 // Parse the 'true' clause (optional)
194 const NodeTextTermFlags term_true = term_chars | NTE_AMPERSAND |
195 (old_style ? NTE_QUESTION : NTE_GREATER);
196
197 node_true = node_container_new();
198 node_parse_many(node_true, str, term_true, defs, parsed_until, err);
199 if (err->position)
200 goto fail;
201
202 str = *parsed_until;
203
204 //----------------------------------------------------------------------------
205 // Parse the 'false' clause (optional)
206
207 node_false = NULL;
208 if (str[0] == '&')
209 {
210 str++;
211 const NodeTextTermFlags term_false = term_chars | (old_style ? NTE_QUESTION : NTE_GREATER);
212
213 node_false = node_container_new();
214 node_parse_many(node_false, str, term_false, defs, parsed_until, err);
215 if (err->position)
216 goto fail;
217
218 str = *parsed_until;
219 }
220
221 //----------------------------------------------------------------------------
222 // Check for the terminator character
223 const char terminator = old_style ? '?' : '>';
224
225 if (str[0] != terminator)
226 {
227 err->position = str;
228 snprintf(err->message, sizeof(err->message),
229 // L10N: Expando is missing a terminator character
230 // e.g. "%[..." is missing the final ']'
231 _("Conditional expando is missing '%c'"), '?');
232 goto fail;
233 }
234
235 *parsed_until = str + 1;
236
237 return node_condition_new(node_cond, node_true, node_false, fmt);
238
239fail:
240 FREE(&fmt);
241 node_free(&node_cond);
242 node_free(&node_true);
243 node_free(&node_false);
244 return NULL;
245}
#define EP_CONDITIONAL
Expando is being used as a condition.
Definition: definition.h:35
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
int node_condbool_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Callback for every bool node - Implements ExpandoNode::render() -.
Definition: node_condbool.c:41
#define FREE(x)
Definition: memory.h:55
#define _(a)
Definition: message.h:28
void node_free(struct ExpandoNode **ptr)
Free an ExpandoNode and its private data.
Definition: node.c:48
@ ENT_EXPANDO
Expando, e.g. 'n'.
Definition: node.h:39
@ ENT_CONDBOOL
True/False boolean condition.
Definition: node.h:42
struct ExpandoNode * node_condition_new(struct ExpandoNode *node_cond, struct ExpandoNode *node_true, struct ExpandoNode *node_false, struct ExpandoFormat *fmt)
Create a new Condition Expando Node.
struct ExpandoNode * node_container_new(void)
Create a new Container ExpandoNode.
struct ExpandoNode * parse_short_name(const char *str, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, struct ExpandoFormat *fmt, const char **parsed_until, struct ExpandoParseError *err)
Create an expando by its short name.
Definition: node_expando.c:245
struct ExpandoFormat * parse_format(const char *str, const char **parsed_until, struct ExpandoParseError *err)
Parse a format string.
Definition: node_expando.c:136
#define NTE_QUESTION
'?' Question mark
Definition: node_text.h:36
#define NTE_GREATER
'>' Greater than
Definition: node_text.h:35
uint8_t NodeTextTermFlags
Special characters that end a text string.
Definition: node_text.h:32
#define NTE_AMPERSAND
'&' Ampersand
Definition: node_text.h:34
Formatting information for an Expando.
Definition: node.h:53
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: