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)
42 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
43 * just initializes. Frees anything already in the buffer. Copies in
46 * Disregards the 'destroy' flag, which seems reserved for caller.
47 * This is bad, but there's no apparent protocol for it.
49 BUFFER *mutt_buffer_from (BUFFER * b, const char *seed)
54 b = mutt_buffer_init (b);
55 b->data = m_strdup(seed);
56 b->dsize = m_strlen(seed);
57 b->dptr = (char *) b->data + b->dsize;
61 void mutt_buffer_addstr (BUFFER * buf, const char *s)
63 mutt_buffer_add (buf, s, m_strlen(s));
66 void mutt_buffer_addch (BUFFER * buf, char c)
68 mutt_buffer_add (buf, &c, 1);
71 void mutt_buffer_free (BUFFER ** p)
76 p_delete(&(*p)->data);
77 /* dptr is just an offset to data and shouldn't be freed */
81 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
82 * Always one byte bigger than necessary for the null terminator, and
83 * the buffer is always null-terminated */
84 void mutt_buffer_add (BUFFER *buf, const char *s, size_t len)
88 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
89 offset = buf->dptr - buf->data;
90 buf->dsize += len < 128 ? 128 : len + 1;
91 p_realloc(&buf->data, buf->dsize);
92 buf->dptr = buf->data + offset;
94 memcpy (buf->dptr, s, len);
99 int mutt_extract_token (BUFFER * dest, BUFFER * tok, int flags)
102 char qc = 0; /* quote char */
105 /* reset the destination pointer to the beginning of the buffer */
106 dest->dptr = dest->data;
109 while ((ch = *tok->dptr)) {
111 if ((ISSPACE (ch) && !(flags & M_TOKEN_SPACE)) ||
112 (ch == '#' && !(flags & M_TOKEN_COMMENT)) ||
113 (ch == '=' && (flags & M_TOKEN_EQUAL)) ||
114 (ch == ';' && !(flags & M_TOKEN_SEMICOLON)) ||
115 ((flags & M_TOKEN_PATTERN) && strchr ("~=!|", ch)))
122 qc = 0; /* end of quote */
123 else if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE))
125 else if (ch == '\\' && qc != '\'') {
127 return -1; /* premature end of token */
128 switch (ch = *tok->dptr++) {
132 return -1; /* premature end of token */
133 mutt_buffer_addch (dest, (toupper ((unsigned char) *tok->dptr)
138 mutt_buffer_addch (dest, '\r');
141 mutt_buffer_addch (dest, '\n');
144 mutt_buffer_addch (dest, '\t');
147 mutt_buffer_addch (dest, '\f');
150 mutt_buffer_addch (dest, '\033');
153 if (isdigit ((unsigned char) ch) &&
154 isdigit ((unsigned char) *tok->dptr) &&
155 isdigit ((unsigned char) *(tok->dptr + 1))) {
157 mutt_buffer_addch (dest,
158 (ch << 6) + (*tok->dptr << 3) + *(tok->dptr +
163 mutt_buffer_addch (dest, ch);
166 else if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
168 return -1; /* premature end of token */
171 mutt_buffer_addch (dest, ch);
173 mutt_buffer_addch (dest, '\033');
174 else if (isalpha ((unsigned char) ch))
175 mutt_buffer_addch (dest, toupper ((unsigned char) ch) - '@');
177 mutt_buffer_addch (dest, '^');
178 mutt_buffer_addch (dest, ch);
181 else if (ch == '`' && (!qc || qc == '"')) {
191 if ((pc = strpbrk (pc, "\\`"))) {
192 /* skip any quoted chars */
196 } while (pc && *pc != '`');
198 debug_print (1, ("mismatched backtics\n"));
201 cmd = str_substrdup (tok->dptr, pc);
202 if ((pid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
203 debug_print (1, ("unable to fork command: %s\n", cmd));
213 expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line);
215 mutt_wait_filter (pid);
217 /* if we got output, make a new string consiting of the shell ouptput
218 plus whatever else was left on the original line */
219 /* BUT: If this is inside a quoted string, directly add output to
221 if (expn.data && qc) {
222 mutt_buffer_addstr (dest, expn.data);
223 p_delete(&expn.data);
225 else if (expn.data) {
226 expnlen = m_strlen(expn.data);
227 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
228 ptr = xmalloc(tok->dsize);
229 memcpy (ptr, expn.data, expnlen);
230 strcpy (ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
232 p_delete(&tok->data);
235 tok->destroy = 1; /* mark that the caller should destroy this data */
237 p_delete(&expn.data);
240 else if (ch == '$' && (!qc || qc == '"')
241 && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr))) {
242 char *env = NULL, *var = NULL;
244 if (*tok->dptr == '{') {
246 if ((pc = strchr (tok->dptr, '}'))) {
247 var = str_substrdup (tok->dptr, pc);
252 for (pc = tok->dptr; isalnum ((unsigned char) *pc) || *pc == '_';
254 var = str_substrdup (tok->dptr, pc);
259 if ((env = getenv (var)) ||
260 (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
261 mutt_buffer_addstr (dest, env);
266 mutt_buffer_addch (dest, ch);
268 mutt_buffer_addch (dest, 0); /* terminate the string */