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
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.
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]
24 * Copyright 2014 Joyent, Inc.
28 * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
29 * Use is subject to license terms.
32 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
33 /* All Rights Reserved */
37 * University Copyright- Copyright (c) 1982, 1986, 1988
38 * The Regents of the University of California
41 * University Acknowledgment- Portions of this document are derived from
42 * software developed by the University of California, Berkeley, and its
53 * Structure used to return a break down of a head
57 typedef struct headline
{
58 custr_t
*hl_from
; /* The name of the sender */
59 custr_t
*hl_tty
; /* Its tty string (if any) */
60 custr_t
*hl_date
; /* The entire date string */
63 boolean_t
is_headline(const char *);
64 int headline_alloc(headline_t
**);
65 void headline_free(headline_t
*);
66 void headline_reset(headline_t
*);
67 int parse_headline(const char *, headline_t
*);
69 static int nextword(const char *, custr_t
*, const char **);
72 * See if the passed line buffer is a mail header.
76 is_headline(const char *linebuf
)
81 if (strncmp("From ", linebuf
, 5) != 0) {
85 if (headline_alloc(&hl
) != 0 || parse_headline(linebuf
, hl
) != 0) {
86 err(1, "could not parse headline");
89 ret
= custr_len(hl
->hl_from
) > 0 ? B_TRUE
: B_FALSE
;
96 * Manage headline_t objects:
99 headline_free(headline_t
*hl
)
101 custr_free(hl
->hl_from
);
102 custr_free(hl
->hl_tty
);
103 custr_free(hl
->hl_date
);
108 headline_alloc(headline_t
**hl
)
113 if ((t
= calloc(1, sizeof (*t
))) == NULL
) {
117 if (custr_alloc(&t
->hl_from
) != 0 || custr_alloc(&t
->hl_tty
) != 0 ||
118 custr_alloc(&t
->hl_date
) != 0) {
132 * Clear all of the strings in a headline_t:
135 headline_reset(headline_t
*hl
)
137 custr_reset(hl
->hl_from
);
138 custr_reset(hl
->hl_tty
);
139 custr_reset(hl
->hl_date
);
143 parse_headline(const char *line
, headline_t
*hl
)
145 const char *c
= line
;
150 * Load the first word from the line and ensure that it is "From".
152 if (nextword(c
, hl
->hl_from
, &c
) != 0) {
155 if (strcmp(custr_cstr(hl
->hl_from
), "From") != 0) {
159 custr_reset(hl
->hl_from
);
162 * The next word will be the From address.
164 if (nextword(c
, hl
->hl_from
, &c
) != 0) {
169 * If there is a next word, the rest of the string is the Date.
172 if (custr_append(hl
->hl_date
, c
) != 0) {
182 * Collect a space- or tab-delimited word into the word buffer, if one is
183 * passed. The double quote character (") can be used to include whitespace
184 * within a word. Set "nextword" to the location of the first character of the
185 * _next_ word, or NULL if there were no more words. Returns 0 on success or
189 nextword(const char *input
, custr_t
*word
, const char **nextword
)
191 boolean_t in_quotes
= B_FALSE
;
192 const char *c
= input
!= NULL
? input
: "";
195 * Collect the first word into the word buffer, if one is provided.
200 * We have reached the end of the string.
208 * Either beginning or ending a quoted string.
210 in_quotes
= in_quotes
? B_FALSE
: B_TRUE
;
213 if (!in_quotes
&& (*c
== ' ' || *c
== '\t')) {
215 * We have reached a whitespace region.
221 * Copy this character into the word buffer.
224 if (custr_appendc(word
, *c
) != 0) {
232 * Find the beginning of the next word, if there is one.
237 * We have reached the end of the string.
242 } else if (*c
!= ' ' && *c
!= '\t') {
244 * We have located the next word.
254 * Copy str1 to str2, return pointer to null in str2.
258 copy(char *str1
, char *str2
)
260 register char *s1
, *s2
;
271 * Is ch any of the characters in str?
275 any(int ch
, char *str
)