| 1 |
/* dctrl-tools - Debian control file inspection tools |
| 2 |
Copyright © 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
| 3 |
2010, 2011, 2012, 2013, 2015 |
| 4 |
Antti-Juhani Kaijanaho |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License along |
| 17 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <argp.h> |
| 22 |
#include <assert.h> |
| 23 |
#include <fcntl.h> |
| 24 |
#include <locale.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <sys/wait.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include "atom.h" |
| 33 |
#include "fnutil.h" |
| 34 |
#include "fsaf.h" |
| 35 |
#include "i18n.h" |
| 36 |
#include "ifile.h" |
| 37 |
#include "misc.h" |
| 38 |
#include "msg.h" |
| 39 |
#include "paragraph.h" |
| 40 |
#include "predicate.h" |
| 41 |
#include "strlist.h" |
| 42 |
#include "util.h" |
| 43 |
|
| 44 |
const char * argp_program_version = "grep-dctrl (dctrl-tools) " VERSION; |
| 45 |
const char * argp_program_bug_address = MAINTAINER; |
| 46 |
|
| 47 |
const char description [] = "Description"; |
| 48 |
struct field_attr *description_attr; |
| 49 |
|
| 50 |
static char progdoc [] = N_("grep-dctrl -- grep Debian control files"); |
| 51 |
|
| 52 |
static char argsdoc [] = N_("FILTER [FILENAME...]"); |
| 53 |
|
| 54 |
enum { |
| 55 |
OPT_OPTPARSE=256, |
| 56 |
OPT_SILENT, |
| 57 |
OPT_EQ, |
| 58 |
OPT_LT, |
| 59 |
OPT_LE, |
| 60 |
OPT_GT, |
| 61 |
OPT_GE, |
| 62 |
OPT_MMAP, |
| 63 |
OPT_IGN_ERRS, |
| 64 |
OPT_ENSURE, |
| 65 |
OPT_COMPAT, |
| 66 |
OPT_PATTERN, |
| 67 |
OPT_ERRORLEVEL, |
| 68 |
}; |
| 69 |
|
| 70 |
static struct argp_option options[] = { |
| 71 |
{ "errorlevel", OPT_ERRORLEVEL, N_("LEVEL"), 0, N_("Set log level to LEVEL."), 0 }, |
| 72 |
{ "field", 'F', N_("FIELD,FIELD,..."), 0, N_("Restrict pattern matching to the FIELDs given."), 0 }, |
| 73 |
{ 0, 'P', 0, 0, N_("This is a shorthand for -FPackage."), 0 }, |
| 74 |
{ 0, 'S', 0, 0, N_("This is a shorthand for -FSource:Package."), 0 }, |
| 75 |
{ "show-field", 's', N_("FIELD,FIELD,..."), 0, N_("Show only the body of these fields from the matching paragraphs."), 0 }, |
| 76 |
{ 0, 'd', 0, 0, N_("Show only the first line of the \"Description\" field from the matching paragraphs."), 0 }, |
| 77 |
{ "no-field-names", 'n', 0, 0, N_("Suppress field names when showing specified fields."), 0 }, |
| 78 |
{ "files-with-matches", 'l', 0, 0, N_("Show only the names of the files that contain matching paragraphs."), 0 }, |
| 79 |
{ "files-without-matches", 'L', 0, 0, N_("Show only the names of the files that do not contain matching paragraphs."), 0 }, |
| 80 |
{ "eregex", 'e', 0, 0, N_("Regard the pattern as an extended POSIX regular expression."), 0 }, |
| 81 |
{ "regex", 'r', 0, 0, N_("Regard the pattern as a standard POSIX regular expression."), 0 }, |
| 82 |
{ "ignore-case", 'i', 0, 0, N_("Ignore case when looking for a match."), 0 }, |
| 83 |
{ "invert-match", 'v', 0, 0, N_("Show only paragraphs that do not match."), 0 }, |
| 84 |
{ "invert-show", 'I', 0, 0, N_("Show those fields that have NOT been selected with -s"), 0 }, |
| 85 |
{ "count", 'c', 0, 0, N_("Show only the count of matching paragraphs."), 0 }, |
| 86 |
{ "exact-match", 'X', 0, 0, N_("Do an exact match."), 0 }, |
| 87 |
{ "copying", 'C', 0, 0, N_("Print out the copyright license."), 0 }, |
| 88 |
{ "and", 'a', 0, 0, N_("Conjunct filters."), 0 }, |
| 89 |
{ "or", 'o', 0, 0, N_("Disjunct filters."), 0 }, |
| 90 |
{ "not", '!', 0, 0, N_("Negate the following filters."), 0 }, |
| 91 |
{ "eq", OPT_EQ, 0, 0, N_("Test for version number equality."), 0 }, |
| 92 |
{ "lt", OPT_LT, 0, 0, N_("Version number comparison: <<."), 0 }, |
| 93 |
{ "le", OPT_LE, 0, 0, N_("Version number comparison: <=."), 0 }, |
| 94 |
{ "gt", OPT_GT, 0, 0, N_("Version number comparison: >>."), 0 }, |
| 95 |
{ "ge", OPT_GE, 0, 0, N_("Version number comparison: >=."), 0 }, |
| 96 |
{ "debug-optparse", OPT_OPTPARSE, 0, 0, N_("Debug option parsing."), 0 }, |
| 97 |
{ "quiet", 'q', 0, 0, N_("Do not output to stdout."), 0 }, |
| 98 |
{ "silent", OPT_SILENT, 0, 0, N_("Do not output to stdout."), 0 }, |
| 99 |
{ "mmap", OPT_MMAP, 0, 0, N_("Attempt mmapping input files"), 0 }, |
| 100 |
{ "ignore-parse-errors", OPT_IGN_ERRS, 0, 0, N_("Ignore parse errors"), 0 }, |
| 101 |
{ "pattern", OPT_PATTERN, N_("PATTERN"), 0, N_("Specify the pattern to search for"), 0 }, |
| 102 |
{ "whole-pkg", 'w', 0, 0, N_("Match only whole package names (this implies -e)"), 0 }, |
| 103 |
{ "ensure-dctrl", OPT_ENSURE, 0, 0, N_("Ensure that the output is in dctrl format (overridden by -n)"), 0 }, |
| 104 |
{ "compat", OPT_COMPAT, 0, 0, N_("Override the effect of an earlier --ensure-dctrl"), 0 }, |
| 105 |
{ 0, 0, 0, 0, 0, 0 } |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
// Tokens |
| 110 |
#define TOK_EOD 0 |
| 111 |
#define TOK_NOT 1 |
| 112 |
#define TOK_AND 2 |
| 113 |
#define TOK_OR 3 |
| 114 |
#define TOK_LP 4 /* left paren */ |
| 115 |
#define TOK_RP 5 /* right paren */ |
| 116 |
#define TOK_EXACT 6 /* -X */ |
| 117 |
#define TOK_ERGEX 7 /* -e */ |
| 118 |
#define TOK_REGEX 8 /* -r */ |
| 119 |
#define TOK_EQ 9 |
| 120 |
#define TOK_LT 10 |
| 121 |
#define TOK_LE 11 |
| 122 |
#define TOK_GT 12 |
| 123 |
#define TOK_GE 13 |
| 124 |
#define TOK_ICASE 14 /* -i */ |
| 125 |
#define TOK_PAT 15 /* --pattern */ |
| 126 |
#define TOK_STR 16 /* a plain string */ |
| 127 |
#define TOK_WHOLE 17 /* --whole-pkg */ |
| 128 |
#define TOK_FIELD 18 /* -F */ |
| 129 |
|
| 130 |
#define MAX_FNAMES 4096 |
| 131 |
#define MAX_TOKS 16384 |
| 132 |
|
| 133 |
static int debug_optparse = 0; |
| 134 |
|
| 135 |
struct arguments { |
| 136 |
/* Parser state flag: last token seen was ')' */ |
| 137 |
bool just_seen_cparen; |
| 138 |
/* Number of file names seen. */ |
| 139 |
size_t num_fnames; |
| 140 |
/**/ |
| 141 |
size_t num_show_fields; |
| 142 |
/* A machine-readable representation of the predicate. */ |
| 143 |
struct predicate * p; |
| 144 |
/* Ignore parse errors? */ |
| 145 |
bool ignore_errors; |
| 146 |
/* Quiet operation? */ |
| 147 |
bool quiet; |
| 148 |
/* Do show field names? */ |
| 149 |
bool show_field_name; |
| 150 |
/* Ensure that the output is in dctrl format? (Ignored if |
| 151 |
show_field_name is false.) */ |
| 152 |
bool ensure_dctrl; |
| 153 |
/* Do show (only) first line of Description? */ |
| 154 |
bool short_descr; |
| 155 |
/* Does show_fields contain Description? */ |
| 156 |
bool description_selected; |
| 157 |
/* Count matching paragraphs? */ |
| 158 |
bool count; |
| 159 |
/* Invert match? */ |
| 160 |
bool invert_match; |
| 161 |
/* Show fields that are NOT listed? */ |
| 162 |
bool invert_show; |
| 163 |
/* Show only file name? */ |
| 164 |
bool only_file_name; |
| 165 |
/* First unused position in toks. */ |
| 166 |
size_t toks_np; |
| 167 |
/* Token read position. */ |
| 168 |
size_t toks_pos; |
| 169 |
/* Token stream for the predicate parser. */ |
| 170 |
int toks[MAX_TOKS]; |
| 171 |
/* The string value, if any, of each token*/ |
| 172 |
char * strings[MAX_TOKS]; |
| 173 |
/* File names seen on the command line. */ |
| 174 |
struct ifile fname[MAX_FNAMES]; |
| 175 |
/**/ |
| 176 |
size_t show_fields[MAX_FIELDS]; |
| 177 |
}; |
| 178 |
|
| 179 |
#define APPTOK(tok) do { apptok(args, (tok)); } while (0) |
| 180 |
|
| 181 |
static void apptok(struct arguments * args, const int tok) |
| 182 |
{ |
| 183 |
debug_message("apptok", 0); |
| 184 |
if (args->toks_np >= MAX_TOKS) { |
| 185 |
message(L_FATAL, 0, _("filter is too long")); |
| 186 |
fail(); |
| 187 |
} |
| 188 |
args->toks[args->toks_np++] = tok; |
| 189 |
args->strings[args->toks_np-1] = 0; |
| 190 |
} |
| 191 |
|
| 192 |
#define APPSTR(tok,str) do { appstr(args, (tok), (str)); } while (0) |
| 193 |
|
| 194 |
static void appstr(struct arguments * args, const int tok, char * str) { |
| 195 |
debug_message("appstr", 0); |
| 196 |
apptok(args, tok); |
| 197 |
args->strings[args->toks_np-1] = str; |
| 198 |
} |
| 199 |
|
| 200 |
static error_t parse_opt (int key, char * arg, struct argp_state * state) |
| 201 |
{ |
| 202 |
struct arguments * args = state->input; |
| 203 |
debug_message("parse_opt", 0); |
| 204 |
switch (key) { |
| 205 |
char *carg; |
| 206 |
case 'C': |
| 207 |
if (!to_stdout (COPYING)) fail(); |
| 208 |
exit(0); |
| 209 |
case OPT_ENSURE: |
| 210 |
args->ensure_dctrl = true; |
| 211 |
break; |
| 212 |
case OPT_COMPAT: |
| 213 |
args->ensure_dctrl = false; |
| 214 |
break; |
| 215 |
case 'v': |
| 216 |
if (args->only_file_name) { |
| 217 |
message(L_FATAL, 0, |
| 218 |
args->invert_match |
| 219 |
? _("-v is incompatible with -L") |
| 220 |
: _("-v is incompatible with -l")); |
| 221 |
fail(); |
| 222 |
} |
| 223 |
args->invert_match = true; |
| 224 |
break; |
| 225 |
case 'c': |
| 226 |
args->count = true; |
| 227 |
break; |
| 228 |
case 'q': case OPT_SILENT: |
| 229 |
debug_message("parse_opt: q", 0); |
| 230 |
args->quiet = true; |
| 231 |
break; |
| 232 |
case 'n': |
| 233 |
debug_message("parse_opt: n", 0); |
| 234 |
args->show_field_name = false; |
| 235 |
break; |
| 236 |
case 'l': |
| 237 |
if (args->only_file_name) { |
| 238 |
message(L_FATAL, 0, |
| 239 |
_("only one -l or -L is allowed")); |
| 240 |
fail(); |
| 241 |
} |
| 242 |
if (args->invert_match) { |
| 243 |
message(L_FATAL, 0, _("-l is incompatible with -v")); |
| 244 |
fail(); |
| 245 |
} |
| 246 |
args->only_file_name = true; |
| 247 |
break; |
| 248 |
case 'L': |
| 249 |
if (args->only_file_name) { |
| 250 |
message(L_FATAL, 0, |
| 251 |
_("only one -l or -L is allowed")); |
| 252 |
fail(); |
| 253 |
} |
| 254 |
if (args->invert_match) { |
| 255 |
message(L_FATAL, 0, _("-L is incompatible with -v")); |
| 256 |
fail(); |
| 257 |
} |
| 258 |
args->only_file_name = true; |
| 259 |
args->invert_match = true; |
| 260 |
break; |
| 261 |
case 'd': |
| 262 |
args->short_descr = true; |
| 263 |
break; |
| 264 |
case 'I': |
| 265 |
args->invert_show = true; |
| 266 |
break; |
| 267 |
case 's': { |
| 268 |
char * carg = strdup(arg); |
| 269 |
if (carg == 0) fatal_enomem(0); |
| 270 |
for (char * s = strtok(carg, ","); s != 0; s = strtok(0, ",")){ |
| 271 |
char * repl = strchr(s, ':'); |
| 272 |
if (repl != NULL) { |
| 273 |
*repl = '\0'; |
| 274 |
++repl; |
| 275 |
} |
| 276 |
struct field_attr *fa = fieldtrie_insert(s); |
| 277 |
if (args->num_show_fields >= MAX_FIELDS) { |
| 278 |
message(L_FATAL, 0, _("too many output fields")); |
| 279 |
fail(); |
| 280 |
} |
| 281 |
args->show_fields[args->num_show_fields] = fa->inx; |
| 282 |
if (fa == description_attr) { |
| 283 |
args->description_selected = true; |
| 284 |
} |
| 285 |
fa->is_show_field = true; |
| 286 |
|
| 287 |
size_t repl_inx = repl == NULL |
| 288 |
? (size_t)(-1) |
| 289 |
: fieldtrie_insert(repl)->inx; |
| 290 |
|
| 291 |
fa->backup_field = repl_inx; |
| 292 |
|
| 293 |
++args->num_show_fields; |
| 294 |
} |
| 295 |
free(carg); |
| 296 |
} |
| 297 |
break; |
| 298 |
case OPT_ERRORLEVEL: { |
| 299 |
int ll = str2loglevel(arg); |
| 300 |
if (ll < 0) |
| 301 |
{ |
| 302 |
message(L_FATAL, 0, _("no such log level '%s'"), arg); |
| 303 |
fail(); |
| 304 |
} |
| 305 |
set_loglevel(ll); |
| 306 |
debug_message("parse_opt: l", 0); |
| 307 |
} |
| 308 |
break; |
| 309 |
case '!': |
| 310 |
debug_message("parse_opt: !", 0); |
| 311 |
APPTOK(TOK_NOT); |
| 312 |
break; |
| 313 |
case 'a': |
| 314 |
debug_message("parse_opt: a", 0); |
| 315 |
APPTOK(TOK_AND); |
| 316 |
break; |
| 317 |
case 'o': |
| 318 |
debug_message("parse_opt: o", 0); |
| 319 |
APPTOK(TOK_OR); |
| 320 |
break; |
| 321 |
case 'S': |
| 322 |
debug_message("parse_opt: S", 0); |
| 323 |
arg = "Source:Package"; |
| 324 |
goto case_F; |
| 325 |
case 'P': |
| 326 |
debug_message("parse_opt: P", 0); |
| 327 |
arg = "Package"; |
| 328 |
goto case_F; |
| 329 |
case_F: |
| 330 |
case 'F': |
| 331 |
debug_message("parse_opt: Fv", 0); |
| 332 |
carg = strdup(arg); |
| 333 |
if (carg == 0) fatal_enomem(0); |
| 334 |
for (char * s = strtok(carg, ","); s != 0; s = strtok(0, ",")){ |
| 335 |
char * tmp = strdup(s); |
| 336 |
if (tmp == 0) fatal_enomem(0); |
| 337 |
APPSTR(TOK_FIELD, tmp); |
| 338 |
} |
| 339 |
free(carg); |
| 340 |
break; |
| 341 |
case 'X': |
| 342 |
debug_message("parse_opt: X", 0); |
| 343 |
APPTOK(TOK_EXACT); |
| 344 |
break; |
| 345 |
case 'r': |
| 346 |
debug_message("parse_opt: r", 0); |
| 347 |
APPTOK(TOK_REGEX); |
| 348 |
break; |
| 349 |
case 'e': |
| 350 |
debug_message("parse_opt: e", 0); |
| 351 |
APPTOK(TOK_ERGEX); |
| 352 |
break; |
| 353 |
case OPT_EQ: |
| 354 |
debug_message("parse_opt: eq", 0); |
| 355 |
APPTOK(TOK_EQ); |
| 356 |
break; |
| 357 |
case OPT_LT: |
| 358 |
debug_message("parse_opt: lt", 0); |
| 359 |
APPTOK(TOK_LT); |
| 360 |
break; |
| 361 |
case OPT_LE: |
| 362 |
debug_message("parse_opt: le", 0); |
| 363 |
APPTOK(TOK_LE); |
| 364 |
break; |
| 365 |
case OPT_GT: |
| 366 |
debug_message("parse_opt: gt", 0); |
| 367 |
APPTOK(TOK_GT); |
| 368 |
break; |
| 369 |
case OPT_GE: |
| 370 |
debug_message("parse_opt: ge", 0); |
| 371 |
APPTOK(TOK_GE); |
| 372 |
break; |
| 373 |
case OPT_MMAP: |
| 374 |
debug_message("parse_opt: mmap", 0); |
| 375 |
/* ignore */ |
| 376 |
break; |
| 377 |
case 'i': |
| 378 |
debug_message("parse_opt: i", 0); |
| 379 |
APPTOK(TOK_ICASE); |
| 380 |
break; |
| 381 |
case OPT_OPTPARSE: |
| 382 |
debug_message("parse_opt: optparse", 0); |
| 383 |
debug_optparse = 1; |
| 384 |
break; |
| 385 |
case OPT_IGN_ERRS: |
| 386 |
debug_message("parse_opt: ignore-parse-errors", 0); |
| 387 |
args->ignore_errors = 1; |
| 388 |
break; |
| 389 |
case OPT_PATTERN: |
| 390 |
debug_message("parse_opt: pattern", 0); |
| 391 |
carg = strdup(arg); |
| 392 |
if (carg == 0) fatal_enomem(0); |
| 393 |
APPSTR(TOK_PAT, carg); |
| 394 |
break; |
| 395 |
case 'w': |
| 396 |
debug_message("parse_opt: whole-pkg", 0); |
| 397 |
APPTOK(TOK_WHOLE); |
| 398 |
break; |
| 399 |
case ARGP_KEY_ARG: |
| 400 |
debug_message("parse_opt: argument", 0); |
| 401 |
if (strcmp(arg, "!") == 0) { |
| 402 |
debug_message("parse_opt: !", 0); |
| 403 |
APPTOK(TOK_NOT); |
| 404 |
break; |
| 405 |
} |
| 406 |
if (strcmp(arg, "(") == 0) { |
| 407 |
debug_message("parse_opt: (", 0); |
| 408 |
APPTOK(TOK_LP); |
| 409 |
break; |
| 410 |
} |
| 411 |
if (strcmp(arg, ")") == 0) { |
| 412 |
debug_message("parse_opt: )", 0); |
| 413 |
APPTOK(TOK_RP); |
| 414 |
break; |
| 415 |
} |
| 416 |
carg = strdup(arg); |
| 417 |
if (carg == 0) fatal_enomem(0); |
| 418 |
APPSTR(TOK_STR, carg); |
| 419 |
break; |
| 420 |
case ARGP_KEY_END: |
| 421 |
debug_message("parse_opt: end", 0); |
| 422 |
break; |
| 423 |
case ARGP_KEY_ARGS: case ARGP_KEY_INIT: case ARGP_KEY_SUCCESS: |
| 424 |
case ARGP_KEY_ERROR: case ARGP_KEY_FINI: case ARGP_KEY_NO_ARGS: |
| 425 |
debug_message("parse_opt: ignored", 0); |
| 426 |
break; |
| 427 |
default: |
| 428 |
return ARGP_ERR_UNKNOWN; |
| 429 |
} |
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
static void dump_args(struct arguments * args) |
| 434 |
{ |
| 435 |
predicate_print(args->p); |
| 436 |
printf("num_fnames = %zi\n", args->num_fnames); |
| 437 |
for (size_t i = 0; i < args->num_fnames; i++) { |
| 438 |
printf("fname[%zi].mode = %s, fname[%zi].s = %s\n", |
| 439 |
i, ifile_modes[args->fname[i].mode], |
| 440 |
i, args->fname[i].s); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
static |
| 445 |
int peek_token(struct arguments const * args) |
| 446 |
{ |
| 447 |
assert(args->toks_pos <= args->toks_np); |
| 448 |
if (args->toks_pos == args->toks_np) return TOK_EOD; |
| 449 |
return args->toks[args->toks_pos]; |
| 450 |
} |
| 451 |
|
| 452 |
static |
| 453 |
int get_token(struct arguments * args) |
| 454 |
{ |
| 455 |
assert(args->toks_pos <= args->toks_np); |
| 456 |
if (args->toks_pos == args->toks_np) return TOK_EOD; |
| 457 |
return args->toks[args->toks_pos++]; |
| 458 |
} |
| 459 |
|
| 460 |
static |
| 461 |
char * get_string(struct arguments * args) |
| 462 |
{ |
| 463 |
assert(args->toks_pos < args->toks_np); |
| 464 |
return args->strings[args->toks_pos++]; |
| 465 |
} |
| 466 |
|
| 467 |
static const char *tokdescr(int tok) { |
| 468 |
switch (tok) { |
| 469 |
case TOK_EOD: |
| 470 |
return "EOD"; |
| 471 |
case TOK_NOT: |
| 472 |
return "!"; |
| 473 |
case TOK_AND: |
| 474 |
return "-a"; |
| 475 |
case TOK_OR : |
| 476 |
return "-o"; |
| 477 |
case TOK_LP : |
| 478 |
return "("; |
| 479 |
case TOK_RP : |
| 480 |
return ")"; |
| 481 |
case TOK_EXACT : |
| 482 |
return "-X"; |
| 483 |
case TOK_ERGEX : |
| 484 |
return "-e"; |
| 485 |
case TOK_REGEX : |
| 486 |
return "-r"; |
| 487 |
case TOK_EQ : |
| 488 |
return "--eq"; |
| 489 |
case TOK_LT : |
| 490 |
return "--lt"; |
| 491 |
case TOK_LE : |
| 492 |
return "--le"; |
| 493 |
case TOK_GT : |
| 494 |
return "--gt"; |
| 495 |
case TOK_GE : |
| 496 |
return "--ge"; |
| 497 |
case TOK_ICASE : |
| 498 |
return "-i"; |
| 499 |
case TOK_PAT : |
| 500 |
return "--pattern"; |
| 501 |
case TOK_STR : |
| 502 |
return "string"; |
| 503 |
case TOK_WHOLE : |
| 504 |
return "-w"; |
| 505 |
case TOK_FIELD : |
| 506 |
return "-F"; |
| 507 |
default: |
| 508 |
message(L_FATAL, 0, |
| 509 |
_("internal error: unknown token %d"), tok); |
| 510 |
fail(); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
static void unexpected(int tok) |
| 515 |
{ |
| 516 |
switch (tok) { |
| 517 |
case TOK_EOD: |
| 518 |
message(L_FATAL, 0, _("unexpected end of filter")); |
| 519 |
fail(); |
| 520 |
case TOK_PAT : |
| 521 |
message(L_FATAL, 0, _("unexpected pattern in command line")); |
| 522 |
fail(); |
| 523 |
case TOK_STR : |
| 524 |
message(L_FATAL, 0, _("unexpected string in command line")); |
| 525 |
fail(); |
| 526 |
default: |
| 527 |
message(L_FATAL, 0, |
| 528 |
_("unexpected '%s' in command line"), |
| 529 |
tokdescr(tok)); |
| 530 |
fail(); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
struct predicate_qualifiers { |
| 535 |
struct strlist *fields; |
| 536 |
enum matching_mode mm; |
| 537 |
bool ignore_case; |
| 538 |
bool whole_pkg; |
| 539 |
}; |
| 540 |
|
| 541 |
static struct predicate * parse_conj(struct arguments * args, |
| 542 |
struct predicate_qualifiers pq); |
| 543 |
|
| 544 |
/* prim -> primtok |
| 545 |
prim -> primtok prim |
| 546 |
prim -> TOK_LP conj TOK_RP |
| 547 |
prim -> TOK_PAT prim' |
| 548 |
prim -> TOK_STR prim' |
| 549 |
|
| 550 |
prim' -> |
| 551 |
prim' -> primtok prim' |
| 552 |
|
| 553 |
primtok -> TOK_FIELD |
| 554 |
primtok -> TOK_ERGEX | TOK_REGEX |
| 555 |
primtok -> TOK_ICASE | TOK_EXACT | TOK_WHOLE |
| 556 |
primtok -> TOK_EQ | TOK_LT | TOK_LE | TOK_GE | TOK_GT |
| 557 |
*/ |
| 558 |
static struct predicate * parse_prim(struct arguments * args, |
| 559 |
struct predicate_qualifiers pq_) |
| 560 |
{ |
| 561 |
char *pattern = 0; |
| 562 |
struct predicate *rv = 0; |
| 563 |
|
| 564 |
struct predicate_qualifiers pq = pq_; |
| 565 |
struct strlist_memento mem = strlist_save(pq.fields); |
| 566 |
|
| 567 |
bool nonempty = false; |
| 568 |
while (1) { |
| 569 |
debug("tok = %s, mm = %d", tokdescr(peek_token(args)), pq.mm); |
| 570 |
switch (peek_token(args)) { |
| 571 |
case TOK_FIELD: |
| 572 |
if (!strlist_append(pq.fields, get_string(args))) { |
| 573 |
enomem(0); |
| 574 |
} |
| 575 |
break; |
| 576 |
case TOK_ERGEX: |
| 577 |
if (pq.mm != M_SUBSTR) goto failmode; |
| 578 |
pq.mm = M_EREGEX; |
| 579 |
get_token(args); |
| 580 |
break; |
| 581 |
case TOK_REGEX: |
| 582 |
if (pq.mm != M_SUBSTR) goto failmode; |
| 583 |
pq.mm = M_REGEX; |
| 584 |
get_token(args); |
| 585 |
break; |
| 586 |
case TOK_ICASE: |
| 587 |
pq.ignore_case = true; |
| 588 |
get_token(args); |
| 589 |
break; |
| 590 |
case TOK_EXACT: |
| 591 |
if (pq.mm != M_SUBSTR) goto failmode; |
| 592 |
pq.mm = M_EXACT; |
| 593 |
get_token(args); |
| 594 |
break; |
| 595 |
case TOK_WHOLE: |
| 596 |
if (pq.mm != M_SUBSTR) goto failmode; |
| 597 |
pq.mm = M_EREGEX; |
| 598 |
pq.whole_pkg = true; |
| 599 |
get_token(args); |
| 600 |
break; |
| 601 |
case TOK_EQ: |
| 602 |
if (pq.mm != M_SUBSTR) goto failmode; |
| 603 |
pq.mm = M_VER_EQ; |
| 604 |
get_token(args); |
| 605 |
break; |
| 606 |
case TOK_LT: |
| 607 |
if (pq.mm != M_SUBSTR) goto failmode; |
| 608 |
pq.mm = M_VER_LT; |
| 609 |
get_token(args); |
| 610 |
break; |
| 611 |
case TOK_LE: |
| 612 |
if (pq.mm != M_SUBSTR) goto failmode; |
| 613 |
pq.mm = M_VER_LE; |
| 614 |
get_token(args); |
| 615 |
break; |
| 616 |
case TOK_GT: |
| 617 |
if (pq.mm != M_SUBSTR) goto failmode; |
| 618 |
pq.mm = M_VER_GT; |
| 619 |
get_token(args); |
| 620 |
break; |
| 621 |
case TOK_GE: |
| 622 |
if (pq.mm != M_SUBSTR) goto failmode; |
| 623 |
pq.mm = M_VER_GE; |
| 624 |
get_token(args); |
| 625 |
break; |
| 626 |
case TOK_LP: |
| 627 |
{ |
| 628 |
get_token(args); |
| 629 |
rv = parse_conj(args, pq); |
| 630 |
if (get_token(args) != TOK_RP) { |
| 631 |
message(L_FATAL, 0, |
| 632 |
_("missing ')' in command line")); |
| 633 |
fail(); |
| 634 |
} |
| 635 |
goto finally; |
| 636 |
} |
| 637 |
case TOK_PAT: |
| 638 |
if (pattern != 0) { |
| 639 |
message(L_FATAL, 0, |
| 640 |
_("Multiple patterns for the same " |
| 641 |
"simple filter are not allowed")); |
| 642 |
fail(); |
| 643 |
} |
| 644 |
/* passthrough */ |
| 645 |
case TOK_STR: |
| 646 |
if (pattern != 0) goto loop_done; |
| 647 |
pattern = get_string(args); |
| 648 |
break; |
| 649 |
default: |
| 650 |
goto loop_done; |
| 651 |
} |
| 652 |
nonempty = true; |
| 653 |
} loop_done: |
| 654 |
|
| 655 |
if (!nonempty) { |
| 656 |
unexpected(get_token(args)); |
| 657 |
} |
| 658 |
|
| 659 |
if (pattern == 0) { |
| 660 |
message(L_FATAL, 0, _("A pattern is mandatory")); |
| 661 |
fail(); |
| 662 |
} |
| 663 |
|
| 664 |
if (strlist_is_empty(pq.fields)) { |
| 665 |
strlist_append(pq.fields, 0); |
| 666 |
} |
| 667 |
|
| 668 |
for (struct strlist_iterator it = strlist_begin(pq.fields); |
| 669 |
!strlist_iterator_at_end(it); strlist_iterator_next(&it)) { |
| 670 |
struct atom * atom = malloc(sizeof *atom); |
| 671 |
if (atom == 0) enomem(0); |
| 672 |
atom->field_name = strlist_iterator_get(it); |
| 673 |
atom->field_inx = -1; |
| 674 |
atom->mode = pq.mm; |
| 675 |
atom->ignore_case = pq.ignore_case; |
| 676 |
atom->whole_pkg = pq.whole_pkg; |
| 677 |
atom->pat = pattern; |
| 678 |
atom->patlen = strlen(pattern); |
| 679 |
atom_finish(atom); |
| 680 |
struct predicate *tmp = predicate_ATOM(atom); |
| 681 |
rv = rv != 0 ? predicate_OR(rv, tmp) : tmp; |
| 682 |
} |
| 683 |
|
| 684 |
finally: |
| 685 |
strlist_restore(mem); |
| 686 |
return rv; |
| 687 |
failmode: |
| 688 |
message(L_FATAL, 0, _("inconsistent modifiers of simple filters")); |
| 689 |
fail(); |
| 690 |
rv = 0; |
| 691 |
goto finally; |
| 692 |
} |
| 693 |
|
| 694 |
/* neg -> TOK_NOT prim |
| 695 |
neg -> prim |
| 696 |
*/ |
| 697 |
static struct predicate * parse_neg(struct arguments * args, |
| 698 |
struct predicate_qualifiers pq) |
| 699 |
{ |
| 700 |
bool neg = false; |
| 701 |
if (peek_token(args) == TOK_NOT) { |
| 702 |
neg = true; |
| 703 |
get_token(args); |
| 704 |
} |
| 705 |
struct predicate * rv = parse_prim(args, pq); |
| 706 |
if (neg) rv = predicate_NOT(rv); |
| 707 |
return rv; |
| 708 |
} |
| 709 |
|
| 710 |
/* disj -> neg |
| 711 |
disj -> disj TOK_OR neg |
| 712 |
*/ |
| 713 |
static struct predicate * parse_disj(struct arguments * args, |
| 714 |
struct predicate_qualifiers pq) |
| 715 |
{ |
| 716 |
struct predicate * rv = parse_neg(args, pq); |
| 717 |
while (peek_token(args) == TOK_OR) { |
| 718 |
get_token(args); |
| 719 |
struct predicate * tmp = parse_neg(args, pq); |
| 720 |
rv = predicate_OR(rv, tmp); |
| 721 |
} |
| 722 |
return rv; |
| 723 |
} |
| 724 |
|
| 725 |
/* conj -> disj |
| 726 |
conj -> conj TOK_AND disj |
| 727 |
*/ |
| 728 |
static struct predicate * parse_conj(struct arguments * args, |
| 729 |
struct predicate_qualifiers pq) |
| 730 |
{ |
| 731 |
struct predicate * rv = parse_disj(args, pq); |
| 732 |
while (peek_token(args) == TOK_AND) { |
| 733 |
get_token(args); |
| 734 |
struct predicate * tmp = parse_disj(args, pq); |
| 735 |
rv = predicate_AND(rv, tmp); |
| 736 |
} |
| 737 |
return rv; |
| 738 |
} |
| 739 |
|
| 740 |
/* predicate -> conj files |
| 741 |
files -> |
| 742 |
files -> TOK_STR files |
| 743 |
*/ |
| 744 |
static void parse_predicate(struct arguments * args) |
| 745 |
{ |
| 746 |
args->toks_pos = 0; |
| 747 |
|
| 748 |
struct predicate_qualifiers pq; |
| 749 |
pq.fields = strlist_new(); |
| 750 |
if (pq.fields == 0) enomem(0); |
| 751 |
pq.mm = M_SUBSTR; |
| 752 |
pq.ignore_case = false; |
| 753 |
pq.whole_pkg = false; |
| 754 |
|
| 755 |
args->p = parse_conj(args, pq); |
| 756 |
|
| 757 |
strlist_free(pq.fields); |
| 758 |
|
| 759 |
while (peek_token(args) == TOK_STR) { |
| 760 |
if (args->num_fnames >= MAX_FNAMES) { |
| 761 |
message(L_FATAL, 0, _("too many file names")); |
| 762 |
fail(); |
| 763 |
} |
| 764 |
char * s = get_string(args); |
| 765 |
args->fname[args->num_fnames++] = |
| 766 |
(struct ifile){ .mode = m_read, .s = s }; |
| 767 |
} |
| 768 |
if (peek_token(args) != TOK_EOD) { |
| 769 |
message(L_FATAL, 0, |
| 770 |
_("file names are not allowed within the filter")); |
| 771 |
fail(); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
static void show_field(struct arguments *args, |
| 776 |
struct paragraph *para, |
| 777 |
struct field_attr *fa) |
| 778 |
{ |
| 779 |
struct field_data fds = |
| 780 |
find_field_wr(para, |
| 781 |
fa->inx, |
| 782 |
fa->backup_field); |
| 783 |
for (struct field_datum *fd = fds.first; fd != NULL; fd = fd->next) { |
| 784 |
struct fsaf_read_rv r = |
| 785 |
fsaf_read(para->common->fp, |
| 786 |
fd->start, fd->end - fd->start); |
| 787 |
|
| 788 |
if (args->short_descr && |
| 789 |
fa == description_attr) { |
| 790 |
char * nl = memchr(r.b, '\n', r.len); |
| 791 |
if (nl != 0) r.len = nl - r.b; |
| 792 |
} |
| 793 |
|
| 794 |
if (r.len == 0) { |
| 795 |
/* don't display a field with an empty value */ |
| 796 |
break; |
| 797 |
} |
| 798 |
|
| 799 |
if (args->show_field_name) { |
| 800 |
struct fsaf_read_rv rn = |
| 801 |
fsaf_read(para->common->fp, |
| 802 |
fd->name_start, |
| 803 |
fd->name_end - fd->name_start); |
| 804 |
fwrite(rn.b, 1, rn.len, stdout); |
| 805 |
fputs(": ", stdout); |
| 806 |
} |
| 807 |
|
| 808 |
fwrite(r.b, 1, r.len, stdout); |
| 809 |
puts(""); |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
static void print_para(struct arguments *args, |
| 814 |
struct paragraph *para) |
| 815 |
{ |
| 816 |
if (args->num_show_fields == 0) { |
| 817 |
struct fsaf_read_rv r = get_whole_para(para); |
| 818 |
fwrite(r.b, 1, r.len, stdout); |
| 819 |
putchar('\n'); |
| 820 |
putchar('\n'); |
| 821 |
return; |
| 822 |
} |
| 823 |
if (args->invert_show) { |
| 824 |
for (size_t j = 0; |
| 825 |
j < fieldtrie_count() && j < para->nfields; |
| 826 |
j++) { |
| 827 |
struct field_attr *fa = fieldtrie_get(j); |
| 828 |
if (fa->is_show_field) continue; |
| 829 |
show_field(args, para, fa); |
| 830 |
} |
| 831 |
puts(""); |
| 832 |
} else { |
| 833 |
for (size_t j = 0; j < args->num_show_fields; j++) { |
| 834 |
size_t inx = args->show_fields[j]; |
| 835 |
struct field_attr *fa = fieldtrie_get(inx); |
| 836 |
assert(fa->is_show_field); |
| 837 |
show_field(args, para, fa); |
| 838 |
} |
| 839 |
if ((args->show_field_name && args->ensure_dctrl) || |
| 840 |
args->num_show_fields > 1) { |
| 841 |
puts(""); |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
static struct argp argp = { .options = options, |
| 848 |
.parser = parse_opt, |
| 849 |
.args_doc = argsdoc, |
| 850 |
.doc = progdoc }; |
| 851 |
|
| 852 |
int main (int argc, char * argv[]) |
| 853 |
{ |
| 854 |
setlocale(LC_ALL, ""); |
| 855 |
bindtextdomain (PACKAGE, LOCALEDIR); |
| 856 |
textdomain(PACKAGE); |
| 857 |
|
| 858 |
fieldtrie_init(); |
| 859 |
|
| 860 |
static struct arguments args; |
| 861 |
args.show_field_name = true; |
| 862 |
msg_set_progname(argv[0]); |
| 863 |
description_attr = fieldtrie_insert(description); |
| 864 |
argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, &args); |
| 865 |
if (debug_optparse) { |
| 866 |
fflush(stderr); |
| 867 |
fputs("tokens:", stdout); |
| 868 |
for (size_t i = 0; i < args.toks_np; i++) { |
| 869 |
putchar(' '); |
| 870 |
fputs(tokdescr(args.toks[i]), stdout); |
| 871 |
if (args.strings[i] != 0) { |
| 872 |
printf("[%s]", args.strings[i]); |
| 873 |
} |
| 874 |
} |
| 875 |
puts(""); |
| 876 |
fflush(stdout); |
| 877 |
} |
| 878 |
|
| 879 |
parse_predicate(&args); |
| 880 |
|
| 881 |
if (debug_optparse) { dump_args(&args); return 0; } |
| 882 |
|
| 883 |
if (!check_predicate(args.p)) { |
| 884 |
message(L_FATAL, 0, _("malformed filter")); |
| 885 |
fail(); |
| 886 |
} |
| 887 |
|
| 888 |
if (args.short_descr && !args.description_selected) { |
| 889 |
if (args.num_show_fields >= MAX_FIELDS) { |
| 890 |
message(L_FATAL, 0, _("too many output fields")); |
| 891 |
fail(); |
| 892 |
} |
| 893 |
message(L_INFORMATIONAL, 0, |
| 894 |
_("Adding \"Description\" to selected output fields because of -d")); |
| 895 |
description_attr->is_show_field = 1; |
| 896 |
args.show_fields[args.num_show_fields] = description_attr->inx; |
| 897 |
++args.num_show_fields; |
| 898 |
} |
| 899 |
|
| 900 |
if (args.invert_show && args.num_show_fields == 0) { |
| 901 |
message(L_FATAL, 0, |
| 902 |
_("-I requires at least one instance of -s")); |
| 903 |
fail(); |
| 904 |
} |
| 905 |
|
| 906 |
if (!args.show_field_name && args.num_show_fields == 0) { |
| 907 |
message(L_FATAL, 0, |
| 908 |
_("cannot suppress field names when showing whole paragraphs")); |
| 909 |
fail(); |
| 910 |
} |
| 911 |
|
| 912 |
size_t count = 0; |
| 913 |
bool found = false; |
| 914 |
for (size_t i = 0; i < args.num_fnames || (i == 0 && args.num_fnames == 0); ++i) { |
| 915 |
int fd; |
| 916 |
struct ifile fname; |
| 917 |
if (args.num_fnames == 0) { |
| 918 |
char * argv0 = fnbase(argv[0]); |
| 919 |
if (strcmp(argv0, "grep-dctrl") == 0) { |
| 920 |
fname = (struct ifile){ .mode = m_read, |
| 921 |
.s = "-" }; |
| 922 |
} else if (strcmp(argv0, "grep-status") == 0) { |
| 923 |
fname = (struct ifile){ |
| 924 |
.mode = m_read, |
| 925 |
.s = "/var/lib/dpkg/status" }; |
| 926 |
} else if (strcmp(argv0, "grep-available") == 0) { |
| 927 |
fname = (struct ifile){ |
| 928 |
.mode = m_read, |
| 929 |
.s = "/var/lib/dpkg/available" }; |
| 930 |
} else if (strcmp(argv0, "grep-aptavail") == 0) { |
| 931 |
fname = (struct ifile){ |
| 932 |
.mode = m_exec, |
| 933 |
.s = "apt-cache dumpavail" }; |
| 934 |
} else if (strcmp(argv0, "grep-debtags") == 0) { |
| 935 |
fname = (struct ifile){ |
| 936 |
.mode = m_exec, |
| 937 |
.s = "debtags dumpavail" }; |
| 938 |
} else { |
| 939 |
message(L_FATAL, 0, |
| 940 |
_("executable name '%s' is not recognised"), |
| 941 |
argv0); |
| 942 |
fail(); |
| 943 |
} |
| 944 |
} else { |
| 945 |
fname = args.fname[i]; |
| 946 |
} |
| 947 |
|
| 948 |
if (fname.mode == m_error) break; |
| 949 |
|
| 950 |
fd = open_ifile(fname); |
| 951 |
if (fd == -1) break; |
| 952 |
|
| 953 |
if (!chk_ifile(fname, fd)) break; |
| 954 |
|
| 955 |
FSAF * fp = fsaf_fdopen(fd, fname.s); |
| 956 |
para_parser_t pp; |
| 957 |
para_parser_init(&pp, fp, true, args.ignore_errors, |
| 958 |
args.invert_show); |
| 959 |
para_t para; |
| 960 |
para_init(&pp, ¶); |
| 961 |
while (1) { |
| 962 |
para_parse_next(¶); |
| 963 |
if (para_eof(&pp)) { |
| 964 |
if (args.only_file_name && args.invert_match) { |
| 965 |
printf("%s\n", fname.s); |
| 966 |
found = true; |
| 967 |
} |
| 968 |
break; |
| 969 |
} |
| 970 |
bool sat = does_para_satisfy(args.p, ¶); |
| 971 |
if (args.only_file_name) { |
| 972 |
if (!sat) continue; |
| 973 |
if (!args.invert_match) { |
| 974 |
printf("%s\n", fname.s); |
| 975 |
found = true; |
| 976 |
} |
| 977 |
break; |
| 978 |
} |
| 979 |
if (sat == args.invert_match) continue; |
| 980 |
|
| 981 |
if (args.quiet) { |
| 982 |
exit(0); |
| 983 |
} |
| 984 |
found = true; |
| 985 |
if (args.count) { |
| 986 |
++count; |
| 987 |
continue; |
| 988 |
} |
| 989 |
|
| 990 |
print_para(&args, ¶); |
| 991 |
} |
| 992 |
fsaf_close(fp); |
| 993 |
close_ifile(fname, fd); |
| 994 |
} |
| 995 |
if (args.count) printf("%zi\n", count); |
| 996 |
return errors_reported() ? 2 : found || args.count ? 0 : 1; |
| 997 |
} |
| 998 |
|