Use good m_ functions, because it smell like a flower, version 2.
[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 void mutt_FormatString (char *dest,     /* output buffer */
332                         ssize_t destlen, /* output buffer len */
333                         const char *src,        /* template string */
334                         format_t * callback,    /* callback for processing */
335                         unsigned long data,     /* callback data */
336                         format_flag flags)
337 {                               /* callback flags */
338   char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
339   char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
340   ssize_t wlen, wid, count, col, len;
341
342   prefix[0] = '\0';
343   destlen--;                    /* save room for the terminal \0 */
344   wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
345   col = wlen;
346
347   while (*src && wlen < destlen) {
348     if (*src == '%') {
349       if (*++src == '%') {
350         *wptr++ = '%';
351         wlen++;
352         col++;
353         src++;
354         continue;
355       }
356
357       if (*src == '?') {
358         flags |= M_FORMAT_OPTIONAL;
359         src++;
360       }
361       else {
362         flags &= ~M_FORMAT_OPTIONAL;
363
364         /* eat the format string */
365         cp = prefix;
366         count = 0;
367         while (count < ssizeof (prefix) &&
368                (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
369         {
370           *cp++ = *src++;
371           count++;
372         }
373         *cp = 0;
374       }
375
376       if (!*src)
377         break;                  /* bad format */
378
379       ch = *src++;              /* save the character to switch on */
380
381       if (flags & M_FORMAT_OPTIONAL) {
382         if (*src != '?')
383           break;                /* bad format */
384         src++;
385
386         /* eat the `if' part of the string */
387         cp = ifstring;
388         count = 0;
389         while (count < ssizeof (ifstring) && *src && *src != '?'
390                && *src != '&') {
391           *cp++ = *src++;
392           count++;
393         }
394         *cp = 0;
395
396         /* eat the `else' part of the string (optional) */
397         if (*src == '&')
398           src++;                /* skip the & */
399         cp = elsestring;
400         count = 0;
401         while (count < ssizeof (elsestring) && *src && *src != '?') {
402           *cp++ = *src++;
403           count++;
404         }
405         *cp = 0;
406
407         if (!*src)
408           break;                /* bad format */
409
410         src++;                  /* move past the trailing `?' */
411       }
412
413       /* handle generic cases first */
414       if (ch == '>') {
415         /* right justify to EOL */
416         ch = *src++;            /* pad char */
417         /* calculate space left on line.  if we've already written more data
418            than will fit on the line, ignore the rest of the line */
419         if (DrawFullLine || option (OPTSTATUSONTOP))
420           count = (COLS < destlen ? COLS : destlen);
421         else
422           count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
423         if (count > col) {
424           count -= col;         /* how many columns left on this line */
425           mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
426           wid = m_strlen(buf);
427           if (count > wid) {
428             count -= wid;       /* how many chars to pad */
429             memset (wptr, ch, count);
430             wptr += count;
431             col += count;
432           }
433           if (wid + wlen > destlen)
434             len = destlen - wlen;
435           else
436             len = wid;
437           memcpy (wptr, buf, len);
438           wptr += len;
439           wlen += len;
440           col += mutt_strwidth (buf);
441         }
442         break;                  /* skip rest of input */
443       }
444       else if (ch == '|') {
445         /* pad to EOL */
446         ch = *src++;
447         if (destlen > COLS)
448           destlen = COLS;
449         if (destlen > wlen) {
450           count = destlen - wlen;
451           memset (wptr, ch, count);
452           wptr += count;
453         }
454         break;                  /* skip rest of input */
455       }
456       else {
457         short lower = 0;
458         short nodots = 0;
459
460         while (ch == '_' || ch == ':') {
461           if (ch == '_')
462             lower = 1;
463           else if (ch == ':')
464             nodots = 1;
465
466           ch = *src++;
467         }
468
469         /* use callback function to handle this case */
470         src =
471           callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
472                     data, flags);
473
474         if (lower)
475           m_strtolower(buf);
476         if (nodots) {
477           char *p = buf;
478
479           for (; *p; p++)
480             if (*p == '.')
481               *p = '_';
482         }
483
484         if ((len = m_strlen(buf)) + wlen > destlen)
485           len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
486
487         memcpy (wptr, buf, len);
488         wptr += len;
489         wlen += len;
490         col += mutt_strwidth (buf);
491       }
492     }
493     else if (*src == '\\') {
494       if (!*++src)
495         break;
496       switch (*src) {
497       case 'n':
498         *wptr = '\n';
499         break;
500       case 't':
501         *wptr = '\t';
502         break;
503       case 'r':
504         *wptr = '\r';
505         break;
506       case 'f':
507         *wptr = '\f';
508         break;
509       case 'v':
510         *wptr = '\v';
511         break;
512       default:
513         *wptr = *src;
514         break;
515       }
516       src++;
517       wptr++;
518       wlen++;
519       col++;
520     }
521     else {
522       unsigned int bar = strcspn(src, "%\\");
523       char *bar2 = p_dupstr(src, bar);
524
525       while (bar--) {
526         *wptr++ = *src++;
527         wlen++;
528       }
529       col += mutt_strwidth (bar2);
530       p_delete(&bar2);
531     }
532   }
533   *wptr = 0;
534 }
535
536 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
537 int mutt_save_confirm (const char *s, struct stat *st)
538 {
539   char tmp[_POSIX_PATH_MAX];
540   int ret = 0;
541   int rc;
542   int magic = 0;
543
544   magic = mx_get_magic (s);
545
546   if (magic == M_POP) {
547     mutt_error _("Can't save message to POP mailbox.");
548
549     return 1;
550   }
551
552 #ifdef USE_NNTP
553   if (magic == M_NNTP) {
554     mutt_error _("Can't save message to newsserver.");
555
556     return 0;
557   }
558 #endif
559
560   if (magic > 0 && !mx_access (s, W_OK)) {
561     if (option (OPTCONFIRMAPPEND) &&
562         (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
563       /* if we're appending to the trash, there's no point in asking */
564       snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
565       if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
566         ret = 1;
567       else if (rc == -1)
568         ret = -1;
569     }
570   }
571
572   if (stat (s, st) != -1) {
573     if (magic == -1) {
574       mutt_error (_("%s is not a mailbox!"), s);
575       return 1;
576     }
577   } else {
578     if (magic != M_IMAP)
579     {
580       st->st_mtime = 0;
581       st->st_atime = 0;
582
583       if (errno == ENOENT) {
584         if (option (OPTCONFIRMCREATE)) {
585           snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
586           if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
587             ret = 1;
588           else if (rc == -1)
589             ret = -1;
590         }
591       } else {
592         mutt_perror (s);
593         return 1;
594       }
595     }
596   }
597
598   CLEARLINE (LINES - 1);
599   return (ret);
600 }
601
602 void mutt_sleep (short s)
603 {
604     sleep(MAX(s, SleepTime));
605 }
606
607 const char *mutt_make_version (int full)
608 {
609   static char vstring[STRING];
610
611   if (full)
612     snprintf (vstring, sizeof (vstring),
613               "Madmutt/%s-r%s (based on Mutt 1.5.11)",
614               MUTT_VERSION, MUTT_REVISION);
615   else
616     snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
617               MUTT_VERSION, MUTT_REVISION);
618   return vstring;
619 }
620
621 /* return 1 if address lists are strictly identical */
622 static int mutt_cmp_addr (const address_t * a, const address_t * b)
623 {
624   while (a && b) {
625     if (m_strcmp(a->mailbox, b->mailbox) ||
626         m_strcmp(a->personal, b->personal))
627       return (0);
628
629     a = a->next;
630     b = b->next;
631   }
632   if (a || b)
633     return (0);
634
635   return (1);
636 }
637
638 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
639 {
640   while (a && b) {
641     if (m_strcmp(a->data, b->data))
642       return (0);
643
644     a = a->next;
645     b = b->next;
646   }
647   if (a || b)
648     return (0);
649
650   return (1);
651 }
652
653 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
654 {
655   if (e1 && e2) {
656     if (m_strcmp(e1->message_id, e2->message_id) ||
657         m_strcmp(e1->subject, e2->subject) ||
658         !mutt_cmp_list (e1->references, e2->references) ||
659         !mutt_cmp_addr (e1->from, e2->from) ||
660         !mutt_cmp_addr (e1->sender, e2->sender) ||
661         !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
662         !mutt_cmp_addr (e1->to, e2->to) ||
663         !mutt_cmp_addr (e1->cc, e2->cc) ||
664         !mutt_cmp_addr (e1->return_path, e2->return_path))
665       return (0);
666     else
667       return (1);
668   }
669   else {
670     if (e1 == NULL && e2 == NULL)
671       return (1);
672     else
673       return (0);
674   }
675 }
676
677 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
678 {
679   if (b1->type != b2->type ||
680       b1->encoding != b2->encoding ||
681       m_strcmp(b1->subtype, b2->subtype) ||
682       m_strcmp(b1->description, b2->description) ||
683       !parameter_equal(b1->parameter, b2->parameter) ||
684       b1->length != b2->length)
685     return (0);
686   return (1);
687 }
688 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
689   if (h1 && h2) {
690     if (h1->received != h2->received ||
691         h1->date_sent != h2->date_sent ||
692         h1->content->length != h2->content->length ||
693         h1->lines != h2->lines ||
694         h1->zhours != h2->zhours ||
695         h1->zminutes != h2->zminutes ||
696         h1->zoccident != h2->zoccident ||
697         h1->mime != h2->mime ||
698         !mutt_cmp_env (h1->env, h2->env) ||
699         !mutt_cmp_body (h1->content, h2->content))
700       return (0);
701     else
702       return (1);
703   }
704   else {
705     if (h1 == NULL && h2 == NULL)
706       return (1);
707     else
708       return (0);
709   }
710 }
711
712
713 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
714 {
715     char ch;
716     char qc = 0;                  /* quote char */
717     char *pc;
718
719     /* reset the destination pointer to the beginning of the buffer */
720     dest->dptr = dest->data;
721
722     tok->dptr = vskipspaces(tok->dptr);
723     while ((ch = *tok->dptr)) {
724         if (!qc) {
725             if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
726             || (ch == '#' && !(flags & M_TOKEN_COMMENT))
727             || (ch == '=' && (flags & M_TOKEN_EQUAL))
728             || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
729             || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
730             {
731                 break;
732             }
733         }
734
735         tok->dptr++;
736
737         if (ch == qc) {
738             qc = 0;                     /* end of quote */
739         } else
740         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
741             qc = ch;
742         } else
743         if (ch == '\\' && qc != '\'') {
744             if (!*tok->dptr)
745                 return -1;              /* premature end of token */
746
747             switch (ch = *tok->dptr++) {
748               case 'c':
749               case 'C':
750                 if (!*tok->dptr)
751                     return -1;          /* premature end of token */
752                 mutt_buffer_addch(dest,
753                                   (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
754                 tok->dptr++;
755                 break;
756               case 'r':
757                 mutt_buffer_addch(dest, '\r');
758                 break;
759               case 'n':
760                 mutt_buffer_addch(dest, '\n');
761                 break;
762               case 't':
763                 mutt_buffer_addch(dest, '\t');
764                 break;
765               case 'f':
766                 mutt_buffer_addch(dest, '\f');
767                 break;
768               case 'e':
769                 mutt_buffer_addch(dest, '\033');
770                 break;
771               default:
772                 if (isdigit((unsigned char)ch)
773                 &&  isdigit((unsigned char)*tok->dptr)
774                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
775                 {
776                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
777                                             *(tok->dptr + 1) - 3504);
778                     tok->dptr += 2;
779                 } else {
780                     mutt_buffer_addch(dest, ch);
781                 }
782             }
783         } else
784         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
785             if (!*tok->dptr)
786                 return -1;              /* premature end of token */
787             ch = *tok->dptr++;
788             if (ch == '^') {
789                 mutt_buffer_addch(dest, ch);
790             } else
791             if (ch == '[') {
792                 mutt_buffer_addch(dest, '\033');
793             } else
794             if (isalpha((unsigned char)ch)) {
795                 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
796             } else {
797                 mutt_buffer_addch(dest, '^');
798                 mutt_buffer_addch(dest, ch);
799             }
800         } else
801         if (ch == '`' && (!qc || qc == '"')) {
802             FILE *fp;
803             pid_t pid;
804             char *cmd, *ptr;
805             ssize_t expnlen;
806             BUFFER expn;
807             int line = 0;
808
809             pc = tok->dptr;
810             do {
811                 if ((pc = strpbrk(pc, "\\`"))) {
812                     /* skip any quoted chars */
813                     if (*pc == '\\')
814                         pc += 2;
815                 }
816             } while (pc && *pc != '`');
817             if (!pc) {
818                 return (-1);
819             }
820
821             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
822             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
823                 p_delete(&cmd);
824                 return -1;
825             }
826             p_delete(&cmd);
827
828             tok->dptr = pc + 1;
829
830             /* read line */
831             p_clear(&expn, 1);
832             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
833             m_fclose(&fp);
834             mutt_wait_filter(pid);
835
836             /* if we got output, make a new string consiting of the shell ouptput
837                plus whatever else was left on the original line */
838             /* BUT: If this is inside a quoted string, directly add output to 
839              * the token */
840             if (expn.data && qc) {
841                 mutt_buffer_addstr(dest, expn.data);
842                 p_delete(&expn.data);
843             } else
844             if (expn.data) {
845                 expnlen = m_strlen(expn.data);
846                 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
847                 ptr = xmalloc(tok->dsize);
848                 memcpy(ptr, expn.data, expnlen);
849                 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
850                 if (tok->destroy)
851                     p_delete(&tok->data);
852                 tok->data = ptr;
853                 tok->dptr = ptr;
854                 tok->destroy = 1;       /* mark that the caller should destroy this data */
855                 ptr = NULL;
856                 p_delete(&expn.data);
857             }
858         } else
859         if (ch == '$' && (!qc || qc == '"')
860         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
861         {
862             char *env = NULL, *var = NULL;
863
864             if (*tok->dptr == '{') {
865                 tok->dptr++;
866                 if ((pc = strchr (tok->dptr, '}'))) {
867                     var = p_dupstr(tok->dptr, pc - tok->dptr);
868                     tok->dptr = pc + 1;
869                 }
870             } else {
871                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
872                      pc++);
873                 var = p_dupstr(tok->dptr, pc - tok->dptr);
874                 tok->dptr = pc;
875             }
876             if (var) {
877                 char tmp[STRING];
878                 if ((env = getenv (var))
879                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
880                 {
881                     mutt_buffer_addstr (dest, env);
882                 }
883             }
884             p_delete(&var);
885         } else {
886             mutt_buffer_addch(dest, ch);
887         }
888     }
889     mutt_buffer_addch(dest, 0);  /* terminate the string */
890     tok->dptr = vskipspaces(tok->dptr);
891     return 0;
892 }