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
|
/*
* 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/include.c
*
* hsc include functions
*
* updated: 16-May-1997
* created: 19-Feb-1996
*/
#include <time.h>
#include "hsclib/inc_base.h"
#include "ugly/ufile.h"
#include "ugly/fname.h"
#include "hsclib/input.h"
#include "hsclib/parse.h"
#define NOEXTERN_HSCLIB_INCLUDE_H
#include "hsclib/include.h"
/*
* hsc_include
*
* read from inpf, parse for hsc-commands and execute them,
* check for html-error,
* write all out to outf and close input file.
*
* params: inpfnm...input file name
* outf.....output file structure, already opended
*
* result: TRUE, if all worked well, else FALSE
*/
static BOOL hsc_include(HSCPRC * hp, INFILE * inpf, ULONG optn, INFILEPOS * base_pos)
{
BOOL ok; /* result */
ok = (inpf != NULL);
if (optn & IH_POS_PARENT)
{
panic("IH_POS_PARENT set");
}
if (inpf) /* file opened? */
{
#if (defined MSDOS) /* HSC_YOUR */
/* check input > 32K */
if (estrlen(inpf->lnbuf) > (32*1024))
{
fprintf(stderr, "** input file too huge\n");
exit(RC_FAIL);
}
#endif
/* push current input file on input-file-stack */
if (hp->inpf)
{
ins_dlnode(hp->inpf_stack, hp->inpf_stack->first, (APTR) hp->inpf);
}
/* set new base position for input-file */
/* (if called from a macro or after eg. <$source>) */
if (base_pos)
{
set_infile_base(inpf, base_pos);
}
/* assign new input file to hsc-process */
hp->inpf = inpf;
/* hide status? */
if ((optn & IH_PARSE_MACRO) || (optn & IH_PARSE_MACRO))
{
optn |= IH_NO_STATUS;
}
/* set char-parse methods */
inpf->is_nc = hsc_normch; /* set is_nc-methode */
inpf->is_ws = hsc_whtspc; /* set is_ws-methode */
/* status message: reading new file */
if (!(optn & IH_NO_STATUS) && !infeof(inpf))
{
hsc_status_file_begin(hp, infget_fname(hp->inpf));
}
/* parse file */
while (!infeof(inpf) && ok)
{
if (!(optn & IH_NO_STATUS) &&
(hp->prev_status_line != infget_y(hp->inpf))
)
{
/* status message */
hsc_status_line(hp);
hp->prev_status_line = infget_y(hp->inpf);
}
/* parse next item */
if (optn & IH_PARSE_SOURCE)
{
ok = hsc_parse_source(hp);
}
else
{
ok = hsc_parse(hp);
}
}
/* parse at end: check for missing tags, .. */
if (ok && (optn & IH_PARSE_END))
{ /* parse end (unclosed tags etc) */
ok = hsc_parse_end(hp);
if (ok && (optn & IH_UPDATE_PRJ))
{
/* update project file */
ok = hsc_parse_end_id(hp);
}
}
/* end of file status */
if (!(optn & IH_NO_STATUS))
{
/* status message: file processed */
hsc_status_file_end(hp);
}
/* close file */
infclose(hp->inpf);
/* pull previous input file from input-file-stack
* or end hsc-process */
if (hp->inpf_stack->first)
{
/* pull first item from stack */
hp->inpf = (INFILE *) hp->inpf_stack->first->data;
hp->inpf_stack->first->data = NULL;
del_dlnode(hp->inpf_stack, hp->inpf_stack->first);
}
else
{
hp->inpf = NULL;
}
}
else
{
panic("no input file");
}
return (ok);
}
/*
* hsc_include_file
*
* open input file and include it
*/
BOOL hsc_base_include_file(HSCPRC * hp, STRPTR filename, ULONG optn, INFILEPOS * base_pos)
{
BOOL ok = FALSE;
INFILE *inpf = NULL;
/* status message: reading input */
if (!(optn & (IH_PARSE_MACRO | IH_PARSE_HSC)))
{
hsc_status_file_begin(hp, filename);
}
/* check for stdin to use as input-file */
if (!strcmp(filename, FILENAME_STDIN))
filename = NULL;
/* open & read input file */
errno = 0;
inpf = infopen(filename, ES_STEP_INFILE);
if (inpf)
{
/* include opened file */
ok = hsc_include(hp, inpf, optn, base_pos);
/* check if this file is the main-source-file
* or an include-file and update project-data
* if neccessary
*/
if (ok && hp->project)
{
if (optn & IH_IS_SOURCE)
{
if (!filename)
filename = FILENAME_STDIN;
D(fprintf(stderr, DHL "INCLUDE source: `%s'\n", filename));
hsc_project_set_source(hp->project, filename);
/*reallocstr(&(hp->document->sourcename), filename); */
}
/* check if this file is an include-file
* and update project-data if neccessary */
if (filename && (optn & IH_IS_INCLUDE))
{
D(fprintf(stderr, DHL "INCLUDE subfile: `%s'\n", filename));
hsc_project_add_include(hp->project, filename);
}
}
}
else
{
hsc_msg_noinput(hp, filename); /* couldn't open file */
}
return (ok);
}
/*
* hsc_include_string
*
* open string as input file and include it
*/
BOOL hsc_base_include_string(HSCPRC * hp, STRPTR filename, STRPTR s, ULONG optn, INFILEPOS * base_pos)
{
BOOL ok;
INFILE *inpf = NULL;
if (optn & IH_POS_PARENT)
{
filename = PARENT_FILE_ID;
optn &= ~IH_POS_PARENT;
}
inpf = infopen_str(filename, s, 0); /* try to open input file */
ok = hsc_include(hp, inpf, optn, base_pos);
return (ok);
}
/*
* find_includefile
*
* scan all include-directories for file to be opened;
* if the file can't be found, the filename in the current
* directory will be returned (which should therefor result
* in an error while processing hsc_include())
*
* NOTE: this function considers the file to be found if it could have
* been opened for input. afterwards, the file is immediatly
* closed. This isn't really a multitasking-conform behavior
* because the file will have to be reopend again by the
* hsc_include() function later and meanwhile could have been
* removed by another task.
*/
static BOOL find_includefile(HSCPRC *hp, EXPSTR * dest, STRPTR filename)
{
BOOL found = FALSE;
/* reset filename */
set_estr(dest, filename);
if (!fexists(filename))
{
DLNODE *nd = dll_first(hp->include_dirs);
/* process all include-directories.. */
while (nd && !found)
{
/* concat incdir+filename, check if it exists,
* process next or abort loop */
link_fname(dest, (STRPTR) dln_data(nd), filename);
D(fprintf(stderr, DHL " try `%s'\n", estr2str(dest)));
if (fexists(estr2str(dest)))
{
found = TRUE;
}
else
nd = dln_next(nd);
}
}
else
{
/* found in current directory */
found = TRUE;
}
if (found)
{
D(fprintf(stderr, DHL " found at `%s'\n", estr2str(dest)));
}
return (found);
}
/* hsc_include_file: include file without base-position */
BOOL hsc_include_file(HSCPRC * hp, STRPTR filename, ULONG optn)
{
BOOL ok = FALSE;
EXPSTR *real_filename = init_estr(64);
/* scan include directories */
find_includefile(hp, real_filename, filename);
/* now include file */
ok = hsc_base_include_file(hp, estr2str(real_filename), optn, NULL);
/* cleanup */
del_estr(real_filename);
return ok;
}
/* hsc_include_string: include string without base-position */
BOOL hsc_include_string(HSCPRC * hp, STRPTR filename, STRPTR s, ULONG optn)
{
return (hsc_base_include_string(hp, filename, s, optn, NULL));
}
|