From c4878a90d7d39acaef9393d9fcf0f26aa178d7de Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Sun, 14 Apr 2024 10:42:22 +0200 Subject: [PATCH] Clean up file descriptor name shadowing --- src/script.c | 20 ++++++------- src/tty.c | 82 ++++++++++++++++++++++++++-------------------------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/script.c b/src/script.c index d8c4487..5a8fd41 100644 --- a/src/script.c +++ b/src/script.c @@ -37,7 +37,7 @@ #define MAX_BUFFER_SIZE 2000 // Maximum size of circular buffer -static int serial_fd; +static int device_fd; static char circular_buffer[MAX_BUFFER_SIZE]; static int buffer_size = 0; @@ -85,7 +85,7 @@ static int high(lua_State *L) return 0; } - tty_line_set(serial_fd, line, LINE_HIGH); + tty_line_set(device_fd, line, LINE_HIGH); return 0; } @@ -100,7 +100,7 @@ static int low(lua_State *L) return 0; } - tty_line_set(serial_fd, line, LINE_LOW); + tty_line_set(device_fd, line, LINE_LOW); return 0; } @@ -115,7 +115,7 @@ static int toggle(lua_State *L) return 0; } - tty_line_toggle(serial_fd, line); + tty_line_toggle(device_fd, line); return 0; } @@ -175,17 +175,17 @@ static int modem_send(lua_State *L) { case XMODEM_1K: tio_printf("Sending file '%s' using XMODEM-1K", file); - tio_printf("%s", xymodem_send(serial_fd, file, XMODEM_1K) < 0 ? "Aborted" : "Done"); + tio_printf("%s", xymodem_send(device_fd, file, XMODEM_1K) < 0 ? "Aborted" : "Done"); break; case XMODEM_CRC: tio_printf("Sending file '%s' using XMODEM-CRC", file); - tio_printf("%s", xymodem_send(serial_fd, file, XMODEM_CRC) < 0 ? "Aborted" : "Done"); + tio_printf("%s", xymodem_send(device_fd, file, XMODEM_CRC) < 0 ? "Aborted" : "Done"); break; case YMODEM: tio_printf("Sending file '%s' using YMODEM", file); - tio_printf("%s", xymodem_send(serial_fd, file, YMODEM) < 0 ? "Aborted" : "Done"); + tio_printf("%s", xymodem_send(device_fd, file, YMODEM) < 0 ? "Aborted" : "Done"); break; } @@ -203,7 +203,7 @@ static int send(lua_State *L) return 0; } - ret = write(serial_fd, string, strlen(string)); + ret = write(device_fd, string, strlen(string)); if (ret < 0) { tio_error_print("%s\n", strerror(errno)); @@ -289,7 +289,7 @@ static int expect(lua_State *L) // Main loop to read and match while (true) { - ssize_t bytes_read = read_poll(serial_fd, &c, 1, timeout); + ssize_t bytes_read = read_poll(device_fd, &c, 1, timeout); if (bytes_read > 0) { putchar(c); @@ -423,7 +423,7 @@ void script_run(int fd) { lua_State *L; - serial_fd = fd; + device_fd = fd; L = luaL_newstate(); luaL_openlibs(L); diff --git a/src/tty.c b/src/tty.c index b85cbb7..84c5b91 100644 --- a/src/tty.c +++ b/src/tty.c @@ -162,7 +162,7 @@ static unsigned long rx_total = 0, tx_total = 0; static bool connected = false; static bool standard_baudrate = true; static void (*print)(char c); -static int fd; +static int device_fd; static bool map_i_ff_escc = false; static bool map_i_nl_crnl = false; static bool map_o_cr_nl = false; @@ -378,7 +378,7 @@ void *tty_stdin_input_thread(void *arg) break; case KEY_SHIFT_F: tio_printf("Flushed data I/O channels") - tcflush(fd, TCIOFLUSH); + tcflush(device_fd, TCIOFLUSH); break; default: break; @@ -442,7 +442,7 @@ static void handle_hex_prompt(char c) unsigned char hex_value = char_to_nibble(hex_chars[0]) << 4 | (char_to_nibble(hex_chars[1]) & 0x0F); hex_char_index = 0; - ssize_t status = tty_write(fd, &hex_value, 1); + ssize_t status = tty_write(device_fd, &hex_value, 1); if (status < 0) { tio_warning_printf("Could not write to tty device"); @@ -496,7 +496,7 @@ void tty_line_config_apply(void) int i = 0; static int state; - if (ioctl(fd, TIOCMGET, &state) < 0) + if (ioctl(device_fd, TIOCMGET, &state) < 0) { tio_warning_printf("Could not get line state (%s)", strerror(errno)); return; @@ -523,7 +523,7 @@ void tty_line_config_apply(void) } } - if (ioctl(fd, TIOCMSET, &state) < 0) + if (ioctl(device_fd, TIOCMSET, &state) < 0) { tio_warning_printf("Could not set line state configuration (%s)", strerror(errno)); } @@ -696,22 +696,22 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) switch (input_char) { case KEY_0: - tty_line_poke(fd, TIOCM_DTR, line_mode, option.dtr_pulse_duration); + tty_line_poke(device_fd, TIOCM_DTR, line_mode, option.dtr_pulse_duration); break; case KEY_1: - tty_line_poke(fd, TIOCM_RTS, line_mode, option.rts_pulse_duration); + tty_line_poke(device_fd, TIOCM_RTS, line_mode, option.rts_pulse_duration); break; case KEY_2: - tty_line_poke(fd, TIOCM_CTS, line_mode, option.cts_pulse_duration); + tty_line_poke(device_fd, TIOCM_CTS, line_mode, option.cts_pulse_duration); break; case KEY_3: - tty_line_poke(fd, TIOCM_DSR, line_mode, option.dsr_pulse_duration); + tty_line_poke(device_fd, TIOCM_DSR, line_mode, option.dsr_pulse_duration); break; case KEY_4: - tty_line_poke(fd, TIOCM_CD, line_mode, option.dcd_pulse_duration); + tty_line_poke(device_fd, TIOCM_CD, line_mode, option.dcd_pulse_duration); break; case KEY_5: - tty_line_poke(fd, TIOCM_RI, line_mode, option.ri_pulse_duration); + tty_line_poke(device_fd, TIOCM_RI, line_mode, option.ri_pulse_duration); break; default: tio_warning_printf("Invalid line number"); @@ -729,7 +729,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) { tio_printf("Sending file '%s' ", line); tio_printf("Press any key to abort transfer"); - tio_printf("%s", xymodem_send(fd, line, XMODEM_CRC) < 0 ? "Aborted" : "Done"); + tio_printf("%s", xymodem_send(device_fd, line, XMODEM_CRC) < 0 ? "Aborted" : "Done"); } break; @@ -740,7 +740,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) { tio_printf("Sending file '%s' ", line); tio_printf("Press any key to abort transfer"); - tio_printf("%s", xymodem_send(fd, line, XMODEM_CRC) < 0 ? "Aborted" : "Done"); + tio_printf("%s", xymodem_send(device_fd, line, XMODEM_CRC) < 0 ? "Aborted" : "Done"); } break; } @@ -797,7 +797,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) break; case KEY_SHIFT_L: - if (ioctl(fd, TIOCMGET, &state) < 0) + if (ioctl(device_fd, TIOCMGET, &state) < 0) { tio_warning_printf("Could not get line state (%s)", strerror(errno)); break; @@ -857,7 +857,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) break; case KEY_B: - tcsendbreak(fd, 0); + tcsendbreak(device_fd, 0); break; case KEY_C: @@ -939,7 +939,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) case KEY_R: /* Run script */ - script_run(fd); + script_run(device_fd); break; case KEY_S: @@ -996,7 +996,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) if (tio_readln()) { tio_printf("Sending file '%s' ", line); tio_printf("Press any key to abort transfer"); - tio_printf("%s", xymodem_send(fd, line, YMODEM) < 0 ? "Aborted" : "Done"); + tio_printf("%s", xymodem_send(device_fd, line, YMODEM) < 0 ? "Aborted" : "Done"); } break; @@ -1424,8 +1424,8 @@ void tty_disconnect(void) if (connected) { tio_printf("Disconnected"); - flock(fd, LOCK_UN); - close(fd); + flock(device_fd, LOCK_UN); + close(device_fd); connected = false; /* Fire alert action */ @@ -1435,12 +1435,12 @@ void tty_disconnect(void) void tty_restore(void) { - tcsetattr(fd, TCSANOW, &tio_old); + tcsetattr(device_fd, TCSANOW, &tio_old); if (option.rs485) { /* Restore original RS-485 mode */ - rs485_mode_restore(fd); + rs485_mode_restore(device_fd); } if (connected) @@ -1538,22 +1538,22 @@ int tty_connect(void) char* now = NULL; /* Open tty device */ - fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK); - if (fd < 0) + device_fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK); + if (device_fd < 0) { tio_error_printf_silent("Could not open tty device (%s)", strerror(errno)); goto error_open; } /* Make sure device is of tty type */ - if (!isatty(fd)) + if (!isatty(device_fd)) { tio_error_printf("Not a tty device"); exit(EXIT_FAILURE);; } /* Lock device file */ - status = flock(fd, LOCK_EX | LOCK_NB); + status = flock(device_fd, LOCK_EX | LOCK_NB); if ((status == -1) && (errno == EWOULDBLOCK)) { tio_error_printf("Device file is locked by another process"); @@ -1561,7 +1561,7 @@ int tty_connect(void) } /* Flush stale I/O data (if any) */ - tcflush(fd, TCIOFLUSH); + tcflush(device_fd, TCIOFLUSH); /* Print connect status */ tio_printf("Connected"); @@ -1580,7 +1580,7 @@ int tty_connect(void) tty_output_mode_set(option.output_mode); /* Save current port settings */ - if (tcgetattr(fd, &tio_old) < 0) + if (tcgetattr(device_fd, &tio_old) < 0) { tio_error_printf_silent("Could not get port settings (%s)", strerror(errno)); goto error_tcgetattr; @@ -1598,7 +1598,7 @@ int tty_connect(void) /* Manage RS-485 mode */ if (option.rs485) { - rs485_mode_enable(fd); + rs485_mode_enable(device_fd); } /* Make sure we restore tty settings on exit */ @@ -1609,7 +1609,7 @@ int tty_connect(void) } /* Activate new port settings */ - status = tcsetattr(fd, TCSANOW, &tio); + status = tcsetattr(device_fd, TCSANOW, &tio); if (status == -1) { tio_error_printf_silent("Could not apply port settings (%s)", strerror(errno)); @@ -1619,7 +1619,7 @@ int tty_connect(void) /* Set arbitrary baudrate (only works on supported platforms) */ if (!standard_baudrate) { - if (setspeed(fd, option.baudrate) != 0) + if (setspeed(device_fd, option.baudrate) != 0) { tio_error_printf_silent("Could not set baudrate speed (%s)", strerror(errno)); goto error_setspeed; @@ -1640,7 +1640,7 @@ int tty_connect(void) else if (ret > 0) { // Forward to tty device - ret = write(fd, &input_char, 1); + ret = write(device_fd, &input_char, 1); if (ret < 0) { tio_error_printf("Could not write to serial device (%s)", strerror(errno)); @@ -1658,7 +1658,7 @@ int tty_connect(void) /* Manage script activation */ if (option.script_run != SCRIPT_RUN_NEVER) { - script_run(fd); + script_run(device_fd); if (option.script_run == SCRIPT_RUN_ONCE) { @@ -1676,10 +1676,10 @@ int tty_connect(void) while (true) { FD_ZERO(&rdfs); - FD_SET(fd, &rdfs); + FD_SET(device_fd, &rdfs); FD_SET(pipefd[0], &rdfs); - maxfd = MAX(fd, pipefd[0]); + maxfd = MAX(device_fd, pipefd[0]); maxfd = MAX(maxfd, socket_add_fds(&rdfs, true)); /* Block until input becomes available */ @@ -1687,10 +1687,10 @@ int tty_connect(void) if (status > 0) { bool forward = false; - if (FD_ISSET(fd, &rdfs)) + if (FD_ISSET(device_fd, &rdfs)) { /* Input from tty device ready */ - ssize_t bytes_read = read(fd, input_buffer, BUFSIZ); + ssize_t bytes_read = read(device_fd, input_buffer, BUFSIZ); if (bytes_read <= 0) { /* Error reading - device is likely unplugged */ @@ -1781,7 +1781,7 @@ int tty_connect(void) else if (bytes_read == 0) { /* Reached EOF (when piping to stdin, never reached) */ - tty_sync(fd); + tty_sync(device_fd); exit(EXIT_SUCCESS); } @@ -1817,11 +1817,11 @@ int tty_connect(void) if (forward) { - forward_to_tty(fd, output_char); + forward_to_tty(device_fd, output_char); } } - tty_sync(fd); + tty_sync(device_fd); } else { @@ -1830,10 +1830,10 @@ int tty_connect(void) if (forward) { - forward_to_tty(fd, output_char); + forward_to_tty(device_fd, output_char); } - tty_sync(fd); + tty_sync(device_fd); } } else if (status == -1)