Blame src/util/ss/invocation.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/*
Packit fd8b60
 * Copyright 2007 Massachusetts Institute of Technology.
Packit fd8b60
 * All Rights Reserved.
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may
Packit fd8b60
 *   require a specific license from the United States Government.
Packit fd8b60
 *   It is the responsibility of any person or organization contemplating
Packit fd8b60
 *   export to obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit fd8b60
 * distribute this software and its documentation for any purpose and
Packit fd8b60
 * without fee is hereby granted, provided that the above copyright
Packit fd8b60
 * notice appear in all copies and that both that copyright notice and
Packit fd8b60
 * this permission notice appear in supporting documentation, and that
Packit fd8b60
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit fd8b60
 * to distribution of the software without specific, written prior
Packit fd8b60
 * permission.  Furthermore if you modify this software you must label
Packit fd8b60
 * your software as modified software and not distribute it in such a
Packit fd8b60
 * fashion that it might be confused with the original M.I.T. software.
Packit fd8b60
 * M.I.T. makes no representations about the suitability of
Packit fd8b60
 * this software for any purpose.  It is provided "as is" without express
Packit fd8b60
 * or implied warranty.
Packit fd8b60
 */
Packit fd8b60
/*
Packit fd8b60
 * Copyright 1987, 1988 by MIT Student Information Processing Board
Packit fd8b60
 *
Packit fd8b60
 * For copyright information, see copyright.h.
Packit fd8b60
 */
Packit fd8b60
#include "ss_internal.h"
Packit fd8b60
#include "copyright.h"
Packit fd8b60
#define size    sizeof(ss_data *)
Packit fd8b60
Packit fd8b60
/* XXX The memory in _ss_table never gets freed up until program exit!
Packit fd8b60
   If you change the code to free it and stick a null pointer into
Packit fd8b60
   _ss_table[sci_idx], make sure you change the allocation routine to
Packit fd8b60
   not assume there are no null pointers in the middle of the
Packit fd8b60
   array.  */
Packit fd8b60
int ss_create_invocation(subsystem_name, version_string, info_ptr,
Packit fd8b60
                         request_table_ptr, code_ptr)
Packit fd8b60
    char *subsystem_name, *version_string;
Packit fd8b60
    char *info_ptr;
Packit fd8b60
    ss_request_table *request_table_ptr;
Packit fd8b60
    int *code_ptr;
Packit fd8b60
{
Packit fd8b60
    int sci_idx;
Packit fd8b60
    ss_data *new_table;
Packit fd8b60
    ss_data **table, **tmp;
Packit fd8b60
Packit fd8b60
    *code_ptr = 0;
Packit fd8b60
    table = _ss_table;
Packit fd8b60
    new_table = (ss_data *) malloc(sizeof(ss_data));
Packit fd8b60
    if (new_table == NULL) {
Packit fd8b60
        *code_ptr = errno;
Packit fd8b60
        return -1;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    if (table == (ss_data **) NULL) {
Packit fd8b60
        table = (ss_data **) malloc(2 * size);
Packit fd8b60
        if (table == NULL) {
Packit fd8b60
            *code_ptr = errno;
Packit fd8b60
            return -1;
Packit fd8b60
        }
Packit fd8b60
        table[0] = table[1] = (ss_data *)NULL;
Packit fd8b60
        _ss_table = table;
Packit fd8b60
    }
Packit fd8b60
    initialize_ss_error_table ();
Packit fd8b60
Packit fd8b60
    for (sci_idx = 1; table[sci_idx] != (ss_data *)NULL; sci_idx++)
Packit fd8b60
        ;
Packit fd8b60
    tmp = (ss_data **) realloc((char *)table,
Packit fd8b60
                               ((unsigned)sci_idx+2)*size);
Packit fd8b60
    if (tmp == NULL) {
Packit fd8b60
        *code_ptr = errno;
Packit fd8b60
        return 0;
Packit fd8b60
    }
Packit fd8b60
    _ss_table = table = tmp;
Packit fd8b60
    table[sci_idx+1] = (ss_data *) NULL;
Packit fd8b60
    table[sci_idx] = NULL;
Packit fd8b60
Packit fd8b60
    new_table->subsystem_name = subsystem_name;
Packit fd8b60
    new_table->subsystem_version = version_string;
Packit fd8b60
    new_table->argv = (char **)NULL;
Packit fd8b60
    new_table->current_request = (char *)NULL;
Packit fd8b60
    new_table->info_dirs = (char **)malloc(sizeof(char *));
Packit fd8b60
    if (new_table->info_dirs == NULL) {
Packit fd8b60
        *code_ptr = errno;
Packit fd8b60
        free(new_table);
Packit fd8b60
        return 0;
Packit fd8b60
    }
Packit fd8b60
    *new_table->info_dirs = (char *)NULL;
Packit fd8b60
    new_table->info_ptr = info_ptr;
Packit fd8b60
    if (asprintf(&new_table->prompt, "%s:  ", subsystem_name) < 0) {
Packit fd8b60
        *code_ptr = errno;
Packit fd8b60
        free(new_table->info_dirs);
Packit fd8b60
        free(new_table);
Packit fd8b60
        return 0;
Packit fd8b60
    }
Packit fd8b60
    new_table->abbrev_info = NULL;
Packit fd8b60
    new_table->flags.escape_disabled = 0;
Packit fd8b60
    new_table->flags.abbrevs_disabled = 0;
Packit fd8b60
    new_table->rqt_tables =
Packit fd8b60
        (ss_request_table **) calloc(2, sizeof(ss_request_table *));
Packit fd8b60
    if (new_table->rqt_tables == NULL) {
Packit fd8b60
        *code_ptr = errno;
Packit fd8b60
        free(new_table->prompt);
Packit fd8b60
        free(new_table->info_dirs);
Packit fd8b60
        free(new_table);
Packit fd8b60
        return 0;
Packit fd8b60
    }
Packit fd8b60
    *(new_table->rqt_tables) = request_table_ptr;
Packit fd8b60
    *(new_table->rqt_tables+1) = (ss_request_table *) NULL;
Packit fd8b60
    table[sci_idx] = new_table;
Packit fd8b60
    return(sci_idx);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
void
Packit fd8b60
ss_delete_invocation(sci_idx)
Packit fd8b60
    int sci_idx;
Packit fd8b60
{
Packit fd8b60
    ss_data *t;
Packit fd8b60
    int ignored_code;
Packit fd8b60
Packit fd8b60
    t = ss_info(sci_idx);
Packit fd8b60
    free(t->prompt);
Packit fd8b60
    free(t->rqt_tables);
Packit fd8b60
    while(t->info_dirs[0] != (char *)NULL)
Packit fd8b60
        ss_delete_info_dir(sci_idx, t->info_dirs[0], &ignored_code);
Packit fd8b60
    free(t->info_dirs);
Packit fd8b60
    free(t);
Packit fd8b60
}