2 * Listener loop for subsystem library libss.a.
7 * Copyright 1987, 1988 by MIT Student Information Processing Board
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose is hereby granted, provided that
11 * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
12 * advertising or publicity pertaining to distribution of the software
13 * without specific, written prior permission. M.I.T. and the
14 * M.I.T. S.I.P.B. make no representations about the suitability of
15 * this software for any purpose. It is provided "as is" without
16 * express or implied warranty.
19 #include "ss_internal.h"
23 #include <sys/param.h>
25 typedef void sigret_t;
27 static ss_data *current_info;
28 static jmp_buf listen_jmpb;
29 static sigret_t (*sig_cont)(int);
31 static sigret_t print_prompt(int sig __SS_ATTR((unused)))
33 if (current_info->redisplay)
34 (*current_info->redisplay)();
36 (void) fputs(current_info->prompt, stdout);
37 (void) fflush(stdout);
41 static sigret_t listen_int_handler(int sig __SS_ATTR((unused)))
44 signal(SIGINT, listen_int_handler);
45 longjmp(listen_jmpb, 1);
48 int ss_listen (int sci_idx)
52 sigret_t (*sig_int)(int), (*old_sig_cont)(int);
54 sigset_t omask, igmask;
57 ss_data *old_info = current_info;
60 current_info = info = ss_info(sci_idx);
61 sig_cont = (sigret_t (*)(int)) 0;
64 sigaddset(&igmask, SIGINT);
65 sigprocmask(SIG_BLOCK, &igmask, &omask);
66 memcpy(old_jmpb, listen_jmpb, sizeof(jmp_buf));
67 sig_int = signal(SIGINT, listen_int_handler);
69 sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
72 old_sig_cont = sig_cont;
73 sig_cont = signal(SIGCONT, print_prompt);
74 if (sig_cont == print_prompt)
75 sig_cont = old_sig_cont;
77 line = (*info->readline)(current_info->prompt);
80 if (fgets(input, BUFSIZ, stdin) == input)
89 (void) signal(SIGCONT, sig_cont);
93 cp = strchr(line, '\n');
99 (void) signal(SIGCONT, sig_cont);
100 if (info->add_history)
101 (*info->add_history)(line);
103 code = ss_execute_line (sci_idx, line);
104 if (code == SS_ET_COMMAND_NOT_FOUND) {
105 register char *c = line;
106 while (*c == ' ' || *c == '\t')
108 cp = strchr (c, ' ');
111 cp = strchr (c, '\t');
114 ss_error (sci_idx, 0,
115 "Unknown request \"%s\". Type \"?\" for a request list.",
123 (void) signal(SIGINT, sig_int);
124 memcpy(listen_jmpb, old_jmpb, sizeof(jmp_buf));
125 current_info = old_info;
129 void ss_abort_subsystem(int sci_idx, int code)
131 ss_info(sci_idx)->abort = 1;
132 ss_info(sci_idx)->exit_status = code;
136 void ss_quit(int argc __SS_ATTR((unused)),
137 const char * const *argv __SS_ATTR((unused)),
138 int sci_idx, pointer infop __SS_ATTR((unused)))
140 ss_abort_subsystem(sci_idx, 0);
144 #define get_request(tbl,idx) ((tbl) -> requests + (idx))
146 static char *cmd_generator(const char *text, int state)
149 static ss_request_table **rqtbl;
151 static char const * const * name;
152 ss_request_entry *request;
157 rqtbl = current_info->rqt_tables;
158 if (!rqtbl || !*rqtbl)
165 if (!name || !*name) {
166 request = get_request(*rqtbl, curr_rqt++);
167 name = request->command_names;
177 if (strncmp(*name, text, len) == 0) {
178 ret = malloc(strlen(*name)+1);
190 char **ss_rl_completion(const char *text, int start,
191 int end __SS_ATTR((unused)))
193 if ((start == 0) && current_info->rl_completion_matches)
194 return (*current_info->rl_completion_matches)
195 (text, cmd_generator);