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.
36 #include <lib-lib/mem.h>
37 #include <lib-lib/str.h>
38 #include <lib-lib/ascii.h>
43 #include "lib/debug.h"
46 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
47 * just initializes. Frees anything already in the buffer.
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_init(BUFFER *b)
63 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
64 * just initializes. Frees anything already in the buffer. Copies in
67 * Disregards the 'destroy' flag, which seems reserved for caller.
68 * This is bad, but there's no apparent protocol for it.
70 BUFFER *mutt_buffer_from(BUFFER * b, const char *seed)
75 b = mutt_buffer_init(b);
76 b->dsize = m_strlen(seed);
77 b->data = m_strdup(seed);
78 b->dptr = (char *)b->data + b->dsize;
82 void mutt_buffer_free(BUFFER **p)
85 p_delete(&(*p)->data);
90 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
91 * Always one byte bigger than necessary for the null terminator, and
92 * the buffer is always null-terminated */
93 void mutt_buffer_add(BUFFER *buf, const char *s, size_t len)
97 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
98 offset = buf->dptr - buf->data;
99 buf->dsize += ((len + 1 + 127) & ~127);
100 p_realloc(&buf->data, buf->dsize);
101 buf->dptr = buf->data + offset;
103 memcpy(buf->dptr, s, len);
108 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
111 char qc = 0; /* quote char */
114 /* reset the destination pointer to the beginning of the buffer */
115 dest->dptr = dest->data;
118 while ((ch = *tok->dptr)) {
120 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
121 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
122 || (ch == '=' && (flags & M_TOKEN_EQUAL))
123 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
124 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
133 qc = 0; /* end of quote */
135 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
138 if (ch == '\\' && qc != '\'') {
140 return -1; /* premature end of token */
142 switch (ch = *tok->dptr++) {
146 return -1; /* premature end of token */
147 mutt_buffer_addch(dest,
148 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
152 mutt_buffer_addch(dest, '\r');
155 mutt_buffer_addch(dest, '\n');
158 mutt_buffer_addch(dest, '\t');
161 mutt_buffer_addch(dest, '\f');
164 mutt_buffer_addch(dest, '\033');
167 if (isdigit((unsigned char)ch)
168 && isdigit((unsigned char)*tok->dptr)
169 && isdigit((unsigned char)*(tok->dptr + 1)))
171 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
172 *(tok->dptr + 1) - 3504);
175 mutt_buffer_addch(dest, ch);
179 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
181 return -1; /* premature end of token */
184 mutt_buffer_addch(dest, ch);
187 mutt_buffer_addch(dest, '\033');
189 if (isalpha((unsigned char)ch)) {
190 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
192 mutt_buffer_addch(dest, '^');
193 mutt_buffer_addch(dest, ch);
196 if (ch == '`' && (!qc || qc == '"')) {
206 if ((pc = strpbrk(pc, "\\`"))) {
207 /* skip any quoted chars */
211 } while (pc && *pc != '`');
213 debug_print (1, ("mismatched backtics\n"));
217 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
218 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
219 debug_print(1, ("unable to fork command: %s\n", cmd));
229 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
231 mutt_wait_filter(pid);
233 /* if we got output, make a new string consiting of the shell ouptput
234 plus whatever else was left on the original line */
235 /* BUT: If this is inside a quoted string, directly add output to
237 if (expn.data && qc) {
238 mutt_buffer_addstr(dest, expn.data);
239 p_delete(&expn.data);
242 expnlen = m_strlen(expn.data);
243 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
244 ptr = xmalloc(tok->dsize);
245 memcpy(ptr, expn.data, expnlen);
246 strcpy(ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
248 p_delete(&tok->data);
251 tok->destroy = 1; /* mark that the caller should destroy this data */
253 p_delete(&expn.data);
256 if (ch == '$' && (!qc || qc == '"')
257 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
259 char *env = NULL, *var = NULL;
261 if (*tok->dptr == '{') {
263 if ((pc = strchr (tok->dptr, '}'))) {
264 var = str_substrdup (tok->dptr, pc);
268 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
270 var = p_dupstr(tok->dptr, pc - tok->dptr);
275 if ((env = getenv (var))
276 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
278 mutt_buffer_addstr (dest, env);
283 mutt_buffer_addch(dest, ch);
286 mutt_buffer_addch(dest, 0); /* terminate the string */