Imported Upstream version 2.8.4
[platform/upstream/man-db.git] / 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 <stdlib.h>
31
32 #include "argp.h"
33 #include "progname.h"
34 #include "xvasprintf.h"
35
36 #include "gettext.h"
37 #include <locale.h>
38 #define _(String) gettext (String)
39 #define N_(String) gettext_noop (String)
40
41 #include "manconfig.h"
42
43 #include "cleanup.h"
44 #include "error.h"
45 #include "pipeline.h"
46 #include "decompress.h"
47 #include "sandbox.h"
48
49 #include "manp.h"
50 #include "zsoelim.h"
51
52 int quiet = 1;
53 man_sandbox *sandbox;
54
55 static char *manpathlist[MAXDIRS];
56
57 static char **files;
58 static int num_files;
59
60 const char *argp_program_version = "zsoelim " PACKAGE_VERSION;
61 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
62 error_t argp_err_exit_status = FAIL;
63
64 static const char args_doc[] = N_("FILE...");
65
66 static struct argp_option options[] = {
67         { "debug",      'd',    0,      0,      N_("emit debugging messages") },
68         { "compatible", 'C',    0,      0,      N_("compatibility switch (ignored)"),   1 },
69         { 0, 'h', 0, OPTION_HIDDEN, 0 }, /* compatibility for --help */
70         { 0 }
71 };
72
73 static error_t parse_opt (int key, char *arg ATTRIBUTE_UNUSED,
74                           struct argp_state *state)
75 {
76         switch (key) {
77                 case 'd':
78                         debug_level = 1;
79                         return 0;
80                 case 'C':
81                         return 0; /* compatibility with GNU soelim */
82                 case 'h':
83                         argp_state_help (state, state->out_stream,
84                                          ARGP_HELP_STD_HELP);
85                         break;
86                 case ARGP_KEY_NO_ARGS:
87                         /* open stdin */
88                         files = xmalloc (sizeof *files);
89                         files[0] = xstrdup ("-");
90                         num_files = 1;
91                         return 0;
92                 case ARGP_KEY_ARGS:
93                         files = state->argv + state->next;
94                         num_files = state->argc - state->next;
95                         return 0;
96         }
97         return ARGP_ERR_UNKNOWN;
98 }
99
100 static struct argp argp = { options, parse_opt, args_doc };
101
102 int main (int argc, char *argv[])
103 {
104         char *multiple_locale = NULL, *internal_locale, *all_locales;
105         char *manp;
106         int i;
107
108         set_program_name (argv[0]);
109
110         init_debug ();
111         pipeline_install_post_fork (pop_all_cleanups);
112         sandbox = sandbox_init ();
113         init_locale ();
114
115         internal_locale = setlocale (LC_MESSAGES, NULL);
116         /* Use LANGUAGE only when LC_MESSAGES locale category is
117          * neither "C" nor "POSIX". */
118         if (internal_locale && strcmp (internal_locale, "C") &&
119             strcmp (internal_locale, "POSIX"))
120                 multiple_locale = getenv ("LANGUAGE");
121         internal_locale = xstrdup (internal_locale ? internal_locale : "C");
122
123         if (argp_parse (&argp, argc, argv, 0, 0, 0))
124                 exit (FAIL);
125
126         if (multiple_locale && *multiple_locale) {
127                 if (internal_locale && *internal_locale)
128                         all_locales = xasprintf ("%s:%s",
129                                                  multiple_locale,
130                                                  internal_locale);
131                 else
132                         all_locales = xstrdup (multiple_locale);
133         } else {
134                 if (internal_locale && *internal_locale)
135                         all_locales = xstrdup (internal_locale);
136                 else
137                         all_locales = NULL;
138         }
139
140         manp = add_nls_manpaths (get_manpath (NULL), all_locales);
141         free (all_locales);
142
143         create_pathlist (manp, manpathlist);
144
145         /* parse files in command line order */
146         for (i = 0; i < num_files; ++i) {
147                 if (zsoelim_open_file (files[i], manpathlist, NULL))
148                         continue;
149                 zsoelim_parse_file (manpathlist, NULL);
150         }
151
152         free_pathlist (manpathlist);
153         free (manp);
154         free (internal_locale);
155
156         return OK;
157 }