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