Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1987, 1988 by MIT Student Information Processing Board |
| 3 | * |
Theodore Ts'o | 06cefee | 1999-10-23 01:16:22 +0000 | [diff] [blame] | 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. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 12 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 13 | |
| 14 | #ifdef HAS_STDLIB_H |
| 15 | #include <stdlib.h> |
| 16 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 17 | #include "ss_internal.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 18 | #define size sizeof(ss_data *) |
Theodore Ts'o | 3ae497e | 2003-03-16 06:26:25 -0500 | [diff] [blame] | 19 | #ifdef HAVE_DLOPEN |
| 20 | #include <dlfcn.h> |
| 21 | #endif |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 22 | #include <errno.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 24 | int 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) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 46 | if (table == NULL) { |
| 47 | *code_ptr = errno; |
| 48 | return 0; |
| 49 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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; |
Theodore Ts'o | 3ae497e | 2003-03-16 06:26:25 -0500 | [diff] [blame] | 74 | |
| 75 | new_table->readline_handle = 0; |
Theodore Ts'o | 06ef971 | 2009-11-12 17:54:45 -0500 | [diff] [blame] | 76 | new_table->readline_shutdown = 0; |
Theodore Ts'o | 3ae497e | 2003-03-16 06:26:25 -0500 | [diff] [blame] | 77 | new_table->readline = 0; |
| 78 | new_table->add_history = 0; |
| 79 | new_table->redisplay = 0; |
| 80 | new_table->rl_completion_matches = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 81 | _ss_table = table; |
Theodore Ts'o | 3ae497e | 2003-03-16 06:26:25 -0500 | [diff] [blame] | 82 | #if defined(HAVE_DLOPEN) && defined(SHARED_ELF_LIB) |
| 83 | ss_get_readline(sci_idx); |
| 84 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 85 | return(sci_idx); |
| 86 | } |
| 87 | |
| 88 | void |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 89 | ss_delete_invocation(int sci_idx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 90 | { |
| 91 | register ss_data *t; |
| 92 | int ignored_code; |
| 93 | |
| 94 | t = ss_info(sci_idx); |
| 95 | free(t->prompt); |
Theodore Ts'o | 91835c1 | 2003-03-30 22:26:13 -0500 | [diff] [blame] | 96 | free(t->rqt_tables); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 97 | while(t->info_dirs[0] != (char *)NULL) |
| 98 | ss_delete_info_dir(sci_idx, t->info_dirs[0], &ignored_code); |
Theodore Ts'o | 91835c1 | 2003-03-30 22:26:13 -0500 | [diff] [blame] | 99 | free(t->info_dirs); |
Theodore Ts'o | 3ae497e | 2003-03-16 06:26:25 -0500 | [diff] [blame] | 100 | #if defined(HAVE_DLOPEN) && defined(SHARED_ELF_LIB) |
| 101 | if (t->readline_shutdown) |
| 102 | (*t->readline_shutdown)(t); |
| 103 | #endif |
Theodore Ts'o | 91835c1 | 2003-03-30 22:26:13 -0500 | [diff] [blame] | 104 | free(t); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | } |