4 * Date: Thu Apr 22 17:29:52 2010
6 * GNU recutils - Common code for the utilities.
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/>.
39 #define COMMON_LONG_ARGS \
40 {"help", no_argument, NULL, HELP_ARG}, \
41 {"version", no_argument, NULL, VERSION_ARG}
43 #define COMMON_ARGS_CASES \
46 recutl_print_help (); \
47 exit (EXIT_SUCCESS); \
52 recutl_print_version (); \
53 exit (EXIT_SUCCESS); \
58 * Record selection arguments.
61 #define RECORD_SELECTION_ARGS \
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 \
80 #define RECORD_SELECTION_ARGS_CASES \
84 recutl_type = xstrdup (optarg); \
85 if (!rec_field_name_p (recutl_type)) \
87 recutl_fatal ("invalid record type %s\n", \
92 case EXPRESSION_ARG: \
95 if (recutl_num_indexes() != 0) \
97 recutl_fatal (_("cannot specify -e and also -n\n"));\
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")); \
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")); \
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"));\
165 recutl_fatal ("please specify just one -m\n"); \
170 long int li = strtol (optarg, &end, 0); \
173 recutl_fatal ("invalid number in -m\n"); \
176 recutl_random = li; \
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); \
196 case INSENSITIVE_ARG: \
199 recutl_insensitive = true; \
203 #if defined REC_CRYPT_SUPPORT
204 # define ENCRYPTION_SHORT_ARGS "s:"
206 # define ENCRYPTION_SHORT_ARGS ""
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
,
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
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 */