Imported Upstream version 2.8.4
[platform/upstream/man-db.git] / src / globbing_test.c
1 /*
2  * globbing_test.c: test program for file-finding functions
3  *  
4  * Copyright (C) 1995 Graeme W. Wilford. (Wilf.)
5  * Copyright (C) 2001, 2002, 2003, 2006, 2007, 2008 Colin Watson.
6  *
7  * This file is part of man-db.
8  *
9  * man-db is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * man-db is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with man-db; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif /* HAVE_CONFIG_H */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "argp.h"
32 #include "progname.h"
33
34 #include "gettext.h"
35 #define _(String) gettext (String)
36 #define N_(String) gettext_noop (String)
37
38 #include "manconfig.h"
39
40 #include "error.h"
41 #include "globbing.h"
42 #include "sandbox.h"
43
44 man_sandbox *sandbox;  /* unused, but needed by libman */
45
46 extern const char *extension;
47 static int match_case = 0;
48 static int regex_opt = 0;
49 static int wildcard = 0;
50 static char **remaining_args;
51
52 const char *argp_program_version = "globbing " PACKAGE_VERSION;
53 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
54 error_t argp_err_exit_status = FAIL;
55
56 static const char args_doc[] = N_("PATH SECTION NAME");
57
58 static struct argp_option options[] = {
59         { "debug",              'd',    0,                      0,      N_("emit debugging messages") },
60         { "extension",          'e',    N_("EXTENSION"),        0,      N_("limit search to extension type EXTENSION") },
61         { "ignore-case",        'i',    0,                      0,      N_("look for pages case-insensitively (default)") },
62         { "match-case",         'I',    0,                      0,      N_("look for pages case-sensitively") },
63         { "regex",              'r',    0,                      0,      N_("interpret page name as a regex") },
64         { "wildcard",           'w',    0,                      0,      N_("the page name contains wildcards") },
65         { 0, 'h', 0, OPTION_HIDDEN, 0 }, /* compatibility for --help */
66         { 0 }
67 };
68
69 static error_t parse_opt (int key, char *arg, struct argp_state *state)
70 {
71         switch (key) {
72                 case 'd':
73                         debug_level = 1;
74                         return 0;
75                 case 'e':
76                         extension = arg;
77                         return 0;
78                 case 'i':
79                         match_case = 0;
80                         return 0;
81                 case 'I':
82                         match_case = 1;
83                         return 0;
84                 case 'r':
85                         regex_opt = 1;
86                         return 0;
87                 case 'w':
88                         wildcard = 1;
89                         return 0;
90                 case 'h':
91                         argp_state_help (state, state->out_stream,
92                                          ARGP_HELP_STD_HELP);
93                         break;
94                 case ARGP_KEY_ARGS:
95                         if (state->argc - state->next != 3)
96                                 argp_usage (state);
97                         remaining_args = state->argv + state->next;
98                         return 0;
99         }
100         return ARGP_ERR_UNKNOWN;
101 }
102
103 static struct argp argp = { options, parse_opt, args_doc };
104
105 int main (int argc, char **argv)
106 {
107         int i;
108
109         set_program_name (argv[0]);
110
111         init_debug ();
112         init_locale ();
113
114         if (argp_parse (&argp, argc, argv, 0, 0, 0))
115                 exit (FAIL);
116
117         for (i = 0; i <= 1; i++) {
118                 char **files;
119
120                 files = look_for_file (remaining_args[0], remaining_args[1],
121                                        remaining_args[2], i,
122                                        (match_case ? LFF_MATCHCASE : 0) |
123                                        (regex_opt ? LFF_REGEX : 0) |
124                                        (wildcard ? LFF_WILDCARD : 0));
125                 if (files)
126                         while (*files)
127                                 printf ("%s\n", *files++);
128         }
129         return 0;
130 }