
/* 
 * $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: mklanginfo.c 1.3 90/05/21 $@(#)mklanginfo.c	1.3 89/12/01"


/*
 * mklanginfo - Copies the source of the langinfo file to
 *	the correct place in the /lib/locale directory
 *
 *
 * SYNOPSIS:
 *	mklanginfo [file]
 *
 * DESCRIPTION:
 *	This program takes the file specified as an argument, and
 *	converts the input to this file into a file suitable for
 *	placement in the /lib/locale/xx/LANGINFO directory.
 *
 */

#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <nl_types.h>
#include <langinfo.h>
#include <locale.h>
#include <string.h>
#include <signal.h>

#ifdef CCS
#include "../../tools/compat.h"
#endif

#define	DEF_LANGINFO	"/LANGINFO"

char *name[MAXSTRMSG+1] = {
	"",
	"DAY_1",
	"DAY_2",
	"DAY_3",
	"DAY_4",
	"DAY_5",
	"DAY_6",
	"DAY_7",
	"ABDAY_1",
	"ABDAY_2",
	"ABDAY_3",
	"ABDAY_4",
	"ABDAY_5",
	"ABDAY_6",
	"ABDAY_7",
	"MON_1",
	"MON_2",
	"MON_3",
	"MON_4",
	"MON_5",
	"MON_6",
	"MON_7",
	"MON_8",
	"MON_9",
	"MON_10",
	"MON_11",
	"MON_12",
	"ABMON_1",
	"ABMON_2",
	"ABMON_3",
	"ABMON_4",
	"ABMON_5",
	"ABMON_6",
	"ABMON_7",
	"ABMON_8",
	"ABMON_9",
	"ABMON_10",
	"ABMON_11",
	"ABMON_12",
	"RADIXCHAR",
	"THOUSEP",
	"YESSTR",
	"NOSTR",
	"CRNCYSTR",
	"D_T_FMT",
	"D_FMT",
	"T_FMT",
	"AM_STR",
	"PM_STR"
};

int	readstd;			/* Process the standard input */
char 	s_val[20];
char	linebuf[256], *p = linebuf;	/* Current line in input file */
char	items[MAXSTRMSG+1][256];	/* Holds each langinfo item */
int	cnt;
int	cont = 0;			/* set if the description continues
					 * on another line */
int	range = 0;			/* set when the range character '-'
					 * follows the string */
char	*cmdname;			/* Save command name */
char	input[24];			/* Save input file name */

#ifndef	CCS
extern char *gettxt();
#endif	/* !CCS */

main(argc, argv)
int	argc;
char	**argv;
{
	int	clean();

	if (cmdname = strrchr(*argv, '/'))
		++cmdname;
	else
		cmdname = *argv;
	if ( (argc != 1)  && (argc != 2) )
		fprintf(stderr, gettxt("mklanginfo:1","Illegal arguments\n"));
	if ( argc == 1 || strcmp(argv[1], "-") == 0 )
		{
		readstd++;
		strcpy(input, "standard input");
		}
	else
		strcpy(input, argv[1]);
	if (signal(SIGINT, SIG_IGN) == SIG_DFL)
		signal(SIGINT, clean);
	if (!readstd && freopen(argv[1], "r", stdin) == NULL) {
		perror(argv[1]);
		 exit(1);
	};
	for (cnt=1; cnt <= MAXSTRMSG; cnt++)
		items[cnt][0] = '\0';
	process(argv[1]);
	exit(0);
}


/* Read line from the standard input (file) and check for correct
 * format */

process(fname)
char *fname;

{

	char	*token();
	register  struct  classname  *cnp;
	register char *c;
	char	ch;
	char s1[40], s2[128];
	char p1[40];
	int val, count;
	count = 0;
		if ( strcmp(input,"standard input") != 0) {
		strcpy(s2, fname);
		strcat(s2, ".out");
		freopen(s2, "w", stdout);
		};
	for (;;) {
		if (fgets(linebuf, sizeof linebuf, stdin) == NULL ) {
			if (ferror(stdin)) {
				perror("mklanginfo (stdin)");
				exit(1);	
				}
			break;	
	        	};
		count++;
		ch = linebuf[0];
		/*  comment line   */
		if ( !isspace(ch) && ch != '#') {
			cnt = sscanf(linebuf, "%s %*[^\"]%[\"]", s_val, p1);
			if (strcmp(p1, "\"\"") == 0) {
				fprintf(stderr, gettxt("mklanginfo:2","%s is= NULL\n"),  s_val ,val);
	
				/* Doing nothing here will imply a null 
				 * message being output later 
				 */
			}
			else {
				if (sscanf(linebuf, "%s %*[^\"]\"%[^\"]\n" , s_val, s1) != 2) {
					fprintf(stderr, gettxt("mklanginfo:3","Syntax error on line %d\n"), count);
					exit(1);
					}
			val = getval(s_val);
			if (val == -1) {
				fprintf(stderr,
				  gettxt("mklanginfo:5","Illegal Symbol %s\n"),
							s_val);
				exit(1);
				}
			strcpy( &items[val][0], s1);
			}
		}		
	}
	
	/*	Now output the strings to stdout */

	for (cnt=1; cnt <= MAXSTRMSG ; cnt++) {
		fprintf(stderr,gettxt("mklanginfo:4","%s is= %s\n"), name[cnt], &items[cnt][0]);
		fprintf(stdout,"%s", & items[cnt][0]);
		fputc('\000', stdout);
	}
}



clean()
{
	signal(SIGINT, SIG_IGN);
	exit(1);
}

int
getval ( s )
char    *s;
{
int val;

	if (strncmp(s,"DAY_",4) == 0) {

		val = atoi((&s[4]));
		if (val < 1  || val > 7)
			return -1;
		else
			return val;
	}

	if (strncmp(s,"ABDAY_",6) == 0) {

		val = atoi((&s[6]));
		if (val < 1  || val > 7)
			return -1;
		else
			return val + 7;
	}

	if (strncmp(s,"MON_",4) == 0) {

		val = atoi((&s[4]));
		if (val < 1  || val > 12)
			return -1;
		else
			return val + 14;
	}

	if (strncmp(s,"ABMON_",6) == 0) {

		val = atoi((&s[6]));
		if (val < 1  || val > 12)
			return -1;
		else
			return val + 26;
	}
	if (strncmp(s,"RADIXCHAR",9) == 0) {

		return 39;
	
	}
	if (strncmp(s,"THOUSEP",7) == 0) {

		return 40;
	
	}
	if (strncmp(s,"YESSTR",6) == 0) {

		return 41;
	
	}
	if (strncmp(s,"NOSTR",5) == 0) {

		return 42;
	
	}
	if (strncmp(s,"CRNCYSTR",8) == 0) {

		return 43;
	
	}
	if (strncmp(s,"D_T_FMT",7) == 0) {

		return 44;
	
	}
	if (strncmp(s,"D_FMT",5) == 0) {

		return 45;
	
	}
	if (strncmp(s,"T_FMT",5) == 0) {

		return 46;
	
	}
	if (strncmp(s,"AM_STR",5) == 0) {

		return 47;
	
	}
	if (strncmp(s,"PM_STR",5) == 0) {

		return 48;
	
	}

	if (isdigit(*s)) {

		val = atoi(s) ;
		if (val > 0 && val <= MAXSTRMSG)
			return val;
		else
			return -1;

	}

	return -1;

}
