%{ /**************************************************************************** * htmlfilter.c * * * * LocalGate Local Filesystem -> WWW Gateway * * * * LocalGate allows a user to access HTML files on the local filesystem * * as if they had a web server to access the files. * * * * htmlfilter.c: html filter for LocalGate. * * NOTE: This file is automatically generated from htmlfilter.l. * * * * Copyright (C) 1997 Michael Chu * * This file is part of the LocalGate local filesystem -> WWW gateway. * * * * This program 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. * * * * This program 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., 675 Mass Ave, Cambridge, MA 02139, USA. * * * * Contact information: Michael Chu * * mmchu@pobox.com * ****************************************************************************/ /*********************************** INCLUDES *******************************/ #include #include #include #include #include #include #include "fileutils.h" #include "netutils.h" #include "htmlfilter.h" /*********************************** GLOBALS ********************************/ /* stores the current file id to use. */ static int filterFileId; /* stores the current socket id to use. */ static int filterSocketId; /* stores the file position in the current file. */ static int filterFilePos; /* flag for whether or not a lexing error occured. */ static int filterLexingError; %} /* defines states of the lexer. */ %x ANCHORDEFINITION /* defines markers and macros for convenience. */ A [aA] HREF [hH][rR][eE][fF] STARTANCHORDEFINITION <{A}.*{HREF}[ ]*=[ ]*\" ENDANCHORDEFINITION \"> FILEURL [fF][iI][lL][eE]: ALLMATCH .|\n %% /* if we have an anchor definition, then go into anchor definition state. */ {STARTANCHORDEFINITION} { /* try to write matched text to socket. */ if ((writeToSocket(filterSocketId, yytext, yyleng)) != yyleng) { /* show error. */ fprintf(stderr, "Error sending to socket! (errno = %d)\n", errno); /* set lex error. */ filterLexingError = 1; } /* go into anchor definition state. */ BEGIN (ANCHORDEFINITION); } /* if we are in anchor definition state and find a file url tag, then remove it. */ {FILEURL} { /* do nothing! */ } /* if we are in anchor definition state and find an end anchor definition, then go back to initial state. */ {ENDANCHORDEFINITION} { /* try to write matched text to socket. */ if ((writeToSocket(filterSocketId, yytext, yyleng)) != yyleng) { /* show error. */ fprintf(stderr, "Error sending to socket! (errno = %d)\n", errno); /* set lex error. */ filterLexingError = 1; } /* go into initial state. */ BEGIN (INITIAL); } /* any other unmatched portions of the file should just be dumped to the socket. this is for any state. */ {ALLMATCH} { /* try to write matched text to socket. */ if ((writeToSocket(filterSocketId, yytext, yyleng)) != yyleng) { /* show error. */ fprintf(stderr, "Error sending to socket! (errno = %d)\n", errno); /* set lex error. */ filterLexingError = 1; } } {ALLMATCH} { /* try to write matched text to socket. */ if ((writeToSocket(filterSocketId, yytext, yyleng)) != yyleng) { /* show error. */ fprintf(stderr, "Error sending to socket! (errno = %d)\n", errno); /* set lex error. */ filterLexingError = 1; } } %% /********************************** FUNCTIONS *******************************/ /**************************************************************************** * isHTMLFileName: * * * * Summary: Checks to see if the filename represents an HTML file. * * * * Input: fileName: filename to check. * * * * Returns: 0 if this is not a HTML filename. * * 1 if this is an HTML filename. * * * * Global State Affected: * * none. * ****************************************************************************/ int isHTMLFileName(char *fileName) { int fileNameLength = 0; /* length of filename. */ char *localFileName = NULL; /* stores modifiable copy of filename. */ int nameIndex = 0; /* used to traverse string. */ /* make sure that fileName is not NULL. */ assert(fileName != NULL); /* get length of fileName. */ fileNameLength = strlen(fileName); /* make a copy of fileName. */ localFileName = (char *) malloc(fileNameLength+1); /* make sure malloc worked. */ assert(localFileName != NULL); /* make all character lowercase. */ for (nameIndex = 0; nameIndex < fileNameLength; nameIndex++) { /* set character to lowercase. */ localFileName[nameIndex] = tolower(fileName[nameIndex]); } /* set end to NULL-termination. */ localFileName[fileNameLength-1] = 0; /* make sure length is long enough. */ if (fileNameLength >= 5) { /* try to match ".html". */ for (nameIndex = (fileNameLength-1); nameIndex >= (fileNameLength-5); nameIndex--) { /* check to see if it matches. */ if (nameIndex == (fileNameLength-5)) { /* check for matches. */ if ((localFileName[nameIndex]) != '.') { /* mismatch, try something else! */ break; } } else if (nameIndex == (fileNameLength-4)) { /* check for matches. */ if ((localFileName[nameIndex]) != 'h') { /* mismatch, try something else! */ break; } } else if (nameIndex == (fileNameLength-3)) { /* check for matches. */ if ((localFileName[nameIndex]) != 't') { /* mismatch, try something else! */ break; } } else if (nameIndex == (fileNameLength-2)) { /* check for matches. */ if ((localFileName[nameIndex]) != 'm') { /* mismatch, try something else! */ break; } } else if (nameIndex == (fileNameLength-1)) { /* check for matches. */ if ((localFileName[nameIndex]) != 'l') { /* mismatch, try something else! */ break; } else { /* free local name. */ free(localFileName); localFileName = NULL; /* return success. */ return(1); } } } } /* make sure length is long enough. */ if (fileNameLength >= 4) { /* try to match ".htm". */ for (nameIndex = (fileNameLength-1); nameIndex >= (fileNameLength-4); nameIndex--) { /* check to see if it matches. */ if (nameIndex == (fileNameLength-4)) { /* check for matches. */ if ((localFileName[nameIndex]) != '.') { /* mismatch, try something else! */ break; } } else if (nameIndex == (fileNameLength-3)) { /* check for matches. */ if ((localFileName[nameIndex]) != 'h') { /* mismatch, try something else! */ break; } } else if (nameIndex == (fileNameLength-2)) { /* check for matches. */ if ((localFileName[nameIndex]) != 't') { /* mismatch, try something else! */ break; } } else if (nameIndex == (fileNameLength-1)) { /* check for matches. */ if ((localFileName[nameIndex]) != 'm') { /* mismatch, try something else! */ break; } else { /* free local name. */ free(localFileName); localFileName = NULL; /* return success. */ return(1); } } } } /* free local name. */ free(localFileName); localFileName = NULL; /* return failure. */ return(0); } /**************************************************************************** * filterHTMLFileToSocket: * * * * Summary: Filters the given file and output result to the given socket. * * * * Input: fileId: file id to read from. * * socketId: socket id to write to. * * * * Returns: 0 if there was an error filtering/sending file. * * 1 if the file was filtered/sent correctly. * * * * Global State Affected: * * Given file is filtered/written out to the given socket. * ****************************************************************************/ int filterHTMLFileToSocket(int fileId, int socketId) { /* make sure that file id is non-negative. */ assert(fileId >= 0); /* make sure that socket id is non-negative. */ assert(socketId >= 0); /* set the global file id for the lexer. */ filterFileId = fileId; /* set the global socket id for the lexer. */ filterSocketId = socketId; /* reset the file position for the lexer. */ filterFilePos = 0; /* reset lexing error flag. */ filterLexingError = 0; /* call the lexer to filter the input. */ yylex(); /* if we had a lexing error, then show error. */ if (filterLexingError) { /* show error. */ fprintf(stderr, "Error lexing file!\n"); /* return error. */ return(0); } /* return success. */ return(1); }