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.
33 #include <lib-lib/mem.h>
34 #include <lib-lib/str.h>
35 #include <lib-lib/ascii.h>
36 #include <lib-lib/buffer.h>
40 #include "lib/debug.h"
43 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
44 * just initializes. Frees anything already in the buffer.
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_init(BUFFER *b)
60 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
61 * just initializes. Frees anything already in the buffer. Copies in
64 * Disregards the 'destroy' flag, which seems reserved for caller.
65 * This is bad, but there's no apparent protocol for it.
67 BUFFER *mutt_buffer_from(BUFFER * b, const char *seed)
72 b = mutt_buffer_init(b);
73 b->dsize = m_strlen(seed);
74 b->data = m_strdup(seed);
75 b->dptr = (char *)b->data + b->dsize;
79 void mutt_buffer_free(BUFFER **p)
82 p_delete(&(*p)->data);
87 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
88 * Always one byte bigger than necessary for the null terminator, and
89 * the buffer is always null-terminated */
90 void mutt_buffer_add(BUFFER *buf, const char *s, size_t len)
94 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
95 offset = buf->dptr - buf->data;
96 buf->dsize += ((len + 1 + 127) & ~127);
97 p_realloc(&buf->data, buf->dsize);
98 buf->dptr = buf->data + offset;
100 memcpy(buf->dptr, s, len);
105 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
108 char qc = 0; /* quote char */
111 /* reset the destination pointer to the beginning of the buffer */
112 dest->dptr = dest->data;
115 while ((ch = *tok->dptr)) {
117 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
118 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
119 || (ch == '=' && (flags & M_TOKEN_EQUAL))
120 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
121 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
130 qc = 0; /* end of quote */
132 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
135 if (ch == '\\' && qc != '\'') {
137 return -1; /* premature end of token */
139 switch (ch = *tok->dptr++) {
143 return -1; /* premature end of token */
144 mutt_buffer_addch(dest,
145 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
149 mutt_buffer_addch(dest, '\r');
152 mutt_buffer_addch(dest, '\n');
155 mutt_buffer_addch(dest, '\t');
158 mutt_buffer_addch(dest, '\f');
161 mutt_buffer_addch(dest, '\033');
164 if (isdigit((unsigned char)ch)
165 && isdigit((unsigned char)*tok->dptr)
166 && isdigit((unsigned char)*(tok->dptr + 1)))
168 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
169 *(tok->dptr + 1) - 3504);
172 mutt_buffer_addch(dest, ch);
176 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
178 return -1; /* premature end of token */
181 mutt_buffer_addch(dest, ch);
184 mutt_buffer_addch(dest, '\033');
186 if (isalpha((unsigned char)ch)) {
187 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
189 mutt_buffer_addch(dest, '^');
190 mutt_buffer_addch(dest, ch);
193 if (ch == '`' && (!qc || qc == '"')) {
203 if ((pc = strpbrk(pc, "\\`"))) {
204 /* skip any quoted chars */
208 } while (pc && *pc != '`');
210 debug_print (1, ("mismatched backtics\n"));
214 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
215 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
216 debug_print(1, ("unable to fork command: %s\n", cmd));
226 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
228 mutt_wait_filter(pid);
230 /* if we got output, make a new string consiting of the shell ouptput
231 plus whatever else was left on the original line */
232 /* BUT: If this is inside a quoted string, directly add output to
234 if (expn.data && qc) {
235 mutt_buffer_addstr(dest, expn.data);
236 p_delete(&expn.data);
239 expnlen = m_strlen(expn.data);
240 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
241 ptr = xmalloc(tok->dsize);
242 memcpy(ptr, expn.data, expnlen);
243 strcpy(ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
245 p_delete(&tok->data);
248 tok->destroy = 1; /* mark that the caller should destroy this data */
250 p_delete(&expn.data);
253 if (ch == '$' && (!qc || qc == '"')
254 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
256 char *env = NULL, *var = NULL;
258 if (*tok->dptr == '{') {
260 if ((pc = strchr (tok->dptr, '}'))) {
261 var = str_substrdup (tok->dptr, pc);
265 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
267 var = p_dupstr(tok->dptr, pc - tok->dptr);
272 if ((env = getenv (var))
273 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
275 mutt_buffer_addstr (dest, env);
280 mutt_buffer_addch(dest, ch);
283 mutt_buffer_addch(dest, 0); /* terminate the string */