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