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