]> andersk Git - moira.git/blobdiff - clients/mrtest/mrtest.c
Three bugfixes and a feature
[moira.git] / clients / mrtest / mrtest.c
index cdeb77a07a9b8956b3cfcd12b36e276b4c8faeca..597784f53949013fab135e196045fee444dc94da 100644 (file)
@@ -4,6 +4,8 @@
  *     $Header$
  *
  *     Copyright (C) 1987 by the Massachusetts Institute of Technology
+ *     For copying and distribution information, please see the file
+ *     <mit-copyright.h>.
  *
  */
 
 static char *rcsid_test_c = "$Header$";
 #endif lint
 
+#include <mit-copyright.h>
 #include <stdio.h>
+#include <sys/types.h>
 #include <sys/file.h>
+#include <fcntl.h>
 #include <ctype.h>
-#include <sms.h>
-#include <ss.h>
+#include <string.h>
+#include <moira.h>
+#include <com_err.h>
+#include <setjmp.h>
+#include <signal.h>
+
+#ifdef USE_READLINE
+#include "readline.h"
+#endif
 
-int ss;
 int recursion = 0;
-extern ss_request_table sms_test;
+extern int errno;
 extern int sending_version_no;
+int count, quit=0;
+char *whoami;
+jmp_buf jb;
+
+#define MAXARGS 20
+
+void discard_input(int, int, struct sigcontext *);
+char *mr_gets(char *, char *, size_t);
 
-#ifndef __SABER__
 main(argc, argv)
        int argc;
        char **argv;
