xref: /titanic_52/usr/src/cmd/mailx/head.c (revision 67e3a03ed4a2813074d36330f062ed6e593a4937)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 /*
32  * University Copyright- Copyright (c) 1982, 1986, 1988
33  * The Regents of the University of California
34  * All Rights Reserved
35  *
36  * University Acknowledgment- Portions of this document are derived from
37  * software developed by the University of California, Berkeley, and its
38  * contributors.
39  */
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 #include "rcv.h"
44 
45 /*
46  * mailx -- a modified version of a University of California at Berkeley
47  *	mail program
48  *
49  * Routines for processing and detecting headlines.
50  */
51 
52 static char	*copyin(char src[], char **space);
53 static char	*nextword(char wp[], char wbuf[]);
54 
55 /*
56  * See if the passed line buffer is a mail header.
57  * Return true if yes.
58  */
59 
60 int
61 ishead(char linebuf[])
62 {
63 	register char *cp;
64 	struct headline hl;
65 	char parbuf[BUFSIZ];
66 
67 	cp = linebuf;
68 	if (strncmp("From ", cp, 5) != 0)
69 		return(0);
70 	parse(cp, &hl, parbuf);
71 	if (hl.l_from == NOSTR) {
72 		return(0);
73 	}
74 	return(1);
75 }
76 
77 /*
78  * Split a headline into its useful components.
79  * Copy the line into dynamic string space, then set
80  * pointers into the copied line in the passed headline
81  * structure.  Actually, it scans.
82  */
83 void
84 parse(char line[], struct headline *hl, char pbuf[])
85 {
86 	register char *cp, *dp;
87 	char *sp;
88 	char word[LINESIZE];
89 
90 	hl->l_from = NOSTR;
91 	hl->l_date = NOSTR;
92 	cp = line;
93 	sp = pbuf;
94 
95 	/*
96 	 * Skip the first "word" of the line, which should be "From"
97 	 * anyway.
98 	 */
99 
100 	cp = nextword(cp, word);
101 	dp = nextword(cp, word);
102 	if (!equal(word, ""))
103 		hl->l_from = copyin(word, &sp);
104 	if (dp != NOSTR)
105 		hl->l_date = copyin(dp, &sp);
106 }
107 
108 /*
109  * Copy the string on the left into the string on the right
110  * and bump the right (reference) string pointer by the length.
111  * Thus, dynamically allocate space in the right string, copying
112  * the left string into it.
113  */
114 
115 static char *
116 copyin(char src[], char **space)
117 {
118 	register char *cp, *top;
119 	register int s;
120 
121 	s = strlen(src);
122 	cp = *space;
123 	top = cp;
124 	strcpy(cp, src);
125 	cp += s + 1;
126 	*space = cp;
127 	return(top);
128 }
129 
130 /*
131  * Collect a liberal (space, tab delimited) word into the word buffer
132  * passed.  Also, return a pointer to the next word following that,
133  * or NOSTR if none follow.
134  */
135 
136 static char *
137 nextword(char wp[], char wbuf[])
138 {
139 	register char *cp, *cp2;
140 
141 	if ((cp = wp) == NOSTR) {
142 		copy("", wbuf);
143 		return(NOSTR);
144 	}
145 	cp2 = wbuf;
146 	while (!any(*cp, " \t") && *cp != '\0')
147 		if (*cp == '"') {
148 			*cp2++ = *cp++;
149 			while (*cp != '\0' && *cp != '"')
150 				*cp2++ = *cp++;
151 			if (*cp == '"')
152 				*cp2++ = *cp++;
153 		} else
154 			*cp2++ = *cp++;
155 	*cp2 = '\0';
156 	while (any(*cp, " \t"))
157 		cp++;
158 	if (*cp == '\0')
159 		return(NOSTR);
160 	return(cp);
161 }
162 
163 /*
164  * Copy str1 to str2, return pointer to null in str2.
165  */
166 
167 char *
168 copy(char *str1, char *str2)
169 {
170 	register char *s1, *s2;
171 
172 	s1 = str1;
173 	s2 = str2;
174 	while (*s1)
175 		*s2++ = *s1++;
176 	*s2 = 0;
177 	return(s2);
178 }
179 
180 /*
181  * Is ch any of the characters in str?
182  */
183 
184 int
185 any(int ch, char *str)
186 {
187 	register char *f;
188 	int c;
189 
190 	f = str;
191 	c = ch;
192 	while (*f)
193 		if (c == *f++)
194 			return(1);
195 	return(0);
196 }
197