~kris/suckless

dmenu

dmenu/patch/dynamicoptions.c -rw-r--r-- 2.2 KiB
a5b5760f — Kris Yotam chore: sync local state after restore (push updates, no pull) a month ago
                                                                                
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
static void
refreshoptions(void)
{
	int dynlen = strlen(dynamic);
	char* cmd= malloc(dynlen + strlen(text) + 2);
	if (cmd == NULL)
		die("malloc:");
	sprintf(cmd, "%s %s", dynamic, text);
	FILE *stream = popen(cmd, "r");
	if (!stream)
		die("popen(%s):", cmd);
	readstream(stream);
	int pc = pclose(stream);
	if (pc == -1)
		die("pclose:");
	free(cmd);
	curr = sel = items;
}

static void
readstream(FILE* stream)
{
	char buf[sizeof text], *p;
	size_t i, imax = 0, size = 0;
	unsigned int tmpmax = 0;

	/* read each line from stdin and add it to the item list */
	for (i = 0; fgets(buf, sizeof buf, stream); i++) {
		if (i + 1 >= size / sizeof *items)
			if (!(items = realloc(items, (size += BUFSIZ))))
				die("cannot realloc %u bytes:", size);
		if ((p = strchr(buf, '\n')))
			*p = '\0';
		if (!(items[i].text = strdup(buf)))
			die("cannot strdup %u bytes:", strlen(buf) + 1);
		#if SEPARATOR_PATCH
		if (separator && (p = sepchr(items[i].text, separator)) != NULL) {
			*p = '\0';
			items[i].text_output = ++p;
		} else {
			items[i].text_output = items[i].text;
		}
		if (separator_reverse) {
			p = items[i].text;
			items[i].text = items[i].text_output;
			items[i].text_output = p;
		}
		#elif TSV_PATCH
		if ((p = strchr(buf, '\t')))
			*p = '\0';
		if (!(items[i].stext = strdup(buf)))
			die("cannot strdup %u bytes:", strlen(buf) + 1);
		#endif // TSV_PATCH
		#if MULTI_SELECTION_PATCH
		items[i].id = i;
		#else
		items[i].out = 0;
		#endif // MULTI_SELECTION_PATCH
		#if HIGHPRIORITY_PATCH
		items[i].hp = arrayhas(hpitems, hplength, items[i].text);
		#endif // HIGHPRIORITY_PATCH
		#if PANGO_PATCH
		drw_font_getexts(drw->font, buf, strlen(buf), &tmpmax, NULL, True);
		#else
		drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
		#endif // PANGO_PATCH
		if (tmpmax > inputw) {
			inputw = tmpmax;
			imax = i;
		}
	}

	/* If the command did not give any output at all, then do not clear the existing items */
	if (!i)
		return;

	if (items)
		items[i].text = NULL;
	#if PANGO_PATCH
	inputw = items ? TEXTWM(items[imax].text) : 0;
	#else
	inputw = items ? TEXTW(items[imax].text) : 0;
	#endif // PANGO_PATCH
	if (!dynamic || !*dynamic)
		lines = MIN(lines, i);
	else {
		text[0] = '\0';
		cursor = 0;
	}
}