14ae8943cedd62c9e90b51851850fde4578886ba
[apps/madmutt.git] / buffer.c
1 /*
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.
6  *
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.
11  *
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,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 /*
21  * Copyright notice from original mutt:
22  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
23  *
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.
27  */
28 #if HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #include <lib-lib/mem.h>
37 #include <lib-lib/str.h>
38 #include <lib-lib/ascii.h>
39
40 #include "buffer.h"
41 #include "mutt.h"
42
43 #include "lib/debug.h"
44
45 /*
46  * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
47  * just initializes. Frees anything already in the buffer.
48  *
49  * Disregards the 'destroy' flag, which seems reserved for caller.
50  * This is bad, but there's no apparent protocol for it.
51  */
52 BUFFER *mutt_buffer_init(BUFFER *b)
53 {
54     if (!b) {
55         b = p_new(BUFFER, 1);
56     }
57     p_delete(&b->data);
58     p_clear(b, 1);
59     return b;
60 }
61
62 /*
63  * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
64  * just initializes. Frees anything already in the buffer. Copies in
65  * the seed string.
66  *
67  * Disregards the 'destroy' flag, which seems reserved for caller.
68  * This is bad, but there's no apparent protocol for it.
69  */
70 BUFFER *mutt_buffer_from(BUFFER * b, const char *seed)
71 {
72     if (!seed)
73         return NULL;
74
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;
79     return b;
80 }
81
82 void mutt_buffer_free(BUFFER **p)
83 {
84     if (p && *p) {
85         p_delete(&(*p)->data);
86         p_delete(p);
87     }
88 }
89
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)
94 {
95     size_t offset;
96
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;
102     }
103     memcpy(buf->dptr, s, len);
104     buf->dptr += len;
105     *buf->dptr = '\0';
106 }
107
108 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
109 {
110     char ch;
111     char qc = 0;                  /* quote char */
112     char *pc;
113
114     /* reset the destination pointer to the beginning of the buffer */
115     dest->dptr = dest->data;
116
117     SKIPWS(tok->dptr);
118     while ((ch = *tok->dptr)) {
119         if (!qc) {
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)))
125             {
126                 break;
127             }
128         }
129
130         tok->dptr++;
131
132         if (ch == qc) {
133             qc = 0;                     /* end of quote */
134         } else
135         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
136             qc = ch;
137         } else
138         if (ch == '\\' && qc != '\'') {
139             if (!*tok->dptr)
140                 return -1;              /* premature end of token */
141
142             switch (ch = *tok->dptr++) {
143               case 'c':
144               case 'C':
145                 if (!*tok->dptr)
146                     return -1;          /* premature end of token */
147                 mutt_buffer_addch(dest,
148                                   (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
149                 tok->dptr++;
150                 break;
151               case 'r':
152                 mutt_buffer_addch(dest, '\r');
153                 break;
154               case 'n':
155                 mutt_buffer_addch(dest, '\n');
156                 break;
157               case 't':
158                 mutt_buffer_addch(dest, '\t');
159                 break;
160               case 'f':
161                 mutt_buffer_addch(dest, '\f');
162                 break;
163               case 'e':
164                 mutt_buffer_addch(dest, '\033');
165                 break;
166               default:
167                 if (isdigit((unsigned char)ch)
168                 &&  isdigit((unsigned char)*tok->dptr)
169                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
170                 {
171                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
172                                             *(tok->dptr + 1) - 3504);
173                     tok->dptr += 2;
174                 } else {
175                     mutt_buffer_addch(dest, ch);
176                 }
177             }
178         } else
179         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
180             if (!*tok->dptr)
181                 return -1;              /* premature end of token */
182             ch = *tok->dptr++;
183             if (ch == '^') {
184                 mutt_buffer_addch(dest, ch);
185             } else
186             if (ch == '[') {
187                 mutt_buffer_addch(dest, '\033');
188             } else
189             if (isalpha((unsigned char)ch)) {
190                 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
191             } else {
192                 mutt_buffer_addch(dest, '^');
193                 mutt_buffer_addch(dest, ch);
194             }
195         } else
196         if (ch == '`' && (!qc || qc == '"')) {
197             FILE *fp;
198             pid_t pid;
199             char *cmd, *ptr;
200             size_t expnlen;
201             BUFFER expn;
202             int line = 0;
203
204             pc = tok->dptr;
205             do {
206                 if ((pc = strpbrk(pc, "\\`"))) {
207                     /* skip any quoted chars */
208                     if (*pc == '\\')
209                         pc += 2;
210                 }
211             } while (pc && *pc != '`');
212             if (!pc) {
213                 debug_print (1, ("mismatched backtics\n"));
214                 return (-1);
215             }
216
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));
220                 p_delete(&cmd);
221                 return -1;
222             }
223             p_delete(&cmd);
224
225             tok->dptr = pc + 1;
226
227             /* read line */
228             p_clear(&expn, 1);
229             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
230             fclose(fp);
231             mutt_wait_filter(pid);
232
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 
236              * the token */
237             if (expn.data && qc) {
238                 mutt_buffer_addstr(dest, expn.data);
239                 p_delete(&expn.data);
240             } else
241             if (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__ */
247                 if (tok->destroy)
248                     p_delete(&tok->data);
249                 tok->data = ptr;
250                 tok->dptr = ptr;
251                 tok->destroy = 1;       /* mark that the caller should destroy this data */
252                 ptr = NULL;
253                 p_delete(&expn.data);
254             }
255         } else
256         if (ch == '$' && (!qc || qc == '"')
257         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
258         {
259             char *env = NULL, *var = NULL;
260
261             if (*tok->dptr == '{') {
262                 tok->dptr++;
263                 if ((pc = strchr (tok->dptr, '}'))) {
264                     var = str_substrdup (tok->dptr, pc);
265                     tok->dptr = pc + 1;
266                 }
267             } else {
268                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
269                      pc++);
270                 var = p_dupstr(tok->dptr, pc - tok->dptr);
271                 tok->dptr = pc;
272             }
273             if (var) {
274                 char tmp[STRING];
275                 if ((env = getenv (var))
276                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
277                 {
278                     mutt_buffer_addstr (dest, env);
279                 }
280             }
281             p_delete(&var);
282         } else {
283             mutt_buffer_addch(dest, ch);
284         }
285     }
286     mutt_buffer_addch(dest, 0);  /* terminate the string */
287     SKIPWS(tok->dptr);
288     return 0;
289 }
290