| 1 |
/* |
| 2 |
* $LynxId: HTWSRC.c,v 1.32 2025/01/06 15:48:56 tom Exp $ |
| 3 |
* |
| 4 |
* Parse WAIS Source file HTWSRC.c |
| 5 |
* ====================== |
| 6 |
* |
| 7 |
* This module parses a stream with WAIS source file |
| 8 |
* format information on it and creates a structured stream. |
| 9 |
* That structured stream is then converted into whatever. |
| 10 |
* |
| 11 |
* 3 June 93 Bug fix: Won't crash if no description |
| 12 |
*/ |
| 13 |
|
| 14 |
#define HTSTREAM_INTERNAL 1 |
| 15 |
|
| 16 |
#include <HTUtils.h> |
| 17 |
|
| 18 |
#include <HTWSRC.h> |
| 19 |
#include <LYUtils.h> |
| 20 |
|
| 21 |
#include <HTML.h> |
| 22 |
#include <HTParse.h> |
| 23 |
|
| 24 |
#include <LYLeaks.h> |
| 25 |
|
| 26 |
#define BIG 10000 /* Arbitrary limit to value length */ |
| 27 |
#define PARAM_MAX BIG |
| 28 |
#define CACHE_PERIOD (7*86400) /* Time to keep .src file in seconds */ |
| 29 |
|
| 30 |
struct _HTStructured { |
| 31 |
const HTStructuredClass *isa; |
| 32 |
/* ... */ |
| 33 |
}; |
| 34 |
|
| 35 |
#define PUTC(c) (*me->target->isa->put_character)(me->target, c) |
| 36 |
#define PUTS(s) (*me->target->isa->put_string)(me->target, s) |
| 37 |
#define START(e) (*me->target->isa->start_element)(me->target, e, NULL, NULL, -1, NULL) |
| 38 |
#define END(e) (*me->target->isa->end_element)(me->target, e, NULL) |
| 39 |
#define MAYBE_END(e) if (HTML_dtd.tags[e].contents != SGML_EMPTY) \ |
| 40 |
(*me->target->isa->end_element)(me->target, e, NULL) |
| 41 |
|
| 42 |
/* Here are the parameters which can be specified in a source file |
| 43 |
*/ |
| 44 |
static const char *par_name[] = |
| 45 |
{ |
| 46 |
"version", |
| 47 |
"ip-address", |
| 48 |
#define PAR_IP_NAME 2 |
| 49 |
"ip-name", |
| 50 |
#define PAR_TCP_PORT 3 |
| 51 |
"tcp-port", |
| 52 |
#define PAR_DATABASE_NAME 4 |
| 53 |
"database-name", |
| 54 |
#define PAR_COST 5 |
| 55 |
"cost", |
| 56 |
#define PAR_COST_UNIT 6 |
| 57 |
"cost-unit", |
| 58 |
#define PAR_FREE 7 |
| 59 |
"free", |
| 60 |
#define PAR_MAINTAINER 8 |
| 61 |
"maintainer", |
| 62 |
#define PAR_DESCRIPTION 9 |
| 63 |
"description", |
| 64 |
"keyword-list", |
| 65 |
"source", |
| 66 |
"window-geometry", |
| 67 |
"configuration", |
| 68 |
"script", |
| 69 |
"update-time", |
| 70 |
"contact-at", |
| 71 |
"last-contacted", |
| 72 |
"confidence", |
| 73 |
"num-docs-to-request", |
| 74 |
"font", |
| 75 |
"font-size", |
| 76 |
#define PAR_UNKNOWN 22 |
| 77 |
"unknown", |
| 78 |
NULL, /* Terminate list */ |
| 79 |
#define PAR_COUNT 23 |
| 80 |
}; |
| 81 |
|
| 82 |
enum tokenstate { |
| 83 |
beginning, |
| 84 |
before_tag, |
| 85 |
colon, |
| 86 |
before_value, |
| 87 |
value, |
| 88 |
bracketed_value, |
| 89 |
quoted_value, |
| 90 |
escape_in_quoted, |
| 91 |
done |
| 92 |
}; |
| 93 |
|
| 94 |
/* Stream Object |
| 95 |
* ------------ |
| 96 |
* |
| 97 |
* The target is the structured stream down which the |
| 98 |
* parsed results will go. |
| 99 |
* |
| 100 |
* all the static stuff below should go in here to make it reentrant |
| 101 |
*/ |
| 102 |
|
| 103 |
struct _HTStream { |
| 104 |
const HTStreamClass *isa; |
| 105 |
HTStructured *target; |
| 106 |
char *par_value[PAR_COUNT]; |
| 107 |
enum tokenstate state; |
| 108 |
char param[BIG + 1]; |
| 109 |
int param_number; |
| 110 |
int param_count; |
| 111 |
}; |
| 112 |
|
| 113 |
/* Decode one hex character |
| 114 |
*/ |
| 115 |
char from_hex(char c) |
| 116 |
{ |
| 117 |
return (char) ((c >= '0') && (c <= '9') ? c - '0' |
| 118 |
: (c >= 'A') && (c <= 'F') ? c - 'A' + 10 |
| 119 |
: (c >= 'a') && (c <= 'f') ? c - 'a' + 10 |
| 120 |
: 0); |
| 121 |
} |
| 122 |
|
| 123 |
/* State machine |
| 124 |
* ------------- |
| 125 |
* |
| 126 |
* On entry, |
| 127 |
* me->state is a valid state (see WSRC_init) |
| 128 |
* c is the next character |
| 129 |
* On exit, |
| 130 |
* returns 1 Done with file |
| 131 |
* 0 Continue. me->state is updated if necessary. |
| 132 |
* -1 Syntax error error |
| 133 |
*/ |
| 134 |
|
| 135 |
/* Treat One Character |
| 136 |
* ------------------- |
| 137 |
*/ |
| 138 |
static void WSRCParser_put_character(HTStream *me, int c) |
| 139 |
{ |
| 140 |
switch (me->state) { |
| 141 |
case beginning: |
| 142 |
if (c == '(') |
| 143 |
me->state = before_tag; |
| 144 |
break; |
| 145 |
|
| 146 |
case before_tag: |
| 147 |
if (c == ')') { |
| 148 |
me->state = done; |
| 149 |
return; /* Done with input file */ |
| 150 |
} else if (c == ':') { |
| 151 |
me->param_count = 0; |
| 152 |
me->state = colon; |
| 153 |
} /* Ignore other text */ |
| 154 |
break; |
| 155 |
|
| 156 |
case colon: |
| 157 |
if (WHITE(c)) { |
| 158 |
me->param[me->param_count++] = 0; /* Terminate */ |
| 159 |
for (me->param_number = 0; |
| 160 |
par_name[me->param_number]; |
| 161 |
me->param_number++) { |
| 162 |
if (0 == strcmp(par_name[me->param_number], me->param)) { |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
if (!par_name[me->param_number]) { /* Unknown field */ |
| 167 |
CTRACE((tfp, "HTWSRC: Unknown field `%s' in source file\n", |
| 168 |
me->param)); |
| 169 |
me->param_number = PAR_UNKNOWN; |
| 170 |
me->state = before_value; /* Could be better ignore */ |
| 171 |
return; |
| 172 |
} |
| 173 |
me->state = before_value; |
| 174 |
} else { |
| 175 |
if (me->param_count < PARAM_MAX) |
| 176 |
me->param[me->param_count++] = (char) c; |
| 177 |
} |
| 178 |
break; |
| 179 |
|
| 180 |
case before_value: |
| 181 |
if (c == ')') { |
| 182 |
me->state = done; |
| 183 |
return; /* Done with input file */ |
| 184 |
} |
| 185 |
if (WHITE(c)) |
| 186 |
return; /* Skip white space */ |
| 187 |
me->param_count = 0; |
| 188 |
if (c == '"') { |
| 189 |
me->state = quoted_value; |
| 190 |
} else { |
| 191 |
me->state = ((c == '(') |
| 192 |
? bracketed_value |
| 193 |
: value); |
| 194 |
me->param[me->param_count++] = (char) c; /* Don't miss first character */ |
| 195 |
} |
| 196 |
break; |
| 197 |
|
| 198 |
case value: |
| 199 |
if (WHITE(c)) { |
| 200 |
me->param[me->param_count] = 0; |
| 201 |
StrAllocCopy(me->par_value[me->param_number], me->param); |
| 202 |
me->state = before_tag; |
| 203 |
} else { |
| 204 |
if (me->param_count < PARAM_MAX) |
| 205 |
me->param[me->param_count++] = (char) c; |
| 206 |
} |
| 207 |
break; |
| 208 |
|
| 209 |
case bracketed_value: |
| 210 |
if (c == ')') { |
| 211 |
me->param[me->param_count] = 0; |
| 212 |
StrAllocCopy(me->par_value[me->param_number], me->param); |
| 213 |
me->state = before_tag; |
| 214 |
break; |
| 215 |
} |
| 216 |
if (me->param_count < PARAM_MAX) |
| 217 |
me->param[me->param_count++] = (char) c; |
| 218 |
break; |
| 219 |
|
| 220 |
case quoted_value: |
| 221 |
if (c == '"') { |
| 222 |
me->param[me->param_count] = 0; |
| 223 |
StrAllocCopy(me->par_value[me->param_number], me->param); |
| 224 |
me->state = before_tag; |
| 225 |
break; |
| 226 |
} |
| 227 |
|
| 228 |
if (c == '\\') { /* Ignore escape but switch state */ |
| 229 |
me->state = escape_in_quoted; |
| 230 |
break; |
| 231 |
} |
| 232 |
/* Fall through! */ |
| 233 |
|
| 234 |
case escape_in_quoted: |
| 235 |
if (me->param_count < PARAM_MAX) |
| 236 |
me->param[me->param_count++] = (char) c; |
| 237 |
me->state = quoted_value; |
| 238 |
break; |
| 239 |
|
| 240 |
case done: /* Ignore anything after EOF */ |
| 241 |
return; |
| 242 |
|
| 243 |
} /* switch me->state */ |
| 244 |
} |
| 245 |
|
| 246 |
/* Open Cache file |
| 247 |
* =============== |
| 248 |
* |
| 249 |
* Bugs: Maybe for filesystem-challenged platforms (MSDOS for example) we |
| 250 |
* should make a hash code for the filename. |
| 251 |
*/ |
| 252 |
|
| 253 |
#ifdef CACHE_FILE_PREFIX |
| 254 |
static BOOL write_cache(HTStream *me) |
| 255 |
{ |
| 256 |
FILE *fp; |
| 257 |
char *cache_file_name = NULL; |
| 258 |
char *www_database; |
| 259 |
int result = NO; |
| 260 |
|
| 261 |
if (!me->par_value[PAR_DATABASE_NAME] |
| 262 |
|| !me->par_value[PAR_IP_NAME] |
| 263 |
) |
| 264 |
return NO; |
| 265 |
|
| 266 |
www_database = HTEscape(me->par_value[PAR_DATABASE_NAME], URL_XALPHAS); |
| 267 |
HTSprintf0(&cache_file_name, "%sWSRC-%s:%s:%.100s.txt", |
| 268 |
CACHE_FILE_PREFIX, |
| 269 |
me->par_value[PAR_IP_NAME], |
| 270 |
(me->par_value[PAR_TCP_PORT] |
| 271 |
? me->par_value[PAR_TCP_PORT] |
| 272 |
: "210"), |
| 273 |
www_database); |
| 274 |
|
| 275 |
if ((fp = fopen(cache_file_name, TXT_W)) != 0) { |
| 276 |
result = YES; |
| 277 |
if (me->par_value[PAR_DESCRIPTION]) |
| 278 |
fputs(me->par_value[PAR_DESCRIPTION], fp); |
| 279 |
else |
| 280 |
fputs("Description not available\n", fp); |
| 281 |
fclose(fp); |
| 282 |
} |
| 283 |
FREE(www_database); |
| 284 |
FREE(cache_file_name); |
| 285 |
return result; |
| 286 |
} |
| 287 |
#endif |
| 288 |
|
| 289 |
/* Output equivalent HTML |
| 290 |
* ---------------------- |
| 291 |
* |
| 292 |
*/ |
| 293 |
|
| 294 |
static void give_parameter(HTStream *me, int p) |
| 295 |
{ |
| 296 |
PUTS(par_name[p]); |
| 297 |
if (me->par_value[p]) { |
| 298 |
PUTS(": "); |
| 299 |
PUTS(me->par_value[p]); |
| 300 |
PUTS("; "); |
| 301 |
} else { |
| 302 |
PUTS(gettext(" NOT GIVEN in source file; ")); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/* Generate Output |
| 307 |
* =============== |
| 308 |
*/ |
| 309 |
static void WSRC_gen_html(HTStream *me, int source_file) |
| 310 |
{ |
| 311 |
if (me->par_value[PAR_DATABASE_NAME]) { |
| 312 |
char *shortname = NULL; |
| 313 |
int l; |
| 314 |
|
| 315 |
StrAllocCopy(shortname, me->par_value[PAR_DATABASE_NAME]); |
| 316 |
l = (int) strlen(shortname); |
| 317 |
if (l > 4 && !strcasecomp(shortname + l - 4, ".src")) { |
| 318 |
shortname[l - 4] = 0; /* Chop of .src -- boring! */ |
| 319 |
} |
| 320 |
|
| 321 |
START(HTML_HEAD); |
| 322 |
PUTC('\n'); |
| 323 |
START(HTML_TITLE); |
| 324 |
PUTS(shortname); |
| 325 |
PUTS(source_file ? gettext(" WAIS source file") : INDEX_SEGMENT); |
| 326 |
END(HTML_TITLE); |
| 327 |
PUTC('\n'); |
| 328 |
END(HTML_HEAD); |
| 329 |
|
| 330 |
START(HTML_H1); |
| 331 |
PUTS(shortname); |
| 332 |
PUTS(source_file ? gettext(" description") : INDEX_SEGMENT); |
| 333 |
END(HTML_H1); |
| 334 |
PUTC('\n'); |
| 335 |
FREE(shortname); |
| 336 |
} |
| 337 |
|
| 338 |
START(HTML_DL); /* Definition list of details */ |
| 339 |
|
| 340 |
if (source_file) { |
| 341 |
START(HTML_DT); |
| 342 |
PUTS(gettext("Access links")); |
| 343 |
MAYBE_END(HTML_DT); |
| 344 |
START(HTML_DD); |
| 345 |
if (me->par_value[PAR_IP_NAME] && |
| 346 |
me->par_value[PAR_DATABASE_NAME]) { |
| 347 |
|
| 348 |
char *WSRC_address = NULL; |
| 349 |
char *www_database; |
| 350 |
|
| 351 |
www_database = HTEscape(me->par_value[PAR_DATABASE_NAME], |
| 352 |
URL_XALPHAS); |
| 353 |
HTSprintf0(&WSRC_address, "%s//%s%s%s/%s", |
| 354 |
STR_WAIS_URL, |
| 355 |
me->par_value[PAR_IP_NAME], |
| 356 |
me->par_value[PAR_TCP_PORT] ? ":" : "", |
| 357 |
(me->par_value[PAR_TCP_PORT] |
| 358 |
? me->par_value[PAR_TCP_PORT] |
| 359 |
: ""), |
| 360 |
www_database); |
| 361 |
|
| 362 |
HTStartAnchor(me->target, NULL, WSRC_address); |
| 363 |
PUTS(gettext("Direct access")); |
| 364 |
END(HTML_A); |
| 365 |
/** Proxy will be used if defined, so let user know that - FM **/ |
| 366 |
PUTS(gettext(" (or via proxy server, if defined)")); |
| 367 |
|
| 368 |
FREE(www_database); |
| 369 |
FREE(WSRC_address); |
| 370 |
|
| 371 |
} else { |
| 372 |
give_parameter(me, PAR_IP_NAME); |
| 373 |
give_parameter(me, PAR_DATABASE_NAME); |
| 374 |
} |
| 375 |
MAYBE_END(HTML_DD); |
| 376 |
|
| 377 |
} |
| 378 |
/* end if source_file */ |
| 379 |
if (me->par_value[PAR_MAINTAINER]) { |
| 380 |
START(HTML_DT); |
| 381 |
PUTS(gettext("Maintainer")); |
| 382 |
MAYBE_END(HTML_DT); |
| 383 |
START(HTML_DD); |
| 384 |
PUTS(me->par_value[PAR_MAINTAINER]); |
| 385 |
MAYBE_END(HTML_DD); |
| 386 |
} |
| 387 |
if (me->par_value[PAR_IP_NAME]) { |
| 388 |
START(HTML_DT); |
| 389 |
PUTS(gettext("Host")); |
| 390 |
MAYBE_END(HTML_DT); |
| 391 |
START(HTML_DD); |
| 392 |
PUTS(me->par_value[PAR_IP_NAME]); |
| 393 |
MAYBE_END(HTML_DD); |
| 394 |
} |
| 395 |
|
| 396 |
END(HTML_DL); |
| 397 |
|
| 398 |
if (me->par_value[PAR_DESCRIPTION]) { |
| 399 |
START(HTML_PRE); /* Preformatted description */ |
| 400 |
PUTS(me->par_value[PAR_DESCRIPTION]); |
| 401 |
END(HTML_PRE); |
| 402 |
} |
| 403 |
|
| 404 |
(*me->target->isa->_free) (me->target); |
| 405 |
|
| 406 |
return; |
| 407 |
} /* generate html */ |
| 408 |
|
| 409 |
static void WSRCParser_put_string(HTStream *context, const char *str) |
| 410 |
{ |
| 411 |
const char *p; |
| 412 |
|
| 413 |
for (p = str; *p; p++) |
| 414 |
WSRCParser_put_character(context, *p); |
| 415 |
} |
| 416 |
|
| 417 |
static void WSRCParser_write(HTStream *context, const char *str, |
| 418 |
int l) |
| 419 |
{ |
| 420 |
const char *p; |
| 421 |
const char *e = str + l; |
| 422 |
|
| 423 |
for (p = str; p < e; p++) |
| 424 |
WSRCParser_put_character(context, *p); |
| 425 |
} |
| 426 |
|
| 427 |
static void WSRCParser_free(HTStream *me) |
| 428 |
{ |
| 429 |
WSRC_gen_html(me, YES); |
| 430 |
#ifdef CACHE_FILE_PREFIX |
| 431 |
write_cache(me); |
| 432 |
#endif |
| 433 |
{ |
| 434 |
int p; |
| 435 |
|
| 436 |
for (p = 0; par_name[p]; p++) { /* Clear out old values */ |
| 437 |
FREE(me->par_value[p]); |
| 438 |
} |
| 439 |
} |
| 440 |
FREE(me); |
| 441 |
} |
| 442 |
|
| 443 |
static void WSRCParser_abort(HTStream *me, HTError e GCC_UNUSED) |
| 444 |
{ |
| 445 |
WSRCParser_free(me); |
| 446 |
} |
| 447 |
|
| 448 |
/* Stream subclass -- method routines |
| 449 |
* --------------- |
| 450 |
*/ |
| 451 |
|
| 452 |
static HTStreamClass WSRCParserClass = |
| 453 |
{ |
| 454 |
"WSRCParser", |
| 455 |
WSRCParser_free, |
| 456 |
WSRCParser_abort, |
| 457 |
WSRCParser_put_character, |
| 458 |
WSRCParser_put_string, |
| 459 |
WSRCParser_write |
| 460 |
}; |
| 461 |
|
| 462 |
/* Converter from WAIS Source to whatever |
| 463 |
* -------------------------------------- |
| 464 |
*/ |
| 465 |
HTStream *HTWSRCConvert(HTPresentation *pres, HTParentAnchor *anchor, |
| 466 |
HTStream *sink) |
| 467 |
{ |
| 468 |
HTStream *me = (HTStream *) malloc(sizeof(*me)); |
| 469 |
|
| 470 |
if (!me) |
| 471 |
outofmem(__FILE__, "HTWSRCConvert"); |
| 472 |
|
| 473 |
me->isa = &WSRCParserClass; |
| 474 |
me->target = HTML_new(anchor, pres->rep_out, sink); |
| 475 |
|
| 476 |
{ |
| 477 |
int p; |
| 478 |
|
| 479 |
for (p = 0; p < PAR_COUNT; p++) { /* Clear out parameter values */ |
| 480 |
me->par_value[p] = NULL; |
| 481 |
} |
| 482 |
} |
| 483 |
me->state = beginning; |
| 484 |
|
| 485 |
return me; |
| 486 |
} |