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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
/*
* This source code is part of hsc, a html-preprocessor,
* Copyright (C) 1995-1998 Thomas Aglassinger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* hsclib/linit.c
*
* functions to init & read preferences for a hsc-process
*
* updated: 17-Dec-1997
* created: 19-Feb-1996
*/
#include "hsclib/inc_base.h"
#include "hsclib/defattr.h"
#include "hsclib/deftag.h"
#include "hsclib/include.h"
#include "hsclib/parse.h"
#include "hsclib/tag_a.h"
#include "hsclib/tag_hsc.h"
#include "hsclib/tag_if.h"
#include "hsclib/tag_macro.h"
#include "hsclib/tag_misc.h"
#include "ugly/fname.h"
#if DEBUG
/*
* debugging print functions
*/
static VOID prt_ent(FILE * stream, APTR data)
{
HSCENT *ent = (HSCENT *) data;
if (ent)
{
fprintf(stream, " %s", ent->name);
if (ent->replace)
fprintf(stream, "=%s", ent->replace);
if (ent->numeric)
fprintf(stream, "=%ld", ent->numeric);
}
else
fprintf(stream, " <NULL>");
}
static VOID prt_tag(FILE * stream, APTR data)
{
if (data)
{
HSCTAG *tag = (HSCTAG *) data;
fprintf(stream, " <");
if (tag->option & HT_CLOSE)
fprintf(stream, "/");
if (tag->o_handle)
fprintf(stderr, "*");
fprintf(stream, "%s", tag->name);
if (tag->c_handle)
fprintf(stderr, "*");
if (tag->option & HT_REQUIRED)
fprintf(stream, "/r");
if (tag->option & HT_RECOMMENDED)
fprintf(stream, "/rcmd");
if (tag->option & HT_ONLYONCE)
fprintf(stream, "/1");
if (tag->option & HT_AUTOCLOSE)
fprintf(stream, "/sc");
if (tag->option & HT_SPECIAL)
fprintf(stream, "/a");
fprintf(stream, ">");
}
else
fprintf(stream, " <NULL>");
}
#endif
/* function to temporarily disable debuggin output */
#if 1
static BOOL dbg_old = FALSE;
#define dbg_disable(hp) {dbg_old = hp->debug; hp->debug=FALSE;}
#define dbg_restore(hp) {hp->debug=dbg_old;}
#else
#define dbg_disable(hp)
#define dbg_restore(hp)
#endif
/*
* find_prefs_fname
*
* find preferences file: first check, if it is located
* somewhere in the paths given via CONFIG_PATH (which
* is a system depandent symbol); if not, check if it
* is in the path described in the envvar HSCPREFS
*
* result: full path & filename of prefs or NULL if not found
*
*/
static STRPTR find_prefs_fname(HSCPRC * hp, EXPSTR *cfgfn)
{
#define ENV_HOME "HOME"
STRPTR prefs_fname = NULL;
STRPTR paths[] = /* paths to search for config file */
{"", "", "", CONFIG_PATH,
NULL, NULL};
STRPTR path = NULL;
UBYTE path_ctr = 0;
FILE *cfgf = NULL; /* prefs file */
EXPSTR *hscpathstr = init_estr(32); /* buffer to read $HSCPATH */
EXPSTR *homepathstr = init_estr(32); /* buffer to read $HOME */
/* add "$HSCPATH/hsc.prefs" to files-to-be-checked */
if (link_envfname(hscpathstr, ENV_HSCPATH, NULL, NULL))
{
/* add envval to paths */
paths[1] = estr2str(hscpathstr);
}
/* add "$HOME/lib/hsc.prefs" to files-to-be-checked */
if (link_envfname(homepathstr, ENV_HOME, "lib", NULL))
{
/* add envval to paths */
paths[2] = estr2str(homepathstr);
}
/* try to open any prefs-file */
do
{ /* loop: */
path = paths[path_ctr]; /* get next path */
if (path)
{ /* is it the last one? */
set_estr(cfgfn, path); /* N->generate filename */
app_estr(cfgfn, CONFIG_FILE);
DC(fprintf(stderr, DHL "try \"%s\"\n", estr2str(cfgfn)));
cfgf = fopen(estr2str(cfgfn), "r"); /* try to open file */
}
path_ctr++; /* process next path */
}
while (path && (!cfgf)); /* until no path left or file opened */
if (cfgf)
{
prefs_fname = estr2str(cfgfn);
fclose(cfgf);
}
del_estr(homepathstr);
del_estr(hscpathstr);
return (prefs_fname);
}
#if 0
/*
* hsc_read_base_info
*
* try to open base-file and read where in memory
* information about defined tags/attributes/entities
* is located.
*
* result: dummy-hsc-process that only contains
* the information read from base-file
*
* see "LoadHscPrefs/LoadHscPrefs.c"
*/
HSCPRC *hsc_read_base_info(VOID)
{
HSCPRC *dummy_hp = NULL;
#ifdef HSCBASE_FILE
FILE *inpf = fopen(HSCBASE_FILE, "r");
DLLIST *hp_deftag = NULL;
DLLIST *hp_defattr = NULL;
DLLIST *hp_defent = NULL;
if (inpf)
{
STRARR s[32];
APTR p = NULL;
while (fscanf(inpf, "%s %p\n", &(s[0]), &p) != EOF)
{
if (!strcmp("DEFTAG", s))
{
hp_deftag = (DLLIST *) p;
printf(DHL "deftag %p\n", hp_deftag);
}
else if (!strcmp("DEFATTR", s))
{
hp_defattr = (DLLIST *) p;
printf(DHL "defattr %p\n", hp_defattr);
}
else if (!strcmp("DEFENT", s))
{
hp_defent = (DLLIST *) p;
printf(DHL "defent %p\n", hp_defent);
}
else
{
printf(DHL "%s %p (unknown)\n", s, p);
}
}
}
fclose(inpf);
if (hp_deftag && hp_defattr && hp_defent)
{
/* assign information to dummy-process */
dummy_hp = new_hscprc();
del_dllist(dummy_hp->defattr);
del_dllist(dummy_hp->defent);
del_dllist(dummy_hp->deftag);
dummy_hp->deftag = hp_deftag;
dummy_hp->defattr = hp_defattr;
dummy_hp->defent = hp_defent;
/* assign new del_data-methodes */
dummy_hp->defattr->del_data = del_hscattr;
dummy_hp->defent->del_data = del_entity;
dummy_hp->deftag->del_data = del_hsctag;
}
#endif
return (dummy_hp);
}
BOOL hsc_copy_base_info(HSCPRC * dest_hp, HSCPRC * dummy_hp)
{
DLNODE *nd = dummy_hp->deftag->first;
/* copy defined tags */
while (nd)
{
HSCTAG *newtag = cpy_hsctag((HSCTAG *) nd->data);
app_dlnode(dest_hp->deftag, (APTR) newtag);
nd = nd->next;
}
return (TRUE);
}
#endif
/*
* hsc_read_prefs
*
* try to open (any) config file and read preferences
* from it
*/
BOOL hsc_read_prefs(HSCPRC * hp, STRPTR prefs_fname)
{
BOOL ok = FALSE;
EXPSTR *prefs_name_buffer = init_estr(32);
/* find prefs file */
if (!prefs_fname)
{
prefs_fname = find_prefs_fname(hp, prefs_name_buffer);
}
/* status message */
if (prefs_fname)
{
dbg_disable(hp);
hsc_status_file_begin(hp, prefs_fname);
ok = hsc_include_file(hp, prefs_fname,
IH_PARSE_HSC | IH_NO_STATUS);
dbg_restore(hp);
if (ok)
{
EXPSTR *msg = init_estr(32);
set_estr(msg, prefs_fname);
app_estr(msg, ": preferences read");
hsc_status_misc(hp, estr2str(msg));
del_estr(msg);
}
}
else
{
hsc_message(hp, MSG_NO_CONFIG,
"can not open preferences file");
}
del_estr(prefs_name_buffer);
return (ok);
}
/*
* hsc_set_tagCB
*/
VOID hsc_set_tagCB(HSCPRC * hp, STRPTR name,
BOOL(*op_hnd) (HSCPRC * inpf, HSCTAG * tag),
BOOL(*cl_hnd) (HSCPRC * inpf, HSCTAG * tag))
{
HSCTAG *tag = find_strtag(hp->deftag, name);
if (tag && !(tag->option & HT_NOHANDLE))
{
/* set handles */
DC(fprintf(stderr, DHL "add handles for <%s> (%p,%p)\n",
name, op_hnd, cl_hnd));
tag->o_handle = op_hnd;
tag->c_handle = cl_hnd;
}
}
/*
* init_hsctags
*
* define hsc tags & attributes; assign tagCBs for them
*
* NOTE: this ones tricky, but a bit perverted somehow
*/
BOOL hsc_init_tagsNattr(HSCPRC * hp)
{
#define INCLUDE_ATTR " PRE:bool SOURCE:bool TEMPORARY:bool" \
" INDENT:num TABSIZE:num=\"4\" "
BYTE i = 0;
BOOL ok = TRUE;
BOOL open_tag;
HSCTAG *tag;
/* define hsc internal tags */
STRPTR hsc_prefs[] =
{
/* tags with special chars as name
*
* IMPORTANT: When adding new tags with names not starting with
* HSC_TAGID, make sure to update `tag.c:is_hsc_tag()'
*/
HSC_COMMENT_STR " /SKIPLF /SPECIAL>",
HSC_VERBATIM_STR" /SPECIAL>",
HSC_INSEXPR_STR " /SPECIAL>",
/* tags starting with HSC_TAGID */
HSC_CONTENT_STR " /SKIPLF>",
HSC_DEFENT_STR " /SKIPLF NAME:string/r RPLC:string NUM:num>",
HSC_DEFICON_STR " /SKIPLF NAME:string/r>",
HSC_DEFINE_STR " /SKIPLF /SPECIAL>",
HSC_DEFTAG_STR " /SKIPLF /SPECIAL>",
HSC_DEPEND_STR " /SKIPLF ON:string/r FILE:bool>",
HSC_ELSE_STR " /SKIPLF /MBI=\"" HSC_IF_STR "\">",
HSC_ELSEIF_STR " /SKIPLF /MBI=\"" HSC_IF_STR "\"" CONDITION_ATTR ":bool>",
HSC_MESSAGE_STR " /SKIPLF TEXT:string/r CLASS:enum(\"note|warning|error|fatal\")='note'>",
HSC_EXEC_STR " /SKIPLF COMMAND:string/r REMOVE:enum(\"on|off|auto\")=\"auto\" ATTRIBUTE:string INCLUDE:bool FILE:string " INCLUDE_ATTR ">",
HSC_EXPORT_STR " /SKIPLF FILE:string/r DATA:string/r APPEND:bool>",
HSC_IF_STR " /SKIPLF /CLOSE " CONDITION_ATTR ":bool>",
HSC_INSERT_STR " /OBSOLETE TEXT:string TIME:bool FORMAT:string>",
HSC_INCLUDE_STR " /SKIPLF FILE:string/r " INCLUDE_ATTR ">",
HSC_LAZY_STR " /SKIPLF /SPECIAL>",
HSC_LET_STR " /SKIPLF /SPECIAL>",
HSC_MACRO_STR " /SKIPLF /SPECIAL>",
HSC_SOURCE_STR " /SKIPLF PRE:bool>",
HSC_STRIPWS_STR " TYPE:enum(\"" STRIPWS_ENUM "\")=\"" STRIPWS_BOTH "\">",
NULL
};
STRPTR hsc_attribs[] =
{
/*
* define hsc attributes
*/
/* name : type : default value */
SYSTEM_ATTR ":string/c" , SYSTEM_ATTR_ID,
ANCHOR_ATTR ":string" , "this is a feature, not a bug",
CONTENT_ATTR ":string/c" , NULL,
RESULT_ATTR ":num=\"0\"", NULL, /* a bit strange */
FILESIZEFORMAT_ATTR ":string" , "%a%u",
TIMEFORMAT_ATTR ":string" , "%d-%b-%Y, %H:%M",
LINEFEED_ATTR ":string>" , NULL,
NULL, NULL};
/* temporarily disable debugging output */
dbg_disable(hp);
/* define hsc-tags */
i = 0;
while (!(hp->fatal) && hsc_prefs[i])
{
STRARR infname[20];
sprintf(infname, SPECIAL_FILE_ID "init%3d", i);
hp->inpf = infopen_str(infname, hsc_prefs[i], 60);
if (hp->inpf)
{
tag = def_tag_name(hp, &open_tag);
ok = (tag && def_tag_args(hp, tag));
infclose(hp->inpf);
}
i++;
}
/* init hsc-attributes */
i = 0;
while (!(hp->fatal) && hsc_attribs[i])
{
define_attr_by_text(hp, hsc_attribs[i], hsc_attribs[i+1], 0);
i+=2;
}
/* assign "\n" to linefeed-attribute */
set_vartext(find_varname(hp->defattr, LINEFEED_ATTR), "\n");
/* assign tag-callbacks to hsc-tags */
if (ok)
{
hsc_set_tagCB(hp, HSC_COMMENT_STR, handle_hsc_comment, NULL);
hsc_set_tagCB(hp, HSC_CONTENT_STR, handle_hsc_content, NULL);
hsc_set_tagCB(hp, HSC_DEFENT_STR, handle_hsc_defent, NULL);
hsc_set_tagCB(hp, HSC_DEFICON_STR, handle_hsc_deficon, NULL);
hsc_set_tagCB(hp, HSC_DEFINE_STR, handle_hsc_define, NULL);
hsc_set_tagCB(hp, HSC_DEFTAG_STR, handle_hsc_deftag, NULL);
hsc_set_tagCB(hp, HSC_DEPEND_STR, handle_hsc_depend, NULL);
hsc_set_tagCB(hp, HSC_ELSE_STR, handle_hsc_else, NULL);
hsc_set_tagCB(hp, HSC_ELSEIF_STR, handle_hsc_elseif, NULL);
hsc_set_tagCB(hp, HSC_EXEC_STR, handle_hsc_exec, NULL);
hsc_set_tagCB(hp, HSC_EXPORT_STR, handle_hsc_export, NULL);
hsc_set_tagCB(hp, HSC_IF_STR, handle_hsc_if, handle_hsc_cif);
hsc_set_tagCB(hp, HSC_INCLUDE_STR, handle_hsc_include, NULL);
hsc_set_tagCB(hp, HSC_INSERT_STR, handle_hsc_insert, NULL);
hsc_set_tagCB(hp, HSC_INSEXPR_STR, handle_hsc_insert_expression, NULL);
hsc_set_tagCB(hp, HSC_COMMENT_STR, handle_hsc_comment, NULL);
hsc_set_tagCB(hp, HSC_LAZY_STR, handle_hsc_lazy, NULL);
hsc_set_tagCB(hp, HSC_LET_STR, handle_hsc_let, NULL);
hsc_set_tagCB(hp, HSC_MACRO_STR, handle_hsc_macro, NULL);
hsc_set_tagCB(hp, HSC_MESSAGE_STR, handle_hsc_message, NULL);
hsc_set_tagCB(hp, HSC_VERBATIM_STR, handle_hsc_verbatim, NULL);
hsc_set_tagCB(hp, HSC_SOURCE_STR, handle_hsc_source, NULL);
hsc_set_tagCB(hp, HSC_STRIPWS_STR, handle_hsc_stripws, NULL);
}
/* restore debugging output */
dbg_restore(hp);
return (ok);
}
/*
* hsc_init_basicEntities
*
* create basic entities (&, >, <, ")
* (should be called BEFORE hsc_read_prefs())
*/
BOOL hsc_init_basicEntities(HSCPRC * hp)
{
BOOL ok = TRUE;
/* entities */
ok &= add_ent(hp->defent, "amp", NULL, 0); /* & */
ok &= add_ent(hp->defent, "lt", NULL, 0); /* < */
ok &= add_ent(hp->defent, "gt", NULL, 0); /* > */
ok &= add_ent(hp->defent, "quot", NULL, 0); /* q */
return (ok);
}
/*
* hsc_assign_tagCBs
*
* assign tag-callbacks to standard html-tags
* (MUST ONLY be called AFTER hsc_read_prefs())
*/
BOOL hsc_assign_tagCBs(HSCPRC * hp)
{
hsc_set_tagCB(hp, "!", handle_sgml_comment, NULL);
hsc_set_tagCB(hp, "A", handle_anchor, handle_canchor);
hsc_set_tagCB(hp, "BASE", handle_base, NULL);
hsc_set_tagCB(hp, "BLINK", handle_blink, NULL);
hsc_set_tagCB(hp, "H1", handle_heading, NULL);
hsc_set_tagCB(hp, "H2", handle_heading, NULL);
hsc_set_tagCB(hp, "H3", handle_heading, NULL);
hsc_set_tagCB(hp, "H4", handle_heading, NULL);
hsc_set_tagCB(hp, "H5", handle_heading, NULL);
hsc_set_tagCB(hp, "H6", handle_heading, NULL);
hsc_set_tagCB(hp, "IMG", handle_img, NULL);
hsc_set_tagCB(hp, "PRE", handle_pre, handle_end_pre);
return (TRUE);
}
/*
* hsc_init_hscprc
*
* - init all items of hsc-process
* - create hsc-tags & special atttributes
* - read preferences file
* - assign tag-callbacks
*/
BOOL hsc_init_hscprc(HSCPRC * hp, STRPTR prefs_fname)
{
BOOL ok = FALSE; /* return value */
if (hsc_init_tagsNattr(hp)
&& hsc_init_basicEntities(hp)
&& hsc_read_prefs(hp, prefs_fname)
&& hsc_assign_tagCBs(hp)
)
{
/* return sucess */
ok = TRUE;
/* printf list of defined tags & entities */
DC(
{
DLNODE * nd;
HSCENT * ent;
nd = hp->defent->first;
if (nd)
{
fprintf(stderr, DHL "Known entities:");
while (nd)
{
ent = (HSCENT *) nd->data;
fprintf(stderr, " &%s;", ent->name);
nd = nd->next;
}
fprintf(stderr, "\n");
}
nd = hp->deftag->first;
if (nd)
{
fprintf(stderr, DHL "Known tags:");
while (nd)
{
prt_tag(stderr, nd->data);
nd = nd->next;
}
fprintf(stderr, "\n");
}
}
); /* DC */
}
hp->click_here_str =
get_vartext_byname(hp->defattr, CLICK_HERE_ATTR);
hp->color_names =
get_vartext_byname(hp->defattr, COLOR_NAMES_ATTR);
return (ok);
}
|