* For mailing lists, Alpine adds a description of the type of link
[alpine.git] / alpine / pipe.c
blob472a284ecaca935e4ac05ff4de10415d7dfedcfc
1 /*
2 * ========================================================================
3 * Copyright 2013-2022 Eduardo Chappa
4 * Copyright 2006-2007 University of Washington
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
16 #include "../pith/headers.h"
17 #include "../pith/pipe.h"
18 #include "../pith/stream.h"
19 #include "../pith/status.h"
21 #include "signal.h"
22 #include "pipe.h"
26 * Support structure and functions to support piping raw message texts...
28 static struct raw_pipe_data {
29 MAILSTREAM *stream;
30 unsigned long msgno, len;
31 long char_limit, flags;
32 char *cur, *body;
33 } raw_pipe;
36 int
37 raw_pipe_getc(unsigned char *c)
39 static char *free_this = NULL;
42 * What is this if doing?
44 * If((just_starting && unsuccessful_fetch_header)
45 * || (no_chars_available && haven't_fetched_body
46 * && (not_supposed_to_fetch
47 * || (supposed_to_fetch_all && unsuccessful_fetch_text)
48 * || (supposed_to_partial_fetch && unsuccessful_partial_fetch))
49 * || (no_chars_available))
50 * return(0);
52 * otherwise, fall through and return next character
54 if((!raw_pipe.cur
55 && !(raw_pipe.cur = mail_fetch_header(raw_pipe.stream, raw_pipe.msgno,
56 NULL, NULL,
57 &raw_pipe.len,
58 raw_pipe.flags)))
59 || ((raw_pipe.len <= 0L) && !raw_pipe.body
60 && (raw_pipe.char_limit == 0L
61 || (raw_pipe.char_limit < 0L
62 && !(raw_pipe.cur = raw_pipe.body =
63 pine_mail_fetch_text(raw_pipe.stream,
64 raw_pipe.msgno,
65 NULL,
66 &raw_pipe.len,
67 raw_pipe.flags)))
68 || (raw_pipe.char_limit > 0L
69 && !(raw_pipe.cur = raw_pipe.body =
70 pine_mail_partial_fetch_wrapper(raw_pipe.stream,
71 raw_pipe.msgno,
72 NULL,
73 &raw_pipe.len,
74 raw_pipe.flags,
75 (unsigned long) raw_pipe.char_limit,
76 &free_this, 1)))))
77 || (raw_pipe.len <= 0L)){
79 if(free_this)
80 fs_give((void **) &free_this);
82 return(0);
85 if(raw_pipe.char_limit > 0L
86 && raw_pipe.body
87 && raw_pipe.len > raw_pipe.char_limit)
88 raw_pipe.len = raw_pipe.char_limit;
90 if(raw_pipe.len > 0L){
91 *c = (unsigned char) *raw_pipe.cur++;
92 raw_pipe.len--;
93 return(1);
95 else
96 return(0);
102 * Set up for using raw_pipe_getc
104 * Args: stream
105 * msgno
106 * char_limit Set to -1 means whole thing
107 * 0 means headers only
108 * > 0 means headers plus char_limit body chars
109 * flags -- passed to fetch functions
111 void
112 prime_raw_pipe_getc(MAILSTREAM *stream, long int msgno, long int char_limit, long int flags)
114 raw_pipe.stream = stream;
115 raw_pipe.msgno = (unsigned long) msgno;
116 raw_pipe.char_limit = char_limit;
117 raw_pipe.len = 0L;
118 raw_pipe.flags = flags;
119 raw_pipe.cur = NULL;
120 raw_pipe.body = NULL;
124 /*----------------------------------------------------------------------
125 Actually open the pipe used to write piped data down
127 Args:
128 Returns: TRUE if success, otherwise FALSE
130 ----*/
131 PIPE_S *
132 cmd_pipe_open(char *cmd, char **result, int flags, gf_io_t *pc)
134 char err[200];
135 PIPE_S *pipe;
137 if((pipe = open_system_pipe(cmd, result, NULL, flags, 0,
138 pipe_callback, pipe_report_error)) != NULL)
139 gf_set_writec(pc, pipe, 0L, PipeStar, (flags & PIPE_RAW) ? 0 : WRITE_TO_LOCALE);
140 else{
141 /* TRANSLATORS: The argument is the command name being piped to. */
142 snprintf(err, sizeof(err), _("Error opening pipe: %s"), cmd ? cmd : "?");
143 q_status_message(SM_ORDER | SM_DING, 3, 3, err) ;
146 return(pipe);