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
|
/*
* fhist - file history and comparison tools
* Copyright (C) 1991-1994, 1998-2000, 2002 Peter Miller;
* All rights reserved.
*
* Derived from a work
* Copyright (C) 1990 David I. Bell.
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: operating system entry point, and parse arguments
*
* fcomp - a file comparison program
*/
#include <ac/errno.h>
#include <ac/stdio.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <linux/stat.h>
#include <arglex.h>
#include <cmalloc.h>
#include <compare.h>
#include <error_intl.h>
#include <fileio.h>
#include <help.h>
#include <isdir.h>
#include <progname.h>
#include <quit.h>
#include <sub.h>
#include <version.h>
enum
{
arglex_token_binary,
arglex_token_blank,
arglex_token_context,
arglex_token_debug,
arglex_token_edit,
arglex_token_failures,
arglex_token_join,
arglex_token_line_numbers,
arglex_token_matching,
arglex_token_output,
arglex_token_quiet,
arglex_token_spaces,
arglex_token_upcase,
arglex_token_verbose,
arglex_token_what,
};
static arglex_table_ty argtab[] =
{
{ "-BINary", arglex_token_binary, },
{ "-Blank", arglex_token_blank, },
{ "-Context", arglex_token_context, },
{ "-Debug", arglex_token_debug, },
{ "-Edit", arglex_token_edit, },
{ "-Failures", arglex_token_failures, },
{ "-Join", arglex_token_join, },
{ "-Number", arglex_token_line_numbers, },
{ "-Matching", arglex_token_matching, },
{ "-Output", arglex_token_output, },
{ "-Quiet", arglex_token_quiet, },
{ "-Spaces", arglex_token_spaces, },
{ "-Upcase", arglex_token_upcase, },
{ "-Verbose", arglex_token_verbose, },
{ "-What", arglex_token_what, },
{ 0, 0, }, /* end marker */
};
/*
* Type out the possible commands.
*/
static void
usage(void)
{
char *progname;
progname = progname_get();
fprintf(stderr, "usage: %s [ <option>... ] <fileA> <fileB>\n", progname);
fprintf(stderr, " %s -Help\n", progname);
fprintf(stderr, " %s -VERSion\n", progname);
quit(1);
}
/*
* give the user some help
*/
static void
main_help(void)
{
help(NULL, usage);
}
/*
* Expand a global variable name.
* Returns original argument if string is not a global variable,
* or returns copied value of global variable.
*/
static char *
expand(char *cp)
{
char *var; /* variable value */
if (!cp || *cp != '.')
return cp;
var = getenv(cp);
if (!var || !*var)
return cp;
cp = r_alloc_and_check(strlen(var) + 1);
strcpy(cp, var);
return cp;
}
/*
* Make a directory name into a file name by applying the last
* component of the second name to it. For example:
* dirfile("/etc", "/foo/bar")
* results in "/etc/bar".
*/
static char *
dirfile(char *dir_name, char *filename)
{
char *cp;
char *endname;
endname = strrchr(filename, '/');
if (!endname)
endname = filename;
cp = r_alloc_and_check(strlen(dir_name) + strlen(endname) + 2);
strcpy(cp, dir_name);
strcat(cp, "/");
strcat(cp, endname);
return cp;
}
/*
* This is the main program.
* This performs all of the parsing of the command line, and then
* calls a subroutine to do all the work. This allows the subroutine
* to be loaded with other programs and called by them easily.
*/
int
main(int argc, char **argv)
{
char *cp;
char *nameA;
char *nameB;
char *ofile;
int dirA;
int dirB;
sub_context_ty *scp;
arglex_init(argc, argv, argtab);
switch (arglex())
{
case arglex_token_help:
main_help();
quit(0);
case arglex_token_version:
version();
quit(0);
default:
break;
}
nameA = NULL;
nameB = NULL;
ofile = NULL;
fc.verbosity = VERBOSE_DEFAULT;
fc.maxchanges = INFINITY;
while (arglex_token != arglex_token_eoln)
{
switch (arglex_token)
{
default:
bad_argument(usage);
/* NOTREACHED */
case arglex_token_string:
/* a file name */
cp = expand(arglex_value.alv_string);
if (!nameA)
nameA = cp;
else if (!nameB)
nameB = cp;
else
{
fatal_intl
(
0,
i18n("only two input files allowed")
);
}
break;
case arglex_token_binary:
/* compare binary bytes */
if (fc.hexify_flag)
goto duplicate;
fc.hexify_flag = 1;
break;
case arglex_token_blank:
/* ignore blank lines */
if (fc.blankflag)
{
duplicate:
scp = sub_context_new();
sub_var_set_charstar(scp, "Name", arglex_value.alv_string);
fatal_intl
(
scp,
i18n("duplicate $name command line option")
);
/* NOTREACHED */
sub_context_delete(scp);
}
fc.blankflag = 1;
break;
case arglex_token_debug:
/* type debugging information */
if (fc.debugflag)
goto duplicate;
fc.debugflag = 1;
break;
case arglex_token_edit:
/* output an edit script */
if (fc.editscriptflag)
goto duplicate;
fc.editscriptflag = 1;
break;
case arglex_token_matching:
/* show lines which match */
if (fc.matchflag)
goto duplicate;
fc.matchflag = 1;
break;
case arglex_token_line_numbers:
/* show line numbers */
if (fc.numflag)
goto duplicate;
fc.numflag = 1;
break;
case arglex_token_quiet:
/* display quick summary */
if (fc.quickflag)
goto duplicate;
fc.quickflag = 1;
break;
case arglex_token_spaces:
/* ignore extra spaces or tabs */
if (fc.spaceflag)
goto duplicate;
fc.spaceflag = 1;
break;
case arglex_token_upcase:
/* translate to upper case */
if (fc.upcaseflag)
goto duplicate;
fc.upcaseflag = 1;
break;
case arglex_token_verbose:
/* be verbose */
if (fc.verbosity != VERBOSE_DEFAULT)
goto duplicate;
fc.verbosity = VERBOSE_FULL;
break;
case arglex_token_what:
/* show what changes are */
if (fc.whatflag)
goto duplicate;
fc.whatflag = 1;
break;
case arglex_token_context:
if (fc.context)
goto duplicate;
if (arglex() != arglex_token_number)
{
scp = sub_context_new();
sub_var_set_charstar(scp, "Name", "-Context");
fatal_intl
(
scp,
i18n("numeric value required for $name option")
);
/* NOTREACHED */
sub_context_delete(scp);
}
fc.context = arglex_value.alv_number;
if (fc.context < 1)
{
scp = sub_context_new();
sub_var_set_format(scp, "Number", "%d", fc.context);
fatal_intl
(
scp,
i18n("context of $number is out of range")
);
/* NOTREACHED */
sub_context_delete(scp);
}
break;
case arglex_token_failures:
if (fc.maxchanges != INFINITY)
goto duplicate;
if (arglex() != arglex_token_number)
{
scp = sub_context_new();
sub_var_set_charstar(scp, "Name", "-Failures");
fatal_intl
(
scp,
i18n("numeric value required for $name option")
);
}
fc.maxchanges = arglex_value.alv_number;
if (fc.maxchanges < 0)
{
scp = sub_context_new();
sub_var_set_format(scp, "Number", "%d", fc.maxchanges);
fatal_intl
(
scp,
i18n("maximum changes of $number is out of range")
);
/* NOTREACHED */
sub_context_delete(scp);
}
break;
case arglex_token_join:
if (fc.maxjoin)
goto duplicate;
if (arglex() != arglex_token_number)
{
scp = sub_context_new();
sub_var_set_charstar(scp, "Name", "-Join");
fatal_intl
(
scp,
i18n("numeric value required for $name option")
);
}
fc.maxjoin = arglex_value.alv_number;
if (fc.maxjoin < 1)
{
scp = sub_context_new();
sub_var_set_format(scp, "Number", "%d", fc.maxjoin);
fatal_intl
(
scp,
i18n("join of $number is out of range")
);
/* NOTREACHED */
sub_context_delete(scp);
}
break;
case arglex_token_output:
/* specify output file */
if (ofile)
goto duplicate;
switch (arglex())
{
default:
fatal_intl(0, i18n("file name required after -Output option"));
case arglex_token_string:
ofile = expand(arglex_value.alv_string);
break;
case arglex_token_stdio:
ofile = "";
break;
}
break;
}
arglex();
}
if (nameA && !*nameA)
nameA = 0;
if (nameB && !*nameB)
nameB = 0;
if (!nameA || !nameB)
error_intl(0, i18n("must have two input files"));
if (ofile && !*ofile)
ofile = 0;
if (fc.matchflag + fc.whatflag + fc.editscriptflag >= 2)
fatal_intl(0, i18n("too many output formats specified"));
/*
* If just one argument is a directory, then modify it by using
* the other argument so as to reference a corresponding file
* within the directory.
*/
dirA = isdir(nameA);
dirB = isdir(nameB);
if (dirA && dirB)
{
scp = sub_context_new();
sub_var_set_charstar(scp, "Name1", nameA);
sub_var_set_charstar(scp, "Name2", nameB);
fatal_intl
(
scp,
i18n("arguments \"$name1\" and \"$name2\" are both directories")
);
/* NOTREACHED */
sub_context_delete(scp);
}
if (dirA)
nameA = dirfile(nameA, nameB);
if (dirB)
nameB = dirfile(nameB, nameA);
/*
* compare the two files
*/
fcomp(nameA, nameB);
/*
* report the difference
*/
if (fc.whatflag)
dumpwhat(ofile);
else if (fc.matchflag)
dumpmatch(ofile);
else if (fc.editscriptflag)
dumplines(ofile);
else
dumpnormal(ofile);
quit(0);
return 0;
}
|