/*--------------------------------------------------------*/ /* page.c 08-aug-1997/bets */ /* */ /* 30-jul-1998/bets added stuff for travis' preprocessor */ /*--------------------------------------------------------*/ #include #include #include #include #include #include "page.h" #define BLKSIZE 100 /*--------------------------------------------------------*/ /* page_begin */ /*--------------------------------------------------------*/ void page_begin( char *title ) { fprintf( stdout,"Content-type: text/html\n\n" ); if ( title == NULL ) { fprintf( stdout,"\n" ); } else { fprintf( stdout, "%s\n", title ); } } /* end of page_begin() */ /*--------------------------------------------------------*/ /* page_end */ /*--------------------------------------------------------*/ void page_end() { fprintf( stdout,"\n" ); } /* end of page_end() */ /*--------------------------------------------------------*/ /* page_error */ /*--------------------------------------------------------*/ void page_error( char *fmt, ... ) { va_list args; fprintf( stdout,"Content-type: text/plain\n\n" ); va_start( args,fmt ); vfprintf( stdout,fmt,args ); va_end( args ); exit( 1 ); } /* end of page_error() */ /*--------------------------------------------------------*/ /* page_preprocessor Jul 14 98 phase */ /* */ /* 30-jul-1998/bets generalized, integrated into tools */ /* */ /* This function opens an HTML file and outputs it to */ /* stdout. When special keywords are found, denoted by */ /* KEYWORD_CHAR's on both sides of the reference, */ /* they are replaced with the actual values passed to */ /* this routine. */ /* */ /* For example, if KEYWORD_CHAR is '#', then #user# gets */ /* replaced. */ /* */ /* Note that the HTML file may have a KEYWORD_CHAR as part*/ /* of its content and not surrounding a keyword. In case */ /* this happens, if a blank is read before a second */ /* KEYWORD_CHAR, then the keyword is abandoned. */ /* */ /* Note that this routine does not start or end a page, */ /* so the calling routine should do so before and after */ /* this routine is invoked. */ /* */ /* If an error is encountered, a message is output to */ /* stdout in html style and the routine returns 0. */ /* If all is well, the routine returns 1. */ /*--------------------------------------------------------*/ int page_preprocessor( char *htmlfile, int npairs, PAIR *pairs ) { char *keyword; int keysize; FILE *in; int i, state; unsigned int k; /* open html file for input */ in = fopen( htmlfile,"r" ); /* trap file i/o error */ if ( in == NULL ) { fprintf( stdout,"PagePreprocessorException: error occurred reading file [%s] ",htmlfile ); return( 0 ); } /* initialize */ if (( keyword = (char *)malloc( BLKSIZE * sizeof( char ))) == NULL ) { fprintf( stdout,"PagePreprocessorException: out of memory" ); return( 0 ); } keysize = BLKSIZE; /* read in html file */ state = 0; while (( k = fgetc( in )) != EOF ) { switch ( state ) { case 0: /* at start of reading keyword */ if ( k == KEYWORD_CHAR ) { i = 0; state = 1; } else { fputc( k,stdout ); } break; case 1: /* in middle of reading keyword */ if ( k == KEYWORD_CHAR ) { /* end of keyword found */ keyword[i] = '\0'; /* substitute for keyword */ fprintf( stdout,"%s",page_substitute( keyword, npairs, pairs )); /* blank out old value of keyword */ strcpy( keyword,"" ); i = 0; state = 0; } else if ( k == ' ' ) { /* blank means this isn't really a keyword; the character was used for something else */ /* output text collected so far */ keyword[i] = '\0'; fprintf( stdout,"%c%s ",KEYWORD_CHAR,keyword ); /* blank out old value of keyword */ strcpy( keyword,"" ); i = 0; state = 0; } else { keyword[i] = (char)k; i++; if ( i >= keysize ) { keysize += BLKSIZE; if (( keyword = (char *)realloc( keyword, keysize*sizeof(char))) == NULL ) { fprintf( stdout,"PagePreprocessorException: out of memory" ); return( 0 ); } } } break; } } fclose( in ); return( 1 ); } /* end of page_preprocessor() */ /*--------------------------------------------------------*/ /* page_substitute */ /* this routine returns the value to substitute for the */ /* keyword from the argument array of keyword/value pairs.*/ /* if the keyword is not found, the routine returns the */ /* keyword argument itself. */ /*--------------------------------------------------------*/ char *page_substitute( char *keyword, int npairs, PAIR *pairs ) { int i; for ( i=0; i