]> mj.ucw.cz Git - libucw.git/commitdiff
Moved shell script support commands to lib/shell.
authorMartin Mares <mj@ucw.cz>
Fri, 23 Aug 2002 08:30:45 +0000 (08:30 +0000)
committerMartin Mares <mj@ucw.cz>
Fri, 23 Aug 2002 08:30:45 +0000 (08:30 +0000)
lib/Makefile
lib/shell/Makefile [new file with mode: 0644]
lib/shell/config.c [new file with mode: 0644]
lib/shell/logger.c [new file with mode: 0644]

index 631a743752e64b03790ed4b0c168a30957c7fb93..32e8a2ae26ed0d64666db42477fa686c56afe2f7 100644 (file)
@@ -23,3 +23,6 @@ obj/lib/lfs-test: obj/lib/lfs-test.o obj/lib/libsh.a
 obj/lib/regex-test: obj/lib/regex-test.o obj/lib/libsh.a
 obj/lib/hash-test: obj/lib/hash-test.o obj/lib/libsh.a
 obj/lib/str-test: obj/lib/str-test.o obj/lib/libsh.a
+
+include lib/perl/Makefile
+include lib/shell/Makefile
diff --git a/lib/shell/Makefile b/lib/shell/Makefile
new file mode 100644 (file)
index 0000000..14462f4
--- /dev/null
@@ -0,0 +1,7 @@
+# Support routines for shell scripts
+
+DIRS+=lib/shell
+PROGS+=obj/lib/shell/config obj/lib/shell/logger
+
+obj/lib/shell/config: obj/lib/shell/config.o obj/lib/libsh.a
+obj/lib/shell/logger: obj/lib/shell/logger.o obj/lib/libsh.a
diff --git a/lib/shell/config.c b/lib/shell/config.c
new file mode 100644 (file)
index 0000000..f0fd32c
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ *     Sherlock Library -- Shell Interface to Configuration Files
+ *
+ *     (c) 2002 Martin Mares <mj@ucw.cz>
+ *
+ *     Once we were using this beautiful Shell version, but it turned out
+ *     that it doesn't work with nested config files:
+ *
+ *             eval `sed <cf/sherlock '/^#/d;/^ *$/d;s/ \+$//;
+ *             h;s@[^  ]*@@;x;s@[      ].*@@;y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/;G;s/\n//;
+ *             /^\[SECTION\]/,/^\[/ {; /^[A-Z]/ { s/^\([^      ]\+\)[  ]*\(.*\)$/SH_\1="\2"/; p; }; };
+ *             d;'`
+ */
+
+#include "lib/lib.h"
+#include "lib/conf.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+static void
+help(void)
+{
+  die("Usage: config [-C<configfile>] [-S<section>.<option>=<value>] <section> <item>[=<default>] <item2> ... [*]");
+}
+
+int main(int argc, char **argv)
+{
+  int i = 1;
+  int start;
+  struct cfitem *items, *c;
+  byte **vars;
+
+  log_init("config");
+  while (i < argc && argv[i][0] == '-')
+    i++;
+  if (i + 1 >= argc)
+    help();
+  start = i;
+  c = items = alloca(sizeof(struct cfitem) * (argc-i+1));
+  vars = alloca(sizeof(byte *) * argc);
+  c->name = argv[i];
+  c->type = CT_SECTION;
+  c->var = NULL;
+  c++;
+  while (++i < argc)
+    {
+      if (!strcmp(argv[i], "*"))
+       items->type = CT_INCOMPLETE_SECTION;
+      else
+       {
+         byte *e = strchr(argv[i], '=');
+         if (e)
+           *e++ = 0;
+         else
+           e = "";
+         c->name = argv[i];
+         c->type = CT_STRING;
+         c->var = &vars[i];
+         *(byte **)c->var = e;
+         c++;
+       }
+    }
+  c->type = CT_STOP;
+  cf_register(items);
+  if (cf_getopt(start, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
+    help();
+
+  c = items+1;
+  while (c->type)
+    {
+      printf("CF_%s=\"%s\"\n", c->name, *(byte **)c->var);
+      c++;
+    }
+
+  return 0;
+}
diff --git a/lib/shell/logger.c b/lib/shell/logger.c
new file mode 100644 (file)
index 0000000..52b901a
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ *     Sherlock Utilities -- A Simple Logger for use in shell scripts
+ *
+ *     (c) 2001 Martin Mares <mj@ucw.cz>
+ */
+
+#include "lib/lib.h"
+
+#include <stdio.h>
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+  byte buf[1024], *c;
+
+  log_init("logger");
+  if (argc < 3 || argc > 4 || strlen(argv[2]) != 1)
+    die("Usage: logger [<logname>:]<progname> <level> [<text>]");
+  if (c = strchr(argv[1], ':'))
+    {
+      *c++ = 0;
+      log_init(c);
+      log_file(argv[1]);
+    }
+  else
+    log_init(argv[1]);
+  if (argc > 3)
+    log(argv[2][0], argv[3]);
+  else
+    while (fgets(buf, sizeof(buf), stdin))
+      {
+       c = strchr(buf, '\n');
+       if (c)
+         *c = 0;
+       log(argv[2][0], buf);
+      }
+  return 0;
+}