File: tabexp.c

package info (click to toggle)
cmus 2.2.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,196 kB
  • ctags: 2,714
  • sloc: ansic: 24,078; sh: 1,024; makefile: 209; python: 26
file content (42 lines) | stat: -rw-r--r-- 734 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
#include "tabexp.h"
#include "xmalloc.h"
#include "xstrjoin.h"
#include "debug.h"

#include <stdlib.h>

struct tabexp tabexp = {
	.head = NULL,
	.tails = NULL
};

char *tabexp_expand(const char *src, void (*load_matches)(const char *src))
{
	static int idx = 0;
	char *expanded;

	if (tabexp.tails == NULL) {
		load_matches(src);
		if (tabexp.tails == NULL) {
			BUG_ON(tabexp.head != NULL);
			return NULL;
		}
		BUG_ON(tabexp.head == NULL);
		idx = 0;
	}

	expanded = xstrjoin(tabexp.head, tabexp.tails[idx++]);
	if (!tabexp.tails[idx])
		idx = 0;
	if (!tabexp.tails[1])
		tabexp_reset();
	return expanded;
}

void tabexp_reset(void)
{
	free_str_array(tabexp.tails);
	free(tabexp.head);
	tabexp.tails = NULL;
	tabexp.head = NULL;
}