From 4aa36e6e9171b191e0c4316e2f22d98c3f174763 Mon Sep 17 00:00:00 2001 From: yabu76 Date: Thu, 14 Aug 2025 13:50:58 +0900 Subject: [PATCH] Adjust code style to comply with clang-format rules --- .clang-format | 4 + src/main.c | 5 +- src/misc.c | 2 + src/options.c | 12 +- src/readline.c | 2 +- src/script.c | 17 ++- src/tty.c | 36 +++-- src/xymodem.c | 362 +++++++++++++++++++++++++++++++------------------ src/xymodem.h | 4 +- 9 files changed, 285 insertions(+), 159 deletions(-) diff --git a/.clang-format b/.clang-format index 682ae09..d896a25 100644 --- a/.clang-format +++ b/.clang-format @@ -3,3 +3,7 @@ IndentWidth: 4 AllowShortFunctionsOnASingleLine: None KeepEmptyLinesAtTheStartOfBlocks: false BreakBeforeBraces: Allman +IndentCaseLabels: true +ColumnLimit: 144 +SortIncludes: false +SkipMacroDefinitionBody: true diff --git a/src/main.c b/src/main.c index 6676bb3..8434caa 100644 --- a/src/main.c +++ b/src/main.c @@ -68,7 +68,7 @@ int main(int argc, char *argv[]) /* Configure input terminal */ if (isatty(fileno(stdin))) { - stdin_configure(); + stdin_configure(); } else { @@ -110,7 +110,8 @@ int main(int argc, char *argv[]) if (interactive_mode) { tio_printf("Press ctrl-%c q to quit", option.prefix_key); - } else + } + else { tio_printf("Non-interactive mode enabled"); tio_printf("Press ctrl-c to quit"); diff --git a/src/misc.c b/src/misc.c index bd0a429..13d601e 100644 --- a/src/misc.c +++ b/src/misc.c @@ -170,6 +170,7 @@ bool match_patterns(const char *string, const char *patterns) pattern = strtok(patterns_copy, ","); while (pattern != NULL) { + // clang-format off // Check if the string matches the current pattern #ifdef FNM_EXTMATCH if (fnmatch(pattern, string, FNM_EXTMATCH) == 0) @@ -180,6 +181,7 @@ bool match_patterns(const char *string, const char *patterns) free(patterns_copy); return true; } + // clang-format on // Move to the next pattern pattern = strtok(NULL, ","); diff --git a/src/options.c b/src/options.c index acff7ce..beddd90 100644 --- a/src/options.c +++ b/src/options.c @@ -61,6 +61,7 @@ enum opt_t OPT_EXEC, }; +// clang-format off /* Default options */ struct option_t option = { @@ -125,6 +126,7 @@ struct option_t option = .map_i_msb2lsb = false, .map_o_ign_cr = false, }; +// clang-format on void option_print_help(char *argv[]) { @@ -833,12 +835,14 @@ void options_print() tio_printf(" Output line delay: %d", option.output_line_delay); tio_printf(" Automatic connect strategy: %s", option_auto_connect_state_to_string(option.auto_connect)); tio_printf(" Automatic reconnect: %s", option.no_reconnect ? "true" : "false"); + // clang-format off tio_printf(" Pulse duration: DTR=%d RTS=%d CTS=%d DSR=%d DCD=%d RI=%d", option.dtr_pulse_duration, option.rts_pulse_duration, option.cts_pulse_duration, option.dsr_pulse_duration, option.dcd_pulse_duration, option.ri_pulse_duration); + // clang-format on tio_printf(" Input mode: %s", option_input_mode_to_string(option.input_mode)); tio_printf(" Output mode: %s", option_output_mode_to_string(option.output_mode)); tio_printf(" Alert: %s", option_alert_state_to_string(option.alert)); @@ -887,8 +891,9 @@ void options_parse(int argc, char *argv[]) option.vt100 = true; } - while (1) + while (true) { + // clang-format off static struct option long_options[] = { {"baudrate", required_argument, 0, 'b' }, @@ -932,6 +937,7 @@ void options_parse(int argc, char *argv[]) {"complete-profiles", no_argument, 0, OPT_COMPLETE_PROFILES }, {0, 0, 0, 0 } }; + // clang-format on /* getopt_long stores the option index here */ int option_index = 0; @@ -1131,7 +1137,7 @@ void options_parse(int argc, char *argv[]) /* Assume first non-option is the target (tty device, profile, tid) */ if (strcmp(option.target, "")) { - optind++; + optind++; } else if (optind < argc) { @@ -1176,6 +1182,7 @@ void options_parse_final(int argc, char *argv[]) #ifdef __CYGWIN__ unsigned char portnum; char *tty_win; + // clang-format off if ( ((strncmp("COM", option.target, 3) == 0) || (strncmp("com", option.target, 3) == 0) ) && (sscanf(option.target + 3, "%hhu", &portnum) == 1) @@ -1184,5 +1191,6 @@ void options_parse_final(int argc, char *argv[]) asprintf(&tty_win, "/dev/ttyS%hhu", portnum - 1); option.target = tty_win; } + // clang-format on #endif } diff --git a/src/readline.c b/src/readline.c index 8176a0b..6cfae00 100644 --- a/src/readline.c +++ b/src/readline.c @@ -90,7 +90,7 @@ static void readline_input_cr(void) rl_history_count++; } else - { + { free(rl_history[0]); memmove(&rl_history[0], &rl_history[1], (RL_HISTORY_MAX - 1) * sizeof(char*)); rl_history[RL_HISTORY_MAX - 1] = strndup(rl_line, rl_line_length); diff --git a/src/script.c b/src/script.c index b69d55b..e050af6 100644 --- a/src/script.c +++ b/src/script.c @@ -45,6 +45,7 @@ static int device_fd; +// clang-format off static char script_init[] = "tio.set = function(arg)\n" " local dtr = arg.DTR or -1\n" @@ -71,6 +72,7 @@ static char script_init[] = "end\n" "tio.alwaysecho = true\n" "setmetatable(tio, tio)\n"; +// clang-format on static bool alwaysecho(lua_State *L) { @@ -100,7 +102,9 @@ static int api_echo(lua_State *L) log_printf("\n[%s] %s", pTimeStampNow, str); } } - } else { + } + else + { for (size_t i=0; i= '0' && c <= '9') + if (c >= '0' && c <= '9') { return c - '0'; } @@ -331,7 +333,7 @@ void *tty_stdin_input_thread(void *arg) pthread_mutex_unlock(&mutex_input_ready); // Input loop for stdin - while (1) + while (true) { /* Input from stdin ready */ byte_count = read(STDIN_FILENO, input_buffer, BUFSIZ); @@ -360,7 +362,8 @@ void *tty_stdin_input_thread(void *arg) for (int i = 0; i 0; size--) { + for (crc = 0; size > 0; size--) + { s = *data++ ^ (crc >> 8); s ^= (s >> 4); crc = (crc << 8) ^ s ^ (s << 5) ^ (s << 12); @@ -81,16 +84,19 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq) /* Drain pending characters from serial line. Insist on the * last drained character being 'C'. */ - while(1) { + while (true) + { if (key_hit) return -1; rc = read_poll(sio, &resp, 1, 50); - if (rc == 0) { + if (rc == 0) + { if (resp == 'C') break; if (resp == CAN) return ERR; continue; } - else if (rc < 0) { + else if (rc < 0) + { tio_error_print("Read sync from serial failed"); return ERR; } @@ -100,7 +106,8 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq) packet.seq = seq; packet.type = STX; - while (len) { + while (len) + { size_t sz, z = 0; char *from, status; @@ -116,11 +123,14 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq) /* Send packet */ from = (char *) &packet; sz = sizeof(packet); - while (sz) { + while (sz) + { if (key_hit) return ERR; - if ((rc = write(sio, from, sz)) < 0 ) { - if (errno == EWOULDBLOCK) { + if ((rc = write(sio, from, sz)) < 0 ) + { + if (errno == EWOULDBLOCK) + { usleep(1000); continue; } @@ -138,30 +148,36 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq) if (seq == 0 && packet.data[0] == 0) resp = ACK; /* Read receiver response, timeout 1 s */ - for(int n=0; n < 20; n++) { + for (int n = 0; n < 20; n++) + { if (key_hit) return ERR; rc = read_poll(sio, &resp, 1, 50); - if (rc < 0) { + if (rc < 0) + { tio_error_print("Read ack/nak from serial failed"); return ERR; - } else if(rc > 0) { + } + else if (rc > 0) + { break; } } /* Update "progress bar" */ - switch (resp) { - case NAK: status = 'N'; break; - case ACK: status = '.'; break; - case 'C': status = 'C'; break; - case CAN: status = '!'; return ERR; - default: status = '?'; + switch (resp) + { + case NAK: status = 'N'; break; + case ACK: status = '.'; break; + case 'C': status = 'C'; break; + case CAN: status = '!'; return ERR; + default: status = '?'; } write(STDOUT_FILENO, &status, 1); /* Move to next block after ACK */ - if (resp == ACK) { + if (resp == ACK) + { packet.seq++; len -= z; buf += z; @@ -169,23 +185,29 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq) } /* Send EOT at 1 Hz until ACK or CAN received */ - while (seq) { + while (seq) + { if (key_hit) return ERR; - if (write(sio, EOT_STR, 1) < 0) { + if (write(sio, EOT_STR, 1) < 0) + { tio_error_print("Write EOT to serial failed"); return ERR; } write(STDOUT_FILENO, "|", 1); /* 1s timeout */ rc = read_poll(sio, &resp, 1, 1000); - if (rc < 0) { + if (rc < 0) + { tio_error_print("Read from serial failed"); return ERR; - } else if(rc == 0) { + } + else if (rc == 0) + { continue; } - if (resp == ACK || resp == CAN) { + if (resp == ACK || resp == CAN) + { write(STDOUT_FILENO, "\r\n", 2); return (resp == ACK) ? OK : ERR; } @@ -203,16 +225,19 @@ static int xmodem(int sio, const void *data, size_t len) /* Drain pending characters from serial line. Insist on the * last drained character being 'C'. */ - while(1) { + while (true) + { if (key_hit) return -1; rc = read_poll(sio, &resp, 1, 50); - if (rc == 0) { + if (rc == 0) + { if (resp == 'C') break; if (resp == CAN) return ERR; continue; } - else if (rc < 0) { + else if (rc < 0) + { tio_error_print("Read sync from serial failed"); return ERR; } @@ -222,7 +247,8 @@ static int xmodem(int sio, const void *data, size_t len) packet.seq = 1; packet.type = SOH; - while (len) { + while (len) + { size_t sz, z = 0; char *from, status; @@ -238,11 +264,14 @@ static int xmodem(int sio, const void *data, size_t len) /* Send packet */ from = (char *) &packet; sz = sizeof(packet); - while (sz) { + while (sz) + { if (key_hit) return ERR; - if ((rc = write(sio, from, sz)) < 0 ) { - if (errno == EWOULDBLOCK) { + if ((rc = write(sio, from, sz)) < 0 ) + { + if (errno == EWOULDBLOCK) + { usleep(1000); continue; } @@ -257,30 +286,36 @@ static int xmodem(int sio, const void *data, size_t len) resp = 0; /* Read receiver response, timeout 1 s */ - for(int n=0; n < 20; n++) { + for (int n = 0; n < 20; n++) + { if (key_hit) return ERR; rc = read_poll(sio, &resp, 1, 50); - if (rc < 0) { + if (rc < 0) + { tio_error_print("Read ack/nak from serial failed"); return ERR; - } else if(rc > 0) { + } + else if (rc > 0) + { break; } } /* Update "progress bar" */ - switch (resp) { - case NAK: status = 'N'; break; - case ACK: status = '.'; break; - case 'C': status = 'C'; break; - case CAN: status = '!'; return ERR; - default: status = '?'; + switch (resp) + { + case NAK: status = 'N'; break; + case ACK: status = '.'; break; + case 'C': status = 'C'; break; + case CAN: status = '!'; return ERR; + default: status = '?'; } write(STDOUT_FILENO, &status, 1); /* Move to next block after ACK */ - if (resp == ACK) { + if (resp == ACK) + { packet.seq++; len -= z; buf += z; @@ -288,23 +323,29 @@ static int xmodem(int sio, const void *data, size_t len) } /* Send EOT at 1 Hz until ACK or CAN received */ - while (1) { + while (true) + { if (key_hit) return ERR; - if (write(sio, EOT_STR, 1) < 0) { + if (write(sio, EOT_STR, 1) < 0) + { tio_error_print("Write EOT to serial failed"); return ERR; } write(STDOUT_FILENO, "|", 1); /* 1s timeout */ rc = read_poll(sio, &resp, 1, 1000); - if (rc < 0) { + if (rc < 0) + { tio_error_print("Read from serial failed"); return ERR; - } else if(rc == 0) { + } + else if (rc == 0) + { continue; } - if (resp == ACK || resp == CAN) { + if (resp == ACK || resp == CAN) + { write(STDOUT_FILENO, "\r\n", 2); return (resp == ACK) ? OK : ERR; } @@ -325,8 +366,10 @@ int start_receive(int sio) seconds. If nothing is received in that time then return false to indicate that the transfer did not start. */ rc = write(sio, "C", 1); - if (rc < 0) { - if (errno == EWOULDBLOCK) { + if (rc < 0) + { + if (errno == EWOULDBLOCK) + { usleep(1000); continue; } @@ -353,7 +396,7 @@ int start_receive(int sio) return rc; } -uint16_t update_CRC(uint16_t crc, char data_char) +uint16_t update_CRC(uint16_t crc, char data_char) { uint8_t data = data_char; crc = crc ^ ((uint16_t)data << 8); @@ -385,19 +428,25 @@ int receive_packet(int sio, struct xpacket packet, int fd) /* Read seq bytes*/ rc = read_poll(sio, &rxSeq1, 1, 3000); - if (rc == 0) { + if (rc == 0) + { tio_error_print("Timeout waiting for first seq byte"); return ERR; - } else if (rc < 0) { - tio_error_print("Error reading first seq byte") + } + else if (rc < 0) + { + tio_error_print("Error reading first seq byte"); return ERR_FATAL; } rc = read_poll(sio, &rxSeq2, 1, 3000); - if (rc == 0) { + if (rc == 0) + { tio_error_print("Timeout waiting for second seq byte"); return ERR; - } else if (rc < 0) { - tio_error_print("Error reading second seq byte") + } + else if (rc < 0) + { + tio_error_print("Error reading second seq byte"); return ERR_FATAL; } if (key_hit) @@ -407,20 +456,24 @@ int receive_packet(int sio, struct xpacket packet, int fd) for (unsigned ix = 0; (ix < sizeof(packet.data)); ix++) { rc = read_poll(sio, &resp, 1, 3000); - /* If the read times out or fails then fail this packet. */ + /* If the read times out or fails then fail this packet. */ if (rc == 0) { tio_error_print("Timeout waiting for next packet char"); rc = write(sio, CAN_STR, 1); - if (rc < 0) { + if (rc < 0) + { tio_error_print("Write cancel packet to serial failed"); return ERR_FATAL; } return ERR; - } else if (rc < 0) { - tio_error_print("Error reading next packet char") + } + else if (rc < 0) + { + tio_error_print("Error reading next packet char"); rc = write(sio, CAN_STR, 1); - if (rc < 0) { + if (rc < 0) + { tio_error_print("Write cancel packet to serial failed"); } return ERR_FATAL; @@ -433,11 +486,14 @@ int receive_packet(int sio, struct xpacket packet, int fd) /* Read CRC */ rc = read_poll(sio, &resp, 1, 3000); - if (rc == 0) { + if (rc == 0) + { tio_error_print("Timeout waiting for first CRC byte"); return ERR; - } else if (rc < 0) { - tio_error_print("Error reading first CRC byte") + } + else if (rc < 0) + { + tio_error_print("Error reading first CRC byte"); return ERR_FATAL; } @@ -446,20 +502,23 @@ int receive_packet(int sio, struct xpacket packet, int fd) rxCrc = uresp16 << 8; rc = read_poll(sio, &resp, 1, 3000); - if (rc == 0) { + if (rc == 0) + { tio_error_print("Timeout waiting for second CRC byte"); return ERR; - } else if (rc < 0) { - tio_error_print("Error reading second CRC byte") + } + else if (rc < 0) + { + tio_error_print("Error reading second CRC byte"); return ERR_FATAL; } - + uresp = resp; uresp16 = uresp; rxCrc |= uresp16; if (key_hit) - return USER_CAN; + return USER_CAN; /* At this point in the code, there should not be anything in the receive buffer because the sender has just sent a complete packet and is waiting on a response. */ @@ -469,7 +528,8 @@ int receive_packet(int sio, struct xpacket packet, int fd) tio_error_print("%s", strerror(errno)); tio_error_print("Poll check error after packet finish"); rc = write(sio, CAN_STR, 1); - if (rc < 0) { + if (rc < 0) + { tio_error_print("Write cancel packet to serial failed"); } return ERR_FATAL; @@ -494,7 +554,8 @@ int receive_packet(int sio, struct xpacket packet, int fd) { /* Resend of previously processed packet. */ rc = write(sio, ACK_STR, 1); - if (rc < 0) { + if (rc < 0) + { tio_error_print("Write acknowlegdement packet to serial failed"); return ERR_FATAL; } @@ -520,7 +581,8 @@ int receive_packet(int sio, struct xpacket packet, int fd) { tio_error_print("Problem writing to file"); rc = write(sio, CAN_STR, 1); - if (rc < 0) { + if (rc < 0) + { tio_error_print("Write cancel packet to serial failed"); } return ERR_FATAL; @@ -545,16 +607,20 @@ int xmodem_receive(int sio, int fd) char status; /* Drain pending characters from serial line.*/ - while(1) { + while (true) + { if (key_hit) return -1; rc = read_poll(sio, &resp, 1, 50); - if (rc == 0) { + if (rc == 0) + { if (resp == CAN) return ERR; break; } - else if (rc < 0) { - if (rc != USER_CAN) { + else if (rc < 0) + { + if (rc != USER_CAN) + { tio_error_print("Read sync from serial failed"); } return ERR; @@ -571,79 +637,95 @@ int xmodem_receive(int sio, int fd) { tio_error_print("Timeout waiting for transfer to start"); return ERR; - } else if (rc < 0) { + } + else if (rc < 0) + { tio_error_print("Error starting XMODEM receive"); return ERR; } - while (!complete) { + while (!complete) + { /* Poll for 1 new byte for 3 seconds */ rc = read_poll(sio, &resp, 1, 3000); - if (rc == 0) { + if (rc == 0) + { tio_error_print("Timeout waiting for start of next packet"); return ERR; - } else if (rc < 0) { - tio_error_print("Error reading start of next packet") + } + else if (rc < 0) + { + tio_error_print("Error reading start of next packet"); return ERR; } if (key_hit) return USER_CAN; - switch(resp) + switch (resp) { case SOH: - /* Start of a packet */ - rc = receive_packet(sio, packet, fd); - if (rc == OK) { - packet.seq++; - status = '.'; - } else if (rc == ERR) { - rc = write(sio, NAK_STR, 1); - if (rc < 0) { - tio_error_print("Writing not acknowledge packet to serial failed"); + /* Start of a packet */ + rc = receive_packet(sio, packet, fd); + if (rc == OK) + { + packet.seq++; + status = '.'; + } + else if (rc == ERR) + { + rc = write(sio, NAK_STR, 1); + if (rc < 0) + { + tio_error_print("Writing not acknowledge packet to serial failed"); + return ERR; + } + status = 'N'; + } + else if (rc == ERR_FATAL) + { + tio_error_print("Receive cancelled due to fatal error"); return ERR; } - status = 'N'; - } else if (rc == ERR_FATAL) { - tio_error_print("Receive cancelled due to fatal error"); - return ERR; - } else if (rc == USER_CAN) { - rc = write(sio, CAN_STR, 1); - if (rc < 0) { - tio_error_print("Writing cancel to serial failed"); - return ERR; + else if (rc == USER_CAN) + { + rc = write(sio, CAN_STR, 1); + if (rc < 0) + { + tio_error_print("Writing cancel to serial failed"); + return ERR; + } + return USER_CAN; } - return USER_CAN; - } else if (rc == RX_IGNORE) { - status = ':'; - } - break; + else if (rc == RX_IGNORE) + { + status = ':'; + } + break; case EOT: - /* End of Transfer */ - rc = write(sio, ACK_STR, 1); - if (rc < 0) - { - tio_error_print("Write acknowlegdement packet to serial failed"); - return ERR; - } - complete = true; - status = '\0'; - write(STDOUT_FILENO, "|\r\n", 3); - break; + /* End of Transfer */ + rc = write(sio, ACK_STR, 1); + if (rc < 0) + { + tio_error_print("Write acknowlegdement packet to serial failed"); + return ERR; + } + complete = true; + status = '\0'; + write(STDOUT_FILENO, "|\r\n", 3); + break; case CAN: - /* Cancel from sender */ - tio_error_print("Transmission cancelled from sender"); - return ERR; - break; + /* Cancel from sender */ + tio_error_print("Transmission cancelled from sender"); + return ERR; + break; default: - tio_error_print("Unexpected character received waiting for next packet"); - return ERR; - break; + tio_error_print("Unexpected character received waiting for next packet"); + return ERR; + break; } - /* Update "progress bar" */ write(STDOUT_FILENO, &status, 1); @@ -660,14 +742,16 @@ int xymodem_send(int sio, const char *filename, modem_mode_t mode) /* Open file, map into memory */ fd = open(filename, O_RDONLY); - if (fd < 0) { + if (fd < 0) + { tio_error_print("Could not open file"); return ERR; } fstat(fd, &stat); len = stat.st_size; buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); - if (!buf) { + if (!buf) + { close(fd); tio_error_print("Could not mmap file"); return ERR; @@ -675,15 +759,19 @@ int xymodem_send(int sio, const char *filename, modem_mode_t mode) /* Do transfer */ key_hit = 0; - if (mode == XMODEM_1K) { + if (mode == XMODEM_1K) + { rc = xmodem_1k(sio, buf, len, 1); } - else if (mode == XMODEM_CRC) { + else if (mode == XMODEM_CRC) + { rc = xmodem(sio, buf, len); } - else { + else + { /* Ymodem: hdr + file + fin */ - while(1) { + while (true) + { char hdr[1024], *p; rc = -1; @@ -694,7 +782,7 @@ int xymodem_send(int sio, const char *filename, modem_mode_t mode) if (xmodem_1k(sio, hdr, p - hdr, 0) < 0) break; /* hdr with metadata */ if (xmodem_1k(sio, buf, len, 1) < 0) break; /* xmodem file */ if (xmodem_1k(sio, "", 1, 0) < 0) break; /* empty hdr = fin */ - rc = 0; break; + rc = 0; break; } } key_hit = 0xff; @@ -712,21 +800,25 @@ int xymodem_receive(int sio, const char *filename, modem_mode_t mode) /* Create new file */ fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); - if (fd < 0) { + if (fd < 0) + { tio_error_print("Could not open file"); return ERR; } /* Do transfer */ key_hit = 0; - if (mode == XMODEM_1K) { + if (mode == XMODEM_1K) + { tio_error_print("Not supported"); rc = -1; } - else if (mode == XMODEM_CRC) { + else if (mode == XMODEM_CRC) + { rc = xmodem_receive(sio, fd); } - else { + else + { tio_error_print("Not supported"); rc = -1; } diff --git a/src/xymodem.h b/src/xymodem.h index 1b46cd7..b5d2b8d 100644 --- a/src/xymodem.h +++ b/src/xymodem.h @@ -21,7 +21,8 @@ #pragma once -typedef enum { +typedef enum +{ XMODEM_1K, XMODEM_CRC, YMODEM, @@ -30,5 +31,4 @@ typedef enum { extern char key_hit; int xymodem_send(int sio, const char *filename, modem_mode_t mode); - int xymodem_receive(int sio, const char *filename, modem_mode_t mode);