invocation.c revision f404167dda29a59d2be2882328aeb074b9899669
1/*
2 * Copyright 1987, 1988 by MIT Student Information Processing Board
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose is hereby granted, provided that
6 * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
7 * advertising or publicity pertaining to distribution of the software
8 * without specific, written prior permission.  M.I.T. and the
9 * M.I.T. S.I.P.B. make no representations about the suitability of
10 * this software for any purpose.  It is provided "as is" without
11 * express or implied warranty.
12 */
13
14#include "config.h"
15#ifdef HAS_STDLIB_H
16#include <stdlib.h>
17#endif
18#include "ss_internal.h"
19#define	size	sizeof(ss_data *)
20#ifdef HAVE_DLOPEN
21#include <dlfcn.h>
22#endif
23#include <errno.h>
24
25int ss_create_invocation(const char *subsystem_name, const char *version_string,
26			 void *info_ptr, ss_request_table *request_table_ptr,
27			 int *code_ptr)
28{
29	register int sci_idx;
30	register ss_data *new_table;
31	register ss_data **table;
32
33	*code_ptr = 0;
34	table = _ss_table;
35	new_table = (ss_data *) malloc(sizeof(ss_data));
36
37	if (table == (ss_data **) NULL) {
38		table = (ss_data **) malloc(2 * size);
39		table[0] = table[1] = (ss_data *)NULL;
40	}
41	initialize_ss_error_table ();
42
43	for (sci_idx = 1; table[sci_idx] != (ss_data *)NULL; sci_idx++)
44		;
45	table = (ss_data **) realloc((char *)table,
46				     ((unsigned)sci_idx+2)*size);
47	if (table == NULL) {
48		*code_ptr = errno;
49		return 0;
50	}
51	table[sci_idx+1] = (ss_data *) NULL;
52	table[sci_idx] = new_table;
53
54	new_table->subsystem_name = subsystem_name;
55	new_table->subsystem_version = version_string;
56	new_table->argv = (char **)NULL;
57	new_table->current_request = (char *)NULL;
58	new_table->info_dirs = (char **)malloc(sizeof(char *));
59	*new_table->info_dirs = (char *)NULL;
60	new_table->info_ptr = info_ptr;
61	new_table->prompt = malloc((unsigned)strlen(subsystem_name)+4);
62	strcpy(new_table->prompt, subsystem_name);
63	strcat(new_table->prompt, ":  ");
64#ifdef silly
65	new_table->abbrev_info = ss_abbrev_initialize("/etc/passwd", code_ptr);
66#else
67	new_table->abbrev_info = NULL;
68#endif
69	new_table->flags.escape_disabled = 0;
70	new_table->flags.abbrevs_disabled = 0;
71	new_table->rqt_tables =
72		(ss_request_table **) calloc(2, sizeof(ss_request_table *));
73	*(new_table->rqt_tables) = request_table_ptr;
74	*(new_table->rqt_tables+1) = (ss_request_table *) NULL;
75
76	new_table->readline_handle = 0;
77	new_table->readline_shutdown = 0;
78	new_table->readline = 0;
79	new_table->add_history = 0;
80	new_table->redisplay = 0;
81	new_table->rl_completion_matches = 0;
82	_ss_table = table;
83#if defined(HAVE_DLOPEN) && defined(SHARED_ELF_LIB)
84	ss_get_readline(sci_idx);
85#endif
86	return(sci_idx);
87}
88
89void
90ss_delete_invocation(int sci_idx)
91{
92	register ss_data *t;
93	int ignored_code;
94
95	t = ss_info(sci_idx);
96	free(t->prompt);
97	free(t->rqt_tables);
98	while(t->info_dirs[0] != (char *)NULL)
99		ss_delete_info_dir(sci_idx, t->info_dirs[0], &ignored_code);
100	free(t->info_dirs);
101#if defined(HAVE_DLOPEN) && defined(SHARED_ELF_LIB)
102	if (t->readline_shutdown)
103		(*t->readline_shutdown)(t);
104#endif
105	free(t);
106}
107