From: git Date: Mon, 16 Feb 2026 16:27:58 +0000 (-0500) Subject: formatting improvements X-Git-Url: https://git.datadissipation.net/?a=commitdiff_plain;h=b32219b55e763b4a496223ab8e9a56dd7212c0fa;p=ust.git formatting improvements --- diff --git a/ust.c b/ust.c index f0c630d..3fd1932 100644 --- a/ust.c +++ b/ust.c @@ -23,6 +23,7 @@ #define LF '\n' #define BS '\b' #define DEL '\x7f' +#define ESC '\x1b' #if defined(CCDTR_IFLOW) && defined(CDSR_OFLOW) #define CDTRDSR (CDTR_IFLOW | CDSR_OFLOW) #endif @@ -120,7 +121,8 @@ main(int argc, char **argv) soft ^= 1; break; case 'd':; char* endptr; - long t = strtol(optarg, &endptr, 10); + long t; + t = strtol(optarg, &endptr, 10); if (errno != 0 || *endptr != '\0' || t < 0) { fprintf(stderr, "invalid character delay\n"); goto ustusage; @@ -284,8 +286,10 @@ openport() if (!isatty(fd)) die(2, "device \"%s\" is not a TTY\n", line); - int flags = fcntl(fd, F_GETFL, 0); - flags &= ~(O_NONBLOCK | O_NDELAY); /* opened to check with non-blocking mode, now set to blocking */ + int flags; + flags = fcntl(fd, F_GETFL, 0); + /* opened to check with non-blocking mode, now set to blocking */ + flags &= ~(O_NONBLOCK | O_NDELAY); if (fcntl(fd, F_SETFL, flags) == -1) die(1, "failed to set the device to blocking mode\n"); @@ -412,24 +416,27 @@ troptions(crlfopt *options, const char *str) options->lfincr = !options->lfincr; return 0; } else if (strcmp(str, "lf-to-cr") == 0) { options->lftocr = !options->lftocr; return 0; - } else + } else { goto trerr; + } break; case 'c': if (strcmp(str, "cr-in-lf") == 0) { options->crinlf = !options->crinlf; return 0; } else if (strcmp(str, "cr-to-lf") == 0) { options->crtolf = !options->crtolf; return 0; - } else + } else { goto trerr; + } break; case 'n': if (strcmp(str, "no-lf") == 0) { options->nolf = !options->nolf; return 0; } else if (strcmp(str, "no-cr") == 0) { options->nocr = !options->nocr; return 0; - } else + } else { goto trerr; + } break; default: trerr: @@ -479,10 +486,10 @@ termchck(const void *term) struct termios *iterm = (struct termios*)term; #define TERMIOS_STRUCT termios #endif - for (size_t i = 0; i < sizeof(struct TERMIOS_STRUCT); i++) { + for (size_t i = 0; i < sizeof(struct TERMIOS_STRUCT); i++) if (((char*)iterm)[i] != 0) return 1; - } + return 0; } @@ -533,8 +540,7 @@ writeport(void *unused) write(fd, &writebuff[i], 1); nanosleep(&wts, NULL); } - } - else { + } else { write(fd, writebuff, 1); } } @@ -586,11 +592,11 @@ readport(void *unused) inline void __attribute__((hot)) replacechar(char *str, ssize_t size, const char find, const char repl) { - for (int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) if (str[i] == find) str[i] = repl; - } } + /* TODO: optimize the function and allow for offsets greater than 1 */ inline ssize_t __attribute__((hot)) addchar(char *str, int *scratch, ssize_t size, const size_t bsize,const char find, const char inp, const int offset) @@ -631,10 +637,11 @@ rmchar(char *str, int *scratch, ssize_t size, const char find) } if (!c) return(size); - for (int i = c; i > 0; i--) { + + for (int i = c; i > 0; i--) for (int s = size; s >= scratch[i]; s--) str[s] = str[s + 1]; - } + return(size - c); } @@ -733,11 +740,7 @@ getcmd(int escape) die(1, "failed to set terminal attributes\n"); break; case 'w': - if (!half || !canonical) { - newterm.c_lflag |= ECHO; - newterm.c_lflag |= ICANON; - settermattr(STDIN_FILENO, &newterm); - } + cechck(); if (fgets(ttr, sizeof(ttr), stdin) != NULL) { replacechar(ttr, 63, LF, '\0'); ssize_t frln; @@ -822,18 +825,18 @@ getcmd(int escape) st ^= TIOCM_DTR; if (ioctl(fd, TIOCMSET, &st) != 0) { fprintf(stderr, "failed to set DTR [assertion]\n"); + break; } nanosleep(&ts, NULL); st ^= TIOCM_DTR; - if (ioctl(fd, TIOCMSET, &st) != 0) { + if (ioctl(fd, TIOCMSET, &st) != 0) fprintf(stderr, "failed to set DTR [negation]\n"); - } break; case BS: - break; case DEL: + case ESC: break; default: fprintf(stderr, "not a valid command [%c]\n", cmdchar);