(root)/
man-db-2.12.0/
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 <assert.h>
      29  #include <stdbool.h>
      30  #include <stdio.h>
      31  #include <stdlib.h>
      32  
      33  #include "argp.h"
      34  #include "error.h"
      35  #include "gl_list.h"
      36  #include "progname.h"
      37  
      38  #include "gettext.h"
      39  #define N_(String) gettext_noop (String)
      40  
      41  #include "manconfig.h"
      42  
      43  #include "debug.h"
      44  #include "glcontainers.h"
      45  #include "util.h"
      46  
      47  #include "globbing.h"
      48  
      49  extern const char *extension;
      50  static bool match_case = false;
      51  static bool regex_opt = false;
      52  static bool wildcard = false;
      53  static char **remaining_args;
      54  
      55  const char *argp_program_version = "globbing " PACKAGE_VERSION;
      56  const char *argp_program_bug_address = PACKAGE_BUGREPORT;
      57  error_t argp_err_exit_status = FAIL;
      58  
      59  static const char args_doc[] = N_("PATH SECTION NAME");
      60  
      61  static struct argp_option options[] = {
      62  	OPT ("debug", 'd', 0, N_("emit debugging messages")),
      63  	OPT ("extension", 'e', N_("EXTENSION"),
      64  	     N_("limit search to extension type EXTENSION")),
      65  	OPT ("ignore-case", 'i', 0,
      66  	     N_("look for pages case-insensitively (default)")),
      67  	OPT ("match-case", 'I', 0, N_("look for pages case-sensitively")),
      68  	OPT ("regex", 'r', 0, N_("interpret page name as a regex")),
      69  	OPT ("wildcard", 'w', 0, N_("the page name contains wildcards")),
      70  	OPT_HELP_COMPAT,
      71  	{ 0 }
      72  };
      73  
      74  static error_t parse_opt (int key, char *arg, struct argp_state *state)
      75  {
      76  	switch (key) {
      77  		case 'd':
      78  			debug_level = true;
      79  			return 0;
      80  		case 'e':
      81  			extension = arg;
      82  			return 0;
      83  		case 'i':
      84  			match_case = false;
      85  			return 0;
      86  		case 'I':
      87  			match_case = true;
      88  			return 0;
      89  		case 'r':
      90  			regex_opt = true;
      91  			return 0;
      92  		case 'w':
      93  			wildcard = true;
      94  			return 0;
      95  		case 'h':
      96  			argp_state_help (state, state->out_stream,
      97  					 ARGP_HELP_STD_HELP);
      98  			break;
      99  		case ARGP_KEY_ARGS:
     100  			if (state->argc - state->next != 3)
     101  				argp_usage (state);
     102  			remaining_args = state->argv + state->next;
     103  			return 0;
     104  		case ARGP_KEY_NO_ARGS:
     105  			argp_usage (state);
     106  			break;
     107  	}
     108  	return ARGP_ERR_UNKNOWN;
     109  }
     110  
     111  static struct argp argp = { options, parse_opt, args_doc };
     112  
     113  int main (int argc, char **argv)
     114  {
     115  	int i;
     116  
     117  	set_program_name (argv[0]);
     118  
     119  	init_debug ();
     120  	init_locale ();
     121  
     122  	if (argp_parse (&argp, argc, argv, 0, 0, 0))
     123  		exit (FAIL);
     124  	assert (remaining_args);
     125  
     126  	for (i = 0; i <= 1; i++) {
     127  		gl_list_t files;
     128  		const char *file;
     129  
     130  		files = look_for_file (remaining_args[0], remaining_args[1],
     131  				       remaining_args[2], (bool) i,
     132  				       (match_case ? LFF_MATCHCASE : 0) |
     133  				       (regex_opt ? LFF_REGEX : 0) |
     134  				       (wildcard ? LFF_WILDCARD : 0));
     135  		GL_LIST_FOREACH (files, file)
     136  			printf ("%s\n", file);
     137  		gl_list_free (files);
     138  	}
     139  	return 0;
     140  }