aboutsummaryrefslogtreecommitdiff
path: root/pcre.c
blob: aff9a3d08d221cad6cf9e08fa52891f38af0ffd8 (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
/*
    make_pcre - PCRE plugin for GNU make.
    Copyright (C) 2014  Dmitry Mikhirev

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>

#include <gnumake.h>
#include <pcre.h>

int plugin_is_GPL_compatible;

const int MAX_CAP = 256;

char *match(const char *name, int argc, char **argv)
{
	char *pat = NULL;
	char *p;
	int co = 0;
	pcre *re;
	const char *err;
	int erroffset;
	char *str = NULL;
	int ncap = 0;
	int ovec[MAX_CAP*3];
	char *retstr = NULL;
	int i;

	if (argc > 2) {
		for (p = argv[2]; *p != '\0'; p++) {
			switch (*p) {
			case 'E':
				pat = gmk_expand(argv[0]);
				break;
			case 'i':
				co |= PCRE_CASELESS;
				break;
			case 'm':
				co |= PCRE_MULTILINE;
				break;
			case 's':
				co |= PCRE_DOTALL;
				break;
			case 'u':
				co |= PCRE_UCP;
				break;
			case 'U':
				co |= PCRE_UNGREEDY;
				break;
			case 'x':
				co |= PCRE_EXTENDED;
				break;
			case 'X':
				co |= PCRE_EXTRA;
				break;
			case '8':
				co |= PCRE_UTF8;
				break;
			default:
				fprintf(stderr, "%s: unknown option `%c'\n",
						name, *p);
				break;
			}
		}
	}

	if (pat == NULL) {
		re = pcre_compile(argv[0], 0, &err, &erroffset, NULL);
	} else {
		re = pcre_compile(pat, 0, &err, &erroffset, NULL);
		gmk_free(pat);
	}
	if (re == NULL) {
		fprintf(stderr, "%s: %d: %s\n", name, erroffset, err);
		goto end_match;
	}

	str = gmk_expand(argv[1]);

	ncap = pcre_exec(re, NULL, str, strlen(str), 0, 0, ovec, MAX_CAP*3);
	pcre_free(re);

end_match:
	if (ncap) {
		int len = ovec[1] - ovec[0];
		retstr = gmk_alloc(len + 1);
		strncpy(retstr, str + ovec[0], len);
		retstr[len] = '\0';
	}
	for (i = 0; i < ncap; i++) {
		char c = *(str + ovec[i*2 + 1]);
		*(str + ovec[i*2 + 1]) = '\0';
		int len = ovec[i*2 + 1] - ovec[i];
		char mk_set[len + 18];
		sprintf(mk_set, "define %d\n%s\nendef\n", i, str + ovec[i*2]);
		*(str + ovec[i*2 + 1]) = c;
		gmk_eval(mk_set, NULL);
	}
	for (; i < MAX_CAP; i++) {
		char mk_set[14];
		sprintf(mk_set, "undefine %d\n", i);
		gmk_eval(mk_set, NULL);
	}
	if (str != NULL) {
		gmk_free(str);
	}
	return retstr;
}

int pcre_gmk_setup()
{
	gmk_add_function("pcre_find", (gmk_func_ptr)match, 2, 3, GMK_FUNC_NOEXPAND);
	gmk_add_function("m", (gmk_func_ptr)match, 2, 3, GMK_FUNC_NOEXPAND);
	return 1;
}