1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
/********************************************************************
* $Author: lindner $
* $Revision: 3.20 $
* $Date: 1995/09/25 05:02:32 $
* $Source: /home/arcwelder/GopherSrc/CVS/gopher+/gopherd/ext.c,v $
* $State: Exp $
*
* Paul Lindner, University of Minnesota CIS.
*
* Copyright 1991, 1992 by the Regents of the University of Minnesota
* see the file "Copyright" in the distribution for conditions of use.
*********************************************************************
* MODULE: ext.c
* These fns define mapping of file extensions to gopher objects.
*********************************************************************
* Revision History:
* $Log: ext.c,v $
* Revision 3.20 1995/09/25 05:02:32 lindner
* Convert to ANSI C
*
* Revision 3.19 1994/09/29 19:57:45 lindner
* Less picky language and view comparisions
*
* Revision 3.18 1994/07/21 15:46:22 lindner
* Brian Coen Patch
*
* Revision 3.17 1994/07/19 20:27:35 lindner
* Use local Locale.h
*
* Revision 3.16 1994/05/02 07:41:11 lindner
* Mods to use setlocale()
*
* Revision 3.15 1994/04/26 22:43:57 lindner
* Fix for Debugmsg
*
* Revision 3.14 1994/04/25 20:49:04 lindner
* Fix for debug code
*
* Revision 3.13 1994/01/20 06:39:18 lindner
* When trying to match an extension to a file type, the server prefers
* exact matches of type & language, but will now fall back to matching
* the type in any language (acf).
*
* Revision 3.12 1993/10/27 20:35:24 lindner
* Plug memory leak
*
* Revision 3.11 1993/10/20 03:17:25 lindner
* Fixed erratic problems with extension adding. Note that the problems
* with directories not working with Gopher0 clients is due to a missing
* parameter in a strncasecmp() call.. Makes one wish ANSI C was more
* widespread...
*
* Also added regular expression code for ignore_patt:
*
* Revision 3.10 1993/10/04 06:39:31 lindner
* Fix for forward declarations
*
* Revision 3.9 1993/09/21 02:35:04 lindner
* Server now adds extensions in a case insensitive manner
*
* Revision 3.8 1993/09/18 03:30:11 lindner
* Remove memory leak
*
* Revision 3.7 1993/09/18 02:22:35 lindner
* More debugging...
*
* Revision 3.6 1993/08/19 20:25:46 lindner
* Mitra's Debug patch
*
* Revision 3.5 1993/08/03 06:03:08 lindner
* Ignored files are now ignored again
*
* Revision 3.4 1993/07/31 00:15:37 lindner
* Fixed weird extension problem
*
* Revision 3.3 1993/07/27 20:15:29 lindner
* Fixed bug where filename was less than extension
*
* Revision 3.2 1993/07/23 03:18:46 lindner
* Mods for using decoder:'s
*
* Revision 3.1.1.1 1993/02/11 18:02:51 lindner
* Gopher+1.2beta release
*
* Revision 1.3 1993/02/09 22:13:19 lindner
* many additions for multilingual gopher
*
* Revision 1.2 1993/01/30 23:56:55 lindner
* Lots of new code for gopher+ stuff.. Change everything to start with EX
*
* Revision 1.1 1992/12/10 23:13:27 lindner
* gopher 1.1 release
*
*
*********************************************************************/
#include <stdio.h>
#include "ext.h"
#include "Malloc.h"
#include "Locale.h"
#include <ctype.h>
#include "util.h"
#include "serverutil.h"
#include "openers.h"
#undef stat
#include "Debug.h"
#include "Dirent.h"
#include "Regex.h"
/*
* Some functions to initialize and destroy sites and extensions..
* (Needed for use with DynArrays...)
*/
Extobj *
EXnew()
{
Extobj *temp;
temp = (Extobj *) malloc(sizeof(Extobj));
if (temp == NULL)
return(NULL);
temp->Objtype = '\0';
temp->View = STRnew();
temp->ViewLang = STRnew();
temp->Prefix = STRnew();
temp->ext = STRnew();
temp->exttype = EXT_ERROR;
temp->GenericData = STRnew();
STRinit(temp->View);
STRinit(temp->ViewLang);
STRinit(temp->Prefix);
STRinit(temp->ext);
STRinit(temp->GenericData);
return(temp);
}
/** Destroy an extension structure **/
void
EXdestroy(Extobj *ext)
{
STRdestroy(ext->View);
STRdestroy(ext->ViewLang);
STRdestroy(ext->Prefix);
STRdestroy(ext->ext);
STRdestroy(ext->GenericData);
free(ext);
}
/** Copy an extension structure **/
void
EXcpy(Extobj *ext1, Extobj*ext2)
{
ext1->Objtype = ext2->Objtype;
STRcpy(ext1->View, ext2->View);
STRcpy(ext1->ViewLang, ext2->ViewLang);
STRcpy(ext1->Prefix, ext2->Prefix);
STRcpy(ext1->ext, ext2->ext);
STRcpy(ext1->GenericData, ext2->GenericData);
EXsetExttype(ext1, EXgetExttype(ext2));
}
/*
* Do the grungy work of adding an extension
*/
char *
EXAfindFile(ExtArray *extarr, char *file, char *view)
{
boolean status = TRUE;
Extobj *ext, *temp;
DIR *ZeDir;
struct dirent *dp;
struct stat statbuf;
static char tmpfile[512];
char directory[512], *cp;
int tmpfilelen = 0, i;
if (file == NULL || *file == '\0')
return(file);
strcpy(tmpfile, file);
ext = EXnew();
/** Find a viewext if available..
Should be able to cycle through all of them... **/
status = EXAviewSearch(extarr, ext, view);
if (status == TRUE)
strcat(tmpfile, EXgetExt(ext));
EXdestroy(ext);
strcpy(directory, tmpfile);
cp = mtm_basename(directory);
*cp = '.';
*(cp+1) = '\0';
if ((ZeDir = ropendir(directory)) == NULL) {
/** Error **/
return(file);
}
cp = mtm_basename(tmpfile);
for (dp = readdir(ZeDir); dp != NULL; dp = readdir(ZeDir)) {
int x;
x = strcasecmp(cp, dp->d_name);
if (x == 0) {
/** Found an exact match **/
strcpy(cp, dp->d_name);
closedir(ZeDir);
return(tmpfile);
}
}
/** Now check for a not so exact match.. **/
rewinddir(ZeDir);
for (dp = readdir(ZeDir); dp != NULL; dp = readdir(ZeDir)) {
int x;
x = strncasecmp(cp, dp->d_name, strlen(dp->d_name));
if (x==0)
/** Found a match for a possible decoder **/
strcpy(cp, dp->d_name);
}
closedir(ZeDir);
/** Hmm can't seem to find it.. Let's try the decoders... **/
tmpfilelen = strlen(tmpfile);
/** Check for decoders.. **/
for (i=0; i< EXAgetNumEntries(extarr); i++) {
temp = EXAgetEntry(extarr,i);
if (EXgetExttype(temp) == EXT_DECODER) {
char decodertmp[256];
strcpy(decodertmp, tmpfile);
strcat(decodertmp, EXgetExt(temp));
if (!rstat(decodertmp, &statbuf)) {
/** Found it! **/
strcpy(tmpfile, decodertmp);
return(tmpfile);
}
}
}
/*** Couldn't find anything... **/
return(file);
}
/*
* Get the parameters associated with a particular extension
*
* Fills in ext with the values..
*/
boolean
EXAsearch(ExtArray *extarr, Extobj *ext, char *fileext, int exttype)
{
int i, extlen;
Extobj *temp;
Debugmsg("EXAsearch:\r\n");
extlen = strlen(fileext);
/*** Linear search. Ick. ***/
for (i=0; i< EXAgetNumEntries(extarr); i++) {
temp = EXAgetEntry(extarr,i);
if (exttype == EXgetExttype(temp)) {
int exlen = strlen(EXgetExt(temp));
if (exlen <= extlen && strcasecmp(fileext+extlen-exlen,
EXgetExt(temp)) == 0) {
EXcpy(ext,temp);
return(TRUE);
}
}
else if (exttype == EXT_IGNORE &&
EXgetExttype(temp) == EXT_IGNOREPAT) {
if (re_comp(EXgetExt(temp)) != NULL)
return(FALSE);
if (re_exec(fileext) == 1) {
EXcpy(ext,temp);
return(TRUE);
}
}
}
return(FALSE);
}
/*
* Do an exact search...
*/
boolean
EXAcasedSearch(ExtArray *extarr, Extobj *ext, char *fileext, int exttype)
{
int i, extlen;
Extobj *temp;
Debugmsg("EXAsearch:\r\n");
extlen = strlen(fileext);
/*** Linear search. Ick. ***/
for (i=0; i< EXAgetNumEntries(extarr); i++) {
temp = EXAgetEntry(extarr,i);
if (exttype == EXgetExttype(temp)) {
if (strcmp(fileext+extlen-strlen(EXgetExt(temp)),
EXgetExt(temp)) == 0) {
EXcpy(ext,temp);
return(TRUE);
}
}
}
return(FALSE);
}
/*
* Search for a specific view extension...
* Prefers exact match of type & lang, but if it can't find one it will
* fall back to matching type in any language.
*/
boolean
EXAviewSearch(ExtArray *extarr, Extobj *ext, char *view)
{
int i;
Extobj *temp;
char viewnlang[256];
boolean found = FALSE;
/*** Linear search. Ick. ***/
for (i=0; i< EXAgetNumEntries(extarr); i++) {
temp = EXAgetEntry(extarr,i);
if (EXgetExttype(temp) == EXT_VIEW) {
strcpy(viewnlang, EXgetView(temp));
if (!found && strcasecmp(view, viewnlang)==0) {
EXcpy(ext,temp);
found = TRUE;
}
if (EXgetVLang(temp) != NULL && *EXgetVLang(temp) != '\0') {
strcat(viewnlang, " ");
strcat(viewnlang, EXgetVLang(temp));
}
if (strncasecmp(view, viewnlang, strlen(viewnlang))==0) {
EXcpy(ext,temp);
return(TRUE);
}
}
}
return(found);
;
}
/*
* Process gopherd.conf lines "viewext", "ignore", "blockext" and others.
*/
boolean
EXAprocessLine(ExtArray *extarr, int exttype, char *inputline, char *deflang)
{
int i;
char ext[64];
char secondparm[64];
char prefix[64];
char Gplustype[64];
char Gpluslang[16];
Extobj *temp;
temp = EXnew();
/*** Set the type of extension we've been given ***/
EXsetExttype(temp, exttype);
if (deflang != NULL)
EXsetVLang(temp, deflang);
inputline = skip_whitespace(inputline);
if (*inputline == '\0') return(FALSE);
/*** The first parameter, the extension ***/
i=0;
while (!isspace(*inputline) && (*inputline != '\0'))
ext[i++] = *inputline++;
ext[i] = '\0';
EXsetExt(temp,ext);
inputline = skip_whitespace(inputline);
if (exttype == EXT_IGNORE || exttype == EXT_IGNOREPAT) {
EXAadd(extarr, temp);
EXdestroy(temp);
return(TRUE);
}
if (*inputline == '\0') return(FALSE);
/*** Second parameter, depends on which token we're using.. ***/
i=0;
while (!isspace(*inputline) && (*inputline != '\0'))
secondparm[i++] = *inputline++;
secondparm[i] = '\0';
if (exttype == EXT_BLOCK || exttype == EXT_BLOCKREF) {
EXsetBlockname(temp, secondparm);
EXAadd(extarr, temp);
EXdestroy(temp);
return(TRUE);
}
else if (exttype == EXT_DECODER) {
EXsetDecoder(temp, secondparm);
EXAadd(extarr, temp);
EXdestroy(temp);
return(TRUE);
}
else if (exttype == EXT_VIEW)
EXsetObjtype(temp, *secondparm);
if (*inputline == '\0') return(FALSE);
inputline = skip_whitespace(inputline);
if (*inputline == '\0') return(FALSE);
/*** Prefix ***/
i=0;
while (!isspace(*inputline) && (*inputline != '\0'))
prefix[i++] = *inputline++;
if (*inputline == '\0') return(FALSE);
prefix[i]='\0';
EXsetPrefix(temp, prefix);
inputline = skip_whitespace(inputline);
if (*inputline == '\0') return(FALSE);
/*** Gopher + view type ***/
i=0;
while (!isspace(*inputline) && (*inputline != '\0'))
Gplustype[i++]= *inputline++;
Gplustype[i] = '\0';
EXsetView(temp, Gplustype);
inputline = skip_whitespace(inputline);
/** Gopher+ view language **/
i=0;
while (!isspace(*inputline) && (*inputline != '\0'))
Gpluslang[i++]= *inputline++;
Gpluslang[i] = '\0';
EXsetVLang(temp, Gpluslang);
EXsetExttype(temp, EXT_VIEW);
EXAadd(extarr, temp);
EXdestroy(temp);
return(TRUE);
}
|