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; } } } }
char *TrimLowercase( char *buffer ) { LYRemoveBlanks( buffer ); strtolower( buffer ); return buffer; }
HTStream *HTSaveToFile( HTPresentation *pres, HTParentAnchor *anchor, HTStream *sink ) { int eax; HTStream *ret_obj; char fnam[256]; char *suffix; char *cp; int c = 0; BOOLEAN IsBinary = 1; ret_obj = calloc( 1, sizeof( HTStream ) ); if ( ret_obj == 0 ) outofmem( "./HTFWriter.c", "HTSaveToFile" ); ret_obj->isa->name[0] = HTFWriter.name; *(int*)&ret_obj->remove_command = 0; *(int*)&ret_obj->end_command = 0; ret_obj->input_format = pres->rep->next; ret_obj->output_format = pres->rep_out; ret_obj->anchor = anchor; ret_obj->sink = sink; if ( dump_output_immediately ) { ret_obj->fp = stdout; if ( HTOutputFormat != HTAtom_for( "www/download" ) ) return ret_obj; } else { LYCancelDownload = 0; if ( HTOutputFormat != HTAtom_for( "www/download" ) ) { if ( traversal || ( no_download && !override_no_download && no_disk_save ) ) { if ( traversal == 0 ) { HTAlert( gettext( "This file cannot be displayed on this terminal." ) ); } LYCancelDownload = 1; if ( traversal ) LYCancelledFetch = 1; if ( ret_obj ) { free( ret_obj ); ret_obj = 0; } return 0; } else { cp = strchr( (char*)pres->rep_out, ';' ); if ( cp && strstr( &cp[1], "charset" ) ) { mustshow = 1; user_message( gettext( "%s D)ownload, or C)ancel" ), (char*)pres->rep_out ); while ( 1 ) { switch ( c ) { case -1: if ( keymap[0] != 47 ) { c = LYgetch_single( ); } break; default: if ( c & 34816 ) { if ( ( c & 255 ) != 47 ) continue; } else if ( keymap[ ( c & 2047 ) + 1 ] != 47 ) continue; break; } switch ( c ) { case -1: if ( keymap[0] == 47 ) { mustshow = 1; statusline( gettext( "Cancelling file." ) ); LYCancelDownload = 1; if ( ret_obj ) { free( ret_obj ); ret_obj = 0; } return 0; } break; default: if ( c & 34816 ) { if ( ( c & 255 ) == 47 ) continue; } else if ( keymap[ ( c & 2047 ) + 1 ] == 47 ) continue; break; } } } else if ( pres->rep_out->next ) { mustshow = 1; user_message( gettext( "%s D)ownload, or C)ancel" ), (char*)pres->rep_out ); } else { mustshow = 1; statusline( gettext( "This file cannot be displayed on this terminal: D)ownload, or C)ancel" ) ); } } } if ( LYCachedTemp( fnam, &anchor->FileCache ) & 255 ) { ret_obj->fp = LYOpenTempRewrite( fnam, ".bin", "wb" ); } else { if ( strcasecomp( (char*)pres->rep_out, "text/html" ) == 0 ) suffix = ".html"; else { if ( strncasecomp( (char*)pres->rep_out, "text/", 5 ) == 0 ) suffix = ".txt"; else { if ( strncasecomp( (char*)pres->rep_out, "application/", 12 ) == 0 ) suffix = ".bin"; else { suffix = HTFileSuffix( &pres->rep->next, &anchor->content_encoding ); if ( suffix == 0 || suffix[0] != '.' ) suffix = ".html"; } } } ret_obj->fp = LYOpenTemp( fnam, suffix, "wb" ); } if ( ret_obj->fp == 0 ) { HTAlert( gettext( "Can't open output file! Cancelling!" ) ); if ( ret_obj ) { free( ret_obj ); ret_obj = 0; } return 0; } else { if ( strncasecomp( (char*)pres->rep_out, "text/", 5 ) == 0 || strcasecomp( (char*)pres->rep_out, "application/postscript" ) == 0 || strcasecomp( (char*)pres->rep_out, "application/x-RUNOFF-MANUAL" ) == 0 ) IsBinary = 0; HTInfoMsg2( gettext( "Content-type: %s" ), (char*)pres->rep_out ); HTSACopy( &WWW_Download_File, fnam ); *(int*)&ret_obj->remove_command = 0; HTAddParam( &ret_obj->remove_command, "%s", 1, fnam ); HTEndParam( &ret_obj->remove_command, "%s", 1 ); HTSACopy( &ret_obj->end_command, "SaveToFile" ); mustshow = 1; statusline( gettext( "Retrieving file. - PLEASE WAIT -" ) ); HTSACopy( &anchor->FileCache, fnam ); } } if ( LYPrependBaseToSource && strncasecomp( (char*)pres->rep_out, "text/html", 9 ) == 0 && anchor->content_encoding == 0 ) { char *temp = 0; if ( anchor->content_base && anchor->content_base[0] ) HTSACopy( &temp, &anchor->content_base ); else if ( anchor->content_location && anchor->content_location[0] ) HTSACopy( &temp, &anchor->content_location ); if ( temp ) { LYRemoveBlanks( temp ); if ( !is_url( temp ) && temp ) { free( temp ); temp = 0; } } fprintf( &ret_obj->fp, "<!-- X-URL: %s -->\n", &anchor->address ); if ( anchor->date && anchor->date[0] ) { fprintf( &ret_obj->fp, "<!-- Date: %s -->\n", &anchor->date ); if ( anchor->last_modified && anchor->last_modified[0] && strcmp( &anchor->last_modified, &anchor->date ) && strcmp( &anchor->last_modified, "Thu, 01 Jan 1970 00:00:01 GMT" ) ) fprintf( &ret_obj->fp, "<!-- Last-Modified: %s -->\n", &anchor->last_modified ); } fprintf( &ret_obj->fp, "<BASE HREF=\"%s\">\n\n", temp ? temp : &anchor->address ); if ( temp ) { free( temp ); } } if ( LYPrependCharsetToSource && strncasecomp( (char*)pres->rep_out, "text/html", 9 ) == 0 && anchor->content_encoding == 0 ) { char *temp = 0; if ( anchor->charset && anchor->charset[0] ) { HTSACopy( &temp, &anchor->charset ); LYRemoveBlanks( temp ); fprintf( &ret_obj->fp, "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=%s\">\n\n", temp ); } if ( temp ) { free( temp ); } } return ret_obj; }
int printfile(DocInfo *newdoc) { BOOLEAN Lpansi = FALSE; DocAddress WWWDoc; char *content_base = NULL; char *content_location = NULL; char *cp = NULL; char *link_info = NULL; char *sug_filename = NULL; int lines_in_file = 0; int pagelen = 0; int printer_number = 0; int type = 0; /* * Extract useful info from URL. */ StrAllocCopy(link_info, newdoc->address + 12); /* * Reload the file we want to print into memory. */ LYpop(newdoc); WWWDoc.address = newdoc->address; WWWDoc.post_data = newdoc->post_data; WWWDoc.post_content_type = newdoc->post_content_type; WWWDoc.bookmark = newdoc->bookmark; WWWDoc.isHEAD = newdoc->isHEAD; WWWDoc.safe = newdoc->safe; if (!HTLoadAbsolute(&WWWDoc)) return (NOT_FOUND); /* * If we have an explicit content-base, we may use it even if not in source * mode. - kw */ if (HText_getContentBase()) { StrAllocCopy(content_base, HText_getContentBase()); LYRemoveBlanks(content_base); if (isEmpty(content_base)) { FREE(content_base); } } /* * If document is source, load the content_base and content_location * strings. - FM */ if (HTisDocumentSource()) { if (HText_getContentLocation()) { StrAllocCopy(content_location, HText_getContentLocation()); LYRemoveBlanks(content_location); if (isEmpty(content_location)) { FREE(content_location); } } if (!content_base) { if ((content_location) && is_url(content_location)) { StrAllocCopy(content_base, content_location); } else { StrAllocCopy(content_base, newdoc->address); } } if (!content_location) { StrAllocCopy(content_location, newdoc->address); } } sug_filename = suggested_filename(newdoc); /* * Get the number of lines in the file. */ if ((cp = strstr(link_info, "lines=")) != NULL) { /* * Terminate prev string here. */ *cp = '\0'; /* * Number of characters in "lines=". */ cp += 6; lines_in_file = atoi(cp); } /* * Determine the type. */ if (strstr(link_info, "LOCAL_FILE")) { type = TO_FILE; } else if (strstr(link_info, "TO_SCREEN")) { type = TO_SCREEN; } else if (strstr(link_info, "LPANSI")) { Lpansi = TRUE; type = TO_SCREEN; } else if (strstr(link_info, "MAIL_FILE")) { type = MAIL; } else if (strstr(link_info, "PRINTER")) { type = PRINTER; if ((cp = strstr(link_info, "number=")) != NULL) { /* number of characters in "number=" */ cp += 7; printer_number = atoi(cp); } if ((cp = strstr(link_info, "pagelen=")) != NULL) { /* number of characters in "pagelen=" */ cp += 8; pagelen = atoi(cp); } else { /* default to 66 lines */ pagelen = 66; } } /* * Act on the request. - FM */ switch (type) { case TO_FILE: send_file_to_file(newdoc, content_base, sug_filename); break; case MAIL: send_file_to_mail(newdoc, content_base, content_location); break; case TO_SCREEN: if (confirm_by_pages(CONFIRM_LONG_SCREEN_PRINT, lines_in_file, LYlines)) send_file_to_screen(newdoc, content_base, Lpansi); break; case PRINTER: if (confirm_by_pages(CONFIRM_LONG_PAGE_PRINT, lines_in_file, pagelen)) send_file_to_printer(newdoc, content_base, sug_filename, printer_number); break; } /* end switch */ FREE(link_info); FREE(sug_filename); FREE(content_base); FREE(content_location); return (NORMAL); }