generate.lib.c
/****************************************************************************************************/
/*                                                                                                  */
/* generate.c:                                                                                      */
/*                                                                                                  */
/****************************************************************************************************/

/****************************************************************************************************/
/*                                                                                                  */
/*     Copyright (C) 2003, 2004 Joerg Kunze                                                         */
/*                                                                                                  */
/*     This file is part of siliconBrain.                                                           */
/*                                                                                                  */
/*     siliconBrain is free software; you can redistribute it and/or modify                         */
/*     it under the terms of the GNU General Public License as published by                         */
/*     the Free Software Foundation; either version 2 of the License, or                            */
/*     (at your option) any later version.                                                          */
/*                                                                                                  */
/*     siliconBrain is distributed in the hope that it will be useful,                              */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of                               */
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                */
/*     GNU General Public License for more details.                                                 */
/*                                                                                                  */
/*     You should have received a copy of the GNU General Public License                            */
/*     along with this program; if not, write to the Free Software                                  */
/*     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                    */
/*                                                                                                  */
/****************************************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>

#include "siliconBrainSpecification"

// static const char *siliconBrainRelease       = "$siliconBrainRelease: 0.2.3 $";
// static const char *siliconBrainRcsIdentifier = "$Id: generate.lib.c,v 1.19 2004/12/14 23:31:26 joerg Exp $";
// static const char *siliconBrainSaveStamp     = "$siliconBrainSaveStamp: 2004/12/14 22:29:48, Joerg Kunze$";

/****************************************************************************************************/
/*                                                                                                  */
/* formatHeader:                                                                                    */
/*                                                                                                  */
/****************************************************************************************************/
extern void formatHeader(
   const char          *comment,
   const Specification *specification,
   const Specification *generatorSpecification,
   const char          *versionFormat
) {
   char *fileName;
   FILE *commentSource;
   int   commentLength = 200;
   char *commentLine   = malloc( commentLength );

   /*-----------------------------------------------*/
   /* open the file, where we find the copying text */
   /*-----------------------------------------------*/
   asprintf( &fileName, "%s/data/copyingGeneratedFile.snippet.text", getenv( "siliconBrainPath" ) );

   commentSource = fopen( fileName, "r" );

   if( !commentSource ) {
      fprintf( stderr, "generate.lib.c: cannot open file \"%s\"\n", fileName );
      exit( 42 );
   }
   free( fileName );

   /*--------------------------------------------------*/
   /* copy the copying text into the source as comment */
   /*--------------------------------------------------*/
   while( -1 != getline( &commentLine, &commentLength, commentSource ) )
      printf( "%s%s%s", comment, commentLine[0] == '\n' ? "" : " ",  commentLine );

   free( commentLine );
   fclose( commentSource );

   /*---------------------------*/
   /* print version information */
   /*---------------------------*/
   time_t         currentTimeAsTimeT      = time( 0 );
   struct tm      currentTimeAsStructTm   ;
   char           currentTimeAsString     [ sizeof( "Tue May 21 13:46:22 1991\n" ) ];
   char          *currentWorkingDirectory = getcwd( 0, 0 );
   size_t         hostnameLength          = 421;
   char          *hostname                = malloc( hostnameLength );
   struct passwd *userInformation         ;

   if( asctime_r( gmtime_r( &currentTimeAsTimeT, &currentTimeAsStructTm ), currentTimeAsString ) )
      currentTimeAsString[ strlen( currentTimeAsString ) - 1 ] = 0;
   else
      strcpy( currentTimeAsString, "(null)" );

   while( -1 == gethostname( hostname, hostnameLength ) )
      if( ENAMETOOLONG == errno )
         hostname = realloc( hostname, hostnameLength <<= 1 );
      else {
         fprintf( stderr, "error in gethostname: %d\n", errno );
         exit( 42 );
      }

   userInformation = getpwuid( getuid() );

   printf(
      versionFormat ?:
         "\n%s %s\n"
         "\n"
         "%s generated at: %s\n"
         "%s in directory: %s\n"
         "%s on host     : %s\n"
         "%s by user     : %s, %s\n"
         "\n"
         "%s generated for specification:\n"
         "%s    %s\n"
         "%s    %s\n"
         "%s    %s\n"
         "\n"
         "%s generated with the following generator:\n"
         "%s    %s\n"
         "%s    %s\n"
         "%s    %s\n",
      comment, specification->title,
      comment, currentTimeAsString,
      comment, currentWorkingDirectory,
      comment, hostname,
      comment, userInformation->pw_name, userInformation->pw_gecos,
      comment,
      comment, specification->release,
      comment, specification->rcsIdentifier,
      comment, specification->saveStamp,
      comment,
      comment, generatorSpecification->release,
      comment, generatorSpecification->rcsIdentifier,
      comment, generatorSpecification->saveStamp
   );

   free( currentWorkingDirectory );
}

/*
$Log: generate.lib.c,v $
Revision 1.19  2004/12/14 23:31:26  joerg
published for new release 0.2.3

Revision 1.18  2004/12/14 23:17:05  joerg
published for new release 0.2.2

Revision 1.17  2004/12/14 22:42:22  joerg
allFiles: all sources have a Log CVS keyword at the end now.

*/