1
0
mirror of https://github.com/cc65/cc65.git synced 2025-01-10 03:30:05 +00:00

isequal: Add options for better handling

--binary: handled binary files
--empty:  check if file is empty

--skipleft=<n>: Skip <n> lines at the start of the left (first) file
--skipright=<n>: Skip <n> lines at the start of the right (second) file

Note that --binary, --empty and one or both of --skipXXX are mutual
exclusive; that is, you cannot specify more than one of them at the same
time, or the behaviour will be unpredictable.
This commit is contained in:
Spiro Trikaliotis 2022-06-18 23:38:18 +02:00
parent 297a11fcec
commit 1446b7dcc3
2 changed files with 149 additions and 22 deletions

View File

@ -43,12 +43,9 @@ $(ISEQUAL): ../../isequal.c | $(WORKDIR)
$(CC) $(CFLAGS) -o $@ $<
$(WORKDIR)/_empty:
touch $@
define LISTING_template
$(WORKDIR)/$1.bin: $1.s $(ISEQUAL) $(WORKDIR)/_empty
$(WORKDIR)/$1.bin: $1.s $(ISEQUAL)
$(if $(QUIET),echo asm/$1.bin)
# compile without generating listing
@ -67,20 +64,20 @@ endif
ifneq ($(wildcard $1.err-ref),)
$(ISEQUAL) $1.err-ref $$(@:.bin=.err)
else
$(ISEQUAL) $(WORKDIR)/_empty $$(@:.bin=.err)
$(ISEQUAL) --empty $$(@:.bin=.err)
endif
ifneq ($(wildcard $1.bin-ref),)
$(ISEQUAL) $1.bin-ref $$@
$(ISEQUAL) --binary $1.bin-ref $$@
endif
ifeq ($(wildcard $1.err),)
$(CA65) -t none -l $$(@:.bin=.list.orig) -o $$(@:.bin=.list-o) $$< > $$(@:.bin=.list-err) 2>&1
$(CA65) -t none -l $$(@:.bin=.lst) -o $$(@:.bin=.list-o) $$< > $$(@:.bin=.list-err) 2>&1
ifeq ($(wildcard $1.no-ld65),)
$(LD65) -t none -o $$(@:.bin=.list-bin) $$(@:.bin=.list-o) none.lib > $$(@:.bin=.ld65-err) 2>&1
endif
else
$(CA65) -t none -l $$(@:.bin=.list.orig) -o $$(@:.bin=.list-o) $$< > $$(@:.bin=.list-err) 2>&1 || true
$(CA65) -t none -l $$(@:.bin=.lst) -o $$(@:.bin=.list-o) $$< > $$(@:.bin=.list-err) 2>&1 || true
ifeq ($(wildcard $1.no-ld65),)
$(LD65) -t none -o $$(@:.bin=.list-bin) $$(@:.bin=.list-o) none.lib > $$(@:.bin=.ld65-err) 2>&1 || true
endif
@ -89,14 +86,14 @@ endif
ifneq ($(wildcard $1.err-ref),)
$(ISEQUAL) $1.err-ref $$(@:.bin=.list-err)
else
$(ISEQUAL) $(WORKDIR)/_empty $$(@:.bin=.list-err)
$(ISEQUAL) --empty $$(@:.bin=.list-err)
endif
ifneq ($(wildcard $(WORKDIR)/$1.ld65-err),)
ifneq ($(wildcard $1.ld65err-ref),)
$(ISEQUAL) $1.ld65err-ref $$(@:.bin=.ld65-err)
else
$(ISEQUAL) $(WORKDIR)/_empty $$(@:.bin=.ld65-err)
$(ISEQUAL) --empty $$(@:.bin=.ld65-err)
endif
endif
@ -109,8 +106,7 @@ ifneq ($(wildcard $1.list-ref),)
# we have a reference file, compare that, too
# remove first line which contains a version number
tail -n +2 $$(@:.bin=.list.orig) > $$(@:.bin=.lst)
$(ISEQUAL) $1.list-ref $$(@:.bin=.lst)
$(ISEQUAL) --skipright=1 $1.list-ref $$(@:.bin=.lst)
endif
endef # LISTING_template

View File

