Whamcloud - gitweb
Convert use of ext2fs_get_mem to ext2fs_get_array for overflow detection
[tools/e2fsprogs.git] / lib / ss / test_ss.c
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
26 extern ss_request_table test_cmds;
27
28 #define TRUE 1
29 #define FALSE 0
30
31 static char subsystem_name[] = "test_ss";
32 static char version[] = "1.0";
33
34 static 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         setbuf(stdout, NULL);
53         setbuf(stderr, NULL);
54         while (!feof(f)) {
55                 if (fgets(buf, sizeof(buf), f) == NULL)
56                         break;
57                 if (buf[0] == '#')
58                         continue;
59                 noecho = 0;
60                 if (buf[0] == '-') {
61                         noecho = 1;
62                         buf[0] = ' ';
63                 }
64                 cp = strchr(buf, '\n');
65                 if (cp)
66                         *cp = 0;
67                 cp = strchr(buf, '\r');
68                 if (cp)
69                         *cp = 0;
70                 if (!noecho)
71                         printf("test_icount: %s\n", buf);
72                 retval = ss_execute_line(sci_idx, buf);
73                 if (retval) {
74                         ss_perror(sci_idx, retval, buf);
75                         exit_status++;
76                 }
77         }
78         return exit_status;
79 }
80
81 int main(int argc, char **argv)
82 {
83         int c, code;
84         char *request = (char *)NULL;
85         char            *cmd_file = 0;
86         int sci_idx;
87         int exit_status = 0;
88         const char      *usage = "Usage: test_ss [-R request] [-f cmd_file]";
89
90         while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
91                 switch (c) {
92                 case 'R':
93                         request = optarg;
94                         break;
95                 case 'f':
96                         cmd_file = optarg;
97                         break;
98                 default:
99                         com_err(argv[0], 0, usage);
100                         exit(1);
101                 }
102         }
103
104         sci_idx = ss_create_invocation(subsystem_name, version,
105                                        (char *)NULL, &test_cmds, &code);
106         if (code) {
107                 ss_perror(sci_idx, code, "creating invocation");
108                 exit(1);
109         }
110
111         (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &code);
112         if (code) {
113                 ss_perror (sci_idx, code, "adding standard requests");
114                 exit (1);
115         }
116
117         printf("test_ss %s.  Type '?' for a list of commands.\n\n",
118                version);
119
120         if (request) {
121                 code = ss_execute_line(sci_idx, request);
122                 if (code) {
123                         ss_perror(sci_idx, code, request);
124                         exit_status++;
125                 }
126         } else if (cmd_file) {
127                 exit_status = source_file(cmd_file, sci_idx);
128         } else {
129                 ss_listen(sci_idx);
130         }
131
132         exit(exit_status);
133 }
134
135
136 void test_cmd (argc, argv)
137     int argc;
138     char **argv;
139 {
140     printf("Hello, world!\n");
141     printf("Args: ");
142     while (++argv, --argc) {
143         printf("'%s'", *argv);
144         if (argc > 1)
145             fputs(", ", stdout);
146     }
147     putchar ('\n');
148 }