invocation.c revision e0ed7404719a9ddd2ba427a80db5365c8bad18c0
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#ifdef HAS_STDLIB_H
15#include <stdlib.h>
16#endif
17#include "ss_internal.h"
18#define	size	sizeof(ss_data *)
19#ifdef HAVE_DLOPEN
20#include <dlfcn.h>
21#endif
22#include <errno.h>
23
24int ss_create_invocation(const char *subsystem_name, const char *version_string,
25			 void *info_ptr, ss_request_table *request_table_ptr,
26			 int *code_ptr)
27{
28	register int sci_idx;
29	register ss_data *new_table;
30	register ss_data **table;
31
32	*code_ptr = 0;
33	table = _ss_table;
34	new_table = (ss_data *) malloc(sizeof(ss_data));
35
36	if (table == (ss_data **) NULL) {
37		table = (ss_data **) malloc(2 * size);
38		table[0] = table[1] = (ss_data *)NULL;
39	}
40	initialize_ss_error_table ();
41
42	for (sci_idx = 1; table[sci_idx] != (ss_data *)NULL; sci_idx++)
43		;
44	table = (ss_data **) realloc((char *)table,
45				     ((unsigned)sci_idx+2)*size);
46	if (table == NULL) {
47		*code_ptr = errno;
48		return 0;
49	}
50	table[sci_idx+1] = (ss_data *) NULL;
51	table[sci_idx] = new_table;
52
53	new_table->subsystem_name = subsystem_name;
54	new_table->subsystem_version = version_string;
55	new_table->argv = (char **)NULL;
56	new_table->current_request = (char *)NULL;
57	new_table->info_dirs = (char **)malloc(sizeof(char *));
58	*new_table->info_dirs = (char *)NULL;
59	new_table->info_ptr = info_ptr;
60	new_table->prompt = malloc((unsigned)strlen(subsystem_name)+4);
61	strcpy(new_table->prompt, subsystem_name);
62	strcat(new_table->prompt, ":  ");
63#ifdef silly
64	new_table->abbrev_info = ss_abbrev_initialize("/etc/passwd", code_ptr);
65#else
66	new_table->abbrev_info = NULL;
67#endif
68	new_table->flags.escape_disabled = 0;
69	new_table->flags.abbrevs_disabled = 0;
70	new_table->rqt_tables =
71		(ss_request_table **) calloc(2, sizeof(ss_request_table *));
72	*(new_table->rqt_tables) = request_table_ptr;
73	*(new_table->rqt_tables+1) = (ss_request_table *) NULL;
74
75	new_table->readline_handle = 0;
76	new_table->readline_shutdown = 0;
77	new_table->readline = 0;
78	new_table->add_history = 0;
79	new_table->redisplay = 0;
80	new_table->rl_completion_matches = 0;
81	_ss_table = table;
82#if defined(HAVE_DLOPEN) && defined(SHARED_ELF_LIB)
83	ss_get_readline(sci_idx);
84#endif
85	return(sci_idx);
86}
87
88void
89ss_delete_invocation(int sci_idx)
90{
91	register ss_data *t;
92	int ignored_code;
93
94	t = ss_info(sci_idx);
95	free(t->prompt);
96	free(t->rqt_tables);
97	while(t->info_dirs[0] != (char *)NULL)
98		ss_delete_info_dir(sci_idx, t->info_dirs[0], &ignored_code);
99	free(t->info_dirs);
100#if defined(HAVE_DLOPEN) && defined(SHARED_ELF_LIB)
101	if (t->readline_shutdown)
102		(*t->readline_shutdown)(t);
103#endif
104	free(t);
105}
106