0a506bbef8b82f1a9e433dbd8ad6341cd9e8aa5b
[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 "mutt.h"
40
41 /*
42  * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
43  * just initializes. Frees anything already in the buffer.
44  *
45  * Disregards the 'destroy' flag, which seems reserved for caller.
46  * This is bad, but there's no apparent protocol for it.
47  */
48 BUFFER *mutt_buffer_init(BUFFER *b)
49 {
50     if (!b) {
51         b = p_new(BUFFER, 1);
52     }
53     p_delete(&b->data);
54     p_clear(b, 1);
55     return b;
56 }
57
58 /*
59  * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
60  * just initializes. Frees anything already in the buffer. Copies in
61  * the seed string.
62  *
63  * Disregards the 'destroy' flag, which seems reserved for caller.
64  * This is bad, but there's no apparent protocol for it.
65  */
66 BUFFER *mutt_buffer_from(BUFFER * b, const char *seed)
67 {
68     if (!seed)
69         return NULL;
70
71     b = mutt_buffer_init(b);
72     b->dsize = m_strlen(seed);
73     b->data  = m_strdup(seed);
74     b->dptr  = (char *)b->data + b->dsize;
75     return b;
76 }
77
78 void mutt_buffer_free(BUFFER **p)
79 {
80     if (p && *p) {
81         p_delete(&(*p)->data);
82         p_delete(p);
83     }
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, ssize_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 + 1 + 127) & ~127);
96         p_realloc(&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     tok->dptr = vskipspaces(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             {
122                 break;
123             }
124         }
125
126         tok->dptr++;
127
128         if (ch == qc) {
129             qc = 0;                     /* end of quote */
130         } else
131         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
132             qc = ch;
133         } else
134         if (ch == '\\' && qc != '\'') {
135             if (!*tok->dptr)
136                 return -1;              /* premature end of token */
137
138             switch (ch = *tok->dptr++) {
139               case 'c':
140               case 'C':
141                 if (!*tok->dptr)
142                     return -1;          /* premature end of token */
143                 mutt_buffer_addch(dest,
144                                   (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
145                 tok->dptr++;
146                 break;
147               case 'r':
148                 mutt_buffer_addch(dest, '\r');
149                 break;
150               case 'n':
151                 mutt_buffer_addch(dest, '\n');
152                 break;
153               case 't':
154                 mutt_buffer_addch(dest, '\t');
155                 break;
156               case 'f':
157                 mutt_buffer_addch(dest, '\f');
158                 break;
159               case 'e':
160                 mutt_buffer_addch(dest, '\033');
161                 break;
162               default:
163                 if (isdigit((unsigned char)ch)
164                 &&  isdigit((unsigned char)*tok->dptr)
165                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
166                 {
167                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
168                                             *(tok->dptr + 1) - 3504);
169                     tok->dptr += 2;
170                 } else {
171                     mutt_buffer_addch(dest, ch);
172                 }
173             }
174         } else
175         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
176             if (!*tok->dptr)
177                 return -1;              /* premature end of token */
178             ch = *tok->dptr++;
179             if (ch == '^') {
180                 mutt_buffer_addch(dest, ch);
181             } else
182             if (ch == '[') {
183                 mutt_buffer_addch(dest, '\033');
184             } else
185             if (isalpha((unsigned char)ch)) {
186                 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
187             } else {
188                 mutt_buffer_addch(dest, '^');
189                 mutt_buffer_addch(dest, ch);
190             }
191         } else
192         if (ch == '`' && (!qc || qc == '"')) {
193             FILE *fp;
194             pid_t pid;
195             char *cmd, *ptr;
196             ssize_t expnlen;
197             BUFFER expn;
198             int line = 0;
199
200             pc = tok->dptr;
201             do {
202                 if ((pc = strpbrk(pc, "\\`"))) {
203                     /* skip any quoted chars */
204                     if (*pc == '\\')
205                         pc += 2;
206                 }
207             } while (pc && *pc != '`');
208             if (!pc) {
209                 return (-1);
210             }
211
212             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
213             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
214                 p_delete(&cmd);
215                 return -1;
216             }
217             p_delete(&cmd);
218
219             tok->dptr = pc + 1;
220
221             /* read line */
222             p_clear(&expn, 1);
223             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
224             fclose(fp);
225             mutt_wait_filter(pid);
226
227             /* if we got output, make a new string consiting of the shell ouptput
228                plus whatever else was left on the original line */
229             /* BUT: If this is inside a quoted string, directly add output to 
230              * the token */
231             if (expn.data && qc) {
232                 mutt_buffer_addstr(dest, expn.data);
233                 p_delete(&expn.data);
234             } else
235             if (expn.data) {
236                 expnlen = m_strlen(expn.data);
237                 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
238                 ptr = xmalloc(tok->dsize);
239                 memcpy(ptr, expn.data, expnlen);
240                 strcpy(ptr + expnlen, tok->dptr);      /* __STRCPY_CHECKED__ */
241                 if (tok->destroy)
242                     p_delete(&tok->data);
243                 tok->data = ptr;
244                 tok->dptr = ptr;
245                 tok->destroy = 1;       /* mark that the caller should destroy this data */
246                 ptr = NULL;
247                 p_delete(&expn.data);
248             }
249         } else
250         if (ch == '$' && (!qc || qc == '"')
251         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
252         {
253             char *env = NULL, *var = NULL;
254
255             if (*tok->dptr == '{') {
256                 tok->dptr++;
257                 if ((pc = strchr (tok->dptr, '}'))) {
258                     var = p_dupstr(tok->dptr, pc - tok->dptr);
259                     tok->dptr = pc + 1;
260                 }
261             } else {
262                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
263                      pc++);
264                 var = p_dupstr(tok->dptr, pc - tok->dptr);
265                 tok->dptr = pc;
266             }
267             if (var) {
268                 char tmp[STRING];
269                 if ((env = getenv (var))
270                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
271                 {
272                     mutt_buffer_addstr (dest, env);
273                 }
274             }
275             p_delete(&var);
276         } else {
277             mutt_buffer_addch(dest, ch);
278         }
279     }
280     mutt_buffer_addch(dest, 0);  /* terminate the string */
281     tok->dptr = vskipspaces(tok->dptr);
282     return 0;
283 }
284