NeoMutt  2025-09-05-2-g4bf191
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
extract.c File Reference

Text parser. More...

#include "config.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "extract.h"
+ Include dependency graph for extract.c:

Go to the source code of this file.

Functions

int parse_extract_token (struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
 Extract one token from a string.
 

Detailed Description

Text parser.

Authors
  • Naveen Nathan
  • Richard Russon
  • Pietro Cerutti

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

Function Documentation

◆ parse_extract_token()

int parse_extract_token ( struct Buffer dest,
struct Buffer tok,
TokenFlags  flags 
)

Extract one token from a string.

Parameters
destBuffer for the result
tokBuffer containing tokens
flagsFlags, see TokenFlags
Return values
0Success
-1Error

Definition at line 48 of file extract.c.

49{
50 if (!dest || !tok)
51 return -1;
52
53 char ch;
54 char qc = '\0'; /* quote char */
55 char *pc = NULL;
56
57 buf_reset(dest);
58
59 SKIPWS(tok->dptr);
60 while ((ch = *tok->dptr))
61 {
62 if (qc == '\0')
63 {
64 if (mutt_isspace(ch) && !(flags & TOKEN_SPACE))
65 break;
66 if ((ch == '#') && !(flags & TOKEN_COMMENT))
67 break;
68 if ((ch == '+') && (flags & TOKEN_PLUS))
69 break;
70 if ((ch == '-') && (flags & TOKEN_MINUS))
71 break;
72 if ((ch == '=') && (flags & TOKEN_EQUAL))
73 break;
74 if ((ch == '?') && (flags & TOKEN_QUESTION))
75 break;
76 if ((ch == ';') && !(flags & TOKEN_SEMICOLON))
77 break;
78 if ((flags & TOKEN_PATTERN) && strchr("~%=!|", ch))
79 break;
80 }
81
82 tok->dptr++;
83
84 if (ch == qc)
85 {
86 qc = 0; /* end of quote */
87 }
88 else if (!qc && ((ch == '\'') || (ch == '"')) && !(flags & TOKEN_QUOTE))
89 {
90 qc = ch;
91 }
92 else if ((ch == '\\') && (qc != '\''))
93 {
94 if (tok->dptr[0] == '\0')
95 return -1; /* premature end of token */
96 switch (ch = *tok->dptr++)
97 {
98 case 'c':
99 case 'C':
100 if (tok->dptr[0] == '\0')
101 return -1; /* premature end of token */
102 buf_addch(dest, (mutt_toupper(tok->dptr[0]) - '@') & 0x7f);
103 tok->dptr++;
104 break;
105 case 'e':
106 buf_addch(dest, '\033'); // Escape
107 break;
108 case 'f':
109 buf_addch(dest, '\f');
110 break;
111 case 'n':
112 buf_addch(dest, '\n');
113 break;
114 case 'r':
115 buf_addch(dest, '\r');
116 break;
117 case 't':
118 buf_addch(dest, '\t');
119 break;
120 default:
121 if (mutt_isdigit(ch) && mutt_isdigit(tok->dptr[0]) && mutt_isdigit(tok->dptr[1]))
122 {
123 buf_addch(dest, (ch << 6) + (tok->dptr[0] << 3) + tok->dptr[1] - 3504);
124 tok->dptr += 2;
125 }
126 else
127 {
128 buf_addch(dest, ch);
129 }
130 }
131 }
132 else if ((ch == '^') && (flags & TOKEN_CONDENSE))
133 {
134 if (tok->dptr[0] == '\0')
135 return -1; /* premature end of token */
136 ch = *tok->dptr++;
137 if (ch == '^')
138 {
139 buf_addch(dest, ch);
140 }
141 else if (ch == '[')
142 {
143 buf_addch(dest, '\033'); // Escape
144 }
145 else if (mutt_isalpha(ch))
146 {
147 buf_addch(dest, mutt_toupper(ch) - '@');
148 }
149 else
150 {
151 buf_addch(dest, '^');
152 buf_addch(dest, ch);
153 }
154 }
155 else if ((ch == '`') && (!qc || (qc == '"')))
156 {
157 FILE *fp = NULL;
158 pid_t pid;
159
160 pc = tok->dptr;
161 do
162 {
163 pc = strpbrk(pc, "\\`");
164 if (pc)
165 {
166 /* skip any quoted chars */
167 if (*pc == '\\')
168 {
169 if (*(pc + 1))
170 pc += 2;
171 else
172 pc = NULL;
173 }
174 }
175 } while (pc && (pc[0] != '`'));
176 if (!pc)
177 {
178 mutt_debug(LL_DEBUG1, "mismatched backticks\n");
179 return -1;
180 }
181 struct Buffer *cmd = buf_pool_get();
182 *pc = '\0';
183 if (flags & TOKEN_BACKTICK_VARS)
184 {
185 /* recursively extract tokens to interpolate variables */
186 parse_extract_token(cmd, tok,
189 }
190 else
191 {
192 buf_strcpy(cmd, tok->dptr);
193 }
194 *pc = '`';
195 pid = filter_create(buf_string(cmd), NULL, &fp, NULL, NeoMutt->env);
196 if (pid < 0)
197 {
198 mutt_debug(LL_DEBUG1, "unable to fork command: %s\n", buf_string(cmd));
199 buf_pool_release(&cmd);
200 return -1;
201 }
202
203 tok->dptr = pc + 1;
204
205 /* read line */
206 char *expn = NULL;
207 size_t expn_len = 0;
208 expn = mutt_file_read_line(expn, &expn_len, fp, NULL, MUTT_RL_NO_FLAGS);
209 mutt_file_fclose(&fp);
210 int rc = filter_wait(pid);
211 if (rc != 0)
212 {
213 mutt_debug(LL_DEBUG1, "backticks exited code %d for command: %s\n", rc,
214 buf_string(cmd));
215 }
216 buf_pool_release(&cmd);
217
218 /* if we got output, make a new string consisting of the shell output
219 * plus whatever else was left on the original line */
220 /* BUT: If this is inside a quoted string, directly add output to
221 * the token */
222 if (expn)
223 {
224 if (qc)
225 {
226 buf_addstr(dest, expn);
227 }
228 else
229 {
230 struct Buffer *copy = buf_pool_get();
231 buf_strcpy(copy, expn);
232 buf_addstr(copy, tok->dptr);
233 buf_copy(tok, copy);
234 buf_seek(tok, 0);
235 buf_pool_release(&copy);
236 }
237 FREE(&expn);
238 }
239 }
240 else if ((ch == '$') && (!qc || (qc == '"')) &&
241 ((tok->dptr[0] == '{') || mutt_isalpha(tok->dptr[0])))
242 {
243 const char *env = NULL;
244 char *var = NULL;
245
246 if (tok->dptr[0] == '{')
247 {
248 pc = strchr(tok->dptr, '}');
249 if (pc)
250 {
251 var = mutt_strn_dup(tok->dptr + 1, pc - (tok->dptr + 1));
252 tok->dptr = pc + 1;
253
254 if ((flags & TOKEN_NOSHELL))
255 {
256 buf_addch(dest, ch);
257 buf_addch(dest, '{');
258 buf_addstr(dest, var);
259 buf_addch(dest, '}');
260 FREE(&var);
261 }
262 }
263 }
264 else
265 {
266 for (pc = tok->dptr; mutt_isalnum(*pc) || (pc[0] == '_'); pc++)
267 ; // do nothing
268
269 var = mutt_strn_dup(tok->dptr, pc - tok->dptr);
270 tok->dptr = pc;
271 }
272 if (var)
273 {
274 struct Buffer *result = buf_pool_get();
275 int rc = cs_subset_str_string_get(NeoMutt->sub, var, result);
276
277 if (CSR_RESULT(rc) == CSR_SUCCESS)
278 {
279 buf_addstr(dest, buf_string(result));
280 }
281 else if (!(flags & TOKEN_NOSHELL) && (env = mutt_str_getenv(var)))
282 {
283 buf_addstr(dest, env);
284 }
285 else
286 {
287 buf_addch(dest, ch);
288 buf_addstr(dest, var);
289 }
290 FREE(&var);
291 buf_pool_release(&result);
292 }
293 }
294 else
295 {
296 buf_addch(dest, ch);
297 }
298 }
299
300 SKIPWS(tok->dptr);
301 return 0;
302}
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:622
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:601
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
#define CSR_RESULT(x)
Definition: set.h:50
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:33
bool mutt_isspace(int arg)
Wrapper for isspace(3)
Definition: ctype.c:95
bool mutt_isalpha(int arg)
Wrapper for isalpha(3)
Definition: ctype.c:52
int mutt_toupper(int arg)
Wrapper for toupper(3)
Definition: ctype.c:139
bool mutt_isalnum(int arg)
Wrapper for isalnum(3)
Definition: ctype.c:39
bool mutt_isdigit(int arg)
Wrapper for isdigit(3)
Definition: ctype.c:65
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:48
#define TOKEN_BACKTICK_VARS
Expand variables within backticks.
Definition: extract.h:52
#define TOKEN_SPACE
Don't treat whitespace as a term.
Definition: extract.h:47
#define TOKEN_QUOTE
Don't interpret quotes.
Definition: extract.h:48
#define TOKEN_NOSHELL
Don't expand environment variables.
Definition: extract.h:53
#define TOKEN_EQUAL
Treat '=' as a special.
Definition: extract.h:45
#define TOKEN_CONDENSE
^(char) to control chars (macros)
Definition: extract.h:46
#define TOKEN_PLUS
Treat '+' as a special.
Definition: extract.h:55
#define TOKEN_COMMENT
Don't reap comments.
Definition: extract.h:50
#define TOKEN_MINUS
Treat '-' as a special.
Definition: extract.h:56
#define TOKEN_PATTERN
~%=!| are terms (for patterns)
Definition: extract.h:49
#define TOKEN_SEMICOLON
Don't treat ; as special.
Definition: extract.h:51
#define TOKEN_QUESTION
Treat '?' as a special.
Definition: extract.h:54
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition: file.c:685
#define mutt_file_fclose(FP)
Definition: file.h:139
#define MUTT_RL_NO_FLAGS
No flags are set.
Definition: file.h:40
#define mutt_debug(LEVEL,...)
Definition: logging2.h:90
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:44
#define FREE(x)
Definition: memory.h:62
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:220
pid_t filter_create(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, char **envlist)
Set up filter program.
Definition: filter.c:209
char * mutt_strn_dup(const char *begin, size_t len)
Duplicate a sub-string.
Definition: string.c:381
const char * mutt_str_getenv(const char *name)
Get an environment variable.
Definition: string.c:725
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
#define SKIPWS(ch)
Definition: string2.h:44
String manipulation buffer.
Definition: buffer.h:36
char * dptr
Current read/write position.
Definition: buffer.h:38
Container for Accounts, Notifications.
Definition: neomutt.h:43
char ** env
Private copy of the environment variables.
Definition: neomutt.h:55
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:47
int cs_subset_str_string_get(const struct ConfigSubset *sub, const char *name, struct Buffer *result)
Get a config item as a string.
Definition: subset.c:350
+ Here is the call graph for this function: