~vcs-imports-ii/global/trunk

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
/*
 * Copyright (c) 2003, 2011 Tama Communications Corporation
 *
 * This file is part of GNU GLOBAL.
 *
 * 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/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>

#include "char.h"
#include "strbuf.h"

#define B	BINARYCHAR
#define R	REGEXCHAR
#define U	URLCHAR
#define RU	REGEXCHAR | URLCHAR
const unsigned char chartype[256] = {
#if '\n' == 0x0a && ' ' == 0x20 && '0' == 0x30 \
  && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
	/* ASCII */
	B, B, B, B, B, B, B, B, 0, 0, 0, 0, 0, 0, B, B,
	B, B, B, B, B, B, B, B, B, B, B, 0, B, B, B, B,
	0, U, 0, 0, R, 0, 0, U,RU,RU,RU, R, 0, U,RU, U,	/*  !"#$%&'()*+,-./ */
	U, U, U, U, U, U, U, U, U, U, 0, 0, 0, 0, 0, R,	/* 0123456789:;<=>? */
	0, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,	/* @ABCDEFGHIJKLMNO */
	U, U, U, U, U, U, U, U, U, U, U, R, R, R, R, U,	/* PQRSTUVWXYZ[\]^_ */
	0, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,	/* `abcdefghijklmno */
	U, U, U, U, U, U, U, U, U, U, U, R, R, R, U,	/* pqrstuvwxyz{|}~ */
#else
#error "Unsupported character encoding."
#endif
};
/**
 * isregex: test whether or not regular expression
 *
 *	@param[in]	s	string
 *	@return		1: is regex, 0: not regex
 */
int
isregex(const char *s)
{
	int c;

	while ((c = *s++) != '\0')
		if (isregexchar(c))
			return 1;
	return 0;
}
/**
 * quote string.
 *
 *  Non-alphanumeric characters are quoted/escaped.
 *
 *	Examples:
 *	'a:a,a' => 'a\:a\,a'
 */
const char *
quote_string(const char *s)
{
	STATIC_STRBUF(sb);

	strbuf_clear(sb);
	for (; *s; s++) {
		if (!isalnum((unsigned char)*s))
			strbuf_putc(sb, '\\');
		strbuf_putc(sb, *s);
	}
	return strbuf_value(sb);
}
/**
 * quote characters in the string.
 *
 *	Examples:
 *	quote_char('a:a,a', :) => 'a\:a,a'
 */
const char *
quote_chars(const char *s, unsigned int c)
{
	STATIC_STRBUF(sb);

	strbuf_clear(sb);
	for (; *s; s++) {
		if ((unsigned char)*s == c)
			strbuf_putc(sb, '\\');
		strbuf_putc(sb, *s);
	}
	return strbuf_value(sb);
}
#if defined(__DJGPP__) || (defined(_WIN32) && !defined(__CYGWIN__))
#define SHELL_QUOTE '"'
#else
#define SHELL_QUOTE '\''
#endif
/**
 * quote for shell.
 *
 *	Examples:
 *	aaa => 'aaa'
 *	a'a => 'a'\''aa'
 */
const char *
quote_shell(const char *s)
{
	STATIC_STRBUF(sb);

	strbuf_clear(sb);
	strbuf_putc(sb, SHELL_QUOTE);
#if defined(__DJGPP__) || (defined(_WIN32) && !defined(__CYGWIN__))
	strbuf_puts(sb, s);
#else
	for (; *s; s++) {
		if (*s == SHELL_QUOTE) {
			strbuf_putc(sb, SHELL_QUOTE);
			strbuf_putc(sb, '\\');
			strbuf_putc(sb, SHELL_QUOTE);
			strbuf_putc(sb, SHELL_QUOTE);
		} else
			strbuf_putc(sb, *s);
	}
#endif
	strbuf_putc(sb, SHELL_QUOTE);
	return strbuf_value(sb);
}