aboutsummaryrefslogtreecommitdiffstats
path: root/libfonts_parse_alias_line.c
blob: 844a400e92b39e80988840278b5d5a0f3041bc67 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
libfonts_parse_alias_line(char **aliasp, char **namep, const char *line, char **endp)
{
	const char *alias_start;
	const char *alias_end;
	const char *name_start;
	const char *name_end;
	int ret = 0;
	size_t len;

	if (aliasp)
		*aliasp = NULL;
	if (namep)
		*namep = NULL;

	while (isblank(*line))
		line++;

	if (!*line || *line == '!')
		goto out;

	if (*line == '"') {
		alias_start = ++line;
		while (*line && *line != '\n' && *line != '"')
			line++;
		if (*line != '"')
			goto ebadmsg;
		alias_end = line++;
	} else {
		alias_start = line;
		while (*line && *line != '\n' && isblank(*line))
			line++;
		alias_end = line;
	}

	if (!isblank(*line))
		goto ebadmsg;
	do {
		line++;
	} while (isblank(*line));

	if (*line == '"') {
		name_start = ++line;
		while (*line && *line != '\n' && *line != '"')
			line++;
		if (*line != '"')
			goto ebadmsg;
		name_end = line++;
	} else {
		name_start = line;
		while (*line && *line != '\n' && isblank(*line))
			line++;
		name_end = line;
	}

	while (isblank(*line))
		line++;
	if (*line && *line != '\n')
		goto ebadmsg;

	if (aliasp) {
		len = (size_t)(alias_end - alias_start);
		*aliasp = malloc(len + 1);
		if (!*aliasp)
			goto enomem;
		memcpy(*aliasp, alias_start, len);
		(*aliasp)[len] = '\0';
	}

	if (namep) {
		len = (size_t)(name_end - name_start);
		*namep = malloc(len + 1);
		if (!*namep)
			goto enomem;
		memcpy(*namep, name_start, len);
		(*namep)[len] = '\0';
	}

	*endp = *(char **)(void *)&line;
	return 1;

ebadmsg:
	errno = EBADMSG;
	ret = -1;
out:
	while (*line && *line != '\n')
		line++;
out_at_end:
	if (endp)
		*endp = *(char **)(void *)&line;
	return ret;

enomem:
	if (aliasp) {
		free(*aliasp);
		*aliasp = NULL;
	}
	errno = ENOMEM;
	ret = -1;
	goto out_at_end;
}


#else


int
main(void)
{
	return 0; /* XXX add test */
}


#endif