move parse.c into rfc822parse.c in the lib-mime as it's what it's about
[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 "mem.h"
34 #include "str.h"
35 #include "ascii.h"
36 #include "buffer.h"
37 #include "file.h"
38
39 #include "lib/debug.h"
40
41 #include "mutt.h"
42
43 /*
44  * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
45  * just initializes. Frees anything already in the buffer.
46  *
47  * Disregards the 'destroy' flag, which seems reserved for caller.
48  * This is bad, but there's no apparent protocol for it.
49  */
50 BUFFER *mutt_buffer_init(BUFFER *b)
51 {
52     if (!b) {
53         b = p_new(BUFFER, 1);
54     }
55     p_delete(&b->data);
56     p_clear(b, 1);
57     return b;
58 }
59
60 /*
61  * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
62  * just initializes. Frees anything already in the buffer. Copies in
63  * the seed string.
64  *
65  * Disregards the 'destroy' flag, which seems reserved for caller.
66  * This is bad, but there's no apparent protocol for it.
67  */
68 BUFFER *mutt_buffer_from(BUFFER * b, const char *seed)
69 {
70     if (!seed)
71         return NULL;
72
73     b = mutt_buffer_init(b);
74     b->dsize = m_strlen(seed);
75     b->data  = m_strdup(seed);
76     b->dptr  = (char *)b->data + b->dsize;
77     return b;
78 }
79
80 void mutt_buffer_free(BUFFER **p)
81 {
82     if (p && *p) {
83         p_delete(&(*p)->data);
84         p_delete(p);
85     }
86 }
87
88 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
89  * Always one byte bigger than necessary for the null terminator, and
90  * the buffer is always null-terminated */
91 void mutt_buffer_add(BUFFER *buf, const char *s, size_t len)
92 {
93     size_t offset;
94
95     if (buf->dptr + len + 1 > buf->data + buf->dsize) {
96         offset = buf->dptr - buf->data;
97         buf->dsize += ((len + 1 + 127) & ~127);
98         p_realloc(&buf->data, buf->dsize);
99         buf->dptr = buf->data + offset;
100     }
101     memcpy(buf->dptr, s, len);
102     buf->dptr += len;
103     *buf->dptr = '\0';
104 }
105
106 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
107 {
108     char ch;
109     char qc = 0;                  /* quote char */
110     char *pc;
111
112     /* reset the destination pointer to the beginning of the buffer */
113     dest->dptr = dest->data;
114
115     tok->dptr = vskipspaces(tok->dptr);
116     while ((ch = *tok->dptr)) {
117         if (!qc) {
118             if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
119             || (ch == '#' && !(flags & M_TOKEN_COMMENT))
120             || (ch == '=' && (flags & M_TOKEN_EQUAL))
121             || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
122             || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
123             {
124                 break;
125             }
126         }
127
128         tok->dptr++;
129
130         if (ch == qc) {
131             qc = 0;                     /* end of quote */
132         } else
133         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
134             qc = ch;
135         } else
136         if (ch == '\\' && qc != '\'') {
137             if (!*tok->dptr)
138                 return -1;              /* premature end of token */
139
140             switch (ch = *tok->dptr++) {
141               case 'c':
142               case 'C':
143                 if (!*tok->dptr)
144                     return -1;          /* premature end of token */
145                 mutt_buffer_addch(dest,
146                                   (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
147                 tok->dptr++;
148                 break;
149               case 'r':
150                 mutt_buffer_addch(dest, '\r');
151                 break;
152               case 'n':
153                 mutt_buffer_addch(dest, '\n');
154                 break;
155               case 't':
156                 mutt_buffer_addch(dest, '\t');
157                 break;
158               case 'f':
159                 mutt_buffer_addch(dest, '\f');
160                 break;
161               case 'e':
162                 mutt_buffer_addch(dest, '\033');
163                 break;
164               default:
165                 if (isdigit((unsigned char)ch)
166                 &&  isdigit((unsigned char)*tok->dptr)
167                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
168                 {
169                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
170                                             *(tok->dptr + 1) - 3504);
171                     tok->dptr += 2;
172                 } else {
173                     mutt_buffer_addch(dest, ch);
174                 }
175             }
176         } else
177         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
178             if (!*tok->dptr)
179                 return -1;              /* premature end of token */
180             ch = *tok->dptr++;
181             if (ch == '^') {
182                 mutt_buffer_addch(dest, ch);
183             } else
184             if (ch == '[') {
185                 mutt_buffer_addch(dest, '\033');
186             } else
187             if (isalpha((unsigned char)ch)) {
188                 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
189             } else {
190                 mutt_buffer_addch(dest, '^');
191                 mutt_buffer_addch(dest, ch);
192             }
193         } else
194         if (ch == '`' && (!qc || qc == '"')) {
195             FILE *fp;
196             pid_t pid;
197             char *cmd, *ptr;
198             size_t expnlen;
199             BUFFER expn;
200             int line = 0;
201
202             pc = tok->dptr;
203             do {
204                 if ((pc = strpbrk(pc, "\\`"))) {
205                     /* skip any quoted chars */
206                     if (*pc == '\\')
207                         pc += 2;
208                 }
209             } while (pc && *pc != '`');
210             if (!pc) {
211                 debug_print (1, ("mismatched backtics\n"));
212                 return (-1);
213             }
214
215             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
216             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
217                 debug_print(1, ("unable to fork command: %s\n", cmd));
218                 p_delete(&cmd);
219                 return -1;
220             }
221             p_delete(&cmd);
222
223             tok->dptr = pc + 1;
224
225             /* read line */
226             p_clear(&expn, 1);
227             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
228             fclose(fp);
229             mutt_wait_filter(pid);
230
231             /* if we got output, make a new string consiting of the shell ouptput
232                plus whatever else was left on the original line */
233             /* BUT: If this is inside a quoted string, directly add output to 
234              * the token */
235             if (expn.data && qc) {
236                 mutt_buffer_addstr(dest, expn.data);
237                 p_delete(&expn.data);
238             } else
239             if (expn.data) {
240                 expnlen = m_strlen(expn.data);
241                 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
242                 ptr = xmalloc(tok->dsize);
243                 memcpy(ptr, expn.data, expnlen);
244                 strcpy(ptr + expnlen, tok->dptr);      /* __STRCPY_CHECKED__ */
245                 if (tok->destroy)
246                     p_delete(&tok->data);
247                 tok->data = ptr;
248                 tok->dptr = ptr;
249                 tok->destroy = 1;       /* mark that the caller should destroy this data */
250                 ptr = NULL;
251                 p_delete(&expn.data);
252             }
253         } else
254         if (ch == '$' && (!qc || qc == '"')
255         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
256         {
257             char *env = NULL, *var = NULL;
258
259             if (*tok->dptr == '{') {
260                 tok->dptr++;
261                 if ((pc = strchr (tok->dptr, '}'))) {
262                     var = p_dupstr(tok->dptr, pc - tok->dptr);
263                     tok->dptr = pc + 1;
264                 }
265             } else {
266                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
267                      pc++);
268                 var = p_dupstr(tok->dptr, pc - tok->dptr);
269                 tok->dptr = pc;
270             }
271             if (var) {
272                 char tmp[STRING];
273                 if ((env = getenv (var))
274                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
275                 {
276                     mutt_buffer_addstr (dest, env);
277                 }
278             }
279             p_delete(&var);
280         } else {
281             mutt_buffer_addch(dest, ch);
282         }
283     }
284     mutt_buffer_addch(dest, 0);  /* terminate the string */
285     tok->dptr = vskipspaces(tok->dptr);
286     return 0;
287 }
288