File: mail_scan.c

package info (click to toggle)
postfix 0.0.19991231pl11-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,044 kB
  • ctags: 4,401
  • sloc: ansic: 33,767; makefile: 5,099; sh: 1,790; awk: 19
file content (271 lines) | stat: -rw-r--r-- 6,682 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*++
/* NAME
/*	mail_scan 3
/* SUMMARY
/*	intra-mail read routine
/* SYNOPSIS
/*	#include <mail_proto.h>
/*
/*	int	mail_scan(stream, format, ...)
/*	VSTREAM *stream;
/*	const char *format;
/*
/*	void	mail_scan_register(letter, name, scan_fn)
/*	int	letter;
/*	const char *name;
/*	int	(*scan_fn)(const char *string, char *result);
/* DESCRIPTION
/*	mail_scan() reads one or more null-delimited strings from
/*	the named stream, each string being converted according to the
/*	contents of the \fIformat\fR argument.
/*	The result value is the number of successful conversions.
/*
/*	mail_scan_register() registers an input conversion function
/*	for the specified letter.
/*
/*	Arguments:
/* .IP stream
/*	Stream to read from.
/* .IP format
/*	Format string, which is interpreted as follows:
/* .RS
/* .IP "white space"
/*	White space in the format string is ignored.
/* .IP %s
/*	The corresponding argument has type (VSTRING *).
/* .IP %d
/*	The corresponding argument has type (int *).
/* .IP %ld
/*	The corresponding argument has type (long *).
/* .IP %letter
/*	Call the input conversion routine that was registered for
/*	the specified letter.
/* .PP
/*	Anything else in a format string is a fatal error.
/* .RE
/* .IP letter
/*      Format letter that is bound to the \fIscan_fn\fR input
/*	conversion function.
/* .IP name
/*      Descriptive string for verbose logging.
/* .IP scan_fn
/*      An input conversion function. It takes as arguments:
/* .RS
/* .IP string
/*	The null-terminated string to be converted.
/* .IP result
/*	A character pointer to the result. It is up to the function
/*	to do any necessary casts to the data-specific type.
/* .RE
/* .PP
/*	The function result values are as follows:
/* .RS
/* .IP MAIL_SCAN_ERROR
/*	The expected input could not be read.
/* .IP MAIL_SCAN_DONE
/*	The operation completed successfully.
/* .IP MAIL_SCAN_MORE
/*	More input is expected.
/* .RE
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>

/* Utility library. */

#include <msg.h>
#include <vstring.h>
#include <vstream.h>
#include <vstring_vstream.h>
#include <mymalloc.h>

/* Global library. */

#include "mail_proto.h"

 /*
  * Provision for the user to register type-specific input conversion
  * routines for applications with unusual requirements.
  */
typedef struct {
    int     letter;
    char   *name;
    MAIL_SCAN_FN scanner;
} MAIL_SCAN;

MAIL_SCAN *mail_scan_tab = 0;
int     mail_scan_tablen = 0;

/* mail_scan_register - register scanner function */

void    mail_scan_register(int letter, const char *name, MAIL_SCAN_FN scanner)
{
    MAIL_SCAN *tp;

    for (tp = 0; tp < mail_scan_tab + mail_scan_tablen; tp++)
	if (tp->letter == letter)
	    msg_panic("mail_scan_register: redefined letter: %c", letter);

    /*
     * Tight fit allocation (as in: Robin Hood and the men that wear tights).
     */
    if (mail_scan_tab == 0) {
	mail_scan_tablen = 1;
	mail_scan_tab = (MAIL_SCAN *)
	    mymalloc(sizeof(*mail_scan_tab) * mail_scan_tablen);
    } else {
	mail_scan_tablen++;
	mail_scan_tab = (MAIL_SCAN *)
	    myrealloc((char *) mail_scan_tab,
		      sizeof(*mail_scan_tab) * mail_scan_tablen);
    }
    tp = mail_scan_tab + mail_scan_tablen - 1;
    tp->letter = letter;
    tp->name = mystrdup(name);
    tp->scanner = scanner;
}

