int ProcessMailcapFile(char *file) /* Search one mailcap file * On entry- * file is the name of the file to be searched * If file has a valid entry for ContentType then on exit- * ProcessMailcapFile=1 * Else on exit- * ProcessMailcapFile=0 ***/ { FILE *fp; int res; res = IsBadFile(file); if (res) { if (DoDebug) { /* Quietly ignore bad files unless debugging */ if (res == 1) { fprintf(stderr, "Mailcap path entry '%s' is a directory.\n", file); } else { fprintf(stderr, "Mailcap path entry '%s' does not exist.\n", file); } } return 0; } fp = fopen(file, "r"); if (DoDebug) fprintf(stderr, "Looking for '%s' in mailcap file '%s'.\n", ContentType, file); while (fp && !feof(fp)) { if (GetMailcapEntry(fp)) { if (CtypeMatch() && PassesTest()) { fclose(fp); return 1; } } } if (fp) fclose(fp); return 0; }
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; } } } }