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.
21 #include "lib/debug.h"
24 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
25 * just initializes. Frees anything already in the buffer.
27 * Disregards the 'destroy' flag, which seems reserved for caller.
28 * This is bad, but there's no apparent protocol for it.
30 BUFFER *mutt_buffer_init (BUFFER * b)
33 b = mem_malloc (sizeof (BUFFER));
40 memset (b, 0, sizeof (BUFFER));
45 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
46 * just initializes. Frees anything already in the buffer. Copies in
49 * Disregards the 'destroy' flag, which seems reserved for caller.
50 * This is bad, but there's no apparent protocol for it.
52 BUFFER *mutt_buffer_from (BUFFER * b, char *seed)
57 b = mutt_buffer_init (b);
58 b->data = str_dup (seed);
59 b->dsize = str_len (seed);
60 b->dptr = (char *) b->data + b->dsize;
64 void mutt_buffer_addstr (BUFFER * buf, const char *s)
66 mutt_buffer_add (buf, s, str_len (s));
69 void mutt_buffer_addch (BUFFER * buf, char c)
71 mutt_buffer_add (buf, &c, 1);
74 void mutt_buffer_free (BUFFER ** p)
79 mem_free (&(*p)->data);
80 /* dptr is just an offset to data and shouldn't be freed */
84 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
85 * Always one byte bigger than necessary for the null terminator, and
86 * the buffer is always null-terminated */
87 void mutt_buffer_add (BUFFER * buf, const char *s, size_t len)
91 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
92 offset = buf->dptr - buf->data;
93 buf->dsize += len < 128 ? 128 : len + 1;
94 mem_realloc ((void **) &buf->data, buf->dsize);
95 buf->dptr = buf->data + offset;
97 memcpy (buf->dptr, s, len);
102 int mutt_extract_token (BUFFER * dest, BUFFER * tok, int flags)
105 char qc = 0; /* quote char */
108 /* reset the destination pointer to the beginning of the buffer */
109 dest->dptr = dest->data;
112 while ((ch = *tok->dptr)) {
114 if ((ISSPACE (ch) && !(flags & M_TOKEN_SPACE)) ||
115 (ch == '#' && !(flags & M_TOKEN_COMMENT)) ||
116 (ch == '=' && (flags & M_TOKEN_EQUAL)) ||
117 (ch == ';' && !(flags & M_TOKEN_SEMICOLON)) ||
118 ((flags & M_TOKEN_PATTERN) && strchr ("~=!|", ch)))
125 qc = 0; /* end of quote */
126 else if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE))
128 else if (ch == '\\' && qc != '\'') {
130 return -1; /* premature end of token */
131 switch (ch = *tok->dptr++) {
135 return -1; /* premature end of token */
136 mutt_buffer_addch (dest, (toupper ((unsigned char) *tok->dptr)
141 mutt_buffer_addch (dest, '\r');
144 mutt_buffer_addch (dest, '\n');
147 mutt_buffer_addch (dest, '\t');
150 mutt_buffer_addch (dest, '\f');
153 mutt_buffer_addch (dest, '\033');
156 if (isdigit ((unsigned char) ch) &&
157 isdigit ((unsigned char) *tok->dptr) &&
158 isdigit ((unsigned char) *(tok->dptr + 1))) {
160 mutt_buffer_addch (dest,
161 (ch << 6) + (*tok->dptr << 3) + *(tok->dptr +
166 mutt_buffer_addch (dest, ch);
169 else if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
171 return -1; /* premature end of token */
174 mutt_buffer_addch (dest, ch);
176 mutt_buffer_addch (dest, '\033');
177 else if (isalpha ((unsigned char) ch))
178 mutt_buffer_addch (dest, toupper ((unsigned char) ch) - '@');
180 mutt_buffer_addch (dest, '^');
181 mutt_buffer_addch (dest, ch);
184 else if (ch == '`' && (!qc || qc == '"')) {
194 if ((pc = strpbrk (pc, "\\`"))) {
195 /* skip any quoted chars */
199 } while (pc && *pc != '`');
201 debug_print (1, ("mismatched backtics\n"));
204 cmd = str_substrdup (tok->dptr, pc);
205 if ((pid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
206 debug_print (1, ("unable to fork command: %s\n", cmd));
215 memset (&expn, 0, sizeof (expn));
216 expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line);
218 mutt_wait_filter (pid);
220 /* if we got output, make a new string consiting of the shell ouptput
221 plus whatever else was left on the original line */
222 /* BUT: If this is inside a quoted string, directly add output to
224 if (expn.data && qc) {
225 mutt_buffer_addstr (dest, expn.data);
226 mem_free (&expn.data);
228 else if (expn.data) {
229 expnlen = str_len (expn.data);
230 tok->dsize = expnlen + str_len (tok->dptr) + 1;
231 ptr = mem_malloc (tok->dsize);
232 memcpy (ptr, expn.data, expnlen);
233 strcpy (ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
235 mem_free (&tok->data);
238 tok->destroy = 1; /* mark that the caller should destroy this data */
240 mem_free (&expn.data);
243 else if (ch == '$' && (!qc || qc == '"')
244 && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr))) {
245 char *env = NULL, *var = NULL;
247 if (*tok->dptr == '{') {
249 if ((pc = strchr (tok->dptr, '}'))) {
250 var = str_substrdup (tok->dptr, pc);
255 for (pc = tok->dptr; isalnum ((unsigned char) *pc) || *pc == '_';
257 var = str_substrdup (tok->dptr, pc);
262 if ((env = getenv (var)) ||
263 (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
264 mutt_buffer_addstr (dest, env);
269 mutt_buffer_addch (dest, ch);
271 mutt_buffer_addch (dest, 0); /* terminate the string */