Clean up file descriptor name shadowing

This commit is contained in:
Martin Lund 2024-04-14 10:42:22 +02:00
parent ae461dc296
commit c4878a90d7
2 changed files with 51 additions and 51 deletions

View file

@ -37,7 +37,7 @@
#define MAX_BUFFER_SIZE 2000 // Maximum size of circular buffer #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 char circular_buffer[MAX_BUFFER_SIZE];
static int buffer_size = 0; static int buffer_size = 0;
@ -85,7 +85,7 @@ static int high(lua_State *L)
return 0; return 0;
} }
tty_line_set(serial_fd, line, LINE_HIGH); tty_line_set(device_fd, line, LINE_HIGH);
return 0; return 0;
} }
@ -100,7 +100,7 @@ static int low(lua_State *L)
return 0; return 0;
} }
tty_line_set(serial_fd, line, LINE_LOW); tty_line_set(device_fd, line, LINE_LOW);
return 0; return 0;
} }
@ -115,7 +115,7 @@ static int toggle(lua_State *L)
return 0; return 0;
} }
tty_line_toggle(serial_fd, line); tty_line_toggle(device_fd, line);
return 0; return 0;
} }
@ -175,17 +175,17 @@ static int modem_send(lua_State *L)
{ {
case XMODEM_1K: case XMODEM_1K:
tio_printf("Sending file '%s' using XMODEM-1K", file); 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; break;
case XMODEM_CRC: case XMODEM_CRC:
tio_printf("Sending file '%s' using XMODEM-CRC", file); 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; break;
case YMODEM: case YMODEM:
tio_printf("Sending file '%s' using YMODEM", file); 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; break;
} }
@ -203,7 +203,7 @@ static int send(lua_State *L)
return 0; return 0;
} }
ret = write(serial_fd, string, strlen(string)); ret = write(device_fd, string, strlen(string));
if (ret < 0) if (ret < 0)
{ {
tio_error_print("%s\n", strerror(errno)); tio_error_print("%s\n", strerror(errno));
@ -289,7 +289,7 @@ static int expect(lua_State *L)
// Main loop to read and match // Main loop to read and match
while (true) 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) if (bytes_read > 0)
{ {
putchar(c); putchar(c);
@ -423,7 +423,7 @@ void script_run(int fd)
{ {
lua_State *L; lua_State *L;
serial_fd = fd; device_fd = fd;
L = luaL_newstate(); L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);

View file

@ -162,7 +162,7 @@ static unsigned long rx_total = 0, tx_total = 0;
static bool connected = false; static bool connected = false;
static bool standard_baudrate = true; static bool standard_baudrate = true;
static void (*print)(char c); static void (*print)(char c);
static int fd; static int device_fd;
static bool map_i_ff_escc = false; static bool map_i_ff_escc = false;
static bool map_i_nl_crnl = false; static bool map_i_nl_crnl = false;
static bool map_o_cr_nl = false; static bool map_o_cr_nl = false;
@ -378,7 +378,7 @@ void *tty_stdin_input_thread(void *arg)
break; break;
case KEY_SHIFT_F: case KEY_SHIFT_F:
tio_printf("Flushed data I/O channels") tio_printf("Flushed data I/O channels")
tcflush(fd, TCIOFLUSH); tcflush(device_fd, TCIOFLUSH);
break; break;
default: default:
break; 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); unsigned char hex_value = char_to_nibble(hex_chars[0]) << 4 | (char_to_nibble(hex_chars[1]) & 0x0F);
hex_char_index = 0; 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) if (status < 0)
{ {
tio_warning_printf("Could not write to tty device"); tio_warning_printf("Could not write to tty device");
@ -496,7 +496,7 @@ void tty_line_config_apply(void)
int i = 0; int i = 0;
static int state; 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)); tio_warning_printf("Could not get line state (%s)", strerror(errno));
return; 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)); 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) switch (input_char)
{ {
case KEY_0: 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; break;
case KEY_1: 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; break;
case KEY_2: 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; break;
case KEY_3: 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; break;
case KEY_4: 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; break;
case KEY_5: 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; break;
default: default:
tio_warning_printf("Invalid line number"); 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("Sending file '%s' ", line);
tio_printf("Press any key to abort transfer"); 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; 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("Sending file '%s' ", line);
tio_printf("Press any key to abort transfer"); 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; break;
} }
@ -797,7 +797,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
break; break;
case KEY_SHIFT_L: 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)); tio_warning_printf("Could not get line state (%s)", strerror(errno));
break; break;
@ -857,7 +857,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
break; break;
case KEY_B: case KEY_B:
tcsendbreak(fd, 0); tcsendbreak(device_fd, 0);
break; break;
case KEY_C: case KEY_C:
@ -939,7 +939,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
case KEY_R: case KEY_R:
/* Run script */ /* Run script */
script_run(fd); script_run(device_fd);
break; break;
case KEY_S: case KEY_S:
@ -996,7 +996,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
if (tio_readln()) { if (tio_readln()) {
tio_printf("Sending file '%s' ", line); tio_printf("Sending file '%s' ", line);
tio_printf("Press any key to abort transfer"); 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; break;
@ -1424,8 +1424,8 @@ void tty_disconnect(void)
if (connected) if (connected)
{ {
tio_printf("Disconnected"); tio_printf("Disconnected");
flock(fd, LOCK_UN); flock(device_fd, LOCK_UN);
close(fd); close(device_fd);
connected = false; connected = false;
/* Fire alert action */ /* Fire alert action */
@ -1435,12 +1435,12 @@ void tty_disconnect(void)
void tty_restore(void) void tty_restore(void)
{ {
tcsetattr(fd, TCSANOW, &tio_old); tcsetattr(device_fd, TCSANOW, &tio_old);
if (option.rs485) if (option.rs485)
{ {
/* Restore original RS-485 mode */ /* Restore original RS-485 mode */
rs485_mode_restore(fd); rs485_mode_restore(device_fd);
} }
if (connected) if (connected)
@ -1538,22 +1538,22 @@ int tty_connect(void)
char* now = NULL; char* now = NULL;
/* Open tty device */ /* Open tty device */
fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK); device_fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0) if (device_fd < 0)
{ {
tio_error_printf_silent("Could not open tty device (%s)", strerror(errno)); tio_error_printf_silent("Could not open tty device (%s)", strerror(errno));
goto error_open; goto error_open;
} }
/* Make sure device is of tty type */ /* Make sure device is of tty type */
if (!isatty(fd)) if (!isatty(device_fd))
{ {
tio_error_printf("Not a tty device"); tio_error_printf("Not a tty device");
exit(EXIT_FAILURE);; exit(EXIT_FAILURE);;
} }
/* Lock device file */ /* Lock device file */
status = flock(fd, LOCK_EX | LOCK_NB); status = flock(device_fd, LOCK_EX | LOCK_NB);
if ((status == -1) && (errno == EWOULDBLOCK)) if ((status == -1) && (errno == EWOULDBLOCK))
{ {
tio_error_printf("Device file is locked by another process"); 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) */ /* Flush stale I/O data (if any) */
tcflush(fd, TCIOFLUSH); tcflush(device_fd, TCIOFLUSH);
/* Print connect status */ /* Print connect status */
tio_printf("Connected"); tio_printf("Connected");
@ -1580,7 +1580,7 @@ int tty_connect(void)
tty_output_mode_set(option.output_mode); tty_output_mode_set(option.output_mode);
/* Save current port settings */ /* 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)); tio_error_printf_silent("Could not get port settings (%s)", strerror(errno));
goto error_tcgetattr; goto error_tcgetattr;
@ -1598,7 +1598,7 @@ int tty_connect(void)
/* Manage RS-485 mode */ /* Manage RS-485 mode */
if (option.rs485) if (option.rs485)
{ {
rs485_mode_enable(fd); rs485_mode_enable(device_fd);
} }
/* Make sure we restore tty settings on exit */ /* Make sure we restore tty settings on exit */
@ -1609,7 +1609,7 @@ int tty_connect(void)
} }
/* Activate new port settings */ /* Activate new port settings */
status = tcsetattr(fd, TCSANOW, &tio); status = tcsetattr(device_fd, TCSANOW, &tio);
if (status == -1) if (status == -1)
{ {
tio_error_printf_silent("Could not apply port settings (%s)", strerror(errno)); 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) */ /* Set arbitrary baudrate (only works on supported platforms) */
if (!standard_baudrate) 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)); tio_error_printf_silent("Could not set baudrate speed (%s)", strerror(errno));
goto error_setspeed; goto error_setspeed;
@ -1640,7 +1640,7 @@ int tty_connect(void)
else if (ret > 0) else if (ret > 0)
{ {
// Forward to tty device // Forward to tty device
ret = write(fd, &input_char, 1); ret = write(device_fd, &input_char, 1);
if (ret < 0) if (ret < 0)
{ {
tio_error_printf("Could not write to serial device (%s)", strerror(errno)); tio_error_printf("Could not write to serial device (%s)", strerror(errno));
@ -1658,7 +1658,7 @@ int tty_connect(void)
/* Manage script activation */ /* Manage script activation */
if (option.script_run != SCRIPT_RUN_NEVER) if (option.script_run != SCRIPT_RUN_NEVER)
{ {
script_run(fd); script_run(device_fd);
if (option.script_run == SCRIPT_RUN_ONCE) if (option.script_run == SCRIPT_RUN_ONCE)
{ {
@ -1676,10 +1676,10 @@ int tty_connect(void)
while (true) while (true)
{ {
FD_ZERO(&rdfs); FD_ZERO(&rdfs);
FD_SET(fd, &rdfs); FD_SET(device_fd, &rdfs);
FD_SET(pipefd[0], &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)); maxfd = MAX(maxfd, socket_add_fds(&rdfs, true));
/* Block until input becomes available */ /* Block until input becomes available */
@ -1687,10 +1687,10 @@ int tty_connect(void)
if (status > 0) if (status > 0)
{ {
bool forward = false; bool forward = false;
if (FD_ISSET(fd, &rdfs)) if (FD_ISSET(device_fd, &rdfs))
{ {
/* Input from tty device ready */ /* 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) if (bytes_read <= 0)
{ {
/* Error reading - device is likely unplugged */ /* Error reading - device is likely unplugged */
@ -1781,7 +1781,7 @@ int tty_connect(void)
else if (bytes_read == 0) else if (bytes_read == 0)
{ {
/* Reached EOF (when piping to stdin, never reached) */ /* Reached EOF (when piping to stdin, never reached) */
tty_sync(fd); tty_sync(device_fd);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -1817,11 +1817,11 @@ int tty_connect(void)
if (forward) if (forward)
{ {
forward_to_tty(fd, output_char); forward_to_tty(device_fd, output_char);
} }
} }
tty_sync(fd); tty_sync(device_fd);
} }
else else
{ {
@ -1830,10 +1830,10 @@ int tty_connect(void)
if (forward) 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) else if (status == -1)