BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / mail / head.c
CommitLineData
9552e6b8
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
0c5f72fb
KB
3 * All rights reserved.
4 *
af359dea
C
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
9552e6b8
DF
32 */
33
acfc7e9b 34#ifndef lint
1c15e888 35static char sccsid[] = "@(#)head.c 5.7 (Berkeley) 6/1/90";
acfc7e9b 36#endif /* not lint */
6a550447
KS
37
38#include "rcv.h"
39
40/*
41 * Mail -- a mail program
42 *
43 * Routines for processing and detecting headlines.
44 */
45
6a550447
KS
46/*
47 * See if the passed line buffer is a mail header.
48 * Return true if yes. Note the extreme pains to
49 * accomodate all funny formats.
50 */
6a550447
KS
51ishead(linebuf)
52 char linebuf[];
53{
54 register char *cp;
55 struct headline hl;
56 char parbuf[BUFSIZ];
57
58 cp = linebuf;
828615a1
EW
59 if (*cp++ != 'F' || *cp++ != 'r' || *cp++ != 'o' || *cp++ != 'm' ||
60 *cp++ != ' ')
61 return (0);
62 parse(linebuf, &hl, parbuf);
6a550447
KS
63 if (hl.l_from == NOSTR || hl.l_date == NOSTR) {
64 fail(linebuf, "No from or date field");
828615a1 65 return (0);
6a550447
KS
66 }
67 if (!isdate(hl.l_date)) {
68 fail(linebuf, "Date field not legal date");
828615a1 69 return (0);
6a550447 70 }
6a550447
KS
71 /*
72 * I guess we got it!
73 */
828615a1 74 return (1);
6a550447
KS
75}
76
828615a1 77/*ARGSUSED*/
6a550447
KS
78fail(linebuf, reason)
79 char linebuf[], reason[];
80{
81
828615a1
EW
82 /*
83 if (value("debug") == NOSTR)
6a550447
KS
84 return;
85 fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
828615a1 86 */
6a550447
KS
87}
88
89/*
90 * Split a headline into its useful components.
91 * Copy the line into dynamic string space, then set
92 * pointers into the copied line in the passed headline
93 * structure. Actually, it scans.
94 */
6a550447
KS
95parse(line, hl, pbuf)
96 char line[], pbuf[];
828615a1 97 register struct headline *hl;
6a550447 98{
828615a1 99 register char *cp;
6a550447
KS
100 char *sp;
101 char word[LINESIZE];
102
103 hl->l_from = NOSTR;
104 hl->l_tty = NOSTR;
105 hl->l_date = NOSTR;
106 cp = line;
107 sp = pbuf;
6a550447 108 /*
828615a1 109 * Skip over "From" first.
6a550447 110 */
6a550447 111 cp = nextword(cp, word);
828615a1
EW
112 cp = nextword(cp, word);
113 if (*word)
6a550447 114 hl->l_from = copyin(word, &sp);
828615a1
EW
115 if (cp != NOSTR && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
116 cp = nextword(cp, word);
6a550447 117 hl->l_tty = copyin(word, &sp);
6a550447 118 }
828615a1
EW
119 if (cp != NOSTR)
120 hl->l_date = copyin(cp, &sp);
6a550447
KS
121}
122
123/*
124 * Copy the string on the left into the string on the right
125 * and bump the right (reference) string pointer by the length.
126 * Thus, dynamically allocate space in the right string, copying
127 * the left string into it.
128 */
6a550447
KS
129char *
130copyin(src, space)
828615a1 131 register char *src;
6a550447
KS
132 char **space;
133{
828615a1
EW
134 register char *cp;
135 char *top;
6a550447 136
828615a1
EW
137 top = cp = *space;
138 while (*cp++ = *src++)
139 ;
6a550447 140 *space = cp;
828615a1 141 return (top);
6a550447
KS
142}
143
6a550447
KS
144/*
145 * Test to see if the passed string is a ctime(3) generated
146 * date string as documented in the manual. The template
147 * below is used as the criterion of correctness.
148 * Also, we check for a possible trailing time zone using
470c33f3 149 * the tmztype template.
6a550447
KS
150 */
151
470c33f3
EW
152/*
153 * 'A' An upper case char
154 * 'a' A lower case char
155 * ' ' A space
156 * '0' A digit
157 * 'O' An optional digit or space
158 * ':' A colon
159 * 'N' A new line
160 */
161char ctype[] = "Aaa Aaa O0 00:00:00 0000";
162char tmztype[] = "Aaa Aaa O0 00:00:00 AAA 0000";
6a550447
KS
163
164isdate(date)
165 char date[];
166{
6a550447 167
470c33f3 168 return cmatch(date, ctype) || cmatch(date, tmztype);
6a550447
KS
169}
170
171/*
828615a1 172 * Match the given string (cp) against the given template (tp).
6a550447
KS
173 * Return 1 if they match, 0 if they don't
174 */
828615a1 175cmatch(cp, tp)
6a550447 176 register char *cp, *tp;
828615a1 177{
6a550447 178
828615a1 179 while (*cp && *tp)
6a550447 180 switch (*tp++) {
470c33f3 181 case 'a':
828615a1
EW
182 if (!islower(*cp++))
183 return 0;
6a550447 184 break;
470c33f3 185 case 'A':
828615a1
EW
186 if (!isupper(*cp++))
187 return 0;
6a550447 188 break;
470c33f3 189 case ' ':
828615a1
EW
190 if (*cp++ != ' ')
191 return 0;
6a550447 192 break;
470c33f3 193 case '0':
828615a1
EW
194 if (!isdigit(*cp++))
195 return 0;
6a550447 196 break;
470c33f3 197 case 'O':
828615a1
EW
198 if (*cp != ' ' && !isdigit(*cp))
199 return 0;
200 cp++;
6a550447 201 break;
470c33f3 202 case ':':
828615a1
EW
203 if (*cp++ != ':')
204 return 0;
6a550447 205 break;
470c33f3 206 case 'N':
828615a1
EW
207 if (*cp++ != '\n')
208 return 0;
6a550447
KS
209 break;
210 }
828615a1
EW
211 if (*cp || *tp)
212 return 0;
213 return (1);
6a550447
KS
214}
215
216/*
217 * Collect a liberal (space, tab delimited) word into the word buffer
218 * passed. Also, return a pointer to the next word following that,
219 * or NOSTR if none follow.
220 */
6a550447
KS
221char *
222nextword(wp, wbuf)
828615a1 223 register char *wp, *wbuf;
6a550447 224{
828615a1 225 register c;
6a550447 226
828615a1
EW
227 if (wp == NOSTR) {
228 *wbuf = 0;
229 return (NOSTR);
6a550447 230 }
828615a1
EW
231 while ((c = *wp++) && c != ' ' && c != '\t') {
232 *wbuf++ = c;
233 if (c == '"') {
234 while ((c = *wp++) && c != '"')
235 *wbuf++ = c;
236 if (c == '"')
237 *wbuf++ = c;
238 else
239 wp--;
240 }
241 }
242 *wbuf = '\0';
243 for (; c == ' ' || c == '\t'; c = *wp++)
244 ;
245 if (c == 0)
246 return (NOSTR);
247 return (wp - 1);
6a550447 248}