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