NeoMutt  2024-04-25-1-g3de005
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_expando.c File Reference

Expando Node for an Expando. More...

#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "node_expando.h"
#include "color/lib.h"
#include "definition.h"
#include "format.h"
#include "helpers.h"
#include "mutt_thread.h"
#include "node.h"
#include "parse.h"
#include "render.h"
+ Include dependency graph for node_expando.c:

Go to the source code of this file.

Functions

struct NodeExpandoPrivatenode_expando_private_new (void)
 Create new Expando private data.
 
void node_expando_private_free (void **ptr)
 Free Expando private data - Implements ExpandoNode::ndata_free()
 
struct ExpandoNodenode_expando_new (const char *start, const char *end, struct ExpandoFormat *fmt, int did, int uid)
 Create a new Expando ExpandoNode.
 
void node_expando_set_color (const struct ExpandoNode *node, int cid)
 Set the colour for an Expando.
 
void node_expando_set_has_tree (const struct ExpandoNode *node, bool has_tree)
 Set the has_tree flag for an Expando.
 
struct ExpandoFormatparse_format (const char *start, const char *end, struct ExpandoParseError *error)
 Parse a format string.
 
struct ExpandoNodenode_expando_parse (const char *str, const char **parsed_until, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, struct ExpandoParseError *error)
 Parse an Expando format string.
 
struct ExpandoNodenode_expando_parse_enclosure (const char *str, const char **parsed_until, int did, int uid, char terminator, struct ExpandoParseError *error)
 Parse an enclosed Expando.
 
void add_color (struct Buffer *buf, enum ColorId cid)
 Add a colour code to a buffer.
 
