2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
17 #include <lib-lib/mem.h>
23 #include "lib/debug.h"
26 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
27 * just initializes. Frees anything already in the buffer.
29 * Disregards the 'destroy' flag, which seems reserved for caller.
30 * This is bad, but there's no apparent protocol for it.
32 BUFFER *mutt_buffer_init(BUFFER *b)
42 memset (b, 0, sizeof (BUFFER));
47 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
48 * just initializes. Frees anything already in the buffer. Copies in
51 * Disregards the 'destroy' flag, which seems reserved for caller.
52 * This is bad, but there's no apparent protocol for it.
54 BUFFER *mutt_buffer_from (BUFFER * b, const char *seed)
59 b = mutt_buffer_init (b);
60 b->data = str_dup (seed);
61 b->dsize = str_len (seed);
62 b->dptr = (char *) b->data + b->dsize;
66 void mutt_buffer_addstr (BUFFER * buf, const char *s)
68 mutt_buffer_add (buf, s, str_len (s));
71 void mutt_buffer_addch (BUFFER * buf, char c)
73 mutt_buffer_add (buf, &c, 1);
76 void mutt_buffer_free (BUFFER ** p)
81 p_delete(&(*p)->data);
82 /* dptr is just an offset to data and shouldn't be freed */
86 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
87 * Always one byte bigger than necessary for the null terminator, and
88 * the buffer is always null-terminated */
89 void mutt_buffer_add (BUFFER * buf, const char *s, size_t len)
93 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
94 offset = buf->dptr - buf->data;
95 buf->dsize += len < 128 ? 128 : len + 1;
96 mem_realloc ((void **) &buf->data, buf->dsize);
97 buf->dptr = buf->data + offset;
99 memcpy (buf->dptr, s, len);
104 int mutt_extract_token (BUFFER * dest, BUFFER * tok, int flags)
107 char qc = 0; /* quote char */
110 /* reset the destination pointer to the beginning of the buffer */
111 dest->dptr = dest->data;
114 while ((ch = *tok->dptr)) {
116 if ((ISSPACE (ch) && !(flags & M_TOKEN_SPACE)) ||
117 (ch == '#' && !(flags & M_TOKEN_COMMENT)) ||
118 (ch == '=' && (flags & M_TOKEN_EQUAL)) ||
119 (ch == ';' && !(flags & M_TOKEN_SEMICOLON)) ||
120 ((flags & M_TOKEN_PATTERN) && strchr ("~=!|", ch)))
127 qc = 0; /* end of quote */
128 else if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE))
130 else if (ch == '\\' && qc != '\'') {
132 return -1; /* premature end of token */
133 switch (ch = *tok->dptr++) {
137 return -1; /* premature end of token */
138 mutt_buffer_addch (dest, (toupper ((unsigned char) *tok->dptr)
143 mutt_buffer_addch (dest, '\r');
146 mutt_buffer_addch (dest, '\n');
149 mutt_buffer_addch (dest, '\t');
152 mutt_buffer_addch (dest, '\f');
155 mutt_buffer_addch (dest, '\033');
158 if (isdigit ((unsigned char) ch) &&
159 isdigit ((unsigned char) *tok->dptr) &&
160 isdigit ((unsigned char) *(tok->dptr + 1))) {
162 mutt_buffer_addch (dest,
163 (ch << 6) + (*tok->dptr << 3) + *(tok->dptr +
168 mutt_buffer_addch (dest, ch);
171 else if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
173 return -1; /* premature end of token */
176 mutt_buffer_addch (dest, ch);
178 mutt_buffer_addch (dest, '\033');
179 else if (isalpha ((unsigned char) ch))
180 mutt_buffer_addch (dest, toupper ((unsigned char) ch) - '@');
182 mutt_buffer_addch (dest, '^');
183 mutt_buffer_addch (dest, ch);
186 else if (ch == '`' && (!qc || qc == '"')) {
196 if ((pc = strpbrk (pc, "\\`"))) {
197 /* skip any quoted chars */
201 } while (pc && *pc != '`');
203 debug_print (1, ("mismatched backtics\n"));
206 cmd = str_substrdup (tok->dptr, pc);
207 if ((pid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
208 debug_print (1, ("unable to fork command: %s\n", cmd));
217 memset (&expn, 0, sizeof (expn));
218 expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line);
220 mutt_wait_filter (pid);
222 /* if we got output, make a new string consiting of the shell ouptput
223 plus whatever else was left on the original line */
224 /* BUT: If this is inside a quoted string, directly add output to
226 if (expn.data && qc) {
227 mutt_buffer_addstr (dest, expn.data);
228 p_delete(&expn.data);
230 else if (expn.data) {
231 expnlen = str_len (expn.data);
232 tok->dsize = expnlen + str_len (tok->dptr) + 1;
233 ptr = xmalloc(tok->dsize);
234 memcpy (ptr, expn.data, expnlen);
235 strcpy (ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
237 p_delete(&tok->data);
240 tok->destroy = 1; /* mark that the caller should destroy this data */
242 p_delete(&expn.data);
245 else if (ch == '$' && (!qc || qc == '"')
246 && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr))) {
247 char *env = NULL, *var = NULL;
249 if (*tok->dptr == '{') {
251 if ((pc = strchr (tok->dptr, '}'))) {
252 var = str_substrdup (tok->dptr, pc);
257 for (pc = tok->dptr; isalnum ((unsigned char) *pc) || *pc == '_';
259 var = str_substrdup (tok->dptr, pc);
264 if ((env = getenv (var)) ||
265 (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
266 mutt_buffer_addstr (dest, env);
271 mutt_buffer_addch (dest, ch);
273 mutt_buffer_addch (dest, 0); /* terminate the string */