yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * test_ss.c |
| 3 | * |
| 4 | * Copyright 1987, 1988 by MIT Student Information Processing Board |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software and |
| 7 | * its documentation for any purpose is hereby granted, provided that |
| 8 | * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. M.I.T. and the |
| 11 | * M.I.T. S.I.P.B. make no representations about the suitability of |
| 12 | * this software for any purpose. It is provided "as is" without |
| 13 | * express or implied warranty. |
| 14 | |
| 15 | */ |
| 16 | |
| 17 | #include "config.h" |
| 18 | #include <unistd.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #ifdef HAVE_GETOPT_H |
| 22 | #include <getopt.h> |
| 23 | #endif |
| 24 | #include <string.h> |
| 25 | #include "ss.h" |
| 26 | |
| 27 | extern ss_request_table test_cmds; |
| 28 | |
| 29 | #define TRUE 1 |
| 30 | #define FALSE 0 |
| 31 | |
| 32 | static char subsystem_name[] = "test_ss"; |
| 33 | static char version[] = "1.0"; |
| 34 | |
| 35 | static int source_file(const char *cmd_file, int sci_idx) |
| 36 | { |
| 37 | FILE *f; |
| 38 | char buf[256]; |
| 39 | char *cp; |
| 40 | int exit_status = 0; |
| 41 | int retval; |
| 42 | int noecho; |
| 43 | |
| 44 | if (strcmp(cmd_file, "-") == 0) |
| 45 | f = stdin; |
| 46 | else { |
| 47 | f = fopen(cmd_file, "r"); |
| 48 | if (!f) { |
| 49 | perror(cmd_file); |
| 50 | exit(1); |
| 51 | } |
| 52 | } |
| 53 | fflush(stdout); |
| 54 | fflush(stderr); |
| 55 | setbuf(stdout, NULL); |
| 56 | setbuf(stderr, NULL); |
| 57 | while (!feof(f)) { |
| 58 | if (fgets(buf, sizeof(buf), f) == NULL) |
| 59 | break; |
| 60 | if (buf[0] == '#') |
| 61 | continue; |
| 62 | noecho = 0; |
| 63 | if (buf[0] == '-') { |
| 64 | noecho = 1; |
| 65 | buf[0] = ' '; |
| 66 | } |
| 67 | cp = strchr(buf, '\n'); |
| 68 | if (cp) |
| 69 | *cp = 0; |
| 70 | cp = strchr(buf, '\r'); |
| 71 | if (cp) |
| 72 | *cp = 0; |
| 73 | if (!noecho) |
| 74 | printf("test_icount: %s\n", buf); |
| 75 | retval = ss_execute_line(sci_idx, buf); |
| 76 | if (retval) { |
| 77 | ss_perror(sci_idx, retval, buf); |
| 78 | exit_status++; |
| 79 | } |
| 80 | } |
| 81 | return exit_status; |
| 82 | } |
| 83 | |
| 84 | int main(int argc, char **argv) |
| 85 | { |
| 86 | int c, code; |
| 87 | char *request = (char *)NULL; |
| 88 | char *cmd_file = 0; |
| 89 | int sci_idx; |
| 90 | int exit_status = 0; |
| 91 | |
| 92 | while ((c = getopt (argc, argv, "wR:f:")) != EOF) { |
| 93 | switch (c) { |
| 94 | case 'R': |
| 95 | request = optarg; |
| 96 | break; |
| 97 | case 'f': |
| 98 | cmd_file = optarg; |
| 99 | break; |
| 100 | default: |
| 101 | com_err(argv[0], 0, "Usage: test_ss [-R request] " |
| 102 | "[-f cmd_file]"); |
| 103 | exit(1); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | sci_idx = ss_create_invocation(subsystem_name, version, |
| 108 | (char *)NULL, &test_cmds, &code); |
| 109 | if (code) { |
| 110 | ss_perror(sci_idx, code, "creating invocation"); |
| 111 | exit(1); |
| 112 | } |
| 113 | |
| 114 | (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &code); |
| 115 | if (code) { |
| 116 | ss_perror (sci_idx, code, "adding standard requests"); |
| 117 | exit (1); |
| 118 | } |
| 119 | |
| 120 | printf("test_ss %s. Type '?' for a list of commands.\n\n", |
| 121 | version); |
| 122 | |
| 123 | if (request) { |
| 124 | code = ss_execute_line(sci_idx, request); |
| 125 | if (code) { |
| 126 | ss_perror(sci_idx, code, request); |
| 127 | exit_status++; |
| 128 | } |
| 129 | } else if (cmd_file) { |
| 130 | exit_status = source_file(cmd_file, sci_idx); |
| 131 | } else { |
| 132 | ss_listen(sci_idx); |
| 133 | } |
| 134 | |
| 135 | exit(exit_status); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void test_cmd (argc, argv) |
| 140 | int argc; |
| 141 | char **argv; |
| 142 | { |
| 143 | printf("Hello, world!\n"); |
| 144 | printf("Args: "); |
| 145 | while (++argv, --argc) { |
| 146 | printf("'%s'", *argv); |
| 147 | if (argc > 1) |
| 148 | fputs(", ", stdout); |
| 149 | } |
| 150 | putchar ('\n'); |
| 151 | } |