summaryrefslogtreecommitdiff
path: root/shell/match.c
blob: 0810fd8f664c2b0d43c5d9ecd3de9ac0cbdd2724 (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
/*
 * ##/%% variable matching code ripped out of ash shell for code sharing
 *
 * Copyright (c) 1989, 1991, 1993, 1994
 *      The Regents of the University of California.  All rights reserved.
 *
 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
 * was re-ported from NetBSD and debianized.
 *
 * This code is derived from software contributed to Berkeley by
 * Kenneth Almquist.
 *
 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
 *
 * Original BSD copyright notice is retained at the end of this file.
 */

#ifdef STANDALONE
# include <stdbool.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
#else
# include "busybox.h"
#endif
#include <fnmatch.h>
#include "match.h"

#define pmatch(a, b) !fnmatch((a), (b), 0)

char *scanleft(char *string, char *pattern, bool zero)
{
	char c;
	char *loc = string;

	do {
		int match;
		const char *s;

		c = *loc;
		if (zero) {
			*loc = '\0';
			s = string;
		} else
			s = loc;
		match = pmatch(pattern, s);
		*loc = c;

		if (match)
			return loc;

		loc++;
	} while (c);

	return NULL;
}

char *scanright(char *string, char *pattern, bool zero)
{
	char c;
	char *loc = string + strlen(string);

	while (loc >= string) {
		int match;
		const char *s;

		c = *loc;
		if (zero) {
			*loc = '\0';
			s = string;
		} else
			s = loc;
		match = pmatch(pattern, s);
		*loc = c;

		if (match)
			return loc;

		loc--;
	}

	return NULL;
}

#ifdef STANDALONE
int main(int argc, char *argv[])
{
	char *string;
	char *op;
	char *pattern;
	bool zero;
	char *loc;

	int i;

	if (argc == 1) {
		puts(
			"Usage: match <test> [test...]\n\n"
			"Where a <test> is the form: <string><op><match>\n"
			"This is to test the shell ${var#val} expression type.\n\n"
			"e.g. `match 'abc#a*'` -> bc"
		);
		return 1;
	}

	for (i = 1; i < argc; ++i) {
		size_t off;
		scan_t scan;

		printf("'%s': ", argv[i]);

		string = strdup(argv[i]);
		off = strcspn(string, "#%");
		if (!off) {
			printf("invalid format\n");
			free(string);
			continue;
		}
		op = string + off;
		scan = pick_scan(op[0], op[1], &zero);
		pattern = op + 1;
		if (op[0] == op[1])
			op[1] = '\0', ++pattern;
		op[0] = '\0';

		loc = scan(string, pattern, zero);

		if (zero) {
			printf("'%s'\n", loc);
		} else {
			*loc = '\0';
			printf("'%s'\n", string);
		}

		free(string);
	}

	return 0;
}
#endif