]> mj.ucw.cz Git - osdd.git/commitdiff
osdc: stdin can be used as an option argument
authorMartin Mares <mj@ucw.cz>
Sat, 17 Jul 2010 17:29:04 +0000 (19:29 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 17 Jul 2010 17:29:04 +0000 (19:29 +0200)
osdc.c

diff --git a/osdc.c b/osdc.c
index 27b6c630e386fbcb3174f668ad4bb76174607d1b..0f01ae745fc561624c771b6c449c10a1cebf794c 100644 (file)
--- a/osdc.c
+++ b/osdc.c
 #include "util.h"
 #include "send.h"
 
+static struct osd_msg *msg;
+
 static void NONRET
 usage(void)
 {
-  fprintf(stderr, "Usage: osdc [--<property>=<value> | <message>]*\n");
+  fprintf(stderr, "\
+Usage: osdc [--<property>=<value> | <message>]*\n\
+\n\
+Either <value> or <message> can be `-' for standard input.\n\
+");
   exit(1);
 }
 
+static void
+parse_stdin(char *key)
+{
+  char line[1024];
+  while (fgets(line, sizeof(line), stdin))
+    {
+      char *nl = strchr(line, '\n');
+      if (nl)
+       *nl = 0;
+      osd_add_line(msg, key, line);
+    }
+}
+
 int main(int argc, char **argv)
 {
-  struct osd_msg *m = osd_new_msg();
+  msg = osd_new_msg();
 
   int more_opts = 1;
   for (int i=1; i<argc; i++)
     {
       char *arg = argv[i];
-      if (more_opts && arg[0] == '-' && arg[1] == '-')
+      if (more_opts && arg[0] == '-')
        {
-         if (!arg[2])
-           more_opts = 0;
-         else
+         if (arg[1] == '-')
            {
-             arg += 2;
-             if (!strcmp(arg, "help"))
-               usage();
-             char *sep = strchr(arg, '=');
-             if (!sep)
-               usage();
-             *sep++ = 0;
-             osd_add_line(m, arg, sep);
+             if (!arg[2])
+               more_opts = 0;
+             else
+               {
+                 arg += 2;
+                 if (!strcmp(arg, "help"))
+                   usage();
+                 char *sep = strchr(arg, '=');
+                 if (!sep)
+                   usage();
+                 *sep++ = 0;
+                 if (!strcmp(sep, "-"))
+                   parse_stdin(arg);
+                 else
+                   osd_add_line(msg, arg, sep);
+               }
            }
+         else if (!arg[1])
+           parse_stdin(NULL);
+         else
+           usage();
        }
       else
-       osd_add_line(m, NULL, arg);
+       osd_add_line(msg, NULL, arg);
     }
 
-  osd_send(m);
+  osd_send(msg);
   return 0;
 }