(root)/
man-db-2.12.0/
src/
zsoelim_main.c
       1  /*
       2   * zsoelim_main.c: eliminate .so includes within *roff source
       3   *
       4   * Copyright (C) 1994, 1995 Graeme W. Wilford. (Wilf.)
       5   * Copyright (C) 1997 Fabrizio Polacco.
       6   * Copyright (C) 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010
       7   * Colin Watson.
       8   *
       9   * This file is part of man-db.
      10   *
      11   * man-db is free software; you can redistribute it and/or modify it
      12   * under the terms of the GNU General Public License as published by
      13   * the Free Software Foundation; either version 2 of the License, or
      14   * (at your option) any later version.
      15   *
      16   * man-db is distributed in the hope that it will be useful, but
      17   * WITHOUT ANY WARRANTY; without even the implied warranty of
      18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19   * GNU General Public License for more details.
      20   *
      21   * You should have received a copy of the GNU General Public License
      22   * along with man-db; if not, write to the Free Software Foundation,
      23   * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      24   */
      25  
      26  #ifdef HAVE_CONFIG_H
      27  #  include "config.h"
      28  #endif /* HAVE_CONFIG_H */
      29  
      30  #include <stdbool.h>
      31  #include <stdlib.h>
      32  
      33  #include "argp.h"
      34  #include "attribute.h"
      35  #include "error.h"
      36  #include "gl_list.h"
      37  #include "progname.h"
      38  #include "xalloc.h"
      39  #include "xvasprintf.h"
      40  
      41  #include "gettext.h"
      42  #include <locale.h>
      43  #define N_(String) gettext_noop (String)
      44  
      45  #include "manconfig.h"
      46  
      47  #include "cleanup.h"
      48  #include "debug.h"
      49  #include "pipeline.h"
      50  #include "sandbox.h"
      51  #include "util.h"
      52  
      53  #include "decompress.h"
      54  #include "manp.h"
      55  #include "zsoelim.h"
      56  
      57  int quiet = 1;
      58  man_sandbox *sandbox;
      59  
      60  static gl_list_t manpathlist;
      61  
      62  static char **files;
      63  static int num_files;
      64  
      65  const char *argp_program_version = "zsoelim " PACKAGE_VERSION;
      66  const char *argp_program_bug_address = PACKAGE_BUGREPORT;
      67  error_t argp_err_exit_status = FAIL;
      68  
      69  static const char args_doc[] = N_("FILE...");
      70  
      71  static struct argp_option options[] = {
      72  	OPT ("debug", 'd', 0, N_("emit debugging messages")),
      73  	OPT ("compatible", 'C', 0, N_("compatibility switch (ignored)"), 1),
      74  	OPT_HELP_COMPAT,
      75  	{ 0 }
      76  };
      77  
      78  static error_t parse_opt (int key, char *arg MAYBE_UNUSED,
      79  			  struct argp_state *state)
      80  {
      81  	switch (key) {
      82  		case 'd':
      83  			debug_level = true;
      84  			return 0;
      85  		case 'C':
      86  			return 0; /* compatibility with GNU soelim */
      87  		case 'h':
      88  			argp_state_help (state, state->out_stream,
      89  					 ARGP_HELP_STD_HELP);
      90  			break;
      91  		case ARGP_KEY_NO_ARGS:
      92  			/* open stdin */
      93  			files = xmalloc (sizeof *files);
      94  			files[0] = xstrdup ("-");
      95  			num_files = 1;
      96  			return 0;
      97  		case ARGP_KEY_ARGS:
      98  			files = state->argv + state->next;
      99  			num_files = state->argc - state->next;
     100  			return 0;
     101  	}
     102  	return ARGP_ERR_UNKNOWN;
     103  }
     104  
     105  static struct argp argp = { options, parse_opt, args_doc };
     106  
     107  int main (int argc, char *argv[])
     108  {
     109  	char *multiple_locale = NULL, *internal_locale, *all_locales;
     110  	char *manp;
     111  	int i;
     112  
     113  	set_program_name (argv[0]);
     114  
     115  	init_debug ();
     116  	pipeline_install_post_fork (pop_all_cleanups);
     117  	sandbox = sandbox_init ();
     118  	init_locale ();
     119  
     120  	internal_locale = setlocale (LC_MESSAGES, NULL);
     121  	/* Use LANGUAGE only when LC_MESSAGES locale category is
     122  	 * neither "C" nor "POSIX". */
     123  	if (internal_locale && strcmp (internal_locale, "C") &&
     124  	    strcmp (internal_locale, "POSIX"))
     125  		multiple_locale = getenv ("LANGUAGE");
     126  	internal_locale = xstrdup (internal_locale ? internal_locale : "C");
     127  
     128  	if (argp_parse (&argp, argc, argv, 0, 0, 0))
     129  		exit (FAIL);
     130  
     131  	if (multiple_locale && *multiple_locale) {
     132  		if (internal_locale && *internal_locale)
     133  			all_locales = xasprintf ("%s:%s",
     134  						 multiple_locale,
     135  						 internal_locale);
     136  		else
     137  			all_locales = xstrdup (multiple_locale);
     138  	} else {
     139  		if (internal_locale && *internal_locale)
     140  			all_locales = xstrdup (internal_locale);
     141  		else
     142  			all_locales = NULL;
     143  	}
     144  
     145  	manp = add_nls_manpaths (get_manpath (NULL), all_locales);
     146  	free (all_locales);
     147  
     148  	manpathlist = create_pathlist (manp);
     149  
     150  	/* parse files in command line order */
     151  	for (i = 0; i < num_files; ++i) {
     152  		if (zsoelim_open_file (files[i], manpathlist, NULL))
     153  			continue;
     154  		zsoelim_parse_file (manpathlist, NULL);
     155  	}
     156  
     157  	free_pathlist (manpathlist);
     158  	free (manp);
     159  	free (internal_locale);
     160  	sandbox_free (sandbox);
     161  
     162  	return OK;
     163  }