-#else __SABER__
-sms()
-#endif __SABER__
 {      
        int status;
-       char *whoami;
+       char cmdbuf[BUFSIZ];
+#ifdef POSIX
+       struct sigaction action;
+#endif
        
-#ifndef __SABER__
        whoami = argv[0];
-#else
-       whoami = "sms";
-#endif __SABER__
        
-       init_ss_err_tbl();
-       init_sms_err_tbl();
-       init_krb_err_tbl();
-
-       ss = ss_create_invocation("sms", "2.0", (char *)NULL,
-                                 &sms_test, &status);
-       if (status != 0) {
-               com_err(whoami, status, "Unable to create invocation");
-               exit(1);
+       initialize_sms_error_table();
+       initialize_krb_error_table();
+
+#ifdef POSIX
+       action.sa_handler = discard_input;
+       action.sa_flags = 0;
+       sigemptyset(&action.sa_mask);
+       sigaction(SIGINT, &action, NULL);
+#else
+       signal(SIGINT, discard_input);
+#endif
+       setjmp(jb);
+
+       while(!quit) {
+               if(!mr_gets("moira:  ",cmdbuf,BUFSIZ)) break;
+               execute_line(cmdbuf);
+       }
+       mr_disconnect();
+       exit(0);
+}
+
+void discard_input(int sig, int code, struct sigcontext *scp)
+{
+  putc('\n', stdout);
+  longjmp(jb, 1);
+}
+
+char *mr_gets(char *prompt, char *buf, size_t len)
+{
+  char *in;
+#ifdef USE_READLINE
+  if(isatty(0)) {
+    in=readline(prompt);
+    
+    if (!in) return NULL;
+    if (*in) {
+      add_history(in);
+      strncpy(buf, in, len-1);
+      buf[len]=0;
+    }
+    
+    return buf;
+  }
+#endif
+  printf("%s", prompt);
+  fflush(stdout);
+  in=fgets(buf, len, stdin);
+  if(!in) return in;
+  if(strchr(buf,'\n')) *(strchr(buf,'\n'))=0;
+  return buf;
+}
+
+execute_line(cmdbuf)
+     char *cmdbuf;
+{
+  int argc;
+  char *argv[MAXARGS];
+
+  argc=parse(cmdbuf, argv);
+  if(argc==0) return;
+  if(!strcmp(argv[0],"noop"))
+    test_noop();
+  else if(!strcmp(argv[0],"connect") || !strcmp(argv[0],"c"))
+    test_connect(argc, argv);
+  else if(!strcmp(argv[0],"disconnect") || !strcmp(argv[0],"d"))
+    test_disconnect();
+  else if(!strcmp(argv[0],"host"))
+    test_host();
+  else if(!strcmp(argv[0],"new") || !strcmp(argv[0],"2"))
+    test_new();
+  else if(!strcmp(argv[0],"old") || !strcmp(argv[0],"1"))
+    test_old();
+  else if(!strcmp(argv[0],"motd"))
+    test_motd();
+  else if(!strcmp(argv[0],"query") || !strcmp(argv[0],"qy"))
+    test_query(argc, argv);
+  else if(!strcmp(argv[0],"auth") || !strcmp(argv[0],"a"))
+    test_auth(argc, argv);
+  else if(!strcmp(argv[0],"access"))
+    test_access(argc, argv);
+  else if(!strcmp(argv[0],"dcm"))
+    test_dcm();
+  else if(!strcmp(argv[0],"script") || !strcmp(argv[0],"s"))
+    test_script(argc, argv);
+  else if(!strcmp(argv[0],"list_requests") ||
+         !strcmp(argv[0],"lr") || !strcmp(argv[0],"?"))
+    test_list_requests();
+  else if(!strcmp(argv[0],"quit") || !strcmp(argv[0],"Q"))
+    quit=1;
+  else fprintf(stderr, "moira: Unknown request \"%s\".  Type \"?\" for a request list.\n", argv[0]);
+}
+
+int
+parse(buf, argv)
+     char *buf, *argv[MAXARGS];
+{
+  char *p;
+  int argc, num;
+  
+  if(!*buf) return 0;
+
+  for(p=buf, argc=0, argv[0]=buf; *p && *p!='\n'; p++) {
+    if(*p=='"') {
+      char *d=p++;
+      /* skip to close-quote, copying back over open-quote */
+      while(*p!='"') {
+       if(!*p || *p=='\n') {
+         fprintf(stderr, "moira: Unbalanced quotes in command line\n");
+         return 0;
        }
-       ss_listen(ss, &status);
-       if (status != 0) {
-               com_err(whoami, status, 0);
-               exit(1);
+       if(*p=='\\') {
+         if(*++p!='"' && (*p<'0' || *p>'9') && (*p!='\\')) {
+           fprintf(stderr, "moira: Bad use of \\\n");
+           return 0;
+         } else if (*p>='0' && *p<='9') {
+           num=(*p-'0')*64 + (*++p-'0')*8 + (*++p-'0');
+           *p=num;
+         }
        }
+       *d++=*p++;
+      }
+      if(p==d+1) {*d='\0'; p++;}
+      else while(p>=d) *p--=' ';
+    }
+    if(*p==' ' || *p=='\t') {
+      /* skip whitespace */
+      for(*p++='\0'; *p==' ' || *p=='\t'; p++);
+      if(*p && *p!='\n') argv[++argc]=p--;
+    }
+  }
+  if(*p=='\n') *p='\0';
+  return argc+1;
 }
 
 test_noop()
 {
-       int status = sms_noop();
-       if (status) ss_perror(ss, status, 0);
+       int status = mr_noop();
+       if (status) com_err("moira (noop)", status, "");
 }
 
 test_new()
 {
-       sending_version_no = SMS_VERSION_2;
+       sending_version_no = MR_VERSION_2;
 }
 
 test_old()
 {
-       sending_version_no = SMS_VERSION_1;
+       sending_version_no = MR_VERSION_1;
 }
 
-test_connect()
+test_connect(argc, argv)
+int argc;
+char *argv[];
 {
-       int status = sms_connect();
-       if (status) ss_perror(ss, status, 0);
+       char *server = "";
+       int status;
+
+       if (argc > 1) {
+           server = argv[1];
+       }
+       status = mr_connect(server);
+       if (status) com_err("moira (connect)", status, "");
 }
 
 test_disconnect()
 {
-       int status = sms_disconnect();
-       if (status) ss_perror(ss, status, 0);
+       int status = mr_disconnect();
+       if (status) com_err("moira (disconnect)", status, "");
 }
 
-test_auth()
+test_host()
+{
+        char host[BUFSIZ];
+        int status;
+
+        memset(host, 0, sizeof(host));
+
+       if (status = mr_host(host, sizeof(host) - 1))
+           com_err("moira (host)", status, "");
+       else
+           printf("You are connected to host %s\n", host);
+}
+
+test_auth(argc, argv)
+int argc;
+char *argv[];
 {
        int status;
 
-       status = sms_auth("smstest");
-       if (status) ss_perror(ss, status, 0);
+       status = mr_auth("mrtest");
+       if (status) com_err("moira (auth)", status, "");
 }
 
 test_script(argc, argv)
@@ -97,22 +251,23 @@ int argc;
 char *argv[];
 {
     FILE *inp;
-    char input[BUFSIZ], *cp, *index();
+    char input[BUFSIZ], *cp;
     int status, oldstdout, oldstderr;
 
     if (recursion > 8) {
-       ss_perror(ss, 0, "too many levels deep in script files\n");
+       com_err("moira (script)", 0, "too many levels deep in script files\n");
        return;
     }
 
     if (argc < 2) {
-       ss_perror(ss, 0, "Usage: script input_file [ output_file ]");
+       com_err("moira (script)", 0, "Usage: script input_file [ output_file ]");
        return;
     }
 
     inp = fopen(argv[1], "r");
     if (inp == NULL) {
-       ss_perror(ss, 0, "Cannot open input file %s", argv[1]);
+       sprintf(input, "Cannot open input file %s", argv[1]);
+       com_err("moira (script)", 0, input);
        return;
     }
 
@@ -126,7 +281,8 @@ char *argv[];
            close(status);
            dup2(oldstdout, 1);
            argc = 2;
-           ss_perror(ss, errno, "Unable to redirect output to %s\n", argv[2]);
+           sprintf(input, "Unable to redirect output to %s\n", argv[2]);
+           com_err("moira (script)", errno, input);
        } else {
            fflush(stderr);
            oldstderr = dup(2);
@@ -140,7 +296,7 @@ char *argv[];
     for(;;) {
        if (fgets(input, BUFSIZ, inp) == NULL)
          break;
-       if ((cp = index(input, '\n')) != (char *)NULL)
+       if ((cp = strchr(input, '\n')) != (char *)NULL)
          *cp = 0;
        if (input[0] == 0) {
            printf("\n");
@@ -152,10 +308,7 @@ char *argv[];
            continue;
        }
        printf("Executing: %s\n", input);
-       ss_execute_line(ss, input, &status);
-       if (status == SS_ET_COMMAND_NOT_FOUND) {
-           printf("Bad command: %s\n", input);
-       }
+       execute_line(input);
     }
 
     recursion--;
@@ -173,48 +326,6 @@ char *argv[];
     }
 }
 
-char *concat(str1, str2)
-       char *str1, *str2;
-{
-       char *rtn;
-       extern char *malloc();
-       
-       if (!str1) {
-               int len = strlen(str2) + 1 ;
-               rtn = malloc(len);
-               bcopy(str2, rtn, len);
-       } else {
-               int len1 = strlen(str1);
-               int len2 = strlen(str2) + 1;
-               rtn = malloc(len1+len2);
-               bcopy(str1, rtn, len1);
-               bcopy(str2, rtn+len1, len2);
-       }
-       return rtn;
-}
-
-test_shutdown(argc, argv)
-       int argc;
-       char **argv;
-{
-       char *reason = NULL;
-       int status, i;
-       
-       if (argc < 2) {
-               ss_perror(ss, 0, "Usage: shutdown reason ...");
-               return;
-       }
-       
-       for (i = 1 ; i < argc; i++) {
-               if (i != 1) reason = concat(reason, " ");
-               reason = concat(reason, argv[i]);
-       }
-       status = sms_shutdown(reason);
-       if (status) ss_perror(ss, status, 0);
-}
-static int count;
-
-
 print_reply(argc, argv)
        int argc;
        char **argv;
@@ -226,6 +337,7 @@ print_reply(argc, argv)
        }
        printf("\n");
        count++;
