let m_strformat accept NULL formats.
[apps/madmutt.git] / muttlib.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 #include <lib-lib/lib-lib.h>
12
13 #include <grp.h>
14 #include <pwd.h>
15
16 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/enter.h>
19 #include <lib-sys/unix.h>
20 #include <lib-mx/mx.h>
21
22 #include "alias.h"
23 #include "mutt.h"
24 #include "attach.h"
25
26 #include "version.h"
27
28 #include <imap/imap.h>
29
30 #include <lib-crypt/crypt.h>
31
32 #define SW              (option(OPTMBOXPANE)?SidebarWidth:0)
33
34 /* Modified by blong to accept a "suggestion" for file name.  If
35  * that file exists, then construct one with unique name but 
36  * keep any extension.  This might fail, I guess.
37  * Renamed to mutt_adv_mktemp so I only have to change where it's
38  * called, and not all possible cases.
39  */
40 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
41 {
42     int fd;
43
44     fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(Tempdir) : dir, s);
45     if (fd < 0) {
46         *s = '\0';
47     } else {
48         close(fd);
49         unlink(s);
50     }
51 }
52
53 /* returns true if the header contained in "s" is in list "t" */
54 int mutt_matches_ignore (const char *s, string_list_t * t)
55 {
56   for (; t; t = t->next) {
57     if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
58         || *t->data == '*')
59       return 1;
60   }
61   return 0;
62 }
63
64 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
65 {
66     char p[_POSIX_PATH_MAX] = "";
67     char tmp[_POSIX_PATH_MAX];
68     const char *tail = "";
69
70     do {
71         const address_t *alias;
72
73         switch (*s) {
74           case '~':
75             if (s[1] == '/' || s[1] == '\0') {
76                 m_strcpy(p, sizeof(p), Homedir);
77                 tail = s + 1;
78             } else {
79                 struct passwd *pw;
80                 tail = m_strchrnul(s + 1, '/');
81
82                 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
83
84                 if ((pw = getpwnam(tmp))) {
85                     m_strcpy(p, sizeof(p), pw->pw_dir);
86                 } else {
87                     /* user not found! */
88                     tail = s;
89                 }
90             }
91             break;
92
93           case '=':
94           case '+':
95             /* if folder = imap[s]://host/: don't append slash */
96             if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
97             &&  Maildir[m_strlen(Maildir) - 1] == '/') {
98                 m_strcpy(p, sizeof(p), Maildir);
99             } else {
100                 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
101             }
102
103             tail = s + 1;
104             break;
105
106             /* elm compatibility, @ expands alias to user name */
107
108           case '@':
109             if ((alias = alias_lookup(s + 1))) {
110                 HEADER h;
111                 header_init(&h);
112                 h.env = envelope_new();
113                 h.env->from = h.env->to = (address_t *)alias;
114                 mutt_default_save(p, sizeof (p), &h);
115                 h.env->from = h.env->to = NULL;
116                 header_wipe(&h);
117
118                 if (*p != '@') {
119                     /* recurse iff the result do not starts with '@' */
120                     m_strcpy(s, slen, p);
121                     continue;
122                 }
123             }
124             break;
125
126           case '>':
127             m_strcpy(p, sizeof(p), Inbox);
128             tail = s + 1;
129             break;
130
131           case '<':
132             m_strcpy(p, sizeof(p), Outbox);
133             tail = s + 1;
134             break;
135
136           case '!':
137             if (s[1] == '!') {
138                 m_strcpy(p, sizeof(p), LastFolder);
139                 tail = s + 2;
140             } else {
141                 m_strcpy(p, sizeof(p), Spoolfile);
142                 tail = s + 1;
143             }
144             break;
145
146           case '-':
147             m_strcpy(p, sizeof(p), NONULL(LastFolder));
148             tail = s + 1;
149             break;
150
151           case '^':
152             m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
153             tail = s + 1;
154             break;
155
156           default:
157             *p = '\0';
158             tail = s;
159         }
160     } while (0);
161
162     if (rx) {
163         char q[_POSIX_PATH_MAX];
164         rx_sanitize_string(q, sizeof(q), p);
165         snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
166     } else {
167         snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
168     }
169
170     return m_strcpy(s, slen, tmp);
171 }
172
173 void mutt_mktemp(char *s)
174 {
175     int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(Tempdir), NULL);
176     if (fd < 0) {
177         *s = '\0';
178     } else {
179         close(fd);
180         unlink(s);
181     }
182 }
183
184 /* collapse the pathname using ~ or = when possible */
185 void mutt_pretty_mailbox (char *s)
186 {
187   char *p = s, *q = s;
188   ssize_t len;
189   url_scheme_t scheme;
190
191   scheme = url_check_scheme (s);
192
193   if (scheme == U_IMAP || scheme == U_IMAPS) {
194     imap_pretty_mailbox (s);
195     return;
196   }
197
198   /* if s is an url, only collapse path component */
199   if (scheme != U_UNKNOWN) {
200     p = strchr (s, ':') + 1;
201     if (!m_strncmp (p, "//", 2))
202       q = strchr (p + 2, '/');
203     if (!q)
204       q = strchr (p, '\0');
205     p = q;
206   }
207
208   /* first attempt to collapse the pathname */
209   while (*p) {
210     if (*p == '/' && p[1] == '/') {
211       *q++ = '/';
212       p += 2;
213     }
214     else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
215       *q++ = '/';
216       p += 3;
217     }
218     else
219       *q++ = *p++;
220   }
221   *q = 0;
222
223   if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
224       s[len] == '/') {
225     *s++ = '=';
226     memmove (s, s + len, m_strlen(s + len) + 1);
227   }
228   else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
229            s[len] == '/') {
230     *s++ = '~';
231     memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
232   }
233 }
234
235 /* return 0 on success, -1 on abort, 1 on error */
236 int mutt_check_overwrite (const char *attname, const char *path,
237                           char *fname, ssize_t flen, int *append,
238                           char **directory)
239 {
240   int rc = 0;
241   char tmp[_POSIX_PATH_MAX];
242   struct stat st;
243
244   m_strcpy(fname, flen, path);
245   if (access (fname, F_OK) != 0)
246     return 0;
247   if (stat (fname, &st) != 0)
248     return -1;
249   if (S_ISDIR (st.st_mode)) {
250     if (directory) {
251       switch (mutt_multi_choice
252               (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
253                _("yna"))) {
254       case 3:                  /* all */
255         m_strreplace(directory, fname);
256         break;
257       case 1:                  /* yes */
258         p_delete(directory);
259         break;
260       case -1:                 /* abort */
261         p_delete(directory);
262         return -1;
263       case 2:                  /* no */
264         p_delete(directory);
265         return 1;
266       }
267     }
268     else
269       if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
270                              M_YES)) != M_YES)
271       return (rc == M_NO) ? 1 : -1;
272
273     if (!attname || !attname[0]) {
274       tmp[0] = 0;
275       if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
276                           M_FILE | M_CLEAR) != 0 || !tmp[0])
277         return (-1);
278       mutt_concat_path(fname, flen, path, tmp);
279     }
280     else
281       mutt_concat_path(fname, flen, path, mutt_basename(attname));
282   }
283
284   if (*append == 0 && access (fname, F_OK) == 0) {
285     switch (mutt_multi_choice
286             (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
287     {
288     case -1:                   /* abort */
289       return -1;
290     case 3:                    /* cancel */
291       return 1;
292
293     case 2:                    /* append */
294       *append = M_SAVE_APPEND;
295       break;
296     case 1:                    /* overwrite */
297       *append = M_SAVE_OVERWRITE;
298       break;
299     }
300   }
301   return 0;
302 }
303
304 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
305 {
306     if (a && a->mailbox) {
307         m_strcpy(d, dsize, a->mailbox);
308
309         if (!option(OPTSAVEADDRESS)) {
310             char *p = strpbrk(d, "%@");
311             if (p)
312                 *p = '\0';
313         }
314         m_strtolower(d);
315     } else {
316         *d = '\0';
317     }
318 }
319
320 void mutt_safe_path(char *s, ssize_t l, address_t *a)
321 {
322     mutt_save_path(s, l, a);
323
324     while (*s) {
325         if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
326             *s = '_';
327         s++;
328     }
329 }
330
331 ssize_t m_strformat(char *dst, ssize_t dlen, const char *fmt,
332                     format_t *callback, anytype cdata, format_flag flags)
333 {
334     ssize_t pos = flags & M_FORMAT_ARROWCURSOR ? 3 : 0;
335
336     m_strpad(dst, dlen, '\0', pos + 1);
337     if (!fmt)
338         return;
339
340     while (*fmt) {
341         int ch;
342
343         if (*fmt == '%') {
344             char ifstr[STRING], elstr[STRING], prefix[STRING];
345
346             *ifstr = *elstr = *prefix = '\0';
347
348             if (*++fmt == '%') {
349                 pos += m_strputc(dst + pos, dlen - pos, *fmt++);
350                 continue;
351             }
352
353             if (*fmt == '?') {
354                 flags |= M_FORMAT_OPTIONAL;
355                 fmt++;
356             } else {
357                 ssize_t pfxlen;
358                 flags &= ~M_FORMAT_OPTIONAL;
359
360                 /* eat the format string */
361                 pfxlen = strspn(fmt, "0123456789.-");
362                 m_strncpy(prefix, sizeof(prefix), fmt, pfxlen);
363                 fmt   += pfxlen;
364             }
365
366             /* save the character to switch on */
367             if (!(ch = *fmt++))
368                 break;
369
370             if (flags & M_FORMAT_OPTIONAL) {
371                 ssize_t iflen;
372                 const char *p;
373
374                 if (*fmt++ != '?')
375                     break;                /* bad format */
376
377                 /* eat the `if' part of the string */
378                 iflen = strcspn(fmt, "?&");
379                 m_strncpy(ifstr, ssizeof(ifstr), fmt, iflen);
380                 fmt  += iflen;
381
382                 /* eat the `else' part of the string (optional) */
383                 if (*fmt == '&')
384                     fmt++;                /* skip the & */
385
386                 p = m_strchrnul(fmt, '?');
387                 m_strncpy(elstr, ssizeof(elstr), fmt, p - fmt);
388                 fmt = p;
389
390                 if (!*fmt++)              /* move past the trailing `?' */
391                     break;                /* bad format */
392             }
393
394             switch (ch) {
395                 ssize_t col;
396                 char lower, nodots, buf[LONG_STRING];
397
398               case '>':                 /* right justify to EOL */
399                 col = mutt_strwidth(dst);
400
401                 ch = *fmt++;            /* pad char */
402
403                 if (COLS - SW > col) {
404                     m_strformat(buf, sizeof(buf), fmt, callback, cdata, flags);
405                     pos += m_strpad(dst + pos, dlen - pos, ch,
406                                     COLS - SW - col - mutt_strwidth(buf));
407                     pos += m_strcpy(dst + pos, dlen - pos, buf);
408                 }
409                 return pos;             /* skip rest of input */
410
411               case '|':                 /* pad to EOL */
412                 return pos + m_strpad(dst + pos, dlen - pos, *fmt,
413                                       COLS - SW - mutt_strwidth(dst));
414
415               default:
416                 lower = nodots = 0;
417
418                 while (ch == '_' || ch == ':') {
419                     lower  |= ch == '_';
420                     nodots |= ch == ':';
421                     ch = *fmt++;
422                 }
423
424                 /* use callback function to handle this case */
425                 fmt = callback(buf, sizeof (buf), ch, fmt, prefix,
426                                ifstr, elstr, cdata, flags);
427
428                 if (lower)
429                     m_strtolower(buf);
430
431                 if (nodots) {
432                     char *p;
433
434                     for (p = buf; *p; p++) {
435                         if (*p == '.')
436                             *p = '_';
437                     }
438                 }
439
440                 pos += m_strcpy(dst + pos, dlen - pos, buf);
441                 break;
442             }
443             continue;
444         }
445
446         if (*fmt == '\\') {
447             if (!*++fmt)
448                 break;
449             switch ((ch = *fmt++)) {
450               case 'n': pos += m_strputc(dst + pos, dlen - pos, '\n'); break;
451               case 't': pos += m_strputc(dst + pos, dlen - pos, '\t'); break;
452               case 'r': pos += m_strputc(dst + pos, dlen - pos, '\r'); break;
453               case 'f': pos += m_strputc(dst + pos, dlen - pos, '\f'); break;
454               case 'v': pos += m_strputc(dst + pos, dlen - pos, '\v'); break;
455               default:  pos += m_strputc(dst + pos, dlen - pos, ch);   break;
456             }
457         } else {
458             ssize_t len = strcspn(fmt, "%\\");
459
460             pos += m_strncpy(dst + pos, dlen - pos, fmt, len);
461             fmt += len;
462         }
463     }
464
465     return pos;
466 }
467
468 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
469 int mutt_save_confirm (const char *s, struct stat *st)
470 {
471   char tmp[_POSIX_PATH_MAX];
472   int ret = 0;
473   int rc;
474   int magic = 0;
475
476   magic = mx_get_magic (s);
477
478   if (magic == M_POP) {
479     mutt_error _("Can't save message to POP mailbox.");
480
481     return 1;
482   }
483
484 #ifdef USE_NNTP
485   if (magic == M_NNTP) {
486     mutt_error _("Can't save message to newsserver.");
487
488     return 0;
489   }
490 #endif
491
492   if (magic > 0 && !mx_access (s, W_OK)) {
493     if (option (OPTCONFIRMAPPEND) &&
494         (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
495       /* if we're appending to the trash, there's no point in asking */
496       snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
497       if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
498         ret = 1;
499       else if (rc == -1)
500         ret = -1;
501     }
502   }
503
504   if (stat (s, st) != -1) {
505     if (magic == -1) {
506       mutt_error (_("%s is not a mailbox!"), s);
507       return 1;
508     }
509   } else {
510     if (magic != M_IMAP)
511     {
512       st->st_mtime = 0;
513       st->st_atime = 0;
514
515       if (errno == ENOENT) {
516         if (option (OPTCONFIRMCREATE)) {
517           snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
518           if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
519             ret = 1;
520           else if (rc == -1)
521             ret = -1;
522         }
523       } else {
524         mutt_perror (s);
525         return 1;
526       }
527     }
528   }
529
530   CLEARLINE (LINES - 1);
531   return (ret);
532 }
533
534 void mutt_sleep (short s)
535 {
536     sleep(MAX(s, SleepTime));
537 }
538
539 const char *mutt_make_version (int full)
540 {
541   static char vstring[STRING];
542
543   if (full)
544     snprintf (vstring, sizeof (vstring),
545               "Madmutt/%s-r%s (based on Mutt 1.5.11)",
546               MUTT_VERSION, MUTT_REVISION);
547   else
548     snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
549               MUTT_VERSION, MUTT_REVISION);
550   return vstring;
551 }
552
553 /* return 1 if address lists are strictly identical */
554 static int mutt_cmp_addr (const address_t * a, const address_t * b)
555 {
556   while (a && b) {
557     if (m_strcmp(a->mailbox, b->mailbox) ||
558         m_strcmp(a->personal, b->personal))
559       return (0);
560
561     a = a->next;
562     b = b->next;
563   }
564   if (a || b)
565     return (0);
566
567   return (1);
568 }
569
570 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
571 {
572   while (a && b) {
573     if (m_strcmp(a->data, b->data))
574       return (0);
575
576     a = a->next;
577     b = b->next;
578   }
579   if (a || b)
580     return (0);
581
582   return (1);
583 }
584
585 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
586 {
587   if (e1 && e2) {
588     if (m_strcmp(e1->message_id, e2->message_id) ||
589         m_strcmp(e1->subject, e2->subject) ||
590         !mutt_cmp_list (e1->references, e2->references) ||
591         !mutt_cmp_addr (e1->from, e2->from) ||
592         !mutt_cmp_addr (e1->sender, e2->sender) ||
593         !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
594         !mutt_cmp_addr (e1->to, e2->to) ||
595         !mutt_cmp_addr (e1->cc, e2->cc) ||
596         !mutt_cmp_addr (e1->return_path, e2->return_path))
597       return (0);
598     else
599       return (1);
600   }
601   else {
602     if (e1 == NULL && e2 == NULL)
603       return (1);
604     else
605       return (0);
606   }
607 }
608
609 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
610 {
611   if (b1->type != b2->type ||
612       b1->encoding != b2->encoding ||
613       m_strcmp(b1->subtype, b2->subtype) ||
614       m_strcmp(b1->description, b2->description) ||
615       !parameter_equal(b1->parameter, b2->parameter) ||
616       b1->length != b2->length)
617     return (0);
618   return (1);
619 }
620 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
621   if (h1 && h2) {
622     if (h1->received != h2->received ||
623         h1->date_sent != h2->date_sent ||
624         h1->content->length != h2->content->length ||
625         h1->lines != h2->lines ||
626         h1->zhours != h2->zhours ||
627         h1->zminutes != h2->zminutes ||
628         h1->zoccident != h2->zoccident ||
629         h1->mime != h2->mime ||
630         !mutt_cmp_env (h1->env, h2->env) ||
631         !mutt_cmp_body (h1->content, h2->content))
632       return (0);
633     else
634       return (1);
635   }
636   else {
637     if (h1 == NULL && h2 == NULL)
638       return (1);
639     else
640       return (0);
641   }
642 }
643
644
645 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
646 {
647     char ch;
648     char qc = 0;                  /* quote char */
649     char *pc;
650
651     /* reset the destination pointer to the beginning of the buffer */
652     dest->dptr = dest->data;
653
654     tok->dptr = vskipspaces(tok->dptr);
655     while ((ch = *tok->dptr)) {
656         if (!qc) {
657             if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
658             || (ch == '#' && !(flags & M_TOKEN_COMMENT))
659             || (ch == '=' && (flags & M_TOKEN_EQUAL))
660             || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
661             || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
662             {
663                 break;
664             }
665         }
666
667         tok->dptr++;
668
669         if (ch == qc) {
670             qc = 0;                     /* end of quote */
671         } else
672         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
673             qc = ch;
674         } else
675         if (ch == '\\' && qc != '\'') {
676             if (!*tok->dptr)
677                 return -1;              /* premature end of token */
678
679             switch (ch = *tok->dptr++) {
680               case 'c':
681               case 'C':
682                 if (!*tok->dptr)
683                     return -1;          /* premature end of token */
684                 mutt_buffer_addch(dest,
685                                   (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
686                 tok->dptr++;
687                 break;
688               case 'r':
689                 mutt_buffer_addch(dest, '\r');
690                 break;
691               case 'n':
692                 mutt_buffer_addch(dest, '\n');
693                 break;
694               case 't':
695                 mutt_buffer_addch(dest, '\t');
696                 break;
697               case 'f':
698                 mutt_buffer_addch(dest, '\f');
699                 break;
700               case 'e':
701                 mutt_buffer_addch(dest, '\033');
702                 break;
703               default:
704                 if (isdigit((unsigned char)ch)
705                 &&  isdigit((unsigned char)*tok->dptr)
706                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
707                 {
708                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
709                                             *(tok->dptr + 1) - 3504);
710                     tok->dptr += 2;
711                 } else {
712                     mutt_buffer_addch(dest, ch);
713                 }
714             }
715         } else
716         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
717             if (!*tok->dptr)
718                 return -1;              /* premature end of token */
719             ch = *tok->dptr++;
720             if (ch == '^') {
721                 mutt_buffer_addch(dest, ch);
722             } else
723             if (ch == '[') {
724                 mutt_buffer_addch(dest, '\033');
725             } else
726             if (isalpha((unsigned char)ch)) {
727                 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
728             } else {
729                 mutt_buffer_addch(dest, '^');
730                 mutt_buffer_addch(dest, ch);
731             }
732         } else
733         if (ch == '`' && (!qc || qc == '"')) {
734             FILE *fp;
735             pid_t pid;
736             char *cmd, *ptr;
737             ssize_t expnlen;
738             BUFFER expn;
739             int line = 0;
740
741             pc = tok->dptr;
742             do {
743                 if ((pc = strpbrk(pc, "\\`"))) {
744                     /* skip any quoted chars */
745                     if (*pc == '\\')
746                         pc += 2;
747                 }
748             } while (pc && *pc != '`');
749             if (!pc) {
750                 return (-1);
751             }
752
753             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
754             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
755                 p_delete(&cmd);
756                 return -1;
757             }
758             p_delete(&cmd);
759
760             tok->dptr = pc + 1;
761
762             /* read line */
763             p_clear(&expn, 1);
764             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
765             m_fclose(&fp);
766             mutt_wait_filter(pid);
767
768             /* if we got output, make a new string consiting of the shell ouptput
769                plus whatever else was left on the original line */
770             /* BUT: If this is inside a quoted string, directly add output to 
771              * the token */
772             if (expn.data && qc) {
773                 mutt_buffer_addstr(dest, expn.data);
774                 p_delete(&expn.data);
775             } else
776             if (expn.data) {
777                 expnlen = m_strlen(expn.data);
778                 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
779                 ptr = xmalloc(tok->dsize);
780                 memcpy(ptr, expn.data, expnlen);
781                 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
782                 if (tok->destroy)
783                     p_delete(&tok->data);
784                 tok->data = ptr;
785                 tok->dptr = ptr;
786                 tok->destroy = 1;       /* mark that the caller should destroy this data */
787                 ptr = NULL;
788                 p_delete(&expn.data);
789             }
790         } else
791         if (ch == '$' && (!qc || qc == '"')
792         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
793         {
794             char *env = NULL, *var = NULL;
795
796             if (*tok->dptr == '{') {
797                 tok->dptr++;
798                 if ((pc = strchr (tok->dptr, '}'))) {
799                     var = p_dupstr(tok->dptr, pc - tok->dptr);
800                     tok->dptr = pc + 1;
801                 }
802             } else {
803                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
804                      pc++);
805                 var = p_dupstr(tok->dptr, pc - tok->dptr);
806                 tok->dptr = pc;
807             }
808             if (var) {
809                 char tmp[STRING];
810                 if ((env = getenv (var))
811                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
812                 {
813                     mutt_buffer_addstr (dest, env);
814                 }
815             }
816             p_delete(&var);
817         } else {
818             mutt_buffer_addch(dest, ch);
819         }
820     }
821     mutt_buffer_addch(dest, 0);  /* terminate the string */
822     tok->dptr = vskipspaces(tok->dptr);
823     return 0;
824 }