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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
/*
Numdiff - compare putatively similar files,
ignoring small numeric differences
Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Ivano Primi <ivprimi@libero.it>
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<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<errno.h>
#ifdef ENABLE_NLS
#include<locale.h>
#endif
#include"getopt.h"
#include"linesplit.h"
#include"numdiff.h" /* For NEWLINE and LINE_INTERR definitions */
#include"ndselect.h"
#include"read_line.c"
static
int we_can_go_on (const char *field,
unsigned long fieldno, unsigned long last_field)
{
if (!last_field)
return (*field != '\0');
else
return (fieldno <= last_field && *field != '\0');
}
static
void print_line (const char *line, const char **ifs, const char* osep,
unsigned long first_field, unsigned long last_field,
unsigned long increment, int omit_if_empty)
{
unsigned long fieldno, lf;
int print_nl, at_least_one_field_printed;
const char *field, *endfield, *nextfield, *stop, *ptr;
if (!last_field)
lf = 0;
else
{
lf = (last_field - first_field) / increment;
lf = lf * increment + first_field;
}
at_least_one_field_printed = 0;
print_nl = strchr (line, NEWLINE) != NULL ? 1 : 0;
field = string_spn (line, ifs, '\0');
for (fieldno = 1; (we_can_go_on (field, fieldno, last_field)) ; fieldno++)
{
endfield = string_cspn (field, ifs, '\0');
nextfield = string_spn (endfield, ifs, '\0');
if (fieldno >= first_field && (fieldno - first_field) % increment == 0)
{
if ((osep))
stop = endfield;
else
stop = (fieldno == lf || *nextfield == '\0') ? endfield : nextfield;
for (ptr = field; ptr < stop; ptr++)
fputc(*ptr, stdout);
if ((osep))
{
fputs (osep, stdout);
}
at_least_one_field_printed = 1;
}
field = nextfield;
}
if ((omit_if_empty))
{
if ((print_nl) && (at_least_one_field_printed))
fputc(NEWLINE, stdout);
}
else
{
if ((print_nl))
fputc(NEWLINE, stdout);
else
fputc(' ', stdout);
}
}
static
int scan_file (char** def_ifs, const Argslist* data)
{
FILE *fp;
char *line_buffer;
char **ifs;
unsigned long lineno;
int errcode, omit_empty_lines;
ifs = (!data->ifs) ? def_ifs : data->ifs;
omit_empty_lines =(getBitAtPosition (&data->optmask, ___X_MASK) ==BIT_ON);
if (!data->file || !*data->file)
fp = stdin;
else
{
if ( !(fp = fopen (data->file, "r")) )
return OPEN_ERROR;
}
if (!data->end_line)
{
for (lineno = 1;
(line_buffer = read_line(fp, &errcode), errcode <= LINE_INTERR);
lineno++)
{
if (lineno >= data->begin_line && (lineno - data->begin_line) % data->step == 0)
print_line (line_buffer, (const char**) ifs, data->osep,
data->first_field, data->last_field, data->increment, omit_empty_lines);
free ((void*)line_buffer);
}
}
else
{
for (lineno = 1;
lineno <= data->end_line &&
(line_buffer = read_line(fp, &errcode), errcode <= LINE_INTERR);
lineno++)
{
if (lineno >= data->begin_line && (lineno - data->begin_line) % data->step == 0)
print_line (line_buffer, (const char**) ifs, data->osep,
data->first_field, data->last_field, data->increment, omit_empty_lines);
free ((void*)line_buffer);
}
}
if (errcode <= EOF_REACHED)
{
if (fp == stdin)
return OK;
else
return fclose (fp) == EOF ? CLOSE_ERROR : OK;
}
else
{
if((line_buffer))
free ((void*)line_buffer);
if (fp != stdin)
fclose (fp);
return READ_ERROR;
}
}
static
void print_selversion (const char* progname)
{
printf ("%s %s\n", progname, VERSION);
printf ("Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 %s <ivprimi@libero.it>\n",
/* TRANSLATORS: This is a proper name. See the gettext
manual, section Names.
Pronounciation is like "evaa-no pree-me". */
_("Ivano Primi"));
printf (_("\
License GPLv3+: GNU GPL version 3 or later,\n\
see <http://gnu.org/licenses/gpl.html>.\n\
This is free software: you are free to change and redistribute it.\n\
There is NO WARRANTY, to the extent permitted by law.\n"));
}
static
void print_selhelp (const char* progname)
{
puts (_("Usage:"));
printf ("%s -h|--help|-v|--version %s\n\n", progname, _("or"));
printf ("%s %s\n", progname, "[-b N][-e N][-s N][-F N][-L N][-I N][-S IFS][-D DELIMS][-O OSEP][-x][-l PATH][-o PATH] [FILE]");
/* %%% */
printf(_("\nPrint to standard output a subset of lines and fields from a given file.\n"));
printf ("\n%s\n%s\n%s\n\n%s\n\n",
_("The argument after the options is the name of the file to read from."),
_("The complete path of the file should be given,\na directory name is not accepted."),
_("If no input file is specified, the program reads from the standard input."),
_("Exit status: 0 in case of normal termination, -1 (255) in case of error"));
printf ("-b, --beginning, --start=N\n %s\n %s\n",
_("Set to N the number of the first line to print"),
_("(The default behavior is to start with line number 1)"));
printf ("-e, --end=N\n %s\n %s\n",
_("Set to N the number of the last line that can be printed"),
_("(The default behavior is to arrive till to the end of the file)"));
printf ("-s, --step=N\n %s\n %s\n",
_("Set to N the increment to use when selecting the lines to print"),
_("(The default value for the increment is 1)"));
printf ("-F, --first-field=N\n %s\n %s\n",
_("Set to N the number of the first field to print"),
_("(The default behavior is to start with field number 1)"));
printf ("-L, --last-field=N\n %s\n %s\n",
_("Set to N the number of the last field that can be printed"),
_("(The default behavior is to arrive till to the end of every line)"));
printf ("-I, --increment=N\n %s\n %s\n",
_("Set to N the increment to use when selecting the fields to print"),
_("(The default value for the increment is 1)"));
printf ("-S, --separators=IFS\n %s\n %s\n",
_("Specify the set of characters to use as delimiters\n while splitting the input lines into fields"),
_("(The default set of delimiters is space, tab and newline)"));
printf ("-D, --delimiters=DELIMS\n %s\n %s\n",
_("Specify the set of strings to use as delimiters\n while splitting the input lines into fields"),
_("(The default set of delimiters is space, tab and newline)"));
printf ("-O, --output-separator=OSEP\n %s\n %s\n",
_("Specify the string to use as separator\n while writing the selected fields to the standard output"),
_("(The default behavior consists in reusing\n the delimiters found in the input lines)"));
printf ("-x, --omit-empty-lines\n %s\n",
_("Do not print empty lines"));
printf ("-l, --warnings-to=PATH\n %s\n",
_("Redirect warning and error messages from stderr to the indicated file"));
printf ("-o, --output=PATH\n %s\n",
_("Redirect output from stdout to the indicated file"));
printf ("-h, --help\n %s\n", _("Show this help message"));
printf ("-v, --version\n %s\n\n", _("Show version number, Copyright, Distribution Terms and NO-Warranty"));
}
extern int errno;
extern char *optarg;
extern int optind;
static
int set_args (int argc, char* argv[], Argslist *list)
{
const char *optstring = "hb:e:s:F:L:I:S:D:O:xl:o:v";
struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"beginning", 1, NULL, 'b'},
{"start", 1, NULL, 'b'},
{"end", 1, NULL, 'e'},
{"step", 1, NULL, 's'},
{"first-field", 1, NULL, 'F'},
{"last-field", 1, NULL, 'L'},
{"increment", 1, NULL, 'I'},
{"separators", 1, NULL, 'S'},
{"delimiters", 1, NULL, 'D'},
{"output-separator", 1, NULL, 'O'},
{"omit-empty-lines", 0, NULL, 'x'},
{"warnings-to", 1, NULL, 'l'},
{"output", 1, NULL, 'o'},
{"version", 0, NULL, 'v'},
{0, 0, 0, 0}
};
int option_index=0;
char *endptr;
int optch;
/*
We start by loading the default values
for the user settable options.
*/
list->optmask = newBitVector (MAX_NDSELECT_OPTIONS);
list->begin_line=1;
list->end_line=0;
list->step=1;
list->first_field=1;
list->last_field=0;
list->increment=1;
list->ifs = NULL;
list->osep = NULL;
list->file = NULL;
while ( (optch = getopt_long (argc, argv, optstring, long_options, &option_index)) != -1 )
{
switch (optch)
{
case 'h':
setBitAtPosition (&list->optmask, ___H_MASK, BIT_ON);
break;
case 'b':
setBitAtPosition (&list->optmask, ___B_MASK, BIT_ON);
errno = 0;
for (endptr = optarg; is_space (*endptr) != 0; endptr++);
if (*endptr == '-' ||
(list->begin_line = strtoul (optarg, &endptr, 10),
errno == ERANGE) || list->begin_line == 0 ||
*endptr != '\0')
{
fprintf (stderr, _("%s: invalid argument after `-%c\' option\n"),
PACKAGE2, optch);
return -1;
}
break;
case 'e':
setBitAtPosition (&list->optmask, ___E_MASK, BIT_ON);
errno = 0;
for (endptr = optarg; is_space (*endptr) != 0; endptr++);
if (*endptr == '-' ||
(list->end_line = strtoul (optarg, &endptr, 10),
errno == ERANGE) ||
*endptr != '\0')
{
fprintf (stderr, _("%s: invalid argument after `-%c\' option\n"),
PACKAGE2, optch);
return -1;
}
break;
case 's':
setBitAtPosition (&list->optmask, ___S_MASK, BIT_ON);
errno = 0;
for (endptr = optarg; is_space (*endptr) != 0; endptr++);
if (*endptr == '-' ||
(list->step = strtoul (optarg, &endptr, 10),
errno == ERANGE) || list->step == 0 ||
*endptr != '\0')
{
fprintf (stderr, _("%s: invalid argument after `-%c\' option\n"),
PACKAGE2, optch);
return -1;
}
break;
case 'F':
setBitAtPosition (&list->optmask, ___SF_MASK, BIT_ON);
errno = 0;
for (endptr = optarg; is_space (*endptr) != 0; endptr++);
if (*endptr == '-' ||
(list->first_field = strtoul (optarg, &endptr, 10),
errno == ERANGE) || list->first_field == 0 ||
*endptr != '\0')
{
fprintf (stderr, _("%s: invalid argument after `-%c\' option\n"),
PACKAGE2, optch);
return -1;
}
break;
case 'L':
setBitAtPosition (&list->optmask, ___SL_MASK, BIT_ON);
errno = 0;
for (endptr = optarg; is_space (*endptr) != 0; endptr++);
if (*endptr == '-' ||
(list->last_field = strtoul (optarg, &endptr, 10),
errno == ERANGE) ||
*endptr != '\0')
{
fprintf (stderr, _("%s: invalid argument after `-%c\' option\n"),
PACKAGE2, optch);
return -1;
}
break;
case 'I':
setBitAtPosition (&list->optmask, ___SI_MASK, BIT_ON);
errno = 0;
for (endptr = optarg; is_space (*endptr) != 0; endptr++);
if (*endptr == '-' ||
(list->increment = strtoul (optarg, &endptr, 10),
errno == ERANGE) || list->increment == 0 ||
*endptr != '\0')
{
fprintf (stderr, _("%s: invalid argument after `-%c\' option\n"),
PACKAGE2, optch);
return -1;
}
break;
case 'S':
if ((list->ifs))
{
delete_string_vector (list->ifs);
list->ifs = NULL;
}
list->ifs = ssplit_former_way (optarg);
if (!list->ifs)
{
fprintf (stderr, _("%s: memory exhausted\n"), PACKAGE2);
return -1;
}
else if ( !is_string_in_vector(NEWLINE_STR, (const char**) list->ifs) )
{
fprintf (stderr, _("%s: invalid argument after `-%c\' option:\n"),
PACKAGE2, optch);
fprintf (stderr, _(" The list of field delimiters cannot be empty and\n must always include the newline character (\'\\n\')\n"));
return -1;
}
else
{
remove_duplicates_from_string_vector (list->ifs);
sort_string_vector (list->ifs); /* This is not strictly necessary */
setBitAtPosition (&list->optmask, ___SS_MASK, BIT_ON);
}
break;
case 'D':
if ((list->ifs))
{
delete_string_vector (list->ifs);
list->ifs = NULL;
}
list->ifs = ssplit (optarg, I_DEF_SEP);
if (!list->ifs)
{
fprintf (stderr, _("%s: memory exhausted\n"), PACKAGE2);
return -1;
}
else if ( !is_string_in_vector(NEWLINE_STR, (const char**) list->ifs) )
{
fprintf (stderr, _("%s: invalid argument after `-%c\' option:\n"),
PACKAGE2, optch);
fprintf (stderr, _(" The list of field delimiters cannot be empty and\n must always include the newline string (\"\\n\")\n"));
return -1;
}
else
{
remove_duplicates_from_string_vector (list->ifs);
sort_string_vector (list->ifs);
setBitAtPosition (&list->optmask, ___SD_MASK, BIT_ON);
}
break;
case 'O':
if ((list->osep))
{
free((void*)list->osep);
list->osep = NULL;
}
list->osep = get_separating_string (optarg);
if (!list->osep)
{
fprintf (stderr, _("%s: memory exhausted\n"), PACKAGE2);
return -1;
}
else
setBitAtPosition (&list->optmask, ___SO_MASK, BIT_ON);
break;
case 'x':
setBitAtPosition (&list->optmask, ___X_MASK, BIT_ON);
break;
case 'l':
if (!freopen (optarg, "w", stderr))
{
fprintf (stderr, _("%s: cannot open file \"%s\":\n"),
PACKAGE2, optarg);
perror(0);
return -1;
}
setBitAtPosition (&list->optmask, ___L_MASK, BIT_ON);
break;
case 'o':
if (!freopen (optarg, "w", stdout))
{
fprintf (stderr, _("%s: cannot open file \"%s\":\n"),
PACKAGE2, optarg);
perror(0);
return -1;
}
setBitAtPosition (&list->optmask, ___O_MASK, BIT_ON);
break;
case 'v':
setBitAtPosition (&list->optmask, ___V_MASK, BIT_ON);
break;
default:
/* fprintf (stderr, */
/* _("%s: unrecognized option `-%c\' \n"), PACKAGE2, optch); */
return -1;
}
}
if (getBitAtPosition (&list->optmask, ___H_MASK) == BIT_OFF &&
getBitAtPosition (&list->optmask, ___V_MASK) == BIT_OFF &&
argc - optind > 1)
{
print_selhelp (PACKAGE2);
return -1;
}
else
{
if (getBitAtPosition (&list->optmask, ___H_MASK) == BIT_OFF &&
getBitAtPosition (&list->optmask, ___V_MASK) == BIT_OFF)
list->file = (const char*) argv[optind];
return 0;
}
}
static
char** def_ifs = NULL;
static
void clean_up_memory (void)
{
delete_string_vector (def_ifs);
}
int main (int argc, char** argv)
{
Argslist arg_list;
int pHelp, pVersion;
def_ifs = ssplit (DEF_IFS, I_DEF_SEP);
if ((atexit(clean_up_memory)) || !def_ifs)
{
fprintf (stderr, _("%s: memory exhausted\n"), PACKAGE2);
return -1;
}
#ifdef ENABLE_NLS
setlocale (LC_CTYPE, "");
setlocale (LC_MESSAGES, "");
#endif
bindtextdomain (PACKAGE2, LOCALEDIR);
textdomain (PACKAGE2);
if ( set_args (argc, argv, &arg_list) != 0 )
return -1;
pHelp = getBitAtPosition (&arg_list.optmask, ___H_MASK) == BIT_ON;
pVersion = getBitAtPosition (&arg_list.optmask, ___V_MASK) == BIT_ON;
if ( (pHelp) || (pVersion) )
{
if ((pVersion))
print_selversion(PACKAGE2);
if ((pHelp))
print_selhelp(PACKAGE2);
return (argc > 2 ? -1 : 0);
}
else
{
switch (scan_file (def_ifs, &arg_list))
{
case OPEN_ERROR:
fprintf (stderr, _("%s: cannot open file \"%s\":\n"), PACKAGE2, arg_list.file);
perror(0);
return -1;
case CLOSE_ERROR:
fprintf (stderr, _("%s: cannot close file \"%s\":\n"), PACKAGE2, arg_list.file);
perror(0);
return -1;
case READ_ERROR:
fprintf (stderr, _("%s: Error occurred while reading from file \"%s\"\n\n"), PACKAGE2, arg_list.file);
return -1;
default:
return 0;
}
}
}
|