* Rewrite support for specific SSL encryption protocols, including
[alpine.git] / alpine / pipe.c
blobb03dc9713adf3f00369d35c8a83b365334beab59
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: pipe.c 155 2006-09-29 23:28:46Z hubert@u.washington.edu $";
3 #endif
4 /*
5 * ========================================================================
6 * Copyright 2013-2018 Eduardo Chappa
7 * Copyright 2006-2007 University of Washington
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * ========================================================================
19 #include "../pith/headers.h"
20 #include "../pith/pipe.h"
21 #include "../pith/stream.h"
22 #include "../pith/status.h"
24 #include "signal.h"
25 #include "pipe.h"
29 * Support structure and functions to support piping raw message texts...
31 static struct raw_pipe_data {
32 MAILSTREAM *stream;
33 unsigned long msgno, len;
34 long char_limit, flags;
35 char *cur, *body;
36 } raw_pipe;
39 int
40 raw_pipe_getc(unsigned char *c)
42 static char *free_this = NULL;
45 * What is this if doing?
47 * If((just_starting && unsuccessful_fetch_header)
48 * || (no_chars_available && haven't_fetched_body
49 * && (not_supposed_to_fetch
50 * || (supposed_to_fetch_all && unsuccessful_fetch_text)
51 * || (supposed_to_partial_fetch && unsuccessful_partial_fetch))
52 * || (no_chars_available))
53 * return(0);
55 * otherwise, fall through and return next character
57 if((!raw_pipe.cur
58 && !(raw_pipe.cur = mail_fetch_header(raw_pipe.stream, raw_pipe.msgno,
59 NULL, NULL,
60 &raw_pipe.len,
61 raw_pipe.flags)))
62 || ((raw_pipe.len <= 0L) && !raw_pipe.body
63 && (raw_pipe.char_limit == 0L
64 || (raw_pipe.char_limit < 0L
65 && !(raw_pipe.cur = raw_pipe.body =
66 pine_mail_fetch_text(raw_pipe.stream,
67 raw_pipe.msgno,
68 NULL,
69 &raw_pipe.len,
70 raw_pipe.flags)))
71 || (raw_pipe.char_limit > 0L
72 && !(raw_pipe.cur = raw_pipe.body =
73 pine_mail_partial_fetch_wrapper(raw_pipe.stream,
74 raw_pipe.msgno,
75 NULL,
76 &raw_pipe.len,
77 raw_pipe.flags,
78 (unsigned long) raw_pipe.char_limit,
79 &free_this, 1)))))
80 || (raw_pipe.len <= 0L)){
82 if(free_this)
83 fs_give((void **) &free_this);
85 return(0);
88 if(raw_pipe.char_limit > 0L
89 && raw_pipe.body
90 && raw_pipe.len > raw_pipe.char_limit)
91 raw_pipe.len = raw_pipe.char_limit;
93 if(raw_pipe.len > 0L){
94 *c = (unsigned char) *raw_pipe.cur++;
95 raw_pipe.len--;
96 return(1);
98 else
99 return(0);
105 * Set up for using raw_pipe_getc
107 * Args: stream
108 * msgno
109 * char_limit Set to -1 means whole thing
110 * 0 means headers only
111 * > 0 means headers plus char_limit body chars
112 * flags -- passed to fetch functions
114 void
115 prime_raw_pipe_getc(MAILSTREAM *stream, long int msgno, long int char_limit, long int flags)
117 raw_pipe.stream = stream;
118 raw_pipe.msgno = (unsigned long) msgno;
119 raw_pipe.char_limit = char_limit;
120 raw_pipe.len = 0L;
121 raw_pipe.flags = flags;
122 raw_pipe.cur = NULL;
123 raw_pipe.body = NULL;
127 /*----------------------------------------------------------------------
128 Actually open the pipe used to write piped data down
130 Args:
131 Returns: TRUE if success, otherwise FALSE
133 ----*/
134 PIPE_S *
135 cmd_pipe_open(char *cmd, char **result, int flags, gf_io_t *pc)
137 char err[200];
138 PIPE_S *pipe;
140 if((pipe = open_system_pipe(cmd, result, NULL, flags, 0,
141 pipe_callback, pipe_report_error)) != NULL)
142 gf_set_writec(pc, pipe, 0L, PipeStar, (flags & PIPE_RAW) ? 0 : WRITE_TO_LOCALE);
143 else{
144 /* TRANSLATORS: The argument is the command name being piped to. */
145 snprintf(err, sizeof(err), _("Error opening pipe: %s"), cmd ? cmd : "?");
146 q_status_message(SM_ORDER | SM_DING, 3, 3, err) ;
149 return(pipe);