/*******************************************************************************
* YORKTOWN ADVANCED SGML PARSER (YASP)                                         *
*         An SGML Parser conforming to International Standard ISO 8879 --      *
*         Standard Generalized Markup Language                                 *
*------------------------------------------------------------------------------*
* Written 1987 - 1993 by                                                       *
*         Pierre G. Richard                                                    *
*         IBM T.J. Watson Research Center                                      *
*         Yorktown Heights, New York                                           *
*         petrus@watson.ibm.com                                                *
*------------------------------------------------------------------------------*
* FUNCTION NAME    = YASP / Auxiliary module                                   *
* STATUS           = Version 0, Release 1, Level 27 and up                     *
* COMPONENT NAME   = genbnr.c                                                  *
* DESCRIPTIVE NAME = Generate the YASP banners                                 *
* DATE_WR          = 03/20/1989                                                *
* AUTHOR           = Pierre G. Richard - PETRUS at YKTVMX                      *
*                                                                              *
* FUNCTIONAL DESCRIPTION =                                                     *
*                                                                              *
* IMPLEMENTATION NOTE =                                                        *
*                                                                              *
* DEPENDENCIES =  (see Include list)                                           *
*                                                                              *
* IMPORT=                                                                      *
*                                                                              *
* EXPORT=                                                                      *
*                                                                              *
* CHANGE HISTORY =                                                             *
* Number Date     Inits    Change & Reason (brief)                             *
*      1 03/30/91 PETRUS   Reworked, BANNER.H now being used by other modules. *
*      2 07/24/92 PETRUS   sprintf does not return the length on Sun           *
*      3 08/09/96 ESPERT   NT platforms                                        *
*******************************************************************************/


#define BNR_NAME     "YASP"
#define BNR_TAIL     "-- IBM Corp. 1993"

/* Banner are different when the OCS/ODT's are not compatible */

#define BNR_PCDOS_SYSTEM     "/PCDOS"
#define BNR_OS2V2_SYSTEM     "/OS2V2"
#define BNR_IBM370_SYSTEM    "/IBM370"
#define BNR_AIX_SYSTEM       "/AIX"
#define BNR_NT_SYSTEM        "/NT"
#define BNR_UNKNOWN_SYSTEM   "/??????"

#define NUMBER_OF_DEFINED_SYSTEMS 5

static char P_ifdef_PCDOS[] =  "#if (defined(DOS_SYS) || defined(OS2V1_SYS))\n";
static char P_ifdef_OS2V2[] =  "#if (defined(OS2V2_SYS))\n";
static char P_ifdef_IBM370[] = "#if (defined(CMS_SYS) || defined(MVS_SYS))\n";
static char P_ifdef_AIX[] =    "#if (defined(AIX_SYS))\n";
static char P_ifdef_NT[] =     "#if (defined(NT_SYS))\n";

/*-------------+
|Include Files |
+-------------*/
#include "machine.h"
#include "portable.h"
#include "banner.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <locale.h>

static void check_length(int);

static char beg_of_prod[] =
"/*@ This file has been produced by GENBNR ----\
-------- do not change directly */\n\
#include \"machine.h\"\n#include \"portable.h\"\n#include \"yaspapi.h\"\n\
#include \"global.h\"\n#include \"yasp.h\"\n\n";

static char P_else[] = "\n#else\n";
static char P_endif[] = "#endif\n";

static char end_of_prod[] =
"/*@ End of production (GENBNR) ---------------\
------------------------------- */\n";

/*----------------------------------------------------------------------------*/
static void check_length(int i)
{
   if (i > 80) {
      printf("*** Error: Banner size (%d) is greater than 80",i);
      exit(4);
   }
}
/*----------------------------------------------------------------------------*/
int main(uint argc, uchar *argv[])
{
   time_t ltime;
   struct tm *tm_p;
   char *c_p, id[250]; /* Take enough ! Check no more than 80 */
   ulong yasp_rvl;
   int i, j;
   FILE *f_p;
   static char st1[] =
   "static struct { ulong rvl; char const str[%d]; } yasp_identifier =\n{\n\
   %luL,\n\
   \"\\%03o%s\"\n};\n YASP_API char const * const C_FCT PARSID = yasp_identifier.str;\n";
   static char st2[] = "uchar * _loadds P_FCT %s() \
{ return((uchar *)\"\\%03o%s\"); }\n";

   f_p = fopen((argc > 1) ? argv[1] : "banner.c","w");
   fprintf(f_p,beg_of_prod);

   setlocale(LC_ALL, "French");
   time(&ltime);
   tm_p = gmtime(&ltime);
   yasp_rvl = (YASP_RELEASE * 10000L) + (YASP_VERSION * 100) + YASP_LEVEL;

   for (j=0; j<= NUMBER_OF_DEFINED_SYSTEMS; ++j) {

      switch (j) {
      case 0:
         fprintf(f_p,P_ifdef_PCDOS);
         c_p = BNR_PCDOS_SYSTEM;
         break;
      case 1:
         fprintf(f_p,P_else);
         fprintf(f_p,P_ifdef_OS2V2);
         c_p = BNR_OS2V2_SYSTEM;
         break;
      case 2:
         fprintf(f_p,P_else);
         fprintf(f_p,P_ifdef_IBM370);
         c_p = BNR_IBM370_SYSTEM;
         break;
      case 3:
         fprintf(f_p,P_else);
         fprintf(f_p,P_ifdef_AIX);
         c_p = BNR_AIX_SYSTEM;
         break;
	  case 4:
		 fprintf(f_p,P_else);
		 fprintf(f_p,P_ifdef_NT);
		 c_p = BNR_NT_SYSTEM;
		 break;
      default:
         fprintf(f_p,P_else);
         c_p = BNR_UNKNOWN_SYSTEM;
         break;
      }

      sprintf(
         id,"%s V%d.%d%s %02d/%02d/%02d %02d:%02d:%02d GMT %s",
         BNR_NAME,YASP_VERSION,YASP_LEVEL,c_p,
         tm_p->tm_mon+1, tm_p->tm_mday, tm_p->tm_year,
         tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec,
         BNR_TAIL
      );
      i = strlen(id);
      fprintf(f_p,st1,i+2,yasp_rvl,i,id);
      check_length(i);

      sprintf(id,"%s OCS/V0.%d%s",BNR_NAME,YASP_OCS_VER,c_p);
      i = strlen(id);
      fprintf(f_p,st2,"bnr_ocs",i,id);
      check_length(i);

      /* A space is added here -V to avoid same lengths... */
      sprintf(id,"%s ODT/V0.%d%s ",BNR_NAME,YASP_ODT_VER,c_p);
      i = strlen(id);
      fprintf(f_p,st2,"bnr_odt",i,id);
      check_length(i);
   }

   for (j=0; j < NUMBER_OF_DEFINED_SYSTEMS; ++j) fprintf(f_p,P_endif);

   fprintf(f_p,end_of_prod);
   fclose(f_p);
   printf("%s succesfully created\n",(argc > 1) ? argv[1] : "banner.c");
   return(0);
}
/******************************************************************************/
