]> datadissipation.net git - ust.git/commitdiff
error messaging tweaks
authorgit <redacted>
Mon, 2 Mar 2026 18:41:20 +0000 (13:41 -0500)
committergit <redacted>
Mon, 2 Mar 2026 18:41:20 +0000 (13:41 -0500)
ust.c

diff --git a/ust.c b/ust.c
index df002ce15f8c716a9f0a082b4ef325fa88f8c034..9cb40535a7dabbd201a268b34cfb9b5ca2c1632c 100644 (file)
--- a/ust.c
+++ b/ust.c
@@ -67,7 +67,7 @@ static int gettermattr(int dv, struct TERMIOS_STRUCT *optst);
 static int settermattr(int dv, const struct TERMIOS_STRUCT *optst);
 static void settermspd(unsigned int lispeed, unsigned int lospeed, struct TERMIOS_STRUCT *optst);
 static int openport(void);
-static unsigned int strtoui(const char *str, const char *msg, unsigned int min);
+static unsigned int strtoui(const char *str, unsigned int min);
 static int troptions(CRLFOpt *options, const char *str);
 static int setroptions(void);
 static inline unsigned int lsbmask(unsigned int n);
@@ -299,14 +299,13 @@ openport(void)
 }
 
 unsigned int
-strtoui(const char *str, const char *msg, unsigned int min)
+strtoui(const char *str, unsigned int min)
 {      
        long t;
        char *endptr;
 
        t = strtol(str, &endptr, 10);
        if (errno != 0 || *endptr != '\0' || t < min || t > uintmax) {
-               fprintf(stderr, msg, str);
                return uintmax;
        }
        /* 
@@ -326,30 +325,25 @@ troptions(CRLFOpt *options, const char *str)
                } else if (!strcmp(str, "lf-to-cr")) {
                        options->lftocr ^= 1; return 0;
                } else {
-                       goto trerr;
+                       return 1;
                }
-               break;
        case 'c':
                if (!strcmp(str, "cr-in-lf")) {
                        options->crinlf ^= 1; return 0;
                } else if (!strcmp(str, "cr-to-lf")) {
                        options->crtolf ^= 1; return 0;
                } else {
-                       goto trerr;
+                       return 1;
                }
-               break;
        case 'n':
                if (!strcmp(str, "no-lf")) {
                        options->nolf ^= 1; return 0;
                } else if (!strcmp(str, "no-cr")) {
                        options->nocr ^= 1; return 0;
                } else {
-                       goto trerr;
+                       return 1;
                }
-               break;
        default:
-       trerr:
-               fprintf(stderr, "invalid translation option: %s\n", str);
                return 1;
        }
 }
@@ -386,7 +380,7 @@ termchck(const struct TERMIOS_STRUCT *term)
 }
 
 void *
-writeport(void *)
+writeport(void *unused)
 {
        Sizes msizes;
        struct timespec ts;
@@ -436,9 +430,11 @@ writeport(void *)
                                        rwc -= write(fd, &writebuff[i], 1);
                                        nanosleep(&wts, NULL);
                                }
+
                        } else {
                                rwc -= write(fd, writebuff, 1);
                        }
+
                } else if (inln == 0 && !interactive) {
                        break;
                }
@@ -535,7 +531,6 @@ addchar(char *buff, ssize_t size, int *scratch, const Sizes *sizes, const Args *
                memmove(&buff[t] + 1, &buff[t], (size + c) - t);
                buff[t + (to ^ 1)] = args->input;
        }
-       printf("%s\n", buff);
        return (size + c);
 }
 
@@ -694,13 +689,15 @@ getcmd(int escape)
                cechck();
                if (fgets(ttr, sizeof(ttr), stdin) != NULL) {
                        replacechar(ttr, 63, &nolfargs);
-                       tspd = strtoui(ttr, "invalid speed\n", 0);
+                       tspd = strtoui(ttr, 0);
                        if (tspd != uintmax) {
                                ospeed = ispeed = tspd;
                                settermspd(ispeed, ospeed, &cntrl);
                                if (settermattr(fd, &cntrl) != 0)
                                        fprintf(stderr, "failed to set baudrate "
                                                        "[RX:%u | TX:%u]\n", ispeed, ospeed);
+                       } else {
+                               fprintf(stderr, "invalid speed: %s\n", ttr);
                        }
                }
                goto finish;
@@ -708,10 +705,12 @@ getcmd(int escape)
                cechck();
                if (fgets(ttr, sizeof(ttr), stdin) != NULL) {
                        replacechar(ttr, 63, &nolfargs);
-                       chardelay = strtoui(ttr, "invalid delay\n", 0);
+                       chardelay = strtoui(ttr, 0);
                        if (chardelay != uintmax) {
                                wts.tv_sec = 0;
                                wts.tv_nsec = chardelay;
+                       } else {
+                               fprintf(stderr, "invalid delay: %s\n", ttr);
                        }
                }
                goto finish;
@@ -720,8 +719,10 @@ getcmd(int escape)
                fprintf(stderr, "additional output translation option: ");
                if (fgets(ttr, sizeof(ttr), stdin) != NULL) {
                        replacechar(ttr, 63, &nolfargs);
-                       if(troptions(&tropts, ttr))
+                       if(troptions(&tropts, ttr)) {
+                               fprintf(stderr, "invalid output translation option: %s\n", optarg);
                                goto finish;
+                       }
                        if (setroptions())
                                fprintf(stderr, "could not set new options\n");         
                }
@@ -731,8 +732,10 @@ getcmd(int escape)
                printf("additional input translation option: ");
                if (fgets(ttr, sizeof(ttr), stdin) != NULL) {
                        replacechar(ttr, 63, &nolfargs);
-                       if (troptions(&itropts, ttr))
-                               goto finish;    
+                       if (troptions(&itropts, ttr)) {
+                               fprintf(stderr, "invalid input translation option: %s\n", optarg);
+                               goto finish;
+                       }       
                        if (setroptions())
                                fprintf(stderr, "could not set new options\n"); 
                }
@@ -786,11 +789,13 @@ void
 die(int code, const char *msg, ...)
 {
        va_list fpa;
+
        while (rwc > 0) {
                int t = rwc;
                nanosleep(&wts, NULL);
                if (t == rwc) break;
        }
+
        if (fd != -1)
                close(fd);
 
@@ -823,13 +828,14 @@ main(int argc, char *argv[])
        int oind, rxspdset, devset, c;
        oind = rxspdset = devset = 0;
 
-       for (int i = 1; i < argc; i++ ) {
+       for (int i = 1; i < argc; i++) {
                if (argv[i][0] == '-' && argv[i][1] >= '0' && argv[i][1] <= '9') {
                        if (asprintf(&t, "-s%s", argv[i] + 1) == -1) {
                                fprintf(stderr, "%s: cannot convert -# to -s#\n", argv[0]);
                                break;
+                       } else {
+                               argv[i] = t;
                        }
-                       argv[i] = t;
                }
        }
 
@@ -872,7 +878,7 @@ main(int argc, char *argv[])
                case 'd':
                        tl = strtol(optarg, &endptr, 10);
                        if (errno != 0 || *endptr != '\0' || t < 0) {
-                               fprintf(stderr, "invalid character delay: %s\n", optarg);
+                               fprintf(stderr, "%s: invalid character delay: %s\n", argv[0],optarg);
                                goto ustusage;
                        } else {
                                chardelay = tl;
@@ -880,7 +886,7 @@ main(int argc, char *argv[])
                        break;
                case 'D':
                        if (optarg[1] != '\0' || !(optarg[0] >= '5' && optarg[0] <= '8')) {
-                               fprintf(stderr, "invalid number of data bits: %s\n", optarg);
+                               fprintf(stderr, "%s: invalid number of data bits: %s\n", argv[0], optarg);
                                goto ustusage;  
                        } else {
                                datab = optarg[0];
@@ -894,9 +900,11 @@ main(int argc, char *argv[])
                        half ^= 1; break;
                case 'i': /* FALLTHROUGH */
                case 's':
-                       tui = strtoui(optarg, "invalid speed: %s\n", 1);
-                       if (tui == uintmax)
+                       tui = strtoui(optarg, 1);
+                       if (tui == uintmax) {
+                               fprintf(stderr, "%s: invalid speed: %s\n", argv[0], optarg);
                                goto ustusage;
+                       }
                        if (c == 'i') {
                                ispeed = tui; rxspdset = 1;
                        } else {
@@ -908,11 +916,11 @@ main(int argc, char *argv[])
                        stopb ^= 1; break;
                case 'l':
                        if (devset) {
-                               fprintf(stderr, "cannot specify multiple devices\n");
+                               fprintf(stderr, "%s: cannot specify multiple devices\n", argv[0]);
                                goto ustusage;
                        }
                        if (strlen(optarg) > 58) {
-                               fprintf(stderr, "device name too long\n");
+                               fprintf(stderr, "%s: device name too long\n", argv[0]);
                                goto ustusage;
                        }
                        if (strchr(optarg, '/')) {
@@ -924,18 +932,24 @@ main(int argc, char *argv[])
                        }
                        break;
                case 'm':
-                       tui = strtoui(optarg, "invalid number of characters: %s\n", 1);
-                       if (tui == uintmax)
+                       tui = strtoui(optarg, 1);
+                       if (tui == uintmax) {
+                               fprintf(stderr, "%s: invalid number of characters: %s\n", argv[0], optarg);
                                goto ustusage;
+                       }
                        minchars = tui;
                        break;
                case 't':
-                       if (troptions(&tropts, optarg))         
+                       if (troptions(&tropts, optarg)) {
+                               fprintf(stderr, "%s: invalid output translation option: %s\n", argv[0], optarg);
                                goto ustusage;
+                       }
                        break;
                case 'T':
-                       if (troptions(&itropts, optarg))
+                       if (troptions(&itropts, optarg)) {
+                               fprintf(stderr, "%s: invalid input translation option: %s\n", argv[0], optarg);
                                goto ustusage;
+                       }
                        break;
                case 'v':
                        verbose ^= 1; break;