aboutsummaryrefslogtreecommitdiffstats
path: root/libfonts_get_default_font.c
blob: d27e661c0d0dc8ee28fdd34627f44909e0495a00 (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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


static char *
find(const char *dir_part1, const char *dir_part2, const char *dir_part3)
{
	return NULL; /* TODO */
}

static char *
gethome(void)
{
	uid_t uid = getuid();
	size_t size = 512;
	char *buf = NULL, *new;
	char *ret = NULL;
	struct passwd pw, *pwp;
	int err;

	goto beginning;

	do {
		if (size > SIZE_MAX / 2)
			goto enomem;
		size *= 2;
	beginning:
		new = realloc(buf, size);
		if (!new) {
		enomem:
			free(buf);
			errno = ENOMEM;
			return NULL;
		}
		buf = new;
	} while ((err = getpwuid_r(uid, &pw, buf, size, &pwp)) == ERANGE);
	if (err) {
		free(buf);
		return NULL;
	}

	if (pwp && pwp->pw_dir)
		ret = strdup(pwp->pw_dir);
	free(buf);
	return ret;
}

static char *
getn(const char *file_part1, size_t file_part1_len, const char *file_part2, const char *name)
{
	size_t file_part2_len = strlen(file_part2);
	char *path;

	if (file_part1_len > SIZE_MAX - file_part2_len - 1)
		goto enomem;

	path = malloc(file_part1_len + file_part2_len + 1);
	if (!path) {
	enomem:
		errno = ENOMEM;
		return NULL;
	}

	memcpy(path, file_part1, file_part1_len);
	memcpy(&path[file_part1_len], file_part2, file_part2_len + 1);

	/* TODO
	 *     sans-serif = $FONTNAME
	 *     serif = $FONTNAME
	 *     monospace = $FONTNAME
	 */

	free(path);
	return NULL;
}

static char *
get(const char *file_part1, const char *file_part2, const char *name)
{
	return getn(file_part1, strlen(file_part1), file_part2, name);
}

char *
libfonts_get_default_font(enum libfonts_default_font font)
{
	const char *env, *var, *next;
	char *ret, *home = NULL;
	int saved_errno = errno;
	size_t len;

	if (font == LIBFONTS_DEFAULT_SANS_SERIF) {
		env = "LIBFONTS_DEFAULT_SANS_SERIF";
		var = "sans-serif";
	} else if (font == LIBFONTS_DEFAULT_SERIF) {
		env = "LIBFONTS_DEFAULT_SERIF";
		var = "serif";
	} else if (font == LIBFONTS_DEFAULT_MONOSPACE) {
		env = "LIBFONTS_DEFAULT_MONOSPACE";
		var = "monospace";
	} else {
		errno = EINVAL;
		return NULL;
	}

	ret = getenv(env);
	if (ret && *ret)
		return strdup(ret);

	env = getenv("XDG_CONFIG_HOME");
	if (env && *env) {
		ret = get(env, "/libfonts/default-fonts.conf", var);
		if (ret)
			goto out;
	}

	env = getenv("HOME");
	if (env && *env) {
		ret = get(env, "/.config/libfonts/default-fonts.conf", var);
		if (ret)
			goto out;
		ret = get(env, "/.libfonts/default-fonts.conf", var);
		if (ret)
			goto out;
	}

	home = gethome();
	if (home && *home) {
		ret = get(home, "/.config/libfonts/default-fonts.conf", var);
		if (ret)
			goto out;
		ret = get(home, "/.libfonts/default-fonts.conf", var);
		if (ret)
			goto out;
	}

	env = getenv("XDG_CONFIG_HOME");
	if (env && *env) {
		ret = find(env, "/libfonts/", var);
		if (ret)
			goto out;
	}

	env = getenv("HOME");
	if (env && *env) {
		ret = find(env, "/.config/libfonts/", var);
		if (ret)
			goto out;
		ret = find(env, "/.libfonts/", var);
		if (ret)
			goto out;
	}

	if (home && *home) {
		ret = find(env, "/.config/libfonts/", var);
		if (ret)
			goto out;
		ret = find(env, "/.libfonts/", var);
		if (ret)
			goto out;
	}

	env = getenv("XDG_CONFIG_DIRS");
	if (env && *env) {
		do {
			next = strchr(&env[1], ':');
			len = next ? (size_t)(next - env) : strlen(env);
			if (len) {
				ret = getn(env, len, "/libfonts/default-fonts.conf", var);
				if (ret)
					goto out;
			}
			env += len + 1;
		} while (next);
	}

	ret = get("/etc", "/libfonts/default-fonts.conf", var);
	if (ret)
		goto out;

	ret = find("/etc", "/libfonts/", var);
	if (ret)
		goto out;

	/* TODO 
	 * as a last resort look around for some
	 * font that matches the specification as
	 * well as possible
	 */

	ret = NULL;

out:
	free(home);
	errno = saved_errno;
	return ret;
}


#else


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


#endif