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