/* mail_scan_any - read one null-delimited string from stream */

static int mail_scan_any(VSTREAM *stream, VSTRING *vp, char *what)
{
    if (vstring_fgets_null(vp, stream) == 0) {
	msg_warn("mail_scan_any: got EOF; expected: %s", what);
	return (-1);
    }
    if (msg_verbose)
	msg_info("mail_scan_any: read %s: %s", what, vstring_str(vp));
    return (0);
}

/* mail_scan_other - read user-defined type from stream */

static int mail_scan_other(VSTREAM *stream, VSTRING *vp,
			           MAIL_SCAN *tp, char *result)
{
    int     ret;

    while ((ret = mail_scan_any(stream, vp, tp->name)) == 0) {
	switch (tp->scanner(vstring_str(vp), result)) {
	case MAIL_SCAN_MORE:
	    break;
	case MAIL_SCAN_DONE:
	    return (0);
	case MAIL_SCAN_ERROR:
	    return (-1);
	}
    }
    return (ret);
}

/* mail_scan_long - read long integer from stream */

static int mail_scan_long(VSTREAM *stream, VSTRING *vp, long *lp)
{
    int     ret;
    char    junk;

    if ((ret = mail_scan_any(stream, vp, "long integer")) == 0) {
	if (sscanf(vstring_str(vp), "%ld%c", lp, &junk) != 1) {
	    msg_warn("mail_scan_long: bad long long: %s", vstring_str(vp));
	    ret = -1;
	}
    }
    return (ret);
}

/* mail_scan_int - read integer from stream */

static int mail_scan_int(VSTREAM *stream, VSTRING *vp, int *lp)
{
    int     ret;
    char    junk;

    if ((ret = mail_scan_any(stream, vp, "integer")) == 0) {
	if (sscanf(vstring_str(vp), "%d%c", lp, &junk) != 1) {
	    msg_warn("mail_scan_int: bad integer: %s", vstring_str(vp));
	    ret = -1;
	}
    }
    return (ret);
}

/* mail_scan - read null-delimited data from stream */

int     mail_scan(VSTREAM *stream, const char *fmt,...)
{
    va_list ap;
    int     count;

    va_start(ap, fmt);
    count = mail_vscan(stream, fmt, ap);
    va_end(ap);
    return (count);
}

/* mail_vscan - read null-delimited data from stream */

int     mail_vscan(VSTREAM *stream, const char *fmt, va_list ap)
{
    const char *cp;
    int     lflag;
    int     count;
    int     error;
    static VSTRING *tmp;
    MAIL_SCAN *tp;

    if (tmp == 0)
	tmp = vstring_alloc(100);

    for (count = 0, error = 0, cp = fmt; error == 0 && *cp != 0; cp++) {
	if (ISSPACE(*cp))
	    continue;
	if (*cp != '%')
	    msg_fatal("mail_scan: bad format: %.*s>%c<%s",
		      cp - fmt, fmt, *cp, cp + 1);
	if ((lflag = (*++cp == 'l')) != 0)
	    cp++;

	switch (*cp) {
	case 's':
	    error = mail_scan_any(stream, va_arg(ap, VSTRING *), "string");
	    break;
	case 'd':
	    if (lflag)
		error = mail_scan_long(stream, tmp, va_arg(ap, long *));
	    else
		error = mail_scan_int(stream, tmp, va_arg(ap, int *));
	    break;
	default:
	    for (tp = mail_scan_tab; tp < mail_scan_tab + mail_scan_tablen; tp++)
		if (tp->letter == *cp) {
		    if (msg_verbose)
			msg_info("mail_scan: %s", tp->name);
		    error = mail_scan_other(stream, tmp, tp, va_arg(ap, char *));
		    break;
		}
	    if (tp >= mail_scan_tab + mail_scan_tablen)
		msg_fatal("mail_scan: bad format: %.*s>%c<%s",
			  cp - fmt, fmt, *cp, cp + 1);
	}
	if (error == 0)
	    count++;
    }
    return (count);
}