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

Text parser. More...

#include <ctype.h>
#include <stdint.h>
+ Include dependency graph for extract.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MoreArgs(buf)   (*(buf)->dptr && (*(buf)->dptr != ';') && (*(buf)->dptr != '#'))
 
#define MoreArgsF(buf, flags)
 
#define TOKEN_NO_FLAGS   0
 No flags are set.
 
#define TOKEN_EQUAL   (1 << 0)
 Treat '=' as a special.
 
#define TOKEN_CONDENSE   (1 << 1)
 ^(char) to control chars (macros)
 
#define TOKEN_SPACE   (1 << 2)
 Don't treat whitespace as a term.
 
#define TOKEN_QUOTE   (1 << 3)
 Don't interpret quotes.
 
#define TOKEN_PATTERN   (1 << 4)
 ~%=!| are terms (for patterns)
 
#define TOKEN_COMMENT   (1 << 5)
 Don't reap comments.
 
#define TOKEN_SEMICOLON   (1 << 6)
 Don't treat ; as special.
 
#define TOKEN_BACKTICK_VARS   (1 << 7)
 Expand variables within backticks.
 
#define TOKEN_NOSHELL   (1 << 8)
 Don't expand environment variables.
 
#define TOKEN_QUESTION   (1 << 9)
 Treat '?' as a special.
 
#define TOKEN_PLUS   (1 << 10)
 Treat '+' as a special.
 
#define TOKEN_MINUS   (1 << 11)
 Treat '-' as a special.
 

Typedefs

typedef uint16_t TokenFlags
 Flags for parse_extract_token(), e.g. TOKEN_EQUAL.
 

Functions

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

Detailed Description

Text parser.

Authors
  • Richard Russon
  • Rayford Shireman

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.h.

Macro Definition Documentation

◆ MoreArgs

#define MoreArgs (   buf)    (*(buf)->dptr && (*(buf)->dptr != ';') && (*(buf)->dptr != '#'))

Definition at line 32 of file extract.h.

◆ MoreArgsF

#define MoreArgsF (   buf,
  flags 
)
Value:
(*(buf)->dptr && \
(!isspace(*(buf)->dptr) || ((flags) & TOKEN_SPACE)) && \
((*(buf)->dptr != '#') || ((flags) & TOKEN_COMMENT)) && \
((*(buf)->dptr != '+') || !((flags) & TOKEN_PLUS)) && \
((*(buf)->dptr != '-') || !((flags) & TOKEN_MINUS)) && \
((*(buf)->dptr != '=') || !((flags) & TOKEN_EQUAL)) && \
((*(buf)->dptr != '?') || !((flags) & TOKEN_QUESTION)) && \
((*(buf)->dptr != ';') || ((flags) & TOKEN_SEMICOLON)) && \
(!((flags) & TOKEN_PATTERN) || strchr("~%=!|", *(buf)->dptr)))
#define TOKEN_SPACE
Don't treat whitespace as a term.
Definition: extract.h:49
#define TOKEN_EQUAL
Treat '=' as a special.
Definition: extract.h:47
#define TOKEN_PLUS
Treat '+' as a special.
Definition: extract.h:57
#define TOKEN_COMMENT
Don't reap comments.
Definition: extract.h:52
#define TOKEN_MINUS
Treat '-' as a special.
Definition: extract.h:58
#define TOKEN_PATTERN
~%=!| are terms (for patterns)
Definition: extract.h:51
#define TOKEN_SEMICOLON
Don't treat ; as special.
Definition: extract.h:53
#define TOKEN_QUESTION
Treat '?' as a special.
Definition: extract.h:56

Definition at line 35 of file extract.h.

◆ TOKEN_NO_FLAGS

#define TOKEN_NO_FLAGS   0

No flags are set.

Definition at line 46 of file extract.h.

◆ TOKEN_EQUAL

#define TOKEN_EQUAL   (1 << 0)

Treat '=' as a special.

Definition at line 47 of file extract.h.

◆ TOKEN_CONDENSE

#define TOKEN_CONDENSE   (1 << 1)

^(char) to control chars (macros)

Definition at line 48 of file extract.h.

◆ TOKEN_SPACE

#define TOKEN_SPACE   (1 << 2)

Don't treat whitespace as a term.

Definition at line 49 of file extract.h.

◆ TOKEN_QUOTE

#define TOKEN_QUOTE   (1 << 3)

Don't interpret quotes.

Definition at line 50 of file extract.h.

◆ TOKEN_PATTERN

#define TOKEN_PATTERN   (1 << 4)

~%=!| are terms (for patterns)

Definition at line 51 of file extract.h.

◆ TOKEN_COMMENT

#define TOKEN_COMMENT   (1 << 5)

Don't reap comments.

Definition at line 52 of file extract.h.

◆ TOKEN_SEMICOLON

#define TOKEN_SEMICOLON   (1 << 6)

Don't treat ; as special.

Definition at line 53 of file extract.h.

◆ TOKEN_BACKTICK_VARS

#define TOKEN_BACKTICK_VARS   (1 << 7)

Expand variables within backticks.

Definition at line 54 of file extract.h.

◆ TOKEN_NOSHELL

#define TOKEN_NOSHELL   (1 << 8)

Don't expand environment variables.

Definition at line 55 of file extract.h.

◆ TOKEN_QUESTION

#define TOKEN_QUESTION   (1 << 9)

Treat '?' as a special.

Definition at line 56 of file extract.h.

◆ TOKEN_PLUS

#define TOKEN_PLUS   (1 << 10)

Treat '+' as a special.

Definition at line 57 of file extract.h.

◆ TOKEN_MINUS

#define TOKEN_MINUS   (1 << 11)

Treat '-' as a special.

Definition at line 58 of file extract.h.

Typedef Documentation

◆ TokenFlags

typedef uint16_t TokenFlags

Flags for parse_extract_token(), e.g. TOKEN_EQUAL.

Definition at line 45 of file extract.h.

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 50 of file extract.c.

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