+       return(MR_CONT);
 }
 
 test_query(argc, argv)
@@ -234,14 +346,14 @@ test_query(argc, argv)
 {
        int status;
        if (argc < 2) {
-               ss_perror(ss, 0, "Usage: query handle [ args ... ]");
+               com_err("moira (query)", 0, "Usage: query handle [ args ... ]");
                return;
        }
 
        count = 0;
-       status = sms_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL);
+       status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL);
        printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
-       if (status) ss_perror(ss, status, 0);
+       if (status) com_err("moira (query)", status, "");
 }
 
 test_access(argc, argv)
@@ -250,9 +362,56 @@ test_access(argc, argv)
 {
        int status;
        if (argc < 2) {
-               ss_perror(ss, 0, "Usage: access handle [ args ... ]");
+               com_err("moira (access)", 0, "Usage: access handle [ args ... ]");
                return;
        }
-       status = sms_access(argv[1], argc-2, argv+2);
-       if (status) ss_perror(ss, status, 0);
+       status = mr_access(argv[1], argc-2, argv+2);
+       if (status) com_err("moira (access)", status, "");
+}
+
+
+test_dcm(argc, argv)
+       int argc;
+       char **argv;
+{
+       int status;
+
+       if (status = mr_do_update())
+         com_err("moira (dcm)", status, " while triggering dcm");
+}
+
+
+test_motd(argc, argv)
+       int argc;
+       char **argv;
+{
+       int status;
+       char *motd;
+
+       if (status = mr_motd(&motd))
+         com_err("moira (motd)", status, " while getting motd");
+       if (motd)
+         printf("%s\n", motd);
+       else
+         printf("No message of the day.\n");
+}
+
+test_list_requests()
+{
+       printf("Available moira requests:\n");
+       printf("\n");
+       printf("noop\t\t\tAsk Moira to do nothing\n");
+       printf("connect, c\t\tConnect to Moira server\n");
+       printf("disconnect, d\t\tDisconnect from server\n");
+       printf("host\t\t\tIdentify the server host\n");
+       printf("new, 2\t\t\tUse new protocol\n");
+       printf("old, 1\t\t\tUse old protocol\n");
+       printf("motd, m\t\t\tGet the Message of the Day\n");
+       printf("query, qy\t\tMake a query.\n");
+       printf("auth, a\t\t\tAuthenticate to Moira.\n");
+       printf("access\t\t\tCheck access to a Moira query.\n");
+       printf("dcm\t\t\tTrigger the DCM\n");
+       printf("script, s\t\tRead commands from a script.\n");
+       printf("list_requests, lr, ?\tList available commands.\n");
+       printf("quit, Q\t\t\tLeave the subsystem.\n");
 }
This page took 0.124255 seconds and 4 git commands to generate.