akaros/tests/csquery.c
<<
>>
Prefs
   1/* 
   2 * This file is part of the UCB release of Plan 9. It is subject to the license
   3 * terms in the LICENSE file found in the top-level directory of this
   4 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   5 * part of the UCB release of Plan 9, including this file, may be copied,
   6 * modified, propagated, or distributed except according to the terms contained
   7 * in the LICENSE file.
   8 */
   9#include <stdlib.h>
  10
  11#include <stdio.h>
  12#include <parlib/parlib.h>
  13#include <unistd.h>
  14#include <signal.h>
  15#include <fcntl.h>
  16#include <error.h>
  17#include <iplib/iplib.h>
  18#include <ndblib/ndb.h>
  19#include <fcall.h>
  20
  21char *server;
  22char *status;
  23int statusonly;
  24
  25void usage(void)
  26{
  27        fprintf(stderr, "CSQUERY:usage: ndb/csquery [/net/cs [addr...]]\n");
  28        fprintf(stderr, "CSQUERY:usage");
  29        exit(1);
  30}
  31
  32void query(char *addr)
  33{
  34        char buf[128];
  35        int fd, n;
  36        int amt;
  37
  38        fd = open(server, O_RDWR);
  39        if (fd < 0)
  40                error(1, 0, "cannot open %s: %r", server);
  41        amt = write(fd, addr, strlen(addr));
  42        if (amt != strlen(addr)) {
  43                printf("CSQUERY:Tried to write %d to fd %d, only wrote %d\n",
  44                       strlen(addr), fd, amt);
  45                if (!statusonly)
  46                        fprintf(stderr,
  47                                "CSQUERY:Writing request: translating %s: %r\n",
  48                                addr);
  49                status = "errors";
  50                close(fd);
  51                return;
  52        }
  53        if (!statusonly) {
  54                lseek(fd, 0, 0);
  55                while ((n = read(fd, buf, sizeof(buf) - 1)) > 0) {
  56                        buf[n] = 0;
  57                }
  58                printf("%s\n", buf);
  59        }
  60        close(fd);
  61}
  62
  63void main(int argc, char **argv)
  64{
  65        char p[512];
  66        int i;
  67
  68        argc--, argv++;
  69        while (argc) {
  70                if (argv[0][0] != '-')
  71                        break;
  72                switch (argv[0][1]) {
  73                        case 's':
  74                                statusonly = 1;
  75                                break;
  76                        default:
  77                                usage();
  78                }
  79                argc--, argv++;
  80        }
  81
  82        if (argc > 0)
  83                server = argv[0];
  84        else
  85                server = "/net/cs";
  86
  87        if (argc > 1) {
  88                for (i = 1; i < argc; i++)
  89                        query(argv[i]);
  90                exit(0);
  91        }
  92
  93        for (;;) {
  94                printf("CSQUERY:> ");
  95                i = 0;
  96                while (read(0, &p[i], 1) > 0){
  97                        /* Attempt to echo our input back to stdout */
  98                        write(1, &p[i], 1);
  99                        if (p[i] == '\n')
 100                                break;
 101                        i++;
 102                }
 103                if (i < 0)
 104                        break;
 105                p[i] = 0;
 106                if (i)
 107                        query(p);
 108        }
 109}
 110