mirror of
https://github.com/sheumann/hush.git
synced 2024-09-12 00:55:11 +00:00
comm: almost total rewrite; add testsuite
function old new delta writeline 128 125 -3 comm_main 540 239 -301 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-304) Total: -304 bytes
This commit is contained in:
parent
51f1b6c0e0
commit
bb13079c8e
@ -14,8 +14,9 @@
|
|||||||
#define COMM_OPT_3 (1 << 2)
|
#define COMM_OPT_3 (1 << 2)
|
||||||
|
|
||||||
/* writeline outputs the input given, appropriately aligned according to class */
|
/* writeline outputs the input given, appropriately aligned according to class */
|
||||||
static void writeline(char *line, int class, int flags)
|
static void writeline(char *line, int class)
|
||||||
{
|
{
|
||||||
|
int flags = option_mask32;
|
||||||
if (class == 0) {
|
if (class == 0) {
|
||||||
if (flags & COMM_OPT_1)
|
if (flags & COMM_OPT_1)
|
||||||
return;
|
return;
|
||||||
@ -32,81 +33,67 @@ static void writeline(char *line, int class, int flags)
|
|||||||
if (!(flags & COMM_OPT_2))
|
if (!(flags & COMM_OPT_2))
|
||||||
putchar('\t');
|
putchar('\t');
|
||||||
}
|
}
|
||||||
fputs(line, stdout);
|
puts(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||||
int comm_main(int argc UNUSED_PARAM, char **argv)
|
int comm_main(int argc UNUSED_PARAM, char **argv)
|
||||||
{
|
{
|
||||||
#define LINE_LEN 100
|
char *thisline[2];
|
||||||
#define BB_EOF_0 0x1
|
FILE *stream[2];
|
||||||
#define BB_EOF_1 0x2
|
|
||||||
char thisline[2][LINE_LEN];
|
|
||||||
FILE *streams[2];
|
|
||||||
int i;
|
int i;
|
||||||
unsigned flags;
|
int order;
|
||||||
|
|
||||||
opt_complementary = "=2";
|
opt_complementary = "=2";
|
||||||
flags = getopt32(argv, "123");
|
getopt32(argv, "123");
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
for (i = 0; i < 2; ++i) {
|
for (i = 0; i < 2; ++i) {
|
||||||
streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r");
|
stream[i] = xfopen_stdin(argv[i]);
|
||||||
fgets(thisline[i], LINE_LEN, streams[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This is the real core of the program - lines are compared here */
|
order = 0;
|
||||||
|
thisline[1] = thisline[0] = NULL;
|
||||||
while (*thisline[0] || *thisline[1]) {
|
while (1) {
|
||||||
int order = 0;
|
if (order <= 0) {
|
||||||
|
free(thisline[0]);
|
||||||
i = 0;
|
thisline[0] = xmalloc_fgetline(stream[0]);
|
||||||
if (feof(streams[0])) i |= BB_EOF_0;
|
}
|
||||||
if (feof(streams[1])) i |= BB_EOF_1;
|
if (order >= 0) {
|
||||||
|
free(thisline[1]);
|
||||||
if (!*thisline[0])
|
thisline[1] = xmalloc_fgetline(stream[1]);
|
||||||
order = 1;
|
|
||||||
else if (!*thisline[1])
|
|
||||||
order = -1;
|
|
||||||
else {
|
|
||||||
int tl0_len, tl1_len;
|
|
||||||
tl0_len = strlen(thisline[0]);
|
|
||||||
tl1_len = strlen(thisline[1]);
|
|
||||||
order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
|
|
||||||
if (!order)
|
|
||||||
order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (order == 0 && !i)
|
i = !thisline[0] + (!thisline[1] << 1);
|
||||||
writeline(thisline[1], 2, flags);
|
if (i)
|
||||||
else if (order > 0 && !(i & BB_EOF_1))
|
|
||||||
writeline(thisline[1], 1, flags);
|
|
||||||
else if (order < 0 && !(i & BB_EOF_0))
|
|
||||||
writeline(thisline[0], 0, flags);
|
|
||||||
|
|
||||||
if (i & BB_EOF_0 & BB_EOF_1) {
|
|
||||||
break;
|
break;
|
||||||
|
order = strcmp(thisline[0], thisline[1]);
|
||||||
|
|
||||||
} else if (i) {
|
|
||||||
i = (i & BB_EOF_0 ? 1 : 0);
|
|
||||||
while (!feof(streams[i])) {
|
|
||||||
if ((order < 0 && i) || (order > 0 && !i))
|
|
||||||
writeline(thisline[i], i, flags);
|
|
||||||
fgets(thisline[i], LINE_LEN, streams[i]);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if (order >= 0)
|
if (order >= 0)
|
||||||
fgets(thisline[1], LINE_LEN, streams[1]);
|
writeline(thisline[1], order ? 1 : 2);
|
||||||
if (order <= 0)
|
else
|
||||||
fgets(thisline[0], LINE_LEN, streams[0]);
|
writeline(thisline[0], 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* EOF at least on one of the streams */
|
||||||
|
i &= 1;
|
||||||
|
if (thisline[i]) {
|
||||||
|
/* stream[i] is not at EOF yet */
|
||||||
|
/* we did not print thisline[i] yet */
|
||||||
|
char *p = thisline[i];
|
||||||
|
writeline(p, i);
|
||||||
|
while (1) {
|
||||||
|
free(p);
|
||||||
|
p = xmalloc_fgetline(stream[i]);
|
||||||
|
if (!p)
|
||||||
|
break;
|
||||||
|
writeline(p, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ENABLE_FEATURE_CLEAN_UP) {
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
||||||
fclose(streams[0]);
|
fclose(stream[0]);
|
||||||
fclose(streams[1]);
|
fclose(stream[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# awk tests.
|
|
||||||
# Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com>
|
# Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com>
|
||||||
# Licensed under GPL v2, see file LICENSE for details.
|
# Licensed under GPL v2, see file LICENSE for details.
|
||||||
|
|
||||||
. testing.sh
|
. testing.sh
|
||||||
|
|
||||||
# testing "description" "arguments" "result" "infile" "stdin"
|
# testing "description" "command" "result" "infile" "stdin"
|
||||||
|
|
||||||
testing "awk -F case 0" "awk -F '[#]' '{ print NF }'" "" "" ""
|
testing "awk -F case 0" "awk -F '[#]' '{ print NF }'" "" "" ""
|
||||||
testing "awk -F case 1" "awk -F '[#]' '{ print NF }'" "0\n" "" "\n"
|
testing "awk -F case 1" "awk -F '[#]' '{ print NF }'" "0\n" "" "\n"
|
||||||
|
19
testsuite/comm.tests
Executable file
19
testsuite/comm.tests
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Copyright 2008 by Denys Vlasenko <vda.linux@googlemail.com>
|
||||||
|
# Licensed under GPL v2, see file LICENSE for details.
|
||||||
|
|
||||||
|
. testing.sh
|
||||||
|
|
||||||
|
# testing "description" "command" "result" "infile" "stdin"
|
||||||
|
|
||||||
|
testing "comm test 1" "comm input -" "\t123\n""456\n""abc\n""\tdef\n" "456\nabc\n" "123\ndef\n"
|
||||||
|
testing "comm test 2" "comm - input" "123\n""\t456\n""\tabc\n""def\n" "456\nabc\n" "123\ndef\n"
|
||||||
|
testing "comm test 3" "comm input -" "abc\n""\tdef\n""xyz\n" "abc\nxyz\n" "def\n"
|
||||||
|
testing "comm test 4" "comm - input" "\tabc\n""def\n""\txyz\n" "abc\nxyz\n" "def\n"
|
||||||
|
testing "comm test 5" "comm input -" "123\n""abc\n""\tdef\n" "123\nabc\n" "def\n"
|
||||||
|
testing "comm test 6" "comm - input" "\t123\n""\tabc\n""def\n" "123\nabc\n" "def\n"
|
||||||
|
testing "comm unterminated line 1" "comm input -" "abc\n""\tdef\n" "abc" "def"
|
||||||
|
testing "comm unterminated line 2" "comm - input" "\tabc\n""def\n" "abc" "def"
|
||||||
|
|
||||||
|
exit $FAILCOUNT
|
Loading…
Reference in New Issue
Block a user