/* This function allows the user to specify a command to read stdout from in place of a normal file. If the last character in the string is a pipe (|), then we assume it is a commmand to run instead of a normal file. */ FILE *mutt_open_read (const char *path, pid_t * thepid) { FILE *f; struct stat s; int len = str_len (path); if (path[len - 1] == '|') { /* read from a pipe */ char *s = str_dup (path); s[len - 1] = 0; mutt_endwin (NULL); *thepid = mutt_create_filter (s, NULL, &f, NULL); mem_free (&s); } else { if (stat (path, &s) < 0) return (NULL); if (S_ISDIR (s.st_mode)) { errno = EINVAL; return (NULL); } f = fopen (path, "r"); *thepid = -1; } return (f); }
static QUERY *run_query (char *s, int quiet) { FILE *fp; QUERY *first = NULL; QUERY *cur = NULL; char cmd[_POSIX_PATH_MAX]; char *buf = NULL; size_t buflen; int dummy = 0; char msg[STRING]; char *p; pid_t thepid; mutt_expand_file_fmt (cmd, sizeof(cmd), QueryCmd, s); if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) { dprint (1, (debugfile, "unable to fork command: %s", cmd)); return 0; } if (!quiet) mutt_message _("Waiting for response..."); fgets (msg, sizeof (msg), fp); if ((p = strrchr (msg, '\n'))) *p = '\0'; while ((buf = mutt_read_line (buf, &buflen, fp, &dummy, 0)) != NULL) { if ((p = strtok(buf, "\t\n"))) { if (first == NULL) { first = (QUERY *) safe_calloc (1, sizeof (QUERY)); cur = first; } else { cur->next = (QUERY *) safe_calloc (1, sizeof (QUERY)); cur = cur->next; } cur->addr = rfc822_parse_adrlist (cur->addr, p); p = strtok(NULL, "\t\n"); if (p) { cur->name = safe_strdup (p); p = strtok(NULL, "\t\n"); if (p) cur->other = safe_strdup (p); } } } FREE (&buf); safe_fclose (&fp); if (mutt_wait_filter (thepid)) { dprint (1, (debugfile, "Error: %s\n", msg)); if (!quiet) mutt_error ("%s", msg); } else { if (!quiet) mutt_message ("%s", msg); } return first; }
/* This function allows the user to specify a command to read stdout from in place of a normal file. If the last character in the string is a pipe (|), then we assume it is a commmand to run instead of a normal file. */ FILE *mutt_open_read (const char *path, pid_t *thepid) { FILE *f; int len = mutt_strlen (path); if (path[len - 1] == '|') { /* read from a pipe */ char *s = safe_strdup (path); s[len - 1] = 0; endwin (); *thepid = mutt_create_filter (s, NULL, &f, NULL); safe_free ((void **) &s); } else { f = fopen (path, "r"); *thepid = -1; } return (f); }
int mutt_print_attachment (FILE *fp, BODY *a) { char newfile[_POSIX_PATH_MAX] = ""; char type[STRING]; pid_t thepid; FILE *ifp, *fpout; short unlink_newfile = 0; snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype); if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) { char command[_POSIX_PATH_MAX+STRING]; rfc1524_entry *entry; int piped = FALSE; dprint (2, (debugfile, "Using mailcap...\n")); entry = rfc1524_new_entry (); rfc1524_mailcap_lookup (a, type, entry, M_PRINT); if (rfc1524_expand_filename (entry->nametemplate, a->filename, newfile, sizeof (newfile))) { if (!fp) { if (safe_symlink(a->filename, newfile) == -1) { if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES) { rfc1524_free_entry (&entry); return 0; } strfcpy (newfile, a->filename, sizeof (newfile)); } else unlink_newfile = 1; } } /* in recv mode, save file to newfile first */ if (fp) mutt_save_attachment (fp, a, newfile, 0, NULL); strfcpy (command, entry->printcommand, sizeof (command)); piped = rfc1524_expand_command (a, newfile, type, command, sizeof (command)); mutt_endwin (NULL); /* interactive program */ if (piped) { if ((ifp = fopen (newfile, "r")) == NULL) { mutt_perror ("fopen"); rfc1524_free_entry (&entry); return (0); } if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) { mutt_perror _("Can't create filter"); rfc1524_free_entry (&entry); safe_fclose (&ifp); return 0; } mutt_copy_stream (ifp, fpout); safe_fclose (&fpout); safe_fclose (&ifp); if (mutt_wait_filter (thepid) || option (OPTWAITKEY)) mutt_any_key_to_continue (NULL); } else { if (mutt_system (command) || option (OPTWAITKEY)) mutt_any_key_to_continue (NULL); } if (fp) mutt_unlink (newfile); else if (unlink_newfile) unlink(newfile); rfc1524_free_entry (&entry); return (1); } if (!ascii_strcasecmp ("text/plain", type) || !ascii_strcasecmp ("application/postscript", type)) { return (mutt_pipe_attachment (fp, a, NONULL(PrintCmd), NULL)); } else if (mutt_can_decode (a)) { /* decode and print */ int rc = 0; ifp = NULL; fpout = NULL; mutt_mktemp (newfile, sizeof (newfile)); if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) { dprint (2, (debugfile, "successfully decoded %s type attachment to %s\n", type, newfile)); if ((ifp = fopen (newfile, "r")) == NULL) { mutt_perror ("fopen"); goto bail0; } dprint (2, (debugfile, "successfully opened %s read-only\n", newfile)); mutt_endwin (NULL); if ((thepid = mutt_create_filter (NONULL(PrintCmd), &fpout, NULL, NULL)) < 0) { mutt_perror _("Can't create filter"); goto bail0; } dprint (2, (debugfile, "Filter created.\n")); mutt_copy_stream (ifp, fpout); safe_fclose (&fpout); safe_fclose (&ifp); if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY)) mutt_any_key_to_continue (NULL); rc = 1; } bail0: safe_fclose (&ifp); safe_fclose (&fpout); mutt_unlink (newfile); return rc; } else { mutt_error _("I don't know how to print that!"); return 0; } }
/* returns 1 on success, 0 on error */ int mutt_pipe_attachment (FILE *fp, BODY *b, const char *path, char *outfile) { pid_t thepid; int out = -1; int rv = 0; if (outfile && *outfile) if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) { mutt_perror ("open"); return 0; } mutt_endwin (NULL); if (fp) { /* recv case */ STATE s; memset (&s, 0, sizeof (STATE)); if (outfile && *outfile) thepid = mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1); else thepid = mutt_create_filter (path, &s.fpout, NULL, NULL); if (thepid < 0) { mutt_perror _("Can't create filter"); goto bail; } s.fpin = fp; mutt_decode_attachment (b, &s); safe_fclose (&s.fpout); } else { /* send case */ FILE *ifp, *ofp; if ((ifp = fopen (b->filename, "r")) == NULL) { mutt_perror ("fopen"); if (outfile && *outfile) { close (out); unlink (outfile); } return 0; } if (outfile && *outfile) thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1); else thepid = mutt_create_filter (path, &ofp, NULL, NULL); if (thepid < 0) { mutt_perror _("Can't create filter"); safe_fclose (&ifp); goto bail; } mutt_copy_stream (ifp, ofp); safe_fclose (&ofp); safe_fclose (&ifp); } rv = 1; bail: if (outfile && *outfile) close (out); /* * check for error exit from child process */ if (mutt_wait_filter (thepid) != 0) rv = 0; if (rv == 0 || option (OPTWAITKEY)) mutt_any_key_to_continue (NULL); return rv; }
static int _mutt_pipe_message (HEADER *h, char *cmd, int decode, int print, int split, char *sep) { int i, rc = 0; pid_t thepid; FILE *fpout; /* mutt_endwin (NULL); is this really needed here ? it makes the screen flicker on pgp and s/mime messages, before asking for a passphrase... Oliver Ehli */ if (h) { mutt_message_hook (Context, h, M_MESSAGEHOOK); if (WithCrypto && decode) { mutt_parse_mime_message (Context, h); if(h->security & ENCRYPT && !crypt_valid_passphrase(h->security)) return 1; } mutt_endwin (NULL); if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) { mutt_perror _("Can't create filter process"); return 1; } pipe_msg (h, fpout, decode, print); safe_fclose (&fpout); rc = mutt_wait_filter (thepid); } else { /* handle tagged messages */ if (WithCrypto && decode) { for (i = 0; i < Context->vcount; i++) if(Context->hdrs[Context->v2r[i]]->tagged) { mutt_message_hook (Context, Context->hdrs[Context->v2r[i]], M_MESSAGEHOOK); mutt_parse_mime_message(Context, Context->hdrs[Context->v2r[i]]); if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT && !crypt_valid_passphrase(Context->hdrs[Context->v2r[i]]->security)) return 1; } } if (split) { for (i = 0; i < Context->vcount; i++) { if (Context->hdrs[Context->v2r[i]]->tagged) { mutt_message_hook (Context, Context->hdrs[Context->v2r[i]], M_MESSAGEHOOK); mutt_endwin (NULL); if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) { mutt_perror _("Can't create filter process"); return 1; } pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print); /* add the message separator */ if (sep) fputs (sep, fpout); safe_fclose (&fpout); if (mutt_wait_filter (thepid) != 0) rc = 1; } } } else { mutt_endwin (NULL); if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) { mutt_perror _("Can't create filter process"); return 1; } for (i = 0; i < Context->vcount; i++) { if (Context->hdrs[Context->v2r[i]]->tagged) { mutt_message_hook (Context, Context->hdrs[Context->v2r[i]], M_MESSAGEHOOK); pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print); /* add the message separator */ if (sep) fputs (sep, fpout); } } safe_fclose (&fpout); if (mutt_wait_filter (thepid) != 0) rc = 1; } } if (rc || option (OPTWAITKEY)) mutt_any_key_to_continue (NULL); return rc; }
int mutt_extract_token (BUFFER *dest, BUFFER *tok, int flags) { char ch; char qc = 0; /* quote char */ char *pc; /* reset the destination pointer to the beginning of the buffer */ dest->dptr = dest->data; SKIPWS (tok->dptr); while ((ch = *tok->dptr)) { if (!qc) { if ((ISSPACE (ch) && !(flags & M_TOKEN_SPACE)) || (ch == '#' && !(flags & M_TOKEN_COMMENT)) || (ch == '=' && (flags & M_TOKEN_EQUAL)) || (ch == ';' && !(flags & M_TOKEN_SEMICOLON)) || ((flags & M_TOKEN_PATTERN) && strchr ("~!|", ch))) break; } tok->dptr++; if (ch == qc) qc = 0; /* end of quote */ else if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) qc = ch; else if (ch == '\\' && qc != '\'') { if (!*tok->dptr) return -1; /* premature end of token */ switch (ch = *tok->dptr++) { case 'c': case 'C': if (!*tok->dptr) return -1; /* premature end of token */ mutt_buffer_addch (dest, (toupper (*tok->dptr) - '@') & 0x7f); tok->dptr++; break; case 'r': mutt_buffer_addch (dest, '\r'); break; case 'n': mutt_buffer_addch (dest, '\n'); break; case 't': mutt_buffer_addch (dest, '\t'); break; case 'f': mutt_buffer_addch (dest, '\f'); break; case 'e': mutt_buffer_addch (dest, '\033'); break; default: if (isdigit ((unsigned char) ch) && isdigit ((unsigned char) *tok->dptr) && isdigit ((unsigned char) *(tok->dptr + 1))) { mutt_buffer_addch (dest, (ch << 6) + (*tok->dptr << 3) + *(tok->dptr + 1) - 3504); tok->dptr += 2; } else mutt_buffer_addch (dest, ch); } } else if (ch == '^' && (flags & M_TOKEN_CONDENSE)) { if (!*tok->dptr) return -1; /* premature end of token */ ch = *tok->dptr++; if (ch == '^') mutt_buffer_addch (dest, ch); else if (ch == '[') mutt_buffer_addch (dest, '\033'); else if (isalpha ((unsigned char) ch)) mutt_buffer_addch (dest, toupper (ch) - '@'); else { mutt_buffer_addch (dest, '^'); mutt_buffer_addch (dest, ch); } } else if (ch == '`' && (!qc || qc == '"')) { #ifndef LIBMUTT FILE *fp; pid_t pid; char *cmd, *ptr; size_t expnlen; BUFFER expn; int line = 0; pc = tok->dptr; do { if ((pc = strpbrk (pc, "\\`"))) { /* skip any quoted chars */ if (*pc == '\\') pc += 2; } } while (pc && *pc != '`'); if (!pc) { dprint (1, (debugfile, "mutt_get_token: mismatched backtics\n")); return (-1); } cmd = mutt_substrdup (tok->dptr, pc); if ((pid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) { dprint (1, (debugfile, "mutt_get_token: unable to fork command: %s", cmd)); return (-1); } FREE (&cmd); tok->dptr = pc + 1; /* read line */ memset (&expn, 0, sizeof (expn)); expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line); fclose (fp); mutt_wait_filter (pid); /* if we got output, make a new string consiting of the shell ouptput plus whatever else was left on the original line */ /* BUT: If this is inside a quoted string, directly add output to * the token */ if (expn.data && qc) { mutt_buffer_addstr (dest, expn.data); FREE (&expn.data); } else if (expn.data) { expnlen = mutt_strlen (expn.data); tok->dsize = expnlen + mutt_strlen (tok->dptr) + 1; ptr = safe_malloc (tok->dsize); memcpy (ptr, expn.data, expnlen); strcpy (ptr + expnlen, tok->dptr); if (tok->destroy) FREE (&tok->data); tok->data = ptr; tok->dptr = ptr; tok->destroy = 1; /* mark that the caller should destroy this data */ ptr = NULL; FREE (&expn.data); } #else int backtick_not_supported__file_bug = 0; assert(backtick_not_supported__file_bug); #endif } else if (ch == '$' && (!qc || qc == '"') && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr))) { char *env = NULL, *var = NULL; if (*tok->dptr == '{') { tok->dptr++; if ((pc = strchr (tok->dptr, '}'))) { var = mutt_substrdup (tok->dptr, pc); tok->dptr = pc + 1; } } else { for (pc = tok->dptr; isalpha ((unsigned char) *pc) || *pc == '_'; pc++) ; var = mutt_substrdup (tok->dptr, pc); tok->dptr = pc; } if (var && (env = getenv (var))) mutt_buffer_addstr (dest, env); FREE (&var); } else mutt_buffer_addch (dest, ch); } mutt_buffer_addch (dest, 0); /* terminate the string */ SKIPWS (tok->dptr); return 0; }