]> git.alsa-project.org Git - alsa-utils.git/commitdiff
allow --send-hex data without quotes
authorClemens Ladisch <clemens@ladisch.de>
Wed, 16 Feb 2005 09:52:25 +0000 (09:52 +0000)
committerClemens Ladisch <clemens@ladisch.de>
Wed, 16 Feb 2005 09:52:25 +0000 (09:52 +0000)
Now all non-option arguments are read as data for the -S option,
instead of requiring exactly one argument for -S.

This fixes the bug that when specifying multiple bytes for the -S
option separated with spaces but without quoting, amidi would silently
ignore all but the first byte.

amidi/amidi.c

index 12bafd8475e912ee41e8ef4c3c3293c813dad13d..2e25f80313f64e66fa690cb72968c53b6cd33df8 100644 (file)
@@ -85,6 +85,16 @@ static void version(void)
        fputs("amidi version " SND_UTIL_VERSION_STR "\n", stderr);
 }
 
+static void *my_malloc(size_t size)
+{
+       void *p = malloc(size);
+       if (!p) {
+               error("out of memory");
+               exit(EXIT_FAILURE);
+       }
+       return p;
+}
+
 static void list_device(snd_ctl_t *ctl, int card, int device)
 {
        snd_rawmidi_info_t *info;
@@ -225,11 +235,7 @@ static void load_file(void)
                error("cannot determine length of %s: %s", send_file_name, strerror(errno));
                goto _error;
        }
-       send_data = malloc(length);
-       if (!send_data) {
-               error("cannot allocate %d bytes: %s", (int)length, strerror(errno));
-               goto _error;
-       }
+       send_data = my_malloc(length);
        lseek(fd, 0, SEEK_SET);
        if (read(fd, send_data, length) != length) {
                error("cannot read from %s: %s", send_file_name, strerror(errno));
@@ -260,7 +266,7 @@ static void parse_data(void)
        const char *p;
        int i, value;
 
-       send_data = malloc(strlen(send_hex)); /* guesstimate */
+       send_data = my_malloc(strlen(send_hex)); /* guesstimate */
        i = 0;
        value = -1; /* value is >= 0 when the first hex digit of a byte has been read */
        for (p = send_hex; *p; ++p) {
@@ -370,9 +376,27 @@ static void sig_handler(int dummy)
        stop = 1;
 }
 
+void add_send_hex_data(const char *str)
+{
+       int length;
+       char *s;
+
+       length = (send_hex ? strlen(send_hex) : 0) + strlen(str) + 1;
+       s = my_malloc(length);
+       if (send_hex) {
+               strcpy(s, send_hex);
+               strcat(s, " ");
+       } else {
+               s[0] = '\0';
+       }
+       strcat(s, str);
+       free(send_hex);
+       send_hex = s;
+}
+
 int main(int argc, char *argv[])
 {
-       static char short_options[] = "hVlLp:s:r:S:dt:a";
+       static char short_options[] = "hVlLp:s:r:S::dt:a";
        static struct option long_options[] = {
                {"help", 0, NULL, 'h'},
                {"version", 0, NULL, 'V'},
@@ -381,7 +405,7 @@ int main(int argc, char *argv[])
                {"port", 1, NULL, 'p'},
                {"send", 1, NULL, 's'},
                {"receive", 1, NULL, 'r'},
-               {"send-hex", 1, NULL, 'S'},
+               {"send-hex", 2, NULL, 'S'},
                {"dump", 0, NULL, 'd'},
                {"timeout", 1, NULL, 't'},
                {"active-sensing", 0, NULL, 'a'},
@@ -389,6 +413,7 @@ int main(int argc, char *argv[])
        };
        int c, err, ok = 0;
        int ignore_active_sensing = 1;
+       int do_send_hex = 0;
 
        while ((c = getopt_long(argc, argv, short_options,
                                long_options, NULL)) != -1) {
@@ -415,7 +440,9 @@ int main(int argc, char *argv[])
                        receive_file_name = optarg;
                        break;
                case 'S':
-                       send_hex = optarg;
+                       do_send_hex = 1;
+                       if (optarg)
+                               add_send_hex_data(optarg);
                        break;
                case 'd':
                        dump = 1;
@@ -431,6 +458,20 @@ int main(int argc, char *argv[])
                        return 1;
                }
        }
+       if (do_send_hex) {
+               /* data for -S can be specified as multiple arguments */
+               if (!send_hex && !argv[optind]) {
+                       error("Please specify some data for --send-hex.");
+                       return 1;
+               }
+               for (; argv[optind]; ++optind)
+                       add_send_hex_data(argv[optind]);
+       } else {
+               if (argv[optind]) {
+                       error("%s is not an option.", argv[optind]);
+                       return 1;
+               }
+       }
 
        if (do_rawmidi_list)
                rawmidi_list();
@@ -548,7 +589,8 @@ int main(int argc, char *argv[])
                                fflush(stdout);
                        }
                }
-               printf("\n%d bytes read\n", read);
+               if (isatty(fileno(stdout)))
+                       printf("\n%d bytes read\n", read);
        }
 
        ok = 1;