2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
21 * Copyright notice from original mutt:
22 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
24 * This file is part of mutt-ng, see http://www.muttng.org/.
25 * It's licensed under the GNU General Public License,
26 * please see the file GPL in the top level source directory.
39 #include "lib/debug.h"
44 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
45 * just initializes. Frees anything already in the buffer.
47 * Disregards the 'destroy' flag, which seems reserved for caller.
48 * This is bad, but there's no apparent protocol for it.
50 BUFFER *mutt_buffer_init(BUFFER *b)
61 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
62 * just initializes. Frees anything already in the buffer. Copies in
65 * Disregards the 'destroy' flag, which seems reserved for caller.
66 * This is bad, but there's no apparent protocol for it.
68 BUFFER *mutt_buffer_from(BUFFER * b, const char *seed)
73 b = mutt_buffer_init(b);
74 b->dsize = m_strlen(seed);
75 b->data = m_strdup(seed);
76 b->dptr = (char *)b->data + b->dsize;
80 void mutt_buffer_free(BUFFER **p)
83 p_delete(&(*p)->data);
88 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
89 * Always one byte bigger than necessary for the null terminator, and
90 * the buffer is always null-terminated */
91 void mutt_buffer_add(BUFFER *buf, const char *s, size_t len)
95 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
96 offset = buf->dptr - buf->data;
97 buf->dsize += ((len + 1 + 127) & ~127);
98 p_realloc(&buf->data, buf->dsize);
99 buf->dptr = buf->data + offset;
101 memcpy(buf->dptr, s, len);
106 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
109 char qc = 0; /* quote char */
112 /* reset the destination pointer to the beginning of the buffer */
113 dest->dptr = dest->data;
115 tok->dptr = vskipspaces(tok->dptr);
116 while ((ch = *tok->dptr)) {
118 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
119 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
120 || (ch == '=' && (flags & M_TOKEN_EQUAL))
121 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
122 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
131 qc = 0; /* end of quote */
133 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
136 if (ch == '\\' && qc != '\'') {
138 return -1; /* premature end of token */
140 switch (ch = *tok->dptr++) {
144 return -1; /* premature end of token */
145 mutt_buffer_addch(dest,
146 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
150 mutt_buffer_addch(dest, '\r');
153 mutt_buffer_addch(dest, '\n');
156 mutt_buffer_addch(dest, '\t');
159 mutt_buffer_addch(dest, '\f');
162 mutt_buffer_addch(dest, '\033');
165 if (isdigit((unsigned char)ch)
166 && isdigit((unsigned char)*tok->dptr)
167 && isdigit((unsigned char)*(tok->dptr + 1)))
169 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
170 *(tok->dptr + 1) - 3504);
173 mutt_buffer_addch(dest, ch);
177 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
179 return -1; /* premature end of token */
182 mutt_buffer_addch(dest, ch);
185 mutt_buffer_addch(dest, '\033');
187 if (isalpha((unsigned char)ch)) {
188 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
190 mutt_buffer_addch(dest, '^');
191 mutt_buffer_addch(dest, ch);
194 if (ch == '`' && (!qc || qc == '"')) {
204 if ((pc = strpbrk(pc, "\\`"))) {
205 /* skip any quoted chars */
209 } while (pc && *pc != '`');
211 debug_print (1, ("mismatched backtics\n"));
215 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
216 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
217 debug_print(1, ("unable to fork command: %s\n", cmd));
227 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
229 mutt_wait_filter(pid);
231 /* if we got output, make a new string consiting of the shell ouptput
232 plus whatever else was left on the original line */
233 /* BUT: If this is inside a quoted string, directly add output to
235 if (expn.data && qc) {
236 mutt_buffer_addstr(dest, expn.data);
237 p_delete(&expn.data);
240 expnlen = m_strlen(expn.data);
241 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
242 ptr = xmalloc(tok->dsize);
243 memcpy(ptr, expn.data, expnlen);
244 strcpy(ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
246 p_delete(&tok->data);
249 tok->destroy = 1; /* mark that the caller should destroy this data */
251 p_delete(&expn.data);
254 if (ch == '$' && (!qc || qc == '"')
255 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
257 char *env = NULL, *var = NULL;
259 if (*tok->dptr == '{') {
261 if ((pc = strchr (tok->dptr, '}'))) {
262 var = str_substrdup (tok->dptr, pc);
266 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
268 var = p_dupstr(tok->dptr, pc - tok->dptr);
273 if ((env = getenv (var))
274 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
276 mutt_buffer_addstr (dest, env);
281 mutt_buffer_addch(dest, ch);
284 mutt_buffer_addch(dest, 0); /* terminate the string */
285 tok->dptr = vskipspaces(tok->dptr);