
/* 
 * $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: xsld.c 1.1 90/01/19 $@(#)xsld.c	1.1 89/12/01"
#include <stdio.h>
#include <sys/signal.h>
#include "xsld.h"

extern char *gettxt();

/*
 * Messages files link editor
 * 	- Determine which modules have to be loaded and in which order
 *	- Create the corresponding C module that calls the initial functions
 *	  with the actual set nr as parameter
 * Parameters:
 *  	-o : Output messages file name
 *	-c : output C file name
 *	-l<name> : system library name (cfr /bin/ld)
 *	-N : Language to be used
 *	-d : Description file name
 *	-i : Name of strings initialisation routine (default strings_init)
 *	-r : Name of strings replacement routine (default strings_replace)
 * Arguments:
 *	Users messages files or libraries (type determined by suffix)
 * Exit codes:
 * 	0 : ok
 *	1 : Some C files must be recompiled
 *	2 : Trying to use a 'strings_extracted' module without messages
 */

char *outmsg = OUTFILE;
char *outC = OUTC;
char *lang;
bool multi_def = FALSE;
bool bad_init = FALSE;
bool bad_undef = FALSE;
char *desc_file;
char *str_replace = STRINGS_REPLACE;
char *str_init = STRINGS_INIT;

main(argc, argv)
  int argc;
  char *argv[];
{
  register int i;
  char *name;
  int fatal();
  
  if (argc == 1){
    fprintf(stderr, gettxt("xsld:43","Usage: %s [options] [files|libraries] [system libs]\n"),
    	argv[0]);
    exit(1);
  }
  
  signal(SIGHUP, fatal);
  signal(SIGINT, fatal);
  signal(SIGQUIT, fatal);
  signal(SIGTERM, fatal);
  
  /*
   * Load the crt module
   */
  load_user_module(CRT_MESSAGES);
    
  /*
   * Scan arguments that are not library specifications
   */
  for (i = 1 ; i < argc ; i++){
    if (argv[i][0] == '-'){
      switch(argv[i][1]){
      case 'o':
        argv[i][0] = 1;
        outmsg = argv[++i];
        continue;
      case 'c':
        argv[i][0] = 1;
        outC = argv[++i];
        continue;
      case 'l':
        continue;
      case 'N':
        argv[i][0] = 1;
        lang = argv[++i];
        continue;
      case 'd':
        argv[i][0] = 1;
        desc_file = argv[++i];
        continue;
      case 'i':
        argv[i][0] = 1;
        str_init = argv[++i];
        continue;
      case 'r':
        argv[i][0] = 1;
        str_replace = argv[++i];
        continue;
      default:
        fprintf(stderr, gettxt("xsld:44","Unknown argument '%c'\n"), argv[i][1]);
        argv[i][0] = 0;
        continue;
      }
    } else {
      if (is_user_lib(argv[i]))
        continue;
      
      load_user_module(argv[i]);
    }
  }
  
  /*
   * Rescan arguments and load libraries if necessary
   */
  for (i = 1 ; i < argc && undef_count() > 0 ; i++){
    if (argv[i][0] <= ' '){
      /*
       * entry to be skipped
       */
      i += argv[i][0];
      continue;
    }
    if (argv[i][0] == '-' && argv[i][1] == 'l'){
      if ((name = system_lib(argv[i] + 2, lang)) != 0)
        load_library(name);
    } else {
      if (is_user_lib(argv[i]))
        load_library(argv[i]);
    }
  }

  /*
   * Scan the default system librairies
   */
  if (undef_count() > 0){
    if ((name = system_lib(AUTO_LIB, lang)) != 0)
      load_library(name);
  }
    
  if (multi_def)
    disp_multi_def();
  if (undef_count() > 0)
    disp_undefined();
  
  gen_out_files(!bad_init && !bad_undef);
  sym_close();
  exit(bad_init ? 1 : bad_undef ? 2 : 0);
}

fatal(){
  unlink(outC);
  unlink(outmsg);
  exit(1);
}