@ -3,6 +3,97 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static int binary = 0;
static int empty = 0;
static int skiplines_left = 0;
static int skiplines_right = 0;
static char * filename_left = 0;
static char * filename_right = 0;
int handleargparameter(int offset, char * parameter)
{
long number = -1;
char * endptr = NULL;
if (parameter[offset++] != '=') {
return -1;
}
number = strtol(parameter + offset, &endptr, 10);
if (endptr == NULL || *endptr != 0) {
return -1;
}
return number;
}
int handleparameter(int argc, char *argv[])
{
static const char opt_binary[] = "--binary";
static const char opt_empty[] = "--empty";
static const char opt_skipleft[] = "--skipleft";
static const char opt_skipright[] = "--skipright";
static const char len_skipleft = sizeof opt_skipleft - 1;
static const char len_skipright = sizeof opt_skipright - 1;
int argindex = 1;
if (argc < 2) {
return -1;
}
while (argindex < argc && argv[argindex] && argv[argindex][0] == '-') {
if (strcmp(argv[argindex], opt_binary) == 0) {
if (empty || skiplines_left || skiplines_right) {
fprintf(stderr, "--empty cannot go with other options.\n");
exit(1);
}
binary = 1;
}
else if (strcmp(argv[argindex], opt_empty) == 0) {
if (binary || skiplines_left || skiplines_right) {
fprintf(stderr, "--binary cannot go with other options.\n");
exit(1);
}
empty = 1;
}
else if (strncmp(argv[argindex], opt_skipleft, len_skipleft) == 0) {
if (binary || empty) {
fprintf(stderr, "%s cannot go with other options.\n", opt_skipleft);
exit(1);
}
skiplines_left = handleargparameter(len_skipleft, argv[argindex]);
if (skiplines_left < 0) {
fprintf(stderr, "%s: you must specify the number of lines\n", opt_skipleft);
}
}
else if (strncmp(argv[argindex], opt_skipright, len_skipright) == 0) {
if (binary || empty) {
fprintf(stderr, "%s cannot go with other options.\n", opt_skipright);
exit(1);
}
skiplines_right = handleargparameter(len_skipright, argv[argindex]);
if (skiplines_right < 0) {
fprintf(stderr, "%s: you must specify the number of lines\n", opt_skipright);
}
}
++argindex;
}
if (argc + empty - argindex != 2) {
fprintf(stderr, "filenames are missing!\n");
return -1;
}
filename_left = argv[argindex++];
filename_right = argv[argindex++];
return 0;
}
/* get the next character from FILE and convert commonly used line-endings all
into the same value (0x0a, as used on *nix systems)
@ -17,7 +108,7 @@
int getnext(FILE *f)
{
int c = fgetc(f);
if (c == 0x0d) {
if (!binary && c == 0x0d) {
if (!feof(f)) {
int n = fgetc(f);
if (n != 0x0a) {
@ -30,25 +121,65 @@ int getnext(FILE *f)
return c;
}
void skiplines(FILE *f, int skipcount)
{
int c;
while (skipcount > 0) {
c = getnext(f);
if (feof(f)) {
return;
}
if (c == 0x0a) {
--skipcount;
}
}
}
int main(int argc, char *argv[])
{
FILE *f1, *f2;
if (argc < 3) {
if (handleparameter(argc, argv) < 0) {
return EXIT_FAILURE;
}
f1 = fopen(argv[1], "rb");
f2 = fopen(argv[2], "rb");
if ((f1 == NULL) || (f2 == NULL)) {
f1 = fopen(filename_left, "rb");
if (f1 == NULL) {
return EXIT_FAILURE;
}
for(;;) {
if (feof(f1) && feof(f2)) {
return EXIT_SUCCESS;
} else if (feof(f1) || feof(f2)) {
if (empty) {
fseek(f1, 0, SEEK_END);
if (ftell(f1) != 0) {
return EXIT_FAILURE;
}
if (getnext(f1) != getnext(f2)) {
else {
return EXIT_SUCCESS;
}
}
else {
if (skiplines_left) {
skiplines(f1, skiplines_left);
}
f2 = fopen(filename_right, "rb");
if (f2 == NULL) {
return EXIT_FAILURE;
}
if (skiplines_right) {
skiplines(f2, skiplines_right);
}
for(;;) {
if (feof(f1) && feof(f2)) {
return EXIT_SUCCESS;
} else if (feof(f1) || feof(f2)) {
return EXIT_FAILURE;
}
if (getnext(f1) != getnext(f2)) {
return EXIT_FAILURE;
}
}
}
}