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