File: parse_word.c

package info (click to toggle)
dcc 1.2.74-4
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 3,548 kB
  • ctags: 4,041
  • sloc: ansic: 41,038; perl: 2,310; sh: 2,185; makefile: 224
file content (124 lines) | stat: -rw-r--r-- 3,132 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Distributed Checksum Clearinghouse
 *
 * Copyright (c) 2005 by Rhyolite Software
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 *
 * Rhyolite Software DCC 1.2.74-1.16 $Revision$
 */

#include "dcc_defs.h"


/* Get a "word", while honoring backslashes
 *	on exit the remainder of the line is trimmed of leading whitespace */
const char *				/* 0 or remainder of line */
dcc_parse_word(DCC_EMSG emsg,
	       char *tgt,		/* copy word to here if not null */
	       int tgt_len,		/* includes trailing '\0' */
	       const char *line,	/* line of words */
	       const char *fieldname,
	       const char *fnm, int lineno)
{
	const char *p;
	char c;

	if (!tgt_len && tgt)
		dcc_logbad(EX_SOFTWARE, "bad tgt_len for dcc_get_word(%s)%s",
			  fieldname, fnm_lineno(fnm, lineno));

	if (!line) {
		if (tgt)
			*tgt = '\0';
		if (fieldname)
			dcc_pemsg(EX_USAGE, emsg, "%s missing%s",
				  fieldname, fnm_lineno(fnm, lineno));
		return 0;
	}

	line = line+strspn(line, DCC_WHITESPACE);   /* skip leading blanks */

	p = line;
	do {
		c = *p;

		if (c != '\0') {
			++p;
			if (c == '\\' && *p != '\0') {
				/* recognize and convert escape sequences to
				 * their real equivalents */
				if ((c = *p++) == 'n') {
					c = '\n';
				} else if (c == 'r') {
					c = '\r';
				} else if (c == 't') {
					c = '\t';
				} else if (c == 'b') {
					c = '\b';
				} else if (c >= '0' && c <= '7') {
					c -= '0';
					if (*p >= '0' && *p <= '7') {
					    c = (c<<3)+(*p++ - '0');
					    if (*p >= '0' && *p <= '7')
						c = (c<<3)+(*p++ - '0');
					}
				}
			} else if (strchr(DCC_WHITESPACE, c)) {
				/* Stop on the first whitespace or delimiter */
				c = '\0';
				/* Skip trailing whitespace */
				p += strspn(p, DCC_WHITESPACE);
			}
		}

		if (tgt) {
			if (!tgt_len) {
				if (fieldname)
					dcc_pemsg(EX_USAGE, emsg,
						  "%s \"%.*s...\" too long%s",
						  fieldname,
						  min(16, (int)(p-line)), line,
						  fnm_lineno(fnm, lineno));
				return 0;
			}
			*tgt++ = c;
			--tgt_len;
		}
	} while (c != '\0');

	return p;
}



/* see if a string starts with a word possibly followed by a comma */
u_char					/* 1=yes */
dcc_ck_word_comma(char **parg, const char *word)
{
	u_int word_len = strlen(word);
	char *arg = *parg;

	if (strncasecmp(arg, word, word_len))
		return 0;
	arg += word_len;
	if (*arg == '\0') {
		*parg = arg;
		return 1;
	}
	if (*arg == ',') {
		*parg = arg+1;
		return 1;
	}
	return 0;
}