test_ss.c revision 5299580c1ced39e7a6d7ac2717a3d6a3cab299b0
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 <unistd.h>
18#include <stdlib.h>
19#include <stdio.h>
20#ifdef HAVE_GETOPT_H
21#include <getopt.h>
22#endif
23#include <string.h>
24#include "ss.h"
25
26extern ss_request_table test_cmds;
27
28#define TRUE 1
29#define FALSE 0
30
31static char subsystem_name[] = "test_ss";
32static char version[] = "1.0";
33
34static int source_file(const char *cmd_file, int sci_idx)
35{
36	FILE		*f;
37	char		buf[256];
38	char		*cp;
39	int		exit_status = 0;
40	int		retval;
41	int 		noecho;
42
43	if (strcmp(cmd_file, "-") == 0)
44		f = stdin;
45	else {
46		f = fopen(cmd_file, "r");
47		if (!f) {
48			perror(cmd_file);
49			exit(1);
50		}
51	}
52	fflush(stdout);
53	fflush(stderr);
54	setbuf(stdout, NULL);
55	setbuf(stderr, NULL);
56	while (!feof(f)) {
57		if (fgets(buf, sizeof(buf), f) == NULL)
58			break;
59		if (buf[0] == '#')
60			continue;
61		noecho = 0;
62		if (buf[0] == '-') {
63			noecho = 1;
64			buf[0] = ' ';
65		}
66		cp = strchr(buf, '\n');
67		if (cp)
68			*cp = 0;
69		cp = strchr(buf, '\r');
70		if (cp)
71			*cp = 0;
72		if (!noecho)
73			printf("test_icount: %s\n", buf);
74		retval = ss_execute_line(sci_idx, buf);
75		if (retval) {
76			ss_perror(sci_idx, retval, buf);
77			exit_status++;
78		}
79	}
80	return exit_status;
81}
82
83int main(int argc, char **argv)
84{
85	int c, code;
86	char *request = (char *)NULL;
87	char		*cmd_file = 0;
88	int sci_idx;
89	int exit_status = 0;
90
91	while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
92		switch (c) {
93		case 'R':
94			request = optarg;
95			break;
96		case 'f':
97			cmd_file = optarg;
98			break;
99		default:
100			com_err(argv[0], 0, "Usage: test_ss [-R request] "
101				"[-f cmd_file]");
102			exit(1);
103		}
104	}
105
106	sci_idx = ss_create_invocation(subsystem_name, version,
107				       (char *)NULL, &test_cmds, &code);
108	if (code) {
109		ss_perror(sci_idx, code, "creating invocation");
110		exit(1);
111	}
112
113	(void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &code);
114	if (code) {
115		ss_perror (sci_idx, code, "adding standard requests");
116		exit (1);
117	}
118
119	printf("test_ss %s.  Type '?' for a list of commands.\n\n",
120	       version);
121
122	if (request) {
123		code = ss_execute_line(sci_idx, request);
124		if (code) {
125			ss_perror(sci_idx, code, request);
126			exit_status++;
127		}
128	} else if (cmd_file) {
129		exit_status = source_file(cmd_file, sci_idx);
130	} else {
131		ss_listen(sci_idx);
132	}
133
134	exit(exit_status);
135}
136
137
138void test_cmd (argc, argv)
139    int argc;
140    char **argv;
141{
142    printf("Hello, world!\n");
143    printf("Args: ");
144    while (++argv, --argc) {
145	printf("'%s'", *argv);
146	if (argc > 1)
147	    fputs(", ", stdout);
148    }
149    putchar ('\n');
150}
151