update devspec.en_US/1.0.general.md.
[devspec.git] / devspec.en_US / project / recutils / utils / recutl.h
blob2f89729469b4453168ec134aff2d6e103a61fe29
1 /* -*- mode: C -*-
3 * File: recutl.h
4 * Date: Thu Apr 22 17:29:52 2010
6 * GNU recutils - Common code for the utilities.
8 */
10 /* Copyright (C) 2010-2019 Jose E. Marchesi */
12 /* This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #ifndef RECUTL_H
27 #define RECUTL_H
29 #include <progname.h>
32 * Common arguments.
35 #define COMMON_ARGS \
36 HELP_ARG, \
37 VERSION_ARG
39 #define COMMON_LONG_ARGS \
40 {"help", no_argument, NULL, HELP_ARG}, \
41 {"version", no_argument, NULL, VERSION_ARG}
43 #define COMMON_ARGS_CASES \
44 case HELP_ARG: \
45 { \
46 recutl_print_help (); \
47 exit (EXIT_SUCCESS); \
48 break; \
49 } \
50 case VERSION_ARG: \
51 { \
52 recutl_print_version (); \
53 exit (EXIT_SUCCESS); \
54 break; \
58 * Record selection arguments.
61 #define RECORD_SELECTION_ARGS \
62 TYPE_ARG, \
63 EXPRESSION_ARG, \
64 QUICK_ARG, \
65 NUM_ARG, \
66 INSENSITIVE_ARG, \
67 RANDOM_ARG
69 #define RECORD_SELECTION_LONG_ARGS \
70 {"type", required_argument, NULL, TYPE_ARG}, \
71 {"expression", required_argument, NULL, EXPRESSION_ARG}, \
72 {"quick", required_argument, NULL, QUICK_ARG}, \
73 {"number", required_argument, NULL, NUM_ARG}, \
74 {"case-insensitive", no_argument, NULL, INSENSITIVE_ARG}, \
75 {"random", required_argument, NULL, RANDOM_ARG}
77 #define RECORD_SELECTION_SHORT_ARGS \
78 "it:e:n:q:m:"
80 #define RECORD_SELECTION_ARGS_CASES \
81 case TYPE_ARG: \
82 case 't': \
83 { \
84 recutl_type = xstrdup (optarg); \
85 if (!rec_field_name_p (recutl_type)) \
86 { \
87 recutl_fatal ("invalid record type %s\n", \
88 recutl_type); \
89 } \
90 break; \
91 } \
92 case EXPRESSION_ARG: \
93 case 'e': \
94 { \
95 if (recutl_num_indexes() != 0) \
96 { \
97 recutl_fatal (_("cannot specify -e and also -n\n"));\
98 } \
100 if (recutl_quick_str) \
102 recutl_fatal (_("cannot specify -e and also -q\n"));\
105 recutl_sex_str = xstrdup (optarg); \
107 /* Compile the search expression. */ \
108 if (recutl_sex_str) \
110 recutl_sex = rec_sex_new (recutl_insensitive); \
111 if (!rec_sex_compile (recutl_sex, recutl_sex_str)) \
113 recutl_fatal(_("invalid selection expression\n")); \
117 break; \
119 case NUM_ARG: \
120 case 'n': \
123 if (recutl_sex) \
125 recutl_fatal(_("cannot specify -n and also -e\n"));\
128 if (recutl_quick_str) \
130 recutl_fatal(_("cannot specify -n and also -q\n"));\
133 if (recutl_num_indexes() != 0) \
135 recutl_fatal ("please specify just one -n\n"); \
138 if (!recutl_index_list_parse (optarg)) \
140 recutl_fatal (_("invalid list of indexes in -n\n")); \
143 break; \
145 case RANDOM_ARG: \
146 case 'm': \
148 if (recutl_sex) \
150 recutl_fatal (_("cannot specify -m and also -e\n"));\
153 if (recutl_quick_str) \
155 recutl_fatal (_("cannot specify -m and also -q\n"));\
158 if (recutl_num_indexes() != 0) \
160 recutl_fatal (_("cannot specify -m and also -n\n"));\
163 if (recutl_random) \
165 recutl_fatal ("please specify just one -m\n"); \
169 char *end; \
170 long int li = strtol (optarg, &end, 0); \
171 if (*end != '\0') \
173 recutl_fatal ("invalid number in -m\n"); \
176 recutl_random = li; \
179 break; \
181 case QUICK_ARG: \
182 case 'q': \
184 if (recutl_sex) \
186 recutl_fatal (_("cannot specify -e and also -n\n"));\
188 if (recutl_num_indexes() != 0) \
190 recutl_fatal (_("cannot specify -e and also -n\n"));\
193 recutl_quick_str = xstrdup (optarg); \
194 break; \
196 case INSENSITIVE_ARG: \
197 case 'i': \
199 recutl_insensitive = true; \
200 break; \
203 #if defined REC_CRYPT_SUPPORT
204 # define ENCRYPTION_SHORT_ARGS "s:"
205 #else
206 # define ENCRYPTION_SHORT_ARGS ""
207 #endif
210 * Function prototypes.
213 void recutl_init (char *util_name);
214 void recutl_print_version (void);
215 void recutl_print_help_common (void);
216 void recutl_print_help_footer (void);
217 void recutl_print_help_record_selection (void);
220 void recutl_error (const char *fmt, ...);
221 void recutl_warning (const char *fmt, ...);
222 void recutl_fatal (const char *fmt, ...);
223 void recutl_out_of_memory (void);
225 bool recutl_parse_db_from_file (FILE *in, char *file_name, rec_db_t db);
226 rec_db_t recutl_build_db (int argc, char **argv);
228 rec_db_t recutl_read_db_from_file (char *file_name);
229 void recutl_write_db_to_file (rec_db_t db, char *file_name);
231 bool recutl_file_is_writable (char *file_name);
233 char *recutl_read_file (char *file_name);
235 void recutl_check_integrity (rec_db_t db,
236 bool verbose_p,
237 bool external_p);
239 bool recutl_yesno (char *prompt);
241 bool recutl_interactive (void);
243 char *recutl_getpass (bool asktwice);
245 /* Parse a list of indexes from the given string and set the internal
246 recutl_indexes accordingly. Return true if a list was found in the
247 string. Return false otherwise. */
249 bool recutl_index_list_parse (const char *str);
251 /* Return the number of indexes in the internal recutl_indexes data
252 structure. */
254 size_t recutl_num_indexes (void);
256 /* Reset the list of indexes to be empty. */
258 void recutl_reset_indexes (void);
260 /* Return the index structure. */
262 size_t *recutl_index (void);
264 #endif /* recutl.h */
266 /* End of recutl.h */