~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to alpine/pipe.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 2006 University of Washington
 
7
 *
 
8
 * Licensed under the Apache License, Version 2.0 (the "License");
 
9
 * you may not use this file except in compliance with the License.
 
10
 * You may obtain a copy of the License at
 
11
 *
 
12
 *     http://www.apache.org/licenses/LICENSE-2.0
 
13
 *
 
14
 * ========================================================================
 
15
 */
 
16
 
 
17
 
 
18
#include "../pith/headers.h"
 
19
#include "../pith/pipe.h"
 
20
#include "../pith/stream.h"
 
21
#include "../pith/status.h"
 
22
 
 
23
#include "signal.h"
 
24
#include "pipe.h"
 
25
 
 
26
 
 
27
/*
 
28
 * Support structure and functions to support piping raw message texts...
 
29
 */
 
30
static struct raw_pipe_data {
 
31
    MAILSTREAM   *stream;
 
32
    unsigned long msgno, len;
 
33
    long          char_limit, flags;
 
34
    char         *cur, *body;
 
35
} raw_pipe;
 
36
 
 
37
 
 
38
int
 
39
raw_pipe_getc(unsigned char *c)
 
40
{
 
41
    static char *free_this = NULL;
 
42
 
 
43
    /*
 
44
     * What is this if doing?
 
45
     *
 
46
     * If((just_starting && unsuccessful_fetch_header)
 
47
     *    || (no_chars_available && haven't_fetched_body
 
48
     *        && (not_supposed_to_fetch
 
49
     *            || (supposed_to_fetch_all && unsuccessful_fetch_text)
 
50
     *            || (supposed_to_partial_fetch && unsuccessful_partial_fetch))
 
51
     *    || (no_chars_available))
 
52
     *   return(0);
 
53
     *
 
54
     * otherwise, fall through and return next character
 
55
     */
 
56
    if((!raw_pipe.cur
 
57
        && !(raw_pipe.cur = mail_fetch_header(raw_pipe.stream, raw_pipe.msgno,
 
58
                                              NULL, NULL,
 
59
                                              &raw_pipe.len,
 
60
                                              raw_pipe.flags)))
 
61
       || ((raw_pipe.len <= 0L) && !raw_pipe.body
 
62
           && (raw_pipe.char_limit == 0L
 
63
               || (raw_pipe.char_limit < 0L
 
64
                   && !(raw_pipe.cur = raw_pipe.body =
 
65
                                   pine_mail_fetch_text(raw_pipe.stream,
 
66
                                                        raw_pipe.msgno,
 
67
                                                        NULL,
 
68
                                                        &raw_pipe.len,
 
69
                                                        raw_pipe.flags)))
 
70
               || (raw_pipe.char_limit > 0L
 
71
                   && !(raw_pipe.cur = raw_pipe.body =
 
72
                        pine_mail_partial_fetch_wrapper(raw_pipe.stream,
 
73
                                           raw_pipe.msgno,
 
74
                                           NULL,
 
75
                                           &raw_pipe.len,
 
76
                                           raw_pipe.flags,
 
77
                                           (unsigned long) raw_pipe.char_limit,
 
78
                                           &free_this, 1)))))
 
79
       || (raw_pipe.len <= 0L)){
 
80
 
 
81
        if(free_this)
 
82
          fs_give((void **) &free_this);
 
83
 
 
84
        return(0);
 
85
    }
 
86
 
 
87
    if(raw_pipe.char_limit > 0L
 
88
       && raw_pipe.body
 
89
       && raw_pipe.len > raw_pipe.char_limit)
 
90
      raw_pipe.len = raw_pipe.char_limit;
 
91
 
 
92
    if(raw_pipe.len > 0L){
 
93
        *c = (unsigned char) *raw_pipe.cur++;
 
94
        raw_pipe.len--;
 
95
        return(1);
 
96
    }
 
97
    else
 
98
      return(0);
 
99
 
 
100
}
 
101
 
 
102
 
 
103
/*
 
104
 * Set up for using raw_pipe_getc
 
105
 *
 
106
 * Args: stream
 
107
 *       msgno
 
108
 *       char_limit  Set to -1 means whole thing
 
109
 *                           0 means headers only
 
110
 *                         > 0 means headers plus char_limit body chars
 
111
 *       flags -- passed to fetch functions
 
112
 */
 
113
void
 
114
prime_raw_pipe_getc(MAILSTREAM *stream, long int msgno, long int char_limit, long int flags)
 
115
{
 
116
    raw_pipe.stream     = stream;
 
117
    raw_pipe.msgno      = (unsigned long) msgno;
 
118
    raw_pipe.char_limit = char_limit;
 
119
    raw_pipe.len        = 0L;
 
120
    raw_pipe.flags      = flags;
 
121
    raw_pipe.cur        = NULL;
 
122
    raw_pipe.body       = NULL;
 
123
}
 
124
 
 
125
 
 
126
/*----------------------------------------------------------------------
 
127
  Actually open the pipe used to write piped data down
 
128
 
 
129
   Args: 
 
130
   Returns: TRUE if success, otherwise FALSE
 
131
 
 
132
  ----*/
 
133
PIPE_S *
 
134
cmd_pipe_open(char *cmd, char **result, int flags, gf_io_t *pc)
 
135
{
 
136
    char    err[200];
 
137
    PIPE_S *pipe;
 
138
 
 
139
    if(pipe = open_system_pipe(cmd, result, NULL, flags, 0,
 
140
                               pipe_callback, pipe_report_error))
 
141
      gf_set_writec(pc, pipe, 0L, PipeStar, WRITE_TO_LOCALE);
 
142
    else{
 
143
        /* TRANSLATORS: The argument is the command name being piped to. */
 
144
        snprintf(err, sizeof(err), _("Error opening pipe: %s"), cmd ? cmd : "?");
 
145
        q_status_message(SM_ORDER | SM_DING, 3, 3, err) ;
 
146
    }
 
147
 
 
148
    return(pipe);
 
149
}