Browse Source

Refactor code.

primary
CismonX 3 weeks ago
parent
commit
76d0f779d8
Signed by: CismonX GPG Key ID: 3094873E29A482FB
  1. 2
      configure.ac
  2. 2
      doc/Makefile.am
  3. 7
      examples/sixdraw.c
  4. 18
      src/ctlseqs.c
  5. 2
      tests/Makefile.am
  6. 40
      tests/tcsgrep.c

2
configure.ac

@ -12,7 +12,7 @@ AC_INIT([ctlseqs], [0.1.0], [bug-report@cismon.net])
AC_CONFIG_SRCDIR([src/ctlseqs.c])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([foreign info-in-builddir])
AM_INIT_AUTOMAKE([foreign])
AM_EXTRA_RECURSIVE_TARGETS([install-man uninstall-man])
LT_PREREQ([2.4.2])
LT_INIT

2
doc/Makefile.am

@ -6,5 +6,7 @@
# this notice are preserved. This file is offered as-is, without any warranty.
#
AUTOMAKE_OPTIONS = info-in-builddir
info_TEXINFOS = ctlseqs.texi
ctlseqs_TEXINFOS = fdl.texi

7
examples/sixdraw.c

@ -179,10 +179,11 @@ decrqm(
print_error(ctx, "failed to get %s status", name);
return false;
}
int mode_value = result[1].num;
if ( result[0].num != mode
|| (result[1].num != DECRQM_SET && result[1].num != DECRQM_RST)
|| (mode_value != DECRQM_SET && mode_value != DECRQM_RST)
) {
print_error(ctx, "%s status not recognizable", name);
print_error(ctx, "%s status (%d) not recognizable", name, mode_value);
return false;
}
return true;
@ -313,7 +314,7 @@ init(
.fd = ctx->in_fd,
.maxlen = 4096,
};
if (ctlseqs_reader_config(ctx->reader, &reader_options) != CTLSEQS_OK) {
if (CTLSEQS_OK != ctlseqs_reader_config(ctx->reader, &reader_options)) {
print_error(ctx, "failed to set reader options");
return false;
}

18
src/ctlseqs.c

@ -604,12 +604,13 @@ ctlseqs_cold void
ctlseqs_matcher_free(
struct ctlseqs_matcher *matcher
) {
if (ctlseqs_likely(matcher != NULL)) {
for (size_t idx = 1; idx <= matcher->pool_idx; ++idx) {
free(matcher->node_pools[idx]);
}
free(matcher);
if (ctlseqs_likely(matcher == NULL)) {
return;
}
for (size_t idx = 1; idx <= matcher->pool_idx; ++idx) {
free(matcher->node_pools[idx]);
}
free(matcher);
}
struct ctlseqs_reader *
@ -701,8 +702,9 @@ ctlseqs_cold void
ctlseqs_reader_free(
struct ctlseqs_reader *reader
) {
if (ctlseqs_likely(reader != NULL)) {
free(reader->rbuf);
free(reader);
if (ctlseqs_unlikely(reader == NULL)) {
return;
}
free(reader->rbuf);
free(reader);
}

2
tests/Makefile.am

@ -14,5 +14,5 @@ tcsgrep_CPPFLAGS = -I$(top_srcdir)/include
tcsgrep_SOURCES = tcsgrep.c
tcsgrep_LDADD = $(top_builddir)/src/libctlseqs.la
RUNTESTDEFAULTFLAGS = TCSGREP_BIN=$(builddir)/tcsgrep
RUNTESTFLAGS = TCSGREP_BIN=$(builddir)/tcsgrep
EXTRA_DEJAGNU_SITE_CONFIG = $(srcdir)/init.exp

40
tests/tcsgrep.c

@ -107,11 +107,11 @@ print_char(
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"
};
if (ch == ' ') {
fprintf(ctx->out_file, " SP");
fwrite(" SP", 3, 1, ctx->out_file);
} else if (isprint(ch)) {
fprintf(ctx->out_file, " %c", ch);
} else if (ch == 0x7f) {
fprintf(ctx->out_file, " DEL");
fwrite(" DEL", 4, 1, ctx->out_file);
} else if (!iscntrl(ch)) {
fprintf(ctx->out_file, " \\x%02x", (unsigned char)ch);
} else {
@ -133,7 +133,7 @@ print_generic_seq(
print_char(ctx, (unsigned)seq[idx]);
}
if (newline) {
fprintf(ctx->out_file, "\n");
fwrite("\n", 1, 1, ctx->out_file);
}
}
@ -148,24 +148,24 @@ print_matching_seq(
print_generic_seq(ctx, "OK", result, false);
result += 2;
} else {
fprintf(ctx->out_file, "OK");
fwrite("OK", 3, 1, ctx->out_file);
}
fprintf(ctx->out_file, " %s", seq->name);
for (int idx = 0; idx < 8; ++idx) {
char placeholder = seq->args[idx];
switch (placeholder) {
case 0x0e: // CTLSEQS_PH_NUM
fprintf(ctx->out_file, " %lu", result[idx].num);
break;
case 0x10: // CTLSEQS_PH_STR
fprintf(ctx->out_file, " %.*s",
(int)result[idx].len, result[idx + 1].str);
break;
case 0x0f: // CTLSEQS_PH_NUMS
for (size_t i = 1; i <= result[idx].len; ++i) {
fprintf(ctx->out_file, " %lu", result[idx + i].num);
}
break;
case 0x0e: // CTLSEQS_PH_NUM
fprintf(ctx->out_file, " %lu", result[idx].num);
break;
case 0x10: // CTLSEQS_PH_STR
fprintf(ctx->out_file, " %.*s",
(int)result[idx].len, result[idx + 1].str);
break;
case 0x0f: // CTLSEQS_PH_NUMS
for (size_t i = 1; i <= result[idx].len; ++i) {
fprintf(ctx->out_file, " %lu", result[idx + i].num);
}
break;
}
}
fprintf(ctx->out_file, "\n");
@ -219,7 +219,7 @@ main(
print_error(&ctx, "failed to get file status flags");
return 1;
}
if (fcntl(ctx.in_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
if (-1 == fcntl(ctx.in_fd, F_SETFL, flags | O_NONBLOCK)) {
print_error(&ctx, "failed to set file status flags");
return 1;
}
@ -400,7 +400,7 @@ main(
.patterns = patterns,
.npatterns = npatterns,
};
if (ctlseqs_matcher_config(matcher, &matcher_options) != CTLSEQS_OK) {
if (CTLSEQS_OK != ctlseqs_matcher_config(matcher, &matcher_options)) {
print_error(&ctx, "matcher setopt failed");
return 1;
}
@ -416,13 +416,13 @@ main(
.result = result,
.flags = ctx.verbose ? CTLSEQS_READER_SAVE_MATCHED_SEQS : 0,
};
if (ctlseqs_reader_config(reader, &reader_options) != CTLSEQS_OK) {
if (CTLSEQS_OK != ctlseqs_reader_config(reader, &reader_options)) {
print_error(&ctx, "reader setopt failed");
return 1;
}
struct termios old_termios;
if (tcgetattr(ctx.in_fd, &old_termios) != 0) {
if (0 != tcgetattr(ctx.in_fd, &old_termios)) {
ctx.not_tty = true;
} else {
struct termios new_termios = old_termios;

Loading…
Cancel
Save