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>
18 #include <lib-lib/str.h>
22 #include "lib/debug.h"
25 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
26 * just initializes. Frees anything already in the buffer.
28 * Disregards the 'destroy' flag, which seems reserved for caller.
29 * This is bad, but there's no apparent protocol for it.
31 BUFFER *mutt_buffer_init(BUFFER *b)
41 memset (b, 0, sizeof (BUFFER));
46 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
47 * just initializes. Frees anything already in the buffer. Copies in
50 * Disregards the 'destroy' flag, which seems reserved for caller.
51 * This is bad, but there's no apparent protocol for it.
53 BUFFER *mutt_buffer_from (BUFFER * b, const char *seed)
58 b = mutt_buffer_init (b);
59 b->data = m_strdup(seed);
60 b->dsize = m_strlen(seed);
61 b->dptr = (char *) b->data + b->dsize;
65 void mutt_buffer_addstr (BUFFER * buf, const char *s)
67 mutt_buffer_add (buf, s, m_strlen(s));
70 void mutt_buffer_addch (BUFFER * buf, char c)
72 mutt_buffer_add (buf, &c, 1);
75 void mutt_buffer_free (BUFFER ** p)
80 p_delete(&(*p)->data);
81 /* dptr is just an offset to data and shouldn't be freed */
85 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
86 * Always one byte bigger than necessary for the null terminator, and
87 * the buffer is always null-terminated */
88 void mutt_buffer_add (BUFFER *buf, const char *s, size_t len)
92 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
93 offset = buf->dptr - buf->data;
94 buf->dsize += len < 128 ? 128 : len + 1;
95 p_realloc(&buf->data, buf->dsize);
96 buf->dptr = buf->data + offset;
98 memcpy (buf->dptr, s, len);
103 int mutt_extract_token (BUFFER * dest, BUFFER * tok, int flags)
106 char qc = 0; /* quote char */
109 /* reset the destination pointer to the beginning of the buffer */
110 dest->dptr = dest->data;
113 while ((ch = *tok->dptr)) {
115 if ((ISSPACE (ch) && !(flags & M_TOKEN_SPACE)) ||
116 (ch == '#' && !(flags & M_TOKEN_COMMENT)) ||
117 (ch == '=' && (flags & M_TOKEN_EQUAL)) ||
118 (ch == ';' && !(flags & M_TOKEN_SEMICOLON)) ||
119 ((flags & M_TOKEN_PATTERN) && strchr ("~=!|", ch)))
126 qc = 0; /* end of quote */
127 else if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE))
129 else if (ch == '\\' && qc != '\'') {
131 return -1; /* premature end of token */
132 switch (ch = *tok->dptr++) {
136 return -1; /* premature end of token */
137 mutt_buffer_addch (dest, (toupper ((unsigned char) *tok->dptr)
142 mutt_buffer_addch (dest, '\r');
145 mutt_buffer_addch (dest, '\n');
148 mutt_buffer_addch (dest, '\t');
151 mutt_buffer_addch (dest, '\f');
154 mutt_buffer_addch (dest, '\033');
157 if (isdigit ((unsigned char) ch) &&
158 isdigit ((unsigned char) *tok->dptr) &&
159 isdigit ((unsigned char) *(tok->dptr + 1))) {
161 mutt_buffer_addch (dest,
162 (ch << 6) + (*tok->dptr << 3) + *(tok->dptr +
167 mutt_buffer_addch (dest, ch);
170 else if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
172 return -1; /* premature end of token */
175 mutt_buffer_addch (dest, ch);
177 mutt_buffer_addch (dest, '\033');
178 else if (isalpha ((unsigned char) ch))
179 mutt_buffer_addch (dest, toupper ((unsigned char) ch) - '@');
181 mutt_buffer_addch (dest, '^');
182 mutt_buffer_addch (dest, ch);
185 else if (ch == '`' && (!qc || qc == '"')) {
195 if ((pc = strpbrk (pc, "\\`"))) {
196 /* skip any quoted chars */
200 } while (pc && *pc != '`');
202 debug_print (1, ("mismatched backtics\n"));
205 cmd = str_substrdup (tok->dptr, pc);
206 if ((pid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
207 debug_print (1, ("unable to fork command: %s\n", cmd));
216 memset (&expn, 0, sizeof (expn));
217 expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line);
219 mutt_wait_filter (pid);
221 /* if we got output, make a new string consiting of the shell ouptput
222 plus whatever else was left on the original line */
223 /* BUT: If this is inside a quoted string, directly add output to
225 if (expn.data && qc) {
226 mutt_buffer_addstr (dest, expn.data);
227 p_delete(&expn.data);
229 else if (expn.data) {
230 expnlen = m_strlen(expn.data);
231 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
232 ptr = xmalloc(tok->dsize);
233 memcpy (ptr, expn.data, expnlen);
234 strcpy (ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
236 p_delete(&tok->data);
239 tok->destroy = 1; /* mark that the caller should destroy this data */
241 p_delete(&expn.data);
244 else if (ch == '$' && (!qc || qc == '"')
245 && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr))) {
246 char *env = NULL, *var = NULL;
248 if (*tok->dptr == '{') {
250 if ((pc = strchr (tok->dptr, '}'))) {
251 var = str_substrdup (tok->dptr, pc);
256 for (pc = tok->dptr; isalnum ((unsigned char) *pc) || *pc == '_';
258 var = str_substrdup (tok->dptr, pc);
263 if ((env = getenv (var)) ||
264 (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
265 mutt_buffer_addstr (dest, env);
270 mutt_buffer_addch (dest, ch);
272 mutt_buffer_addch (dest, 0); /* terminate the string */