char *Cleanse( char *s ) { LYTrimLeading( s ); LYTrimTrailing( s ); LYLowerCase( s ); return s; }
/* HTVMSBrowseDir() ** ** This function generates a directory listing as an HTML-object ** for local file URL's. It assumes the first two elements of ** of the path are a device followed by a directory: ** ** file://localhost/device/directory[/[foo]] ** ** Will not accept 000000 as a directory name. ** Will offer links to parent through the top directory, unless ** a terminal slash was included in the calling URL. ** ** Returns HT_LOADED on success, HTLoadError() messages on error. ** ** Developed for Lynx by Foteos Macrides ([email protected]). */ PUBLIC int HTVMSBrowseDir ARGS4( CONST char *, address, HTParentAnchor *, anchor, HTFormat, format_out, HTStream *, sink ) { HTStructured* target; HTStructuredClass targetClass; char *pathname = HTParse(address, "", PARSE_PATH + PARSE_PUNCTUATION); char *tail = NULL; char *title = NULL; char *header = NULL; char *parent = NULL; char *relative = NULL; char *cp, *cp1; int pathend, len; DIR *dp; struct stat file_info; time_t NowTime; static char ThisYear[8]; VMSEntryInfo *entry_info = 0; char string_buffer[64]; HTUnEscape(pathname); CTRACE((tfp,"HTVMSBrowseDir: Browsing `%s\'\n", pathname)); /* * Require at least two elements (presumably a device and directory) * and disallow the device root (000000 directory). Symbolic paths * (e.g., sys$help) should have been translated and expanded (e.g., * to /sys$sysroot/syshlp) before calling this routine. */ if (((*pathname != '/') || (cp = strchr(pathname+1, '/')) == NULL || *(cp + 1) == '\0' || 0 == strncmp((cp + 1), "000000", 6)) || (dp = HTVMSopendir(pathname)) == NULL) { FREE(pathname); return HTLoadError(sink, 403, COULD_NOT_ACCESS_DIR); } /* * Set up the output stream. */ _HTProgress (BUILDING_DIR_LIST); if (UCLYhndl_HTFile_for_unspec >= 0) { HTAnchor_setUCInfoStage(anchor, UCLYhndl_HTFile_for_unspec, UCT_STAGE_PARSER, UCT_SETBY_DEFAULT); } target = HTML_new(anchor, format_out, sink); targetClass = *(target->isa); /* * Set up the offset string of the anchor reference, * and strings for the title and header. */ cp = strrchr(pathname, '/'); /* find lastslash */ StrAllocCopy(tail, (cp+1)); /* take slash off the beginning */ if (*tail != '\0') { StrAllocCopy(title, tail); *cp = '\0'; if ((cp1=strrchr(pathname, '/')) != NULL && cp1 != pathname && strncmp((cp1+1), "000000", 6)) StrAllocCopy(parent, (cp1+1)); *cp = '/'; } else { pathname[strlen(pathname)-1] = '\0'; cp = strrchr(pathname, '/'); StrAllocCopy(title, (cp+1)); pathname[strlen(pathname)] = '/'; } StrAllocCopy(header, pathname); /* * Initialize path name for HTStat(). */ pathend = strlen(pathname); if (*(pathname+pathend-1) != '/') { StrAllocCat(pathname, "/"); pathend++; } /* * Output the title and header. */ START(HTML_HTML); PUTC('\n'); START(HTML_HEAD); PUTC('\n'); HTUnEscape(title); START(HTML_TITLE); PUTS(title); PUTS(" directory"); END(HTML_TITLE); PUTC('\n'); FREE(title); END(HTML_HEAD); PUTC('\n'); START(HTML_BODY); PUTC('\n'); HTUnEscape(header); START(HTML_H1); PUTS(header); END(HTML_H1); PUTC('\n'); if (HTDirReadme == HT_DIR_README_TOP) { FILE * fp; if (header[strlen(header)-1] != '/') StrAllocCat(header, "/"); StrAllocCat(header, HT_DIR_README_FILE); if ((fp = fopen(header, "r")) != NULL) { START(HTML_PRE); for(;;) { char c = fgetc(fp); if (c == (char)EOF) break; #ifdef NOTDEFINED switch (c) { case '&': case '<': case '>': PUTC('&'); PUTC('#'); PUTC((char)(c / 10)); PUTC((char) (c % 10)); PUTC(';'); break; default: PUTC(c); } #else PUTC(c); #endif /* NOTDEFINED */ } END(HTML_PRE); fclose(fp); } } FREE(header); if (parent) { HTSprintf0(&relative, "%s/..", tail); HTStartAnchor(target, "", relative); PUTS("Up to "); HTUnEscape(parent); PUTS(parent); END(HTML_A); START(HTML_P); PUTC('\n'); FREE(relative); FREE(parent); } /* * Set up the date comparison. */ NowTime = time(NULL); strcpy(ThisYear, (char *)ctime(&NowTime)+20); ThisYear[4] = '\0'; /* * Now, generate the Btree and put it out to the output stream. */ { char dottest = 2; /* To avoid two strcmp() each time */ STRUCT_DIRENT *dirbuf; HTBTree *bt; /* Set up sort key and initialize BTree */ bt = HTBTree_new((HTComparer) compare_VMSEntryInfo_structs); /* Build tree */ while ((dirbuf = HTVMSreaddir(dp))) { HTAtom *encoding = NULL; HTFormat format; /* Skip if not used */ if (!dirbuf->d_ino) { continue; } /* Current and parent directories are never shown in list */ if (dottest && (!strcmp(dirbuf->d_name, ".") || !strcmp(dirbuf->d_name, ".."))) { dottest--; continue; } /* Don't show the selective enabling file * unless version numbers are included */ if (!strcasecomp(dirbuf->d_name, HT_DIR_ENABLE_FILE)) { continue; } /* Skip files beginning with a dot? */ if ((no_dotfiles || !show_dotfiles) && *dirbuf->d_name == '.') { continue; } /* OK, make an lstat() and get a key ready. */ *(pathname+pathend) = '\0'; StrAllocCat(pathname, dirbuf->d_name); if (HTStat(pathname, &file_info)) { /* for VMS the failure here means the file is not readable... we however continue to browse through the directory... */ continue; } entry_info = (VMSEntryInfo *)malloc(sizeof(VMSEntryInfo)); if (entry_info == NULL) outofmem(__FILE__, "HTVMSBrowseDir"); entry_info->type = 0; entry_info->size = 0; entry_info->date = 0; entry_info->filename = 0; entry_info->display = TRUE; /* Get the type */ format = HTFileFormat(dirbuf->d_name, &encoding, (CONST char **)&cp); if (!cp) { if(!strncmp(HTAtom_name(format), "application",11)) { cp = HTAtom_name(format) + 12; if(!strncmp(cp,"x-", 2)) cp += 2; } else cp = HTAtom_name(format); } StrAllocCopy(entry_info->type, cp); StrAllocCopy(entry_info->filename, dirbuf->d_name); if (S_ISDIR(file_info.st_mode)) { /* strip .DIR part... */ char *dot; dot = strstr(entry_info->filename, ".DIR"); if (dot) *dot = '\0'; LYLowerCase(entry_info->filename); StrAllocCopy(entry_info->type, "Directory"); } else { if ((cp = strstr(entry_info->filename, "READ")) == NULL) { cp = entry_info->filename; } else { cp += 4; if (!strncmp(cp, "ME", 2)) { cp += 2; while (cp && *cp && *cp != '.') { cp++; } } else if (!strncmp(cp, ".ME", 3)) { cp = (entry_info->filename + strlen(entry_info->filename)); } else { cp = entry_info->filename; } } LYLowerCase(cp); if (((len = strlen(entry_info->filename)) > 2) && entry_info->filename[len-1] == 'z') { if (entry_info->filename[len-2] == '.' || entry_info->filename[len-2] == '_') entry_info->filename[len-1] = 'Z'; } } /* Get the date */ { char *t = (char *)ctime((CONST time_t *)&file_info.st_ctime); *(t+24) = '\0'; StrAllocCopy(entry_info->date, (t+4)); *((entry_info->date)+7) = '\0'; if ((atoi((t+19))) < atoi(ThisYear)) StrAllocCat(entry_info->date, (t+19)); else { StrAllocCat(entry_info->date, (t+11)); *((entry_info->date)+12) = '\0'; } } /* Get the size */ if (!S_ISDIR(file_info.st_mode)) entry_info->size = (unsigned int)file_info.st_size; else entry_info->size = 0; /* Now, update the BTree etc. */ if(entry_info->display) { CTRACE((tfp,"Adding file to BTree: %s\n", entry_info->filename)); HTBTree_add(bt, entry_info); } } /* End while HTVMSreaddir() */ FREE(pathname); HTVMSclosedir(dp); START(HTML_PRE); /* * Run through the BTree printing out in order */ { HTBTElement * ele; int i; for (ele = HTBTree_next(bt, NULL); ele != NULL; ele = HTBTree_next(bt, ele)) { entry_info = (VMSEntryInfo *)HTBTree_object(ele); /* Output the date */ if(entry_info->date) { PUTS(entry_info->date); PUTS(" "); } else PUTS(" * "); /* Output the type */ if(entry_info->type) { for(i = 0; entry_info->type[i] != '\0' && i < 15; i++) PUTC(entry_info->type[i]); for(; i < 17; i++) PUTC(' '); } /* Output the link for the name */ HTDirEntry(target, tail, entry_info->filename); PUTS(entry_info->filename); END(HTML_A); /* Output the size */ if(entry_info->size) { if(entry_info->size < 1024) sprintf(string_buffer," %d bytes", entry_info->size); else sprintf(string_buffer," %dKb", entry_info->size/1024); PUTS(string_buffer); } PUTC('\n'); /* end of this entry */ free_VMSEntryInfo_contents(entry_info); } } HTBTreeAndObject_free(bt); } /* End of both BTree loops */ /* * Complete the output stream. */ END(HTML_PRE); PUTC('\n'); END(HTML_BODY); PUTC('\n'); END(HTML_HTML); PUTC('\n'); FREE(tail); FREE_TARGET; return HT_LOADED; } /* End of directory reading section */
int ProcessMailcapEntry( FILE *fp, struct MailcapEntry *mc, AcceptMedia media ) { size_t rawentryalloc = 2000; size_t len; size_t need; char *rawentry, *s, *t; char *LineBuf = 0; rawentry = malloc( ( rawentryalloc ) * sizeof( char ) ); if ( rawentry == 0 ) { ExitWithError( gettext( "Memory exhausted! Program aborted!" ) ); } rawentry[0] = 0; while ( LYSafeGets( &LineBuf, fp ) == 0 ) { LYTrimNewline( LineBuf ); if ( LineBuf[0] != '#' && LineBuf[0] ) { len = strlen( LineBuf ); need = len + strlen( rawentry ) + 1; if ( rawentryalloc < need ) { rawentryalloc = rawentryalloc + need + 2000; rawentry = realloc( rawentry, ( rawentryalloc ) * sizeof( char ) ); if ( rawentry == 0 ) { ExitWithError( gettext( "Memory exhausted! Program aborted!" ) ); } } if ( len && LineBuf[ len + -1 ] == '\\' ) { LineBuf[ len + -1 ] = 0; strcat( rawentry, LineBuf ); } else { strcat( rawentry, LineBuf ); break; } } } if ( LineBuf ) { free( LineBuf ); LineBuf = 0; } t = s = LYSkipBlanks( rawentry ); if ( s[0] == 0 ) { if ( rawentry ) { free( rawentry ); rawentry = 0; } return 0; } else { s = strchr( rawentry, ';' ); if ( s == 0 ) { if ( WWW_TraceFlag && ( WWW_TraceMask & 8 ) ) { fprintf( TraceFP( ), "ProcessMailcapEntry: Ignoring invalid mailcap entry: %s\n", rawentry ); } if ( rawentry ) { free( rawentry ); rawentry = 0; } return 0; } else { s[0] = 0; s++; if ( strncasecomp( t, "text/html", 9 ) == 0 || strncasecomp( t, "text/plain", 10 ) == 0 ) { s = &s[ -1 ]; s[0] = ';'; if ( WWW_TraceFlag && ( WWW_TraceMask & 8 ) ) { fprintf( TraceFP( ), "ProcessMailcapEntry: Ignoring mailcap entry: %s\n", rawentry ); } if ( rawentry ) { free( rawentry ); rawentry = 0; } return 0; } else { LYRemoveBlanks( rawentry ); LYLowerCase( rawentry ); *(int*)&mc->needsterminal = 0; *(int*)&mc->copiousoutput = 0; *(int*)&mc->needtofree = 1; *(int*)&mc->testcommand = 0; *(int*)&mc->label = 0; *(int*)&mc->printcommand = 0; *(int*)&mc->contenttype[0] = 0; HTSACopy( &mc->contenttype, rawentry ); *(int*)&mc->quality = 0x3f800000; *(int*)&mc->maxbytes = 0; t = GetCommand( s, &mc->command ); if ( t ) { s = LYSkipBlanks( t ); for ( ; s; ) { char *arg, *eq, *mallocd_string; t = GetCommand( s, &mallocd_string ); arg = mallocd_string; eq = strchr( arg, '=' ); if ( eq ) { eq[0] = 0; eq++; eq = LYSkipBlanks( eq ); } if ( arg && arg[0] ) { arg = Cleanse( arg ); if ( strcmp( arg, "needsterminal" ) == 0 ) *(int*)&mc->needsterminal = 1; else { if ( strcmp( arg, "copiousoutput" ) == 0 ) *(int*)&mc->copiousoutput = 1; else if ( eq && strcmp( arg, "test" ) == 0 ) { *(int*)&mc->testcommand = 0; HTSACopy( &mc->testcommand, eq ); TrimCommand( &mc->testcommand ); if ( WWW_TraceFlag && ( WWW_TraceMask & 8 ) ) { fprintf( TraceFP( ), "ProcessMailcapEntry: Found testcommand:%s\n", &mc->testcommand ); } } else if ( eq && strcmp( arg, "description" ) == 0 ) mc->label = eq; else if ( eq && strcmp( arg, "label" ) == 0 ) mc->label = eq; else if ( eq && strcmp( arg, "print" ) == 0 ) mc->printcommand = eq; else if ( eq == 0 || strcmp( arg, "textualnewlines" ) ) { if ( eq && strcmp( arg, "q" ) == 0 ) { mc->quality = atof( eq ); if ( mc->quality > 0 && mc->quality < 0.001000000000 ) *(int*)&mc->quality = 0x3a83126f; } else if ( eq && strcmp( arg, "mxb" ) == 0 ) { mc->maxbytes = atol( eq ); if ( mc->maxbytes < 0 ) *(int*)&mc->maxbytes = 0; } else { if ( strcmp( arg, "notes" ) && arg[0] && WWW_TraceFlag && ( WWW_TraceMask & 8 ) ) { fprintf( TraceFP( ), "ProcessMailcapEntry: Ignoring mailcap flag '%s'.\n", arg ); } } } } } if ( mallocd_string ) { free( mallocd_string ); mallocd_string = 0; } s = t; } } if ( rawentry ) { free( rawentry ); rawentry = 0; } if ( PassesTest( mc ) ) { if ( WWW_TraceFlag && ( WWW_TraceMask & 8 ) ) { fprintf( TraceFP( ), "ProcessMailcapEntry Setting up conversion %s : %s\n", &mc->contenttype[0], &mc->command ); } HTSetPresentation( &mc->contenttype[0], &mc->command, &mc->testcommand, mc->quality, 3.000000000000, 0.000000000000, mc->maxbytes, media ); } if ( mc->command ) { free( &mc->command ); *(int*)&mc->command = 0; } if ( mc->testcommand ) { free( &mc->testcommand ); *(int*)&mc->testcommand = 0; } if ( mc->contenttype[0] ) { free( &mc->contenttype[0] ); *(int*)&mc->contenttype[0] = 0; } return 1; } } } }
int HTLoadExtensionsConfigFile( char *fn ) { int eax; char line[256]; char word[256]; char *ct; FILE *f; int count = 0; if ( WWW_TraceFlag && ( WWW_TraceMask & 8 ) ) { fprintf( TraceFP( ), "HTLoadExtensionsConfigFile: Loading file '%s'.\n", fn ); } f = fopen64( fn, "r" ); if ( f == 0 ) { if ( WWW_TraceFlag && ( WWW_TraceMask & 8 ) ) { fprintf( TraceFP( ), "HTLoadExtensionsConfigFile: Could not open '%s'.\n", fn ); } return count; } else { while ( HTGetLine( line, 256, f ) == 0 ) { HTGetWord( word, line, ' ', '\t' ); if ( line[0] && word[0] != '#' ) { ct = 0; HTSACopy( &ct, word ); LYLowerCase( ct ); while ( line[0] ) { HTGetWord( word, line, ' ', '\t' ); if ( word[0] && word[0] != ' ' ) { char *ext = 0; HTSprintf0( &ext, ".%s", word[0] ); LYLowerCase( ext ); if ( WWW_TraceFlag && ( WWW_TraceMask & 8 ) ) { fprintf( TraceFP( ), "setting suffix '%s' to '%s'.\n", ext, ct ); } if ( strstr( ct, "tex" ) || strstr( ct, "postscript" ) || strstr( ct, "sh" ) || strstr( ct, "troff" ) || strstr( ct, "rtf" ) ) HTSetSuffix5( ext, ct, "8bit", 0, 1.000000000000 ); else HTSetSuffix5( ext, ct, "binary", 0, 1.000000000000 ); count++; if ( ext ) { free( ext ); } } } if ( ct ) { free( ct ); ct = 0; } } } LYCloseInput( f ); return count; } }