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.
42 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
43 * just initializes. Frees anything already in the buffer.
45 * Disregards the 'destroy' flag, which seems reserved for caller.
46 * This is bad, but there's no apparent protocol for it.
48 BUFFER *mutt_buffer_init(BUFFER *b)
59 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
60 * just initializes. Frees anything already in the buffer. Copies in
63 * Disregards the 'destroy' flag, which seems reserved for caller.
64 * This is bad, but there's no apparent protocol for it.
66 BUFFER *mutt_buffer_from(BUFFER * b, const char *seed)
71 b = mutt_buffer_init(b);
72 b->dsize = m_strlen(seed);
73 b->data = m_strdup(seed);
74 b->dptr = (char *)b->data + b->dsize;
78 void mutt_buffer_free(BUFFER **p)
81 p_delete(&(*p)->data);
86 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
87 * Always one byte bigger than necessary for the null terminator, and
88 * the buffer is always null-terminated */
89 void mutt_buffer_add(BUFFER *buf, const char *s, ssize_t len)
93 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
94 offset = buf->dptr - buf->data;
95 buf->dsize += ((len + 1 + 127) & ~127);
96 p_realloc(&buf->data, buf->dsize);
97 buf->dptr = buf->data + offset;
99 memcpy(buf->dptr, s, len);
104 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
107 char qc = 0; /* quote char */
110 /* reset the destination pointer to the beginning of the buffer */
111 dest->dptr = dest->data;
113 tok->dptr = vskipspaces(tok->dptr);
114 while ((ch = *tok->dptr)) {
116 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
117 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
118 || (ch == '=' && (flags & M_TOKEN_EQUAL))
119 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
120 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
129 qc = 0; /* end of quote */
131 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
134 if (ch == '\\' && qc != '\'') {
136 return -1; /* premature end of token */
138 switch (ch = *tok->dptr++) {
142 return -1; /* premature end of token */
143 mutt_buffer_addch(dest,
144 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
148 mutt_buffer_addch(dest, '\r');
151 mutt_buffer_addch(dest, '\n');
154 mutt_buffer_addch(dest, '\t');
157 mutt_buffer_addch(dest, '\f');
160 mutt_buffer_addch(dest, '\033');
163 if (isdigit((unsigned char)ch)
164 && isdigit((unsigned char)*tok->dptr)
165 && isdigit((unsigned char)*(tok->dptr + 1)))
167 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
168 *(tok->dptr + 1) - 3504);
171 mutt_buffer_addch(dest, ch);
175 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
177 return -1; /* premature end of token */
180 mutt_buffer_addch(dest, ch);
183 mutt_buffer_addch(dest, '\033');
185 if (isalpha((unsigned char)ch)) {
186 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
188 mutt_buffer_addch(dest, '^');
189 mutt_buffer_addch(dest, ch);
192 if (ch == '`' && (!qc || qc == '"')) {
202 if ((pc = strpbrk(pc, "\\`"))) {
203 /* skip any quoted chars */
207 } while (pc && *pc != '`');
212 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
213 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
223 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
225 mutt_wait_filter(pid);
227 /* if we got output, make a new string consiting of the shell ouptput
228 plus whatever else was left on the original line */
229 /* BUT: If this is inside a quoted string, directly add output to
231 if (expn.data && qc) {
232 mutt_buffer_addstr(dest, expn.data);
233 p_delete(&expn.data);
236 expnlen = m_strlen(expn.data);
237 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
238 ptr = xmalloc(tok->dsize);
239 memcpy(ptr, expn.data, expnlen);
240 strcpy(ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
242 p_delete(&tok->data);
245 tok->destroy = 1; /* mark that the caller should destroy this data */
247 p_delete(&expn.data);
250 if (ch == '$' && (!qc || qc == '"')
251 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
253 char *env = NULL, *var = NULL;
255 if (*tok->dptr == '{') {
257 if ((pc = strchr (tok->dptr, '}'))) {
258 var = p_dupstr(tok->dptr, pc - tok->dptr);
262 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
264 var = p_dupstr(tok->dptr, pc - tok->dptr);
269 if ((env = getenv (var))
270 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
272 mutt_buffer_addstr (dest, env);
277 mutt_buffer_addch(dest, ch);
280 mutt_buffer_addch(dest, 0); /* terminate the string */
281 tok->dptr = vskipspaces(tok->dptr);