summaryrefslogtreecommitdiffstats
path: root/src/util/split_qnameval.c
blob: b166125e2f45c8963352cdbf2313f80720d0f5f2 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*++
/* NAME
/*	split_qnameval 3
/* SUMMARY
/*	name-value splitter
/* SYNOPSIS
/*	#include <stringops.h>
/*
/*	const char *split_qnameval(buf, name, value)
/*	char    *buf;
/*	char    **name;
/*	char    **value;
/* DESCRIPTION
/*	split_qnameval() expects text of the form "key = value"
/*	or "key =", where the key may be quoted with backslash or
/*      double quotes. The buffer argument is broken up into the key
/*      and value substrings.
/*
/*	Arguments:
/* .IP buf
/*	Result from readlline() or equivalent. The buffer is modified.
/* .IP key
/*	Upon successful completion, this is set to the key
/*	substring.
/* .IP value
/*	Upon successful completion, this is set to the value
/*	substring.
/* SEE ALSO
/*	split_nameval(3) name-value splitter
/* BUGS
/* DIAGNOSTICS
/*	The result is a null pointer in case of success, a string
/*	describing the error otherwise: missing '=' after attribute
/*	name; missing attribute name.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

/* System libraries. */

#include <sys_defs.h>
#include <ctype.h>
#include <string.h>

/* Utility library. */

#include <msg.h>
#include <stringops.h>

/* split_qnameval - split "key = value", support quoted key */

const char *split_qnameval(char *buf, char **pkey, char **pvalue)
{
    int     in_quotes = 0;
    char   *key;
    char   *key_end;
    char   *value;

    for (key = buf; *key && ISSPACE(*key); key++)
	 /* void */ ;
    if (*key == 0)
	return ("no key found; expected format: key = value");

    for (key_end = key; *key_end; key_end++) {
	if (*key_end == '\\') {
	    if (*++key_end == 0)
		break;
	} else if (ISSPACE(*key_end) || *key_end == '=') {
	    if (!in_quotes)
		break;
	} else if (*key_end == '"') {
	    in_quotes = !in_quotes;
	}
    }
    if (in_quotes) {
	return ("unbalanced '\"\'");
    }
    value = key_end;
    while (ISSPACE(*value))
	value++;
    if (*value != '=')
	return ("missing '=' after attribute name");
    *key_end = 0;
    do {
	value++;
    } while (ISSPACE(*value));
    trimblanks(value, 0)[0] = 0;
    *pkey = key;
    *pvalue = value;
    return (0);
}

#ifdef TEST

#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <mymalloc.h>

static int compare(int test_number, const char *what,
		           const char *expect, const char *real)
{
    if ((expect == 0 && real == 0)
	|| (expect != 0 && real != 0 && strcmp(expect, real) == 0)) {
	return (0);
    } else {
	msg_warn("test %d: %s mis-match: expect='%s', real='%s'",
		 test_number, what, expect ? expect : "(null)",
		 real ? real : "(null)");
	return (1);
    }
}

int     main(int argc, char **argv)
{
    struct test_info {
	const char *input;
	const char *expect_result;
	const char *expect_key;
	const char *expect_value;
    };
    static const struct test_info test_info[] = {
	/* Unquoted keys. */
	{"xx = yy", 0, "xx", "yy"},
	{"xx=yy", 0, "xx", "yy"},
	{"xx =", 0, "xx", ""},
	{"xx=", 0, "xx", ""},
	{"xx", "missing '=' after attribute name", 0, 0},
	/* Quoted keys. */
	{"\"xx \" = yy", 0, "\"xx \"", "yy"},
	{"\"xx \"= yy", 0, "\"xx \"", "yy"},
	{"\"xx \" =", 0, "\"xx \"", ""},
	{"\"xx \"=", 0, "\"xx \"", ""},
	{"\"xx \"", "missing '=' after attribute name", 0, 0},
	{"\"xx ", "unbalanced '\"'", 0, 0},
	/* Backslash escapes. */
	{"\"\\\"xx \" = yy", 0, "\"\\\"xx \"", "yy"},
	{0,},
    };

    int     errs = 0;
    const struct test_info *tp;

    for (tp = test_info; tp->input != 0; tp++) {
	const char *result;
	char   *key = 0;
	char   *value = 0;
	char   *buf = mystrdup(tp->input);
	int     test_number = (int) (tp - test_info);

	result = split_qnameval(buf, &key, &value);
	errs += compare(test_number, "result", tp->expect_result, result);
	errs += compare(test_number, "key", tp->expect_key, key);
	errs += compare(test_number, "value", tp->expect_value, value);
	myfree(buf);
    }
    exit(errs);
}

#endif