summaryrefslogtreecommitdiffstats
path: root/src/globbing_test.c
blob: 61fcd54894e7d058d0049bde4bdfcb04d1c2e7a9 (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
/*
 * globbing_test.c: test program for file-finding functions
 *
 * Copyright (C) 1995 Graeme W. Wilford. (Wilf.)
 * Copyright (C) 2001, 2002, 2003, 2006, 2007, 2008 Colin Watson.
 *
 * This file is part of man-db.
 *
 * man-db 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 2 of the License, or
 * (at your option) any later version.
 *
 * man-db 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 man-db; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif /* HAVE_CONFIG_H */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "argp.h"
#include "error.h"
#include "gl_list.h"
#include "progname.h"

#include "gettext.h"
#define N_(String) gettext_noop (String)

#include "manconfig.h"

#include "debug.h"
#include "glcontainers.h"
#include "util.h"

#include "globbing.h"

extern const char *extension;
static bool match_case = false;
static bool regex_opt = false;
static bool wildcard = false;
static char **remaining_args;

const char *argp_program_version = "globbing " PACKAGE_VERSION;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
error_t argp_err_exit_status = FAIL;

static const char args_doc[] = N_("PATH SECTION NAME");

static struct argp_option options[] = {
	OPT ("debug", 'd', 0, N_("emit debugging messages")),
	OPT ("extension", 'e', N_("EXTENSION"),
	     N_("limit search to extension type EXTENSION")),
	OPT ("ignore-case", 'i', 0,
	     N_("look for pages case-insensitively (default)")),
	OPT ("match-case", 'I', 0, N_("look for pages case-sensitively")),
	OPT ("regex", 'r', 0, N_("interpret page name as a regex")),
	OPT ("wildcard", 'w', 0, N_("the page name contains wildcards")),
	OPT_HELP_COMPAT,
	{ 0 }
};

static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
	switch (key) {
		case 'd':
			debug_level = true;
			return 0;
		case 'e':
			extension = arg;
			return 0;
		case 'i':
			match_case = false;
			return 0;
		case 'I':
			match_case = true;
			return 0;
		case 'r':
			regex_opt = true;
			return 0;
		case 'w':
			wildcard = true;
			return 0;
		case 'h':
			argp_state_help (state, state->out_stream,
					 ARGP_HELP_STD_HELP);
			break;
		case ARGP_KEY_ARGS:
			if (state->argc - state->next != 3)
				argp_usage (state);
			remaining_args = state->argv + state->next;
			return 0;
		case ARGP_KEY_NO_ARGS:
			argp_usage (state);
			break;
	}
	return ARGP_ERR_UNKNOWN;
}

static struct argp argp = { options, parse_opt, args_doc };

int main (int argc, char **argv)
{
	int i;

	set_program_name (argv[0]);

	init_debug ();
	init_locale ();

	if (argp_parse (&argp, argc, argv, 0, 0, 0))
		exit (FAIL);
	assert (remaining_args);

	for (i = 0; i <= 1; i++) {
		gl_list_t files;
		const char *file;

		files = look_for_file (remaining_args[0], remaining_args[1],
				       remaining_args[2], (bool) i,
				       (match_case ? LFF_MATCHCASE : 0) |
				       (regex_opt ? LFF_REGEX : 0) |
				       (wildcard ? LFF_WILDCARD : 0));
		GL_LIST_FOREACH (files, file)
			printf ("%s\n", file);
		gl_list_free (files);
	}
	return 0;
}