From 76d0f779d8af95c576237e2e3e6c1232f88a126c Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 12 Nov 2021 18:16:19 +0800 Subject: [PATCH] Refactor code. --- configure.ac | 2 +- doc/Makefile.am | 2 ++ examples/sixdraw.c | 7 ++++--- src/ctlseqs.c | 18 ++++++++++-------- tests/Makefile.am | 2 +- tests/tcsgrep.c | 40 ++++++++++++++++++++-------------------- 6 files changed, 38 insertions(+), 33 deletions(-) diff --git a/configure.ac b/configure.ac index 8f36580..18bee12 100644 --- a/configure.ac +++ b/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 diff --git a/doc/Makefile.am b/doc/Makefile.am index 292e5b3..8c6dd61 100644 --- a/doc/Makefile.am +++ b/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 diff --git a/examples/sixdraw.c b/examples/sixdraw.c index 4edf333..064ccd9 100644 --- a/examples/sixdraw.c +++ b/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; } diff --git a/src/ctlseqs.c b/src/ctlseqs.c index 88289b0..aa6c042 100644 --- a/src/ctlseqs.c +++ b/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); } diff --git a/tests/Makefile.am b/tests/Makefile.am index f01b404..54ed9b9 100644 --- a/tests/Makefile.am +++ b/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 diff --git a/tests/tcsgrep.c b/tests/tcsgrep.c index 75f6c99..16bc00d 100644 --- a/tests/tcsgrep.c +++ b/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;