1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/* -*- mode: C -*-
*
* File: recdel.c
* Date: Mon Dec 28 08:54:38 2009
*
* GNU recutils - recdel
*
*/
/* Copyright (C) 2009, 2010, 2011, 2012 Jose E. Marchesi */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <gettext.h>
#define _(str) gettext (str)
#include <xalloc.h>
#include <rec.h>
#include <recutl.h>
/* Forward declarations. */
void recdel_delete_records (rec_db_t db);
void recdel_parse_args (int argc, char **argv);
/*
* Global variables
*/
char *recutl_type = NULL;
bool recdel_comment = false;
rec_sex_t recutl_sex = NULL;
char *recutl_quick_str = NULL;
char *recutl_sex_str = NULL;
bool recutl_insensitive = false;
bool recdel_force = false;
bool recdel_verbose = false;
bool recdel_external = true;
size_t recutl_random = 0;
char *recdel_file = NULL; /* File from where to delete the
records. */
/*
* Command line options management
*/
enum
{
COMMON_ARGS,
RECORD_SELECTION_ARGS,
COMMENT_ARG,
FORCE_ARG,
VERBOSE_ARG,
NO_EXTERNAL_ARG
};
static const struct option GNU_longOptions[] =
{
COMMON_LONG_ARGS,
RECORD_SELECTION_LONG_ARGS,
{"comment", no_argument, NULL, COMMENT_ARG},
{"force", no_argument, NULL, FORCE_ARG},
{"verbose", no_argument, NULL, VERBOSE_ARG},
{"no-external", no_argument, NULL, NO_EXTERNAL_ARG},
{NULL, 0, NULL, 0}
};
void
recutl_print_help (void)
{
/* TRANSLATORS: --help output, recdel synopsis.
no-wrap */
printf (_("\
Usage: recdel [OPTIONS]... [-t TYPE] [-n NUM | -e EXPR | -q STR | -m NUM] [FILE]\n"));
/* TRANSLATORS: --help output, recdel short description.
no-wrap */
fputs (_("\
Remove (or comment out) records from a rec file.\n"),
stdout);
puts ("");
/* TRANSLATORS: --help output, recdel arguments.
no-wrap */
fputs (_("\
-c, --comment comment out the matching records instead of\n\
deleting them.\n\
--force delete even in potentially dangerous situations,\n\
and if the deletion is violating record restrictions.\n\
--no-external don't use external descriptors.\n\
--verbose give a detailed report if the integrity check\n\
fails.\n"),
stdout);
recutl_print_help_common ();
puts ("");
recutl_print_help_record_selection ();
puts ("");
/* TRANSLATORS: --help output, notes on recdel.
no-wrap */
fputs (_("\
If no FILE is specified then the command acts like a filter, getting\n\
the data from standard input and writing the result to standard output.\n"),
stdout);
puts ("");
recutl_print_help_footer ();
}
void
recdel_delete_records (rec_db_t db)
{
int n_rset;
int numrec;
int rset_size;
rec_rset_t rset;
rec_record_t record;
rec_comment_t comment;
bool parse_status = true;
rec_mset_iterator_t iter;
rec_mset_elem_t elem;
if (!rec_db_type_p (db, recutl_type))
{
recutl_fatal (_("no records of type %s found.\n"),
recutl_type ? recutl_type : "<default>");
}
for (n_rset = 0; n_rset < rec_db_size (db); n_rset++)
{
rset = rec_db_get_rset (db, n_rset);
rset_size = rec_rset_num_records (rset);
/* Don't process empty record sets. */
if (rset_size == 0)
{
continue;
}
/* If the user specified a type, print the record set only if it
is of the given type. */
if (recutl_type
&& (!rec_rset_type (rset)
|| (strcmp (recutl_type, rec_rset_type (rset)) != 0)))
{
continue;
}
/* If the user didn't specified a type, process the record set if and only if:
*
* - It is the default record set.
* - The file contains just one record set.
*/
if (!recutl_type
&& rec_rset_type (rset)
&& (rec_db_size (db) > 1))
{
continue;
}
/* Process this record set. */
numrec = 0;
/* If the user requested to delete random records, calculate
them now for this record set. */
if (recutl_random > 0)
{
recutl_reset_indexes ();
recutl_index_add_random (recutl_random, rec_rset_num_records (rset));
}
iter = rec_mset_iterator (rec_rset_mset (rset));
while (rec_mset_iterator_next (&iter, MSET_RECORD, (const void **) &record, &elem))
{
if ((recutl_quick_str && rec_record_contains_value (record,
recutl_quick_str,
recutl_insensitive))
|| (!recutl_quick_str && (((recutl_num_indexes() == 0) && !recutl_sex)
|| (((recutl_num_indexes() == 0) &&
(recutl_sex &&
(rec_sex_eval (recutl_sex, record, &parse_status))))
|| (recutl_index_p (numrec))))))
{
if (recdel_comment)
{
/* Replace the records with a comment in the current
element. */
comment = rec_record_to_comment (record);
rec_record_destroy (record);
rec_mset_elem_set_data (elem, (void *) comment);
rec_mset_elem_set_type (elem, MSET_COMMENT);
}
else
{
/* Remove the record from the list and dispose
it. */
rec_mset_remove_elem (rec_rset_mset (rset), elem);
}
}
if (!parse_status)
{
recutl_fatal (_("evaluating the selection expression.\n"));
}
numrec++;
}
rec_mset_iterator_free (&iter);
}
/* Integrity check. */
if (!recdel_force && db)
{
recutl_check_integrity (db, recdel_verbose, recdel_external);
}
}
void
recdel_parse_args (int argc,
char **argv)
{
int ret;
char c;
while ((ret = getopt_long (argc,
argv,
RECORD_SELECTION_SHORT_ARGS
"c",
GNU_longOptions,
NULL)) != -1)
{
c = ret;
switch (c)
{
COMMON_ARGS_CASES
RECORD_SELECTION_ARGS_CASES
case FORCE_ARG:
{
recdel_force = true;
break;
}
case VERBOSE_ARG:
{
recdel_verbose = true;
break;
}
case NO_EXTERNAL_ARG:
{
recdel_external = false;
break;
}
case COMMENT_ARG:
case 'c':
{
recdel_comment = true;
break;
}
default:
{
exit (EXIT_FAILURE);
}
}
}
if ((recutl_num_indexes() == 0) && !recutl_sex_str && !recutl_quick_str && !recdel_force && (recutl_random == 0))
{
recutl_error (_("ignoring a request to delete all records of type %s.\n"),
recutl_type ? recutl_type : "unknown");
recutl_fatal (_("use --force if you really want to proceed, or use either -n or -e.\n"));
}
if (recutl_sex_str)
{
recutl_sex = rec_sex_new (recutl_insensitive);
if (!rec_sex_compile (recutl_sex, recutl_sex_str))
{
recutl_fatal (_("invalid selection expression.\n"));
}
}
/* Read the name of the file where to delete the records. */
if (optind < argc)
{
if ((argc - optind) != 1)
{
recutl_print_help ();
exit (EXIT_FAILURE);
}
recdel_file = argv[optind++];
}
}
int
main (int argc, char *argv[])
{
rec_db_t db;
recutl_init ("recdel");
recdel_parse_args (argc, argv);
db = recutl_read_db_from_file (recdel_file);
if (!db)
{
recutl_fatal (_("cannot read file %s\n"), recdel_file);
}
if (((recutl_num_indexes() != 0) || recutl_sex || recutl_quick_str) || recdel_force || (recutl_random > 0))
{
recdel_delete_records (db);
}
recutl_write_db_to_file (db, recdel_file);
return 0;
}
/* End of recdel.c */
|