/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccs_id_message_build[] = "@(#)message_build.c	1.10 10/20/92 06:11:32 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	7/17/90


PURPOSE:
	This program reads a file of messages and creates a header file and
	associated data structure to hold them.

IN:
	3 arguments:
		message file filename
		C filename
		H filename
	message file of messages in 4-line format:
		numeric representation (64)
		symbol (64)
		category (64)
		message text (512)

OUT:
	C file containing the data structures
	H file defining the symbols

	
MODIFICATION HISTORY:


*/

#include <stdio.h>
#define MESSAGE_NUMBER_SIZE	64
#define MESSAGE_SYMBOL_SIZE	64
#define MESSAGE_CATEGORY_SIZE	48
#define MESSAGE_REJECT_SIZE	48
#define MESSAGE_ABORT_SIZE	48
#define MESSAGE_TEXT_SIZE	512

main(argc,argv)

   int argc;
   char *argv[];

{
   FILE *f,*g,*h;
   char number[MESSAGE_NUMBER_SIZE],symbol[MESSAGE_SYMBOL_SIZE],
	category[MESSAGE_CATEGORY_SIZE],
	reject[MESSAGE_REJECT_SIZE], abort[MESSAGE_ABORT_SIZE],
	textin[MESSAGE_TEXT_SIZE],textout[MESSAGE_TEXT_SIZE];
   int count = 0;

   if (argc != 4)
   {
      perror("Can't make errors without 3 filenames\n");
      exit(1);
   }


   f = fopen(argv[1],"r");
   g = fopen(argv[2],"w");
   h = fopen(argv[3],"w");

   fprintf(h,"#ifndef MESSAGES_H\n");
   fprintf(h,"#define MESSAGES_H\n\n");
   fprintf(g,"#include <elros/exceptions.h>\n\n");
   fprintf(h,"#define __ROc_MESSAGE_TEXT_SIZE %d\n\n",MESSAGE_TEXT_SIZE);
   fprintf(g,"__RO_Message_table_Entry __RO_Messages[] = {\n");

   /* scan over lines until we have read sccs_id line */
   fgets(textin,MESSAGE_TEXT_SIZE,f);	/* pick off the sccs ID line */
   while (strncmp(textin,"sccs_id",6))
     fgets(textin,MESSAGE_TEXT_SIZE,f);	/* pick off the sccs ID line */

   while (
       fgets(number,MESSAGE_NUMBER_SIZE,f),
       fgets(symbol,MESSAGE_SYMBOL_SIZE,f),
       fgets(category,MESSAGE_CATEGORY_SIZE,f),
       fgets(reject,MESSAGE_REJECT_SIZE,f),
       fgets(abort,MESSAGE_ABORT_SIZE,f),
       fgets(textin,MESSAGE_TEXT_SIZE,f))
   {
      number[strlen(number)-1] = 0,
      symbol[strlen(symbol)-1] = 0,
      category[strlen(category)-1] = 0,
      reject[strlen(reject)-1] = 0,
      abort[strlen(abort)-1] = 0,
      textin[strlen(textin)-1] = 0;


/*      printf("%s\n%s\n%s\n\n",number,symbol,textin);*/
      sprintf(textout,"%%%s(%s) - %s",symbol,category,textin);
      fprintf(g,"/*%d*/\t{ %-30s, %-30s,\n",count,symbol,category);
      fprintf(g,"\t  %-30s, %-30s,\n",reject,abort);
      fprintf(g,"\t\"%s\\n\"},\n",textout);
      fprintf(h,"#define %-30s\t%s\n",symbol,number);
      count++;
   }
   fprintf(g,"};\n\n");
   fprintf(h,"#define __RO_MESSAGES_COUNT\t%d\n\n",count);
   fprintf(h,"extern __RO_Message_table_Entry __RO_Messages[];\n\n");
   fprintf(h,"#endif\n");
   printf("%d error messages processed from file '%s'\n",count,argv[1]);

   fclose(f);
   fclose(g);
   fclose(h);

   return(0);
}


