1 /* Options common to ar and ranlib.
2    Copyright (C) 2012 Red Hat, Inc.
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include <argp.h>
23 #include <libintl.h>
24 
25 #include "arlib.h"
26 
27 bool arlib_deterministic_output = DEFAULT_AR_DETERMINISTIC;
28 
29 static const struct argp_option options[] =
30   {
31     { NULL, 'D', NULL, 0,
32       N_("Use zero for uid, gid, and date in archive members."), 0 },
33     { NULL, 'U', NULL, 0,
34       N_("Use actual uid, gid, and date in archive members."), 0 },
35 
36     { NULL, 0, NULL, 0, NULL, 0 }
37   };
38 
39 static error_t
parse_opt(int key,char * arg,struct argp_state * state)40 parse_opt (int key, char *arg __attribute__ ((unused)),
41            struct argp_state *state __attribute__ ((unused)))
42 {
43   switch (key)
44     {
45     case 'D':
46       arlib_deterministic_output = true;
47       break;
48 
49     case 'U':
50       arlib_deterministic_output = false;
51       break;
52 
53     default:
54       return ARGP_ERR_UNKNOWN;
55     }
56   return 0;
57 }
58 
59 static char *
text_for_default(const char * text)60 text_for_default (const char *text)
61 {
62   char *new_text;
63   if (unlikely (asprintf (&new_text, _("%s (default)"), text) < 0))
64     return (char *) text;
65   return new_text;
66 }
67 
68 static char *
help_filter(int key,const char * text,void * input)69 help_filter (int key, const char *text, void *input __attribute__ ((unused)))
70 {
71   switch (key)
72     {
73     case 'D':
74       if (DEFAULT_AR_DETERMINISTIC)
75         return text_for_default (text);
76       break;
77     case 'U':
78       if (! DEFAULT_AR_DETERMINISTIC)
79         return text_for_default (text);
80       break;
81     }
82 
83   return (char *) text;
84 }
85 
86 static const struct argp argp =
87   {
88     options, parse_opt, NULL, NULL, NULL, help_filter, NULL
89   };
90 
91 const struct argp_child arlib_argp_children[] =
92   {
93     { &argp, 0, "", 2 },
94     { NULL, 0, NULL, 0 }
95   };
96