Imported Upstream version 0.153
[platform/upstream/elfutils.git] / src / arlib-argp.c
1 /* Options common to ar and ranlib.
2    Copyright (C) 2012 Red Hat, Inc.
3
4    Red Hat elfutils is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by the
6    Free Software Foundation; version 2 of the License.
7
8    Red Hat elfutils is distributed in the hope that it will be useful, but
9    WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    General Public License for more details.
12
13    You should have received a copy of the GNU General Public License along
14    with Red Hat elfutils; if not, write to the Free Software Foundation,
15    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
16
17    Red Hat elfutils is an included package of the Open Invention Network.
18    An included package of the Open Invention Network is a package for which
19    Open Invention Network licensees cross-license their patents.  No patent
20    license is granted, either expressly or impliedly, by designation as an
21    included package.  Should you wish to participate in the Open Invention
22    Network licensing program, please visit www.openinventionnetwork.com
23    <http://www.openinventionnetwork.com>.  */
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <argp.h>
30 #include <libintl.h>
31
32 #include "arlib.h"
33
34 bool arlib_deterministic_output = DEFAULT_AR_DETERMINISTIC;
35
36 static const struct argp_option options[] =
37   {
38     { NULL, 'D', NULL, 0,
39       N_("Use zero for uid, gid, and date in archive members."), 0 },
40     { NULL, 'U', NULL, 0,
41       N_("Use actual uid, gid, and date in archive members."), 0 },
42
43     { NULL, 0, NULL, 0, NULL, 0 }
44   };
45
46 static error_t
47 parse_opt (int key, char *arg __attribute__ ((unused)),
48            struct argp_state *state __attribute__ ((unused)))
49 {
50   switch (key)
51     {
52     case 'D':
53       arlib_deterministic_output = true;
54       break;
55
56     case 'U':
57       arlib_deterministic_output = false;
58       break;
59
60     default:
61       return ARGP_ERR_UNKNOWN;
62     }
63   return 0;
64 }
65
66 static char *
67 help_filter (int key, const char *text, void *input __attribute__ ((unused)))
68 {
69   inline char *text_for_default (void)
70   {
71     char *new_text;
72     if (unlikely (asprintf (&new_text, gettext ("%s (default)"), text) < 0))
73       return (char *) text;
74     return new_text;
75   }
76
77   switch (key)
78     {
79     case 'D':
80       if (DEFAULT_AR_DETERMINISTIC)
81         return text_for_default ();
82       break;
83     case 'U':
84       if (! DEFAULT_AR_DETERMINISTIC)
85         return text_for_default ();
86       break;
87     }
88
89   return (char *) text;
90 }
91
92 static const struct argp argp =
93   {
94     options, parse_opt, NULL, NULL, NULL, help_filter, NULL
95   };
96
97 const struct argp_child arlib_argp_children[] =
98   {
99     { &argp, 0, "", 2 },
100     { NULL, 0, NULL, 0 }
101   };