Move /var/svc/log to /var/log/svc
[unleashed/lotheac.git] / usr / src / cmd / fmt / head.c
blobeffd6e9f9d9a51a6615bbad430fe76bd2f445045
1 /*
2 * CDDL HEADER START
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.
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]
20 * CDDL HEADER END
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
39 * All Rights Reserved
41 * University Acknowledgment- Portions of this document are derived from
42 * software developed by the University of California, Berkeley, and its
43 * contributors.
46 #include <err.h>
47 #include <errno.h>
48 #include <libcustr.h>
49 #include <string.h>
50 #include <stdlib.h>
53 * Structure used to return a break down of a head
54 * line
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 */
61 } headline_t;
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.
73 * Return true if yes.
75 boolean_t
76 is_headline(const char *linebuf)
78 headline_t *hl;
79 boolean_t ret;
81 if (strncmp("From ", linebuf, 5) != 0) {
82 return (B_FALSE);
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;
91 headline_free(hl);
92 return (ret);
96 * Manage headline_t objects:
98 void
99 headline_free(headline_t *hl)
101 custr_free(hl->hl_from);
102 custr_free(hl->hl_tty);
103 custr_free(hl->hl_date);
104 free(hl);
108 headline_alloc(headline_t **hl)
110 int en;
111 headline_t *t;
113 if ((t = calloc(1, sizeof (*t))) == NULL) {
114 return (-1);
117 if (custr_alloc(&t->hl_from) != 0 || custr_alloc(&t->hl_tty) != 0 ||
118 custr_alloc(&t->hl_date) != 0) {
119 en = errno;
121 headline_free(t);
123 errno = en;
124 return (-1);
127 *hl = t;
128 return (0);
132 * Clear all of the strings in a headline_t:
134 void
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;
147 headline_reset(hl);
150 * Load the first word from the line and ensure that it is "From".
152 if (nextword(c, hl->hl_from, &c) != 0) {
153 return (-1);
155 if (strcmp(custr_cstr(hl->hl_from), "From") != 0) {
156 errno = EINVAL;
157 return (-1);
159 custr_reset(hl->hl_from);
162 * The next word will be the From address.
164 if (nextword(c, hl->hl_from, &c) != 0) {
165 return (-1);
169 * If there is a next word, the rest of the string is the Date.
171 if (c != NULL) {
172 if (custr_append(hl->hl_date, c) != 0) {
173 return (-1);
177 errno = 0;
178 return (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
186 * -1 otherwise.
188 static int
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.
197 for (;;) {
198 if (*c == '\0') {
200 * We have reached the end of the string.
202 *nextword = NULL;
203 return (0);
206 if (*c == '"') {
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.
217 break;
221 * Copy this character into the word buffer.
223 if (word != NULL) {
224 if (custr_appendc(word, *c) != 0) {
225 return (-1);
228 c++;
232 * Find the beginning of the next word, if there is one.
234 for (;;) {
235 if (*c == '\0') {
237 * We have reached the end of the string.
239 *nextword = NULL;
240 return (0);
242 } else if (*c != ' ' && *c != '\t') {
244 * We have located the next word.
246 *nextword = c;
247 return (0);
249 c++;
254 * Copy str1 to str2, return pointer to null in str2.
257 char *
258 copy(char *str1, char *str2)
260 register char *s1, *s2;
262 s1 = str1;
263 s2 = str2;
264 while (*s1)
265 *s2++ = *s1++;
266 *s2 = 0;
267 return(s2);
271 * Is ch any of the characters in str?
274 int
275 any(int ch, char *str)
277 register char *f;
278 int c;
280 f = str;
281 c = ch;
282 while (*f)
283 if (c == *f++)
284 return(1);
285 return(0);