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