
/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/
#ident "$Header: load.c 1.1 90/01/19 $@(#)load.c	1.1 89/12/01"
#include <stdio.h>
#include "xsld.h"
#include "xsar.h"


extern char *gettxt();

char buf[BUFSIZ];

void
load_user_module(name)
  char *name;
{
  FILE *fd;
  MODULE mo_pos;
  char *ptr;
  long curoff;
  
  if ((fd = fopen(name, "r")) == NULL){
    perror(name);
    return;
  }
  
  mo_pos = add_module(name, null_lib);
  skip_hdrE1(fd);
  mo_msg_start_new(mo_pos, ftell(fd));
  mo_used_new(mo_pos, TRUE);

  /*
   * Seek symtab
   */
  curoff = ftell(fd);
  while ((ptr = fgets(buf, BUFSIZ, fd)) != 0){
    if (is_hdrE2()){
      mo_msg_end_new(mo_pos, curoff);
      break;
    }
    curoff = ftell(fd);
  }
  if (ptr == 0){
    fprintf(stderr, gettxt("xsld:15","%s: Cannot find symbol table\n"), name);
    fclose(fd);
    return;
  }
  load_module(fd, mo_pos, TRUE, TRUE);
  fclose(fd);
}

void
load_library(name)
  char *name;
{
  FILE *fd;
  MODULE mo_pos;
  LIB lib;
  long lastoff, curoff;
  char *ptr;
  
  if ((fd = fopen(name, "r")) == NULL){
    perror(name);
    return;
  }
  mo_mark_start();
  mo_pos = null_mo;
  lib = add_library(name);
  
  skip_hdrI1(fd);
  
  while ((ptr = fgets(buf, BUFSIZ, fd)) != NULL){
    if (is_hdrI2()){
      /*
       * Reached the symbol tables
       */
      if (!is_null_mo(mo_pos))
        mo_msg_end_new(mo_pos, lastoff);
      break;
    }
    if (is_msgI1()){
      /*
       * Found a module name
       */
      if (!is_null_mo(mo_pos))
        mo_msg_end_new(mo_pos, lastoff);
      buf[strlen(buf) - 1] = '\0';
      mo_pos = add_module(buf + len_msgI1, lib);
      mo_msg_start_new(mo_pos, ftell(fd));
    } else if (is_msgI2()){
      /*
       * Found the end of previous entry
       */
      lastoff = curoff;
      curoff = ftell(fd);
    } else
      curoff = ftell(fd);
  }
  if (ptr == 0){
    fprintf(stderr, gettxt("xsld:16","%s: Cannot find symbol tables\n"), name);
    return;
  }
  mo_mark_stop();
  for (mo_pos = mo_mark_begin() ; !is_null_mo(mo_pos) ; 
          mo_pos = mo_mark_next()){
    if (undef_count() == 0)
      break;
    sym_next1(fd);
    if (fgets(buf, BUFSIZ, fd) == NULL){
      fclose(fd);
      return;
    }
    load_module(fd, mo_pos, FALSE, FALSE);
  }
  fclose(fd);
}

void
load_module(fd, mo, used, to_eof)
  FILE *fd;
  MODULE mo;
  bool used, to_eof;
{
  mo_used_new(mo, used);
  
  while (fgets(buf, BUFSIZ, fd) != NULL){
    if (is_fct()){
      /*
       * Found function associated with module
       */
      buf[strlen(buf) -1] = '\0';
      mo_func_new(mo, buf + len_fct);
    } else if (!to_eof && is_symI2()){
      /*
       * Found end of symbol table
       */
      break;
    } else if (is_def()){
      /*
       * Found a defined symbol
       */
      buf[strlen(buf) - 1] = '\0';
      load_def(buf + len_def, mo, FALSE);
    } else if (is_init()){
       /*
        * Found an initial symbol
        */
      buf[strlen(buf) - 1] = '\0';
      load_def(buf + len_init, mo, TRUE);
    } else if(is_undef()){
      /*
       * Found an undefined symbol
       */
      buf[strlen(buf) - 1] = '\0';
      load_undef(buf + len_undef, mo, FALSE);
    } else if (is_uninit()){
      /*
       * Found an undefined init symbol
       */
      buf[strlen(buf) - 1] = '\0';
      load_undef(buf + len_uninit, mo, TRUE);
    }
  }
  load_to_be_used();
}