int node_expando_render (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render an Expando Node - Implements ExpandoNode::render() -.
 

Detailed Description

Expando Node for an 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 node_expando.c.

Function Documentation

◆ node_expando_private_new()

struct NodeExpandoPrivate * node_expando_private_new ( void  )

Create new Expando private data.

Return values
ptrNew Expando private data

Definition at line 50 of file node_expando.c.

51{
52 struct NodeExpandoPrivate *priv = mutt_mem_calloc(1, sizeof(struct NodeExpandoPrivate));
53
54 // NOTE(g0mb4): Expando definition should contain this
55 priv->color = -1;
56
57 return priv;
58}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
Private data for an Expando.
Definition: node_expando.h:40
int color
ColorId to use.
Definition: node_expando.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_expando_private_free()

void node_expando_private_free ( void **  ptr)

Free Expando private data - Implements ExpandoNode::ndata_free()

Parameters
ptrData to free

Definition at line 64 of file node_expando.c.

65{
66 if (!ptr || !*ptr)
67 return;
68
69 FREE(ptr);
70}
#define FREE(x)
Definition: memory.h:45
+ Here is the caller graph for this function:

◆ node_expando_new()

struct ExpandoNode * node_expando_new ( const char *  start,
const char *  end,
struct ExpandoFormat fmt,
int  did,
int  uid 
)

Create a new Expando ExpandoNode.

Parameters
startStart of Expando string
endEnd of Expando string
fmtFormatting data
didDomain ID
uidUnique ID
Return values
ptrNew Expando ExpandoNode

Definition at line 81 of file node_expando.c.

83{
84 struct ExpandoNode *node = node_new();
85
86 node->type = ENT_EXPANDO;
87 node->start = start;
88 node->end = end;
89
90 node->did = did;
91 node->uid = uid;
93
94 node->format = fmt;
95
98
99 return node;
100}
int node_expando_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render an Expando Node - Implements ExpandoNode::render() -.
Definition: node_expando.c:345
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
@ ENT_EXPANDO
Expando, e.g. 'n'.
Definition: node.h:39
void node_expando_private_free(void **ptr)
Free Expando private data - Implements ExpandoNode::ndata_free()
Definition: node_expando.c:64
struct NodeExpandoPrivate * node_expando_private_new(void)
Create new Expando private data.
Definition: node_expando.c:50
Basic Expando Node.
Definition: node.h:69
int uid
Unique ID, e.g. ED_EMA_SIZE.
Definition: node.h:73
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:96
void * ndata
Private node data.
Definition: node.h:82
struct ExpandoFormat * format
Formatting info.
Definition: node.h:75
const char * end
End of string data.
Definition: node.h:80
int did
Domain ID, e.g. ED_EMAIL.
Definition: node.h:72
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:70
const char * start
Start of string data.
Definition: node.h:79
void(* ndata_free)(void **ptr)
Function to free the private node data.
Definition: node.h:83
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_expando_set_color()

void node_expando_set_color ( const struct ExpandoNode node,
int  cid 
)

Set the colour for an Expando.

Parameters
nodeNode to alter
cidColour

Definition at line 107 of file node_expando.c.

108{
109 if (!node || (node->type != ENT_EXPANDO) || !node->ndata)
110 return;
111
112 struct NodeExpandoPrivate *priv = node->ndata;
113
114 priv->color = cid;
115}
+ Here is the caller graph for this function:

◆ node_expando_set_has_tree()

void node_expando_set_has_tree ( const struct ExpandoNode node,
bool  has_tree 
)

Set the has_tree flag for an Expando.

Parameters
nodeNode to alter
has_treeFlag to set

Definition at line 122 of file node_expando.c.

123{
124 if (!node || (node->type != ENT_EXPANDO) || !node->ndata)
125 return;
126
127 struct NodeExpandoPrivate *priv = node->ndata;
128
129 priv->has_tree = has_tree;
130}
bool has_tree
Contains tree characters, used in $index_format's s.
Definition: node_expando.h:42
+ Here is the caller graph for this function:

◆ parse_format()

struct ExpandoFormat * parse_format ( const char *  start,
const char *  end,
struct ExpandoParseError error 
)

Parse a format string.

Parameters
startStart of string
endEnd of string
errorBuffer for errors
Return values
ptrNew ExpandoFormat object

Parse a printf()-style format, e.g. '-15.20x'

Definition at line 141 of file node_expando.c.

143{
144 if (start == end)
145 return NULL;
146
147 struct ExpandoFormat *fmt = mutt_mem_calloc(1, sizeof(struct ExpandoFormat));
148
149 fmt->leader = ' ';
150 fmt->start = start;
151 fmt->end = end;
153 fmt->min_cols = 0;
154 fmt->max_cols = INT_MAX;
155
156 if (*start == '-')
157 {
159 start++;
160 }
161 else if (*start == '=')
162 {
164 start++;
165 }
166
167 if (*start == '0')
168 {
169 fmt->leader = '0';
170 start++;
171 }
172
173 if (isdigit(*start))
174 {
175 unsigned short number = 0;
176 const char *end_ptr = mutt_str_atous(start, &number);
177
178 // NOTE(g0mb4): start is NOT null-terminated
179 if (!end_ptr || (end_ptr > end) || (number == USHRT_MAX))
180 {
181 error->position = start;
182 snprintf(error->message, sizeof(error->message), _("Invalid number: %s"), start);
183 FREE(&fmt);
184 return NULL;
185 }
186
187 fmt->min_cols = number;
188 start = end_ptr;
189 };
190
191 if (*start == '.')
192 {
193 start++;
194
195 if (!isdigit(*start))
196 {
197 error->position = start;
198 // L10N: Expando format expected a number
199 snprintf(error->message, sizeof(error->message), _("Number is expected"));
200 FREE(&fmt);
201 return NULL;
202 }
203
204 unsigned short number = 0;
205 const char *end_ptr = mutt_str_atous(start, &number);
206
207 // NOTE(g0mb4): start is NOT null-terminated
208 if (!end_ptr || (end_ptr > end) || (number == USHRT_MAX))
209 {
210 error->position = start;
211 snprintf(error->message, sizeof(error->message), _("Invalid number: %s"), start);
212 FREE(&fmt);
213 return NULL;
214 }
215
216 fmt->max_cols = number;
217 start = end_ptr;
218 }
219
220 if (*start == '_')
221 {
222 fmt->lower = true;
223 start++;
224 }
225
226 return fmt;
227}
const char * mutt_str_atous(const char *str, unsigned short *dst)
Convert ASCII string to an unsigned short.
Definition: atoi.c:266
@ JUSTIFY_RIGHT
Right justify the text.
Definition: format.h:36
@ JUSTIFY_LEFT
Left justify the text.
Definition: format.h:34
@ JUSTIFY_CENTER
Centre the text.
Definition: format.h:35
#define _(a)
Definition: message.h:28
Formatting information for an Expando.
Definition: node.h:53
char leader
Leader character, 0 or space.
Definition: node.h:57
enum FormatJustify justification
Justification: left, centre, right.
Definition: node.h:56
const char * start
Start of Expando specifier string.
Definition: node.h:59
int min_cols
Minimum number of screen columns.
Definition: node.h:54
int max_cols
Maximum number of screen columns.
Definition: node.h:55
bool lower
Display in lower case.
Definition: node.h:58
const char * end
End of Expando specifier string.
Definition: node.h:60
char message[128]
Error message.
Definition: parse.h:35
const char * position
Position of error in original string.
Definition: parse.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_expando_parse()

struct ExpandoNode * node_expando_parse ( const char *  str,
const char **  parsed_until,
const struct ExpandoDefinition defs,
ExpandoParserFlags  flags,
struct ExpandoParseError error 
)

Parse an Expando format string.

Parameters
[in]strString to parse
[out]parsed_untilFirst character after parsed string
[in]defsExpando definitions
[in]flagsFlag for conditional expandos
[out]errorBuffer for errors
Return values
ptrNew ExpandoNode

Definition at line 238 of file node_expando.c.

242{
243 const struct ExpandoDefinition *definition = defs;
244
245 const char *format_end = skip_until_classic_expando(str);
246 const char *expando_end = skip_classic_expando(format_end, defs);
247 char expando[128] = { 0 };
248 const int expando_len = expando_end - format_end;
249 mutt_strn_copy(expando, format_end, expando_len, sizeof(expando));
250
251 struct ExpandoFormat *fmt = parse_format(str, format_end, error);
252 if (error->position)
253 {
254 FREE(&fmt);
255 return NULL;
256 }
257
258 while (definition && definition->short_name)
259 {
260 if (mutt_str_equal(definition->short_name, expando))
261 {
262 if (definition->parse && !(flags & EP_NO_CUSTOM_PARSE))
263 {
264 FREE(&fmt);
265 return definition->parse(str, parsed_until, definition->did,
266 definition->uid, flags, error);
267 }
268 else
269 {
270 *parsed_until = expando_end;
271 return node_expando_new(format_end, expando_end, fmt, definition->did,
272 definition->uid);
273 }
274 }
275
276 definition++;
277 }
278
279 error->position = format_end;
280 // L10N: e.g. "Unknown expando: %Q"
281 snprintf(error->message, sizeof(error->message), _("Unknown expando: %%%.*s"),
282 expando_len, format_end);
283 FREE(&fmt);
284 return NULL;
285}
#define EP_NO_CUSTOM_PARSE
Don't use the custom parser.
Definition: definition.h:44
const char * skip_classic_expando(const char *str, const struct ExpandoDefinition *defs)
Skip over the text of an Expando.
Definition: helpers.c:145
const char * skip_until_classic_expando(const char *start)
Search through string until we reach an Expando character.
Definition: helpers.c:129
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:654
char * mutt_strn_copy(char *dest, const char *src, size_t len, size_t dsize)
Copy a sub-string into a buffer.
Definition: string.c:354
struct ExpandoFormat * parse_format(const char *start, const char *end, struct ExpandoParseError *error)
Parse a format string.
Definition: node_expando.c:141
struct ExpandoNode * node_expando_new(const char *start, const char *end, struct ExpandoFormat *fmt, int did, int uid)
Create a new Expando ExpandoNode.
Definition: node_expando.c:81
Definition of a format string.
Definition: definition.h:52
short uid
Unique ID in domain.
Definition: definition.h:56
short did
Domain ID.
Definition: definition.h:55
struct ExpandoNode *(* parse)(const char *str, const char **parsed_until, int did, int uid, ExpandoParserFlags flags, struct ExpandoParseError *error)
Definition: definition.h:70
const char * short_name
Short Expando name, e.g. "n".
Definition: definition.h:53
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_expando_parse_enclosure()

struct ExpandoNode * node_expando_parse_enclosure ( const char *  str,
const char **  parsed_until,
int  did,
int  uid,
char  terminator,
struct ExpandoParseError error 
)

Parse an enclosed Expando.

Parameters
strString to parse
parsed_untilFirst character after the parsed string
didDomain ID
uidUnique ID
terminatorTerminating character
errorBuffer for errors
Return values
ptrNew ExpandoNode

Definition at line 297 of file node_expando.c.

300{
301 const char *format_end = skip_until_classic_expando(str);
302
303 format_end++; // skip opening char
304
305 const char *expando_end = skip_until_ch(format_end, terminator);
306
307 if (*expando_end != terminator)
308 {
309 error->position = expando_end;
310 snprintf(error->message, sizeof(error->message),
311 // L10N: Expando is missing a terminator character
312 // e.g. "%[..." is missing the final ']'
313 _("Expando is missing terminator: '%c'"), terminator);
314 return NULL;
315 }
316
317 // revert skipping for fmt
318 struct ExpandoFormat *fmt = parse_format(str, format_end - 1, error);
319 if (error->position)
320 {
321 FREE(&fmt);
322 return NULL;
323 }
324
325 *parsed_until = expando_end + 1;
326 return node_expando_new(format_end, expando_end, fmt, did, uid);
327}
const char * skip_until_ch(const char *start, char terminator)
Search a string for a terminator character.
Definition: helpers.c:95
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ add_color()

void add_color ( struct Buffer buf,
enum ColorId  cid 
)

Add a colour code to a buffer.

Parameters
bufBuffer for colour code
cidColour

Definition at line 334 of file node_expando.c.

335{
336 assert(cid < MT_COLOR_MAX);
337
339 buf_addch(buf, cid);
340}
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
@ MT_COLOR_MAX
Definition: color.h:94
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition: mutt_thread.h:73
+ Here is the call graph for this function:
+ Here is the caller graph for this function: