1 /* $NetBSD: head.c,v 1.20 2006/11/28 18:45:32 christos Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)head.c 8.2 (Berkeley) 4/20/95";
37 __RCSID("$NetBSD: head.c,v 1.20 2006/11/28 18:45:32 christos Exp $");
45 * Mail -- a mail program
47 * Routines for processing and detecting headlines.
51 * Match the given string (cp) against the given template (tp).
52 * Return 1 if they match, 0 if they don't
55 cmatch(const char *cp
, char *tp
)
61 if (!islower((unsigned char)*cp
++))
65 if (!isupper((unsigned char)*cp
++))
73 if (!isdigit((unsigned char)*cp
++))
77 if (*cp
!= ' ' && !isdigit((unsigned char)*cp
))
96 * Test to see if the passed string is a ctime(3) generated
97 * date string as documented in the manual. The template
98 * below is used as the criterion of correctness.
99 * Also, we check for a possible trailing time zone using
100 * the tmztype template.
104 * 'A' An upper case char
105 * 'a' A lower case char
108 * 'O' An optional digit or space
112 static char ctype
[] = "Aaa Aaa O0 00:00:00 0000";
113 static char SysV_ctype
[] = "Aaa Aaa O0 00:00 0000";
114 static char tmztype
[] = "Aaa Aaa O0 00:00:00 AAA 0000";
115 static char SysV_tmztype
[] = "Aaa Aaa O0 00:00 AAA 0000";
118 isdate(const char date
[])
121 return cmatch(date
, ctype
) ||
122 cmatch(date
, tmztype
) ||
123 cmatch(date
, SysV_tmztype
) || cmatch(date
, SysV_ctype
);
127 fail(const char linebuf
[], const char reason
[])
131 (void)fprintf(stderr
, "\"%s\"\nnot a header because %s\n",
137 * Collect a liberal (space, tab delimited) word into the word buffer
138 * passed. Also, return a pointer to the next word following that,
139 * or NULL if none follow.
142 nextword(const char *wp
, char *wbuf
)
148 while (*wp
&& !is_WSP(*wp
)) {
151 while (*wp
&& *wp
!= '"')
165 * Copy the string on the left into the string on the right
166 * and bump the right (reference) string pointer by the length.
167 * Thus, dynamically allocate space in the right string, copying
168 * the left string into it.
171 copyin(const char *src
, char **space
)
177 while ((*cp
++ = *src
++) != '\0')
184 * Split a headline into its useful components.
185 * Copy the line into dynamic string space, then set
186 * pointers into the copied line in the passed headline
187 * structure. Actually, it scans.
189 * XXX - line[], pbuf[], and word[] must be LINESIZE in length or
190 * overflow can occur in nextword() or copyin().
193 parse(const char line
[], struct headline
*hl
, char pbuf
[])
205 * Skip over "From" first.
207 cp
= nextword(cp
, word
);
208 cp
= nextword(cp
, word
);
210 hl
->l_from
= copyin(word
, &sp
);
211 if (cp
!= NULL
&& cp
[0] == 't' && cp
[1] == 't' && cp
[2] == 'y') {
212 cp
= nextword(cp
, word
);
213 hl
->l_tty
= copyin(word
, &sp
);
216 hl
->l_date
= copyin(cp
, &sp
);
220 * See if the passed line buffer is a mail header.
221 * Return true if yes. Note the extreme pains to
222 * accomodate all funny formats.
225 ishead(const char linebuf
[])
229 char parbuf
[LINESIZE
];
232 if (*cp
++ != 'F' || *cp
++ != 'r' || *cp
++ != 'o' || *cp
++ != 'm' ||
235 parse(linebuf
, &hl
, parbuf
);
236 if (hl
.l_from
== NULL
|| hl
.l_date
== NULL
) {
237 fail(linebuf
, "No from or date field");
240 if (!isdate(hl
.l_date
)) {
241 fail(linebuf
, "Date field not legal date");