/*
 * |-----------------------------------------------------------|
 * | Copyright (c) 1990 MIPS Computer Systems, Inc.            |
 * | All Rights Reserved                                       |
 * |-----------------------------------------------------------|
 * |          Restricted Rights Legend                         |
 * | Use, duplication, or disclosure by the Government is      |
 * | subject to restrictions as set forth in                   |
 * | subparagraph (c)(1)(ii) of the Rights in Technical        |
 * | Data and Computer Software Clause of DFARS 52.227-7013.   |
 * |         MIPS Computer Systems, Inc.                       |
 * |         928 Arques Avenue                                 |
 * |         Sunnyvale, CA 94086                               |
 * |-----------------------------------------------------------|
 */
#ident	"$Header: gettxt.c,v 1.1.2.1 92/06/24 14:08:34 marker Exp $"

/*	Copyright (c) 1988 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.	*/
/*MNLS*/

#ident	"@(#)libgen:gettxt.c/1.02 1.0 AT&T-UP"

#include <ctype.h>
#include <string.h>
#include <locale.h>
#ifdef mips
#include <a.out.h>
#endif

#define	P_locale	"/svr3/usr/lib/locale/"
#define L_locale	(sizeof(P_locale))
#define MAXDB	6	/* maximum number of data bases per program */
#define DEF_LOCALE	"/svr3/usr/lib/locale/C/LC_MESSAGES/"
#define MESSAGES 	"/LC_MESSAGES/"
#define DB_NAME_LEN	15
#define NULL	0

extern	char	_cur_locale[][15];

extern  int	atoi();
extern  int	libattach();
extern	int	libdetach();

extern 	int	errno;

/* support multiple versions of a package */

char	*Msgdb = (char *)NULL;

static 	char	saved_locale[15] = "";
static  char	not_found[] = "Message not found!!\n";


static	struct	db_info {
	char	db_name[DB_NAME_LEN];
	int	addr;
#ifdef mips
	int	taddr;
#endif
} db_info[MAXDB];

static	int	db_count;   	/* number of currently accessible data bases */

char *
gettxt(msg_id, dflt_str)
char	*msg_id;
char	*dflt_str;
{
	char  name[DB_NAME_LEN];
	int   rec_num;
	char  pathname[128];
	char  buf[200];
	int   i;
	int   detach = 0;
	int   addr;
	char  *tokp;
	int   name_len;

	int   saveerrno;

#ifdef mips
	struct aouthdr *aouthdr;
#endif

	/* parse msg_id */

	if (((tokp = strchr(msg_id, ':')) == NULL) || *(tokp+1) == '\0')
		return(not_found);
	if ((name_len = (tokp - msg_id)) >= DB_NAME_LEN)
		return(not_found);
	if (name_len) {
		(void)strncpy(name, msg_id, name_len);
		name[name_len] = '\0';
	}
	else
		if (Msgdb) {
			/* assuming that Msgdb is <= to 14 characters */
			(void)strcpy(name, Msgdb);
		}
		else
			return(not_found);
	while (*++tokp)
		if (!isdigit(*tokp))
			return(not_found);
	rec_num = atoi(msg_id + name_len + 1);

	/* Has locale been changed? */

	if (strcmp(_cur_locale[LC_MESSAGES], saved_locale) == 0) {
		for(i = 0; i < db_count; i++)
			if(strcmp(db_info[i].db_name, name) == 0)
				break;
	}
	else {
		(void)strcpy(saved_locale, _cur_locale[LC_MESSAGES]);
		for(i = 0; i < db_count; i++) {
			saveerrno = errno;
			libdetach(db_info[i].addr);
			errno = saveerrno;
			(void)strcpy(db_info[i].db_name, "");
			detach++;
		}
		db_count = 0;
	}
	if (detach || i == db_count) {
		(void)strcpy(pathname, P_locale);
		(void)strcpy(&pathname[L_locale - 1], saved_locale);
		(void)strcat(pathname, MESSAGES);
		(void)strcat(pathname, name);
		if (db_count == MAXDB)
			return(not_found);
			saveerrno = errno;
		if ((addr = libattach(pathname, buf, 0)) == -1) {
			errno = saveerrno;
			(void)strcpy(pathname, DEF_LOCALE);
			(void)strcat(pathname, name);
			saveerrno = errno;
			if ((addr = libattach(pathname, buf, 0)) == -1) { 
			errno = saveerrno;
				if (*dflt_str == '\0')
					return(not_found);
				else
					return(dflt_str);
			}
			else {
				/* Change locale to C */
				/* detach libraries to maintain consistency */
				(void)strcpy(saved_locale, "C");
				for(i = 0; i < db_count; i++) {
					saveerrno = errno;
					libdetach(db_info[i].addr);
					errno = saveerrno;
					(void)strcpy(db_info[i].db_name, "");
				}
				db_count = 0;
			}
		}
		(void)strcpy(db_info[db_count].db_name, name);
		i = db_count;
#ifdef mips
		aouthdr = (struct aouthdr *)(addr + FILHSZ);
		db_info[db_count].taddr = addr + aouthdr->entry;
#endif
		db_info[db_count++].addr = addr;
	}
#ifndef mips
	if (rec_num == 0 || rec_num > *(int *)(db_info[i].addr))
#else
	if (rec_num == 0 || rec_num > *(int *)(db_info[i].taddr))
#endif
		return(not_found);
#ifndef mips
	return((char *)(*(int *)(db_info[i].addr + sizeof(int) * (rec_num)) + db_info[i].addr));
#else
	return((char *)(*(int *)(db_info[i].taddr + sizeof(int) * (rec_num)) + db_info[i].addr));
#endif
}
