| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1988-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 |
.TYPE Module |
| 30 |
.IDENTIFICATION tex.c |
| 31 |
.AUTHOR Francois Ochsenbein [ESO-IPG] |
| 32 |
.LANGUAGE C |
| 33 |
.KEYWORDS Text scanning |
| 34 |
.ENVIRONMENT Any |
| 35 |
.COMMENTS |
| 36 |
\begin{TeX} |
| 37 |
This module includes the necessary functions to scan a piece of TeX |
| 38 |
text, and to perform the macro definitions / substitutions. |
| 39 |
The actions are executed \via the functions (included in the TeX structure): |
| 40 |
\begin{itemize} |
| 41 |
\item output function (text, length) which performs the |
| 42 |
actual output. A typical example of such a function could be |
| 43 |
{\tt write(1,{\rm text, length})}. |
| 44 |
\item action routine, triggered by the macro {\tt \b action\{{\em text}\}}. |
| 45 |
Parameters are the {\em text} action and the length of this action. |
| 46 |
Return is 0 / -1 (Stop) / len |
| 47 |
\end{itemize} |
| 48 |
|
| 49 |
All these user-defined functions return the number of bytes processed; a 0 value |
| 50 |
indicates ``stop processing'' |
| 51 |
|
| 52 |
Standard actions (calling the action routine, with a one-byte argument) |
| 53 |
are: |
| 54 |
$$\begin{tabular}{|ll|} \hline |
| 55 |
\{ &triggered by \{\\ |
| 56 |
\} &triggered by \}\\ |
| 57 |
\ &stretchable blank (one or several blanks / newlines / tabs)\\ |
| 58 |
\b r &triggered by \b\b (go to next line)\\ |
| 59 |
\b n &new paragraph (triggered by blank lines)\\ |
| 60 |
\^{ } & triggered by the carret\\ |
| 61 |
\_ &triggered by the underscore\\ |
| 62 |
\& &triggered by the ampersand\\ |
| 63 |
\$ &triggered by the \$ sign\\ |
| 64 |
1\dots9 &returns in TeX structure the address of parameter \#$n$\\ |
| 65 |
\hline |
| 66 |
\end{tabular}$$ |
| 67 |
|
| 68 |
Primitives macros are: |
| 69 |
\begin{itemize} |
| 70 |
\item {\tt\b def} macro |
| 71 |
\eg {\tt\b def\b{\em name}\{{\em equivalence}\}} |
| 72 |
\item {\tt\b action\{{\em action string}\}} |
| 73 |
\item {\tt\b begin\{verbatim\}} and {\tt\b end\{verbatim\}} |
| 74 |
\item {\tt\b len\{{\em text}\}} computes length of a text |
| 75 |
\item {\tt\b input\{{\em filename}\}} |
| 76 |
\item {\tt\b inputverbatim\{{\em filename}\}} |
| 77 |
\item {\tt\b defenv} macro, as {\tt\b defenv\b{\em name[\#$n$]}\{{\em |
| 78 |
start options}\}\{{\em end options}\} }. |
| 79 |
It is suggested to use {\tt\b0} as start option, triggering |
| 80 |
action $0$ and providing in the TeX structure the environment |
| 81 |
name. |
| 82 |
\item {\tt\b b} to issue a backslash. |
| 83 |
\item {\tt\b EOF} end-of-file marker |
| 84 |
\item {\tt \~{ }} non-breakable space |
| 85 |
\item {\tt \b\b} go to next line |
| 86 |
\item {\tt \b\{} \quad {\tt\b\}} \quad {\tt\b\$} \quad {\tt\b\_} |
| 87 |
\quad {\tt\b\^{ }} \quad {\tt\b\~{ }} \quad {\tt\b\#} \quad {\tt\b\&} |
| 88 |
to issue the single (escape) character |
| 89 |
\item {\tt\b iftrue} {\tt\b iffalse} {\tt\b else} {\tt\b fi} {\tt\b ifdef} |
| 90 |
for tests |
| 91 |
\item {\tt\b obeylines} {\tt\b obeyspaces} {\tt\b noobey} to keep |
| 92 |
the line breaks / spaces \\ |
| 93 |
{\br Remark}: the {\tt \{verbatim\}} mode stops the {\tt obey} mode. |
| 94 |
\item {\tt\b today} {\tt\b now} {\tt\b time} |
| 95 |
for current date / time |
| 96 |
\item {\tt\b0} to {\tt\b9} call action routine, setting parameter \#$n$ |
| 97 |
in the TeX structure |
| 98 |
\end{itemize} |
| 99 |
|
| 100 |
{\bf Description of Method} |
| 101 |
|
| 102 |
Pieces of text to scan are defined via {\em SDV} descriptors, which are |
| 103 |
used here instead of standard character strings. |
| 104 |
The text after substitution is stacked into the {\em substitute} |
| 105 |
buffer, which can be automatically expanded. |
| 106 |
|
| 107 |
An {\em SDV} descriptor contains: |
| 108 |
\begin{itemize} |
| 109 |
\item the position of the text to scan, as a {\em text} char pointer |
| 110 |
and an {\em offset} number. The {\em text} pointer is null |
| 111 |
if the text is located in the {\em substitute} buffer, |
| 112 |
because the address may change when the {\em substitute} buffer |
| 113 |
is expanded. |
| 114 |
\item the {\em length} of the text |
| 115 |
\item a {\em pos} position of the text in the {\em substitute} buffer, |
| 116 |
taking a value of $-1$ if the text is not in the {\em substitute} |
| 117 |
buffer or is not to be deallocated from this buffer. |
| 118 |
\end{itemize} |
| 119 |
|
| 120 |
Macro definitions are stored as a hash table, which address is a part |
| 121 |
of the TeX structure. The equivalence string of a macro in the |
| 122 |
hash table is a character string, starting with a byte indicating |
| 123 |
the number of parameters. |
| 124 |
|
| 125 |
Two functions, included in the TeX structure, are required: |
| 126 |
\begin{enumerate} |
| 127 |
\item the {\em output} function, called to output the substituted text |
| 128 |
\item the {\em action} function, called when one of the actions |
| 129 |
listed above is found, or when the {\tt\b action} macro |
| 130 |
is encountered. |
| 131 |
\end{enumerate} |
| 132 |
|
| 133 |
\end{TeX} |
| 134 |
|
| 135 |
.VERSION 1.0 25-Apr-1988: Creation |
| 136 |
.VERSION 1.1 26-May-1988: Added \if \else \fi \today \now \time \len |
| 137 |
.VERSION 1.2 13-Oct-1988: Cosmetic Modifications |
| 138 |
.VERSION 1.3 14-Mar-1989: Removed bug in tex_subs (\iffalse executions) |
| 139 |
.VERSION 1.4 29-Mar-1989: Include file default extension = ``.tex'' |
| 140 |
Also corrected \if's stacks |
| 141 |
.VERSION 1.5 21-Jul-1989: Removed bug in getparm |
| 142 |
.VERSION 1.6 22-Mar-1990: Changed tex_rescan to have another parameter |
| 143 |
asking not to record errors. |
| 144 |
.VERSION 1.7 27-Oct-1990: Added call to atex_ to force inclusion of |
| 145 |
the atex module (problem with Guu-cc on Vax/Vms) |
| 146 |
.VERSION 1.8 21-Nov-1990: Added \obeylines \obeyspaces facility. |
| 147 |
|
| 148 |
090831 last modif |
| 149 |
----------------------------------------------------------------------------*/ |
| 150 |
|
| 151 |
#define DEBUG 0 /* For debugging only */ |
| 152 |
|
| 153 |
#define PM_LEVEL LEVEL_STR |
| 154 |
|
| 155 |
#include <midas_def.h> |
| 156 |
|
| 157 |
#define PASCAL_DEF 0 |
| 158 |
#include <stesodef.h> |
| 159 |
#include <tex.h> /* Definition of TeX structure */ |
| 160 |
#include <atex.h> /* Definition of TeX attributes */ |
| 161 |
#include <hash.h> /* Uses a Hash table for Macros */ |
| 162 |
#include <buffer.h> /* Need to know buffers */ |
| 163 |
#include <trtime.h> /* Time operations */ |
| 164 |
#include <string.h> |
| 165 |
|
| 166 |
extern int eh_ed_str2(), eh_ed_i(), eh_put1(), ed_t(), h_log(); |
| 167 |
extern int pm_enter(), pm_ed_trace(), pm_iexit(), pm_ed_tr2(); |
| 168 |
extern int fi_load(); |
| 169 |
|
| 170 |
extern unsigned char *atex_(); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
int tex_unit(); |
| 176 |
|
| 177 |
|
| 178 |
#define HSIZE 200 /* Default Size of Hash table */ |
| 179 |
#define DEFAULT_EXTENSION ".tex" /* Default extension for include files */ |
| 180 |
#define SUBSTITUTE_MAX 32 /* Maximal substitution */ |
| 181 |
#define IFS_MAX 16 /* Maximal embedded if's */ |
| 182 |
|
| 183 |
typedef struct s_SDV{ /* Descriptor of pieces of memory */ |
| 184 |
int pos; /* Position in substitute buffer */ |
| 185 |
int offset; /* Present position */ |
| 186 |
int len; /* Length of string */ |
| 187 |
char *text; /* Text to scan: NULL if in substitute buffer */ |
| 188 |
} SDV; |
| 189 |
typedef struct { |
| 190 |
short len; |
| 191 |
char buf[TeX_BUF]; |
| 192 |
} OUT_BUF; |
| 193 |
static OUT_BUF *out_buf; |
| 194 |
|
| 195 |
static unsigned char ifs[IFS_MAX]; |
| 196 |
static unsigned char *ifc; |
| 197 |
#define ifs_min (&ifs[0]) |
| 198 |
#define ifs_max (&ifs[IFS_MAX]) |
| 199 |
|
| 200 |
#define StackIFS() if (++ifc >= ifs_max) { \ |
| 201 |
ERROR("Too many embedded \\if's"); \ |
| 202 |
ifc--; status = NOK; FINISH; } \ |
| 203 |
else *ifc = *(ifc-1) |
| 204 |
#define UnstackIFS() if (ifc > ifs_min) ifc-- |
| 205 |
|
| 206 |
static SDV sdv_pieces[SUBSTITUTE_MAX]; |
| 207 |
#define sdv_min (&sdv_pieces[0]) |
| 208 |
#define sdv_max (&sdv_pieces[SUBSTITUTE_MAX]) |
| 209 |
static SDV *sdvc; /* Current piece to substitute */ |
| 210 |
static SDV sdvb0 = {-1, 0, 1, (char *)0}; /* Blank descriptor */ |
| 211 |
|
| 212 |
static SDV ssdvp[10]; /* Where are the parameters */ |
| 213 |
static SDV *sdvp; /* Parameters addresses */ |
| 214 |
|
| 215 |
#define GetString(sdv) ((sdv)->text ? (sdv)->text : substitute->buf) |
| 216 |
#define GetText(sdv) GetString(sdv) + (sdv)->offset |
| 217 |
#define Text(string,sdv) string + (sdv)->offset |
| 218 |
#define Length(sdv) ((sdv)->len - (sdv)->offset) |
| 219 |
#define GobbleSpaces(s,sdv) \ |
| 220 |
(sdv)->offset += skip_spaces(Text(s,sdv),Length(sdv),\ |
| 221 |
(char *)0) |
| 222 |
#define GobbleNewlines(s,sdv,ab) \ |
| 223 |
(sdv)->offset += skip_spaces(Text(s,sdv),Length(sdv),\ |
| 224 |
ab) |
| 225 |
#define StackSDV() if (++sdvc >= sdv_max) { \ |
| 226 |
ERR_SDV(); status = NOK; FINISH; } |
| 227 |
#define UnstackSDV() if (sdvc->pos >= 0) substitute->used = sdvc->pos;\ |
| 228 |
sdvc-- |
| 229 |
|
| 230 |
static TeX *stex; |
| 231 |
static char *mode; |
| 232 |
static H_TABLE *macros; /* Table of Macros */ |
| 233 |
static BUFFER *substitute; /* Stack of Substitutions */ |
| 234 |
static BUFFER *param; /* Stack of Parameters */ |
| 235 |
static int (*fout)(); /* Output function */ |
| 236 |
static int (*fact)(); /* Action function */ |
| 237 |
static char len_text[4]; /* Length of a text */ |
| 238 |
static int *alen; |
| 239 |
static int error_opt = 1; /* Error report option */ |
| 240 |
|
| 241 |
static char EOF_marker[2] = { '0', -1}; |
| 242 |
MID_RSTATIC char char0 = 0; |
| 243 |
|
| 244 |
#define time_pic "dd MMMMMMMMMM YYYY, hh:mm:ss" |
| 245 |
#define o_time 20 |
| 246 |
static char time_buf[sizeof(time_pic)]; |
| 247 |
static SDV sdv_time = {-1, 0, sizeof(time_buf)-1, time_buf}; |
| 248 |
|
| 249 |
#define FINISH goto FIN |
| 250 |
|
| 251 |
/* Static Functions used before they are defined */ |
| 252 |
|
| 253 |
static int tex_rescan(); /* Mandatory, recursivity... */ |
| 254 |
|
| 255 |
/* MONITOR(TeX); K.B. status is here unnecessary */ |
| 256 |
|
| 257 |
/*==========================================================================*/ |
| 258 |
static int ERR_SDV() |
| 259 |
/*+++ |
| 260 |
.PURPOSE Error (too many sdv's) |
| 261 |
.RETURNS NOK |
| 262 |
.REMARKS |
| 263 |
---*/ |
| 264 |
{ |
| 265 |
register SDV *sdv; |
| 266 |
|
| 267 |
for (sdv = sdv_max-5; sdv < sdv_max; sdv++) |
| 268 |
ERR_ED_STR2("... ", GetText(sdv), Length(sdv)); |
| 269 |
sdvc--; |
| 270 |
ERR_ED_I("Max. substitutions reached: ", SUBSTITUTE_MAX); |
| 271 |
|
| 272 |
return(NOK); |
| 273 |
} |
| 274 |
|
| 275 |
/*==========================================================================*/ |
| 276 |
static int skip_comment (str, len) |
| 277 |
/*+++ |
| 278 |
.PURPOSE Skip the comment, gobbling the newline character. |
| 279 |
.RETURNS Index of first character following comment |
| 280 |
.REMARKS Not traced. |
| 281 |
---*/ |
| 282 |
char *str; /* IN: text; first byte is % */ |
| 283 |
int len; /* IN: Length of text */ |
| 284 |
{ |
| 285 |
register int i; |
| 286 |
|
| 287 |
i = oscloc(str, len, '\n'); |
| 288 |
if (i < len) i++; |
| 289 |
|
| 290 |
return(i); |
| 291 |
} |
| 292 |
|
| 293 |
/*==========================================================================*/ |
| 294 |
static int skip_spaces (str, len, blank) |
| 295 |
/*+++ |
| 296 |
.PURPOSE Span spaces, including comments |
| 297 |
.RETURNS Index of first non-space character |
| 298 |
.REMARKS Blank contains in return a blank or a newline (if empty lines) |
| 299 |
---*/ |
| 300 |
char *str; /* IN: text */ |
| 301 |
int len; /* IN: Length of text */ |
| 302 |
char *blank; /* OUT: blank or newline; if null pointer, stop at \ |
| 303 |
the first newline */ |
| 304 |
{ |
| 305 |
char *p, *pe; |
| 306 |
int nl; |
| 307 |
|
| 308 |
for (nl = 0, p = str, pe = str + len; p < pe; ) { |
| 309 |
if (*p == '\n') { |
| 310 |
if_not(blank) FINISH; |
| 311 |
nl++, p++; |
| 312 |
continue; |
| 313 |
} |
| 314 |
|
| 315 |
if (isTeX(*p, _TeX_SPACE_)) { |
| 316 |
p++; |
| 317 |
continue; |
| 318 |
} |
| 319 |
|
| 320 |
if (*p == '\0') { |
| 321 |
p++; |
| 322 |
continue; |
| 323 |
} |
| 324 |
|
| 325 |
if (*p == '%') { |
| 326 |
p += skip_comment(p, pe-p); |
| 327 |
continue; |
| 328 |
} |
| 329 |
break; |
| 330 |
} |
| 331 |
if_not(blank) FINISH; |
| 332 |
*blank = (nl >= 2 ? '\n' : ' '); |
| 333 |
|
| 334 |
FIN: |
| 335 |
return(p-str); |
| 336 |
} |
| 337 |
|
| 338 |
/*==========================================================================*/ |
| 339 |
static int tex_pout(str, len) |
| 340 |
/*+++ |
| 341 |
.PURPOSE Output substituted text in param buffer |
| 342 |
.RETURNS Length of scanned text |
| 343 |
.REMARKS *** For internal use ***. At input str-> macro to define. |
| 344 |
---*/ |
| 345 |
char *str; /* IN: String to scan */ |
| 346 |
int len; /* IN: Length of str */ |
| 347 |
{ |
| 348 |
|
| 349 |
BUF_AppendItems(param, char, str, len); |
| 350 |
|
| 351 |
return(len); |
| 352 |
} |
| 353 |
|
| 354 |
/*==========================================================================*/ |
| 355 |
static int tex_lact(str, len) |
| 356 |
/*+++ |
| 357 |
.PURPOSE Action routine four count. |
| 358 |
.RETURNS len |
| 359 |
.REMARKS Count only blanks. |
| 360 |
---*/ |
| 361 |
char *str; /* IN: String to scan */ |
| 362 |
int len; /* IN: Length of str */ |
| 363 |
{ |
| 364 |
char *p, *pe; |
| 365 |
|
| 366 |
for (p = str, pe = p + len; p < pe; p ++) |
| 367 |
{ if (*p == ' ') *alen += 1; |
| 368 |
} |
| 369 |
|
| 370 |
return(len); |
| 371 |
} |
| 372 |
|
| 373 |
/*==========================================================================*/ |
| 374 |
static int tex_lout(str, len) |
| 375 |
/*+++ |
| 376 |
.PURPOSE Count total length |
| 377 |
.RETURNS len |
| 378 |
.REMARKS Zero bytes are not counted. |
| 379 |
---*/ |
| 380 |
char *str; /* IN: String to scan */ |
| 381 |
int len; /* IN: Length of str */ |
| 382 |
{ |
| 383 |
char *p, *pe; |
| 384 |
int i; |
| 385 |
|
| 386 |
for (p = str, pe = p + len; p < pe; p += i) |
| 387 |
{ i = oscloc(p, pe-p, '\0'); |
| 388 |
*alen += i; |
| 389 |
if (i == 0) i = 1; |
| 390 |
} |
| 391 |
|
| 392 |
return(len); |
| 393 |
} |
| 394 |
|
| 395 |
/*==========================================================================*/ |
| 396 |
static int act0() |
| 397 |
/*+++ |
| 398 |
.PURPOSE Internal action routine, to output the collected buffer |
| 399 |
.RETURNS OK (processed) / NOK (not fully processed) |
| 400 |
.REMARKS |
| 401 |
---*/ |
| 402 |
{ |
| 403 |
int i; |
| 404 |
|
| 405 |
i = (*fout)((&out_buf->buf[0]), out_buf->len); |
| 406 |
if (i < out_buf->len) /* Not all bytes processed */ |
| 407 |
out_buf->len = oscopy(&out_buf->buf[0], |
| 408 |
&out_buf->buf[i], out_buf->len - i), |
| 409 |
i = NOK; |
| 410 |
else out_buf->len = 0, |
| 411 |
i = OK; |
| 412 |
|
| 413 |
return(i); |
| 414 |
} |
| 415 |
|
| 416 |
/*==========================================================================*/ |
| 417 |
static int tex_macro(str, len) |
| 418 |
/*+++ |
| 419 |
.PURPOSE Compute length of a macro |
| 420 |
.RETURNS Length |
| 421 |
.REMARKS Not traced. |
| 422 |
---*/ |
| 423 |
char *str; /* IN: text; first byte is \ */ |
| 424 |
int len; /* IN: Length of text */ |
| 425 |
{ |
| 426 |
register int i; |
| 427 |
|
| 428 |
if (len < 2) return(len); |
| 429 |
|
| 430 |
if (isTeX(*(str+1), _TeX_ALPHA_)) /* Macro name with alpha char */ |
| 431 |
i = 1 + TeX_span(str+1, len-1, _TeX_ALPHA_); |
| 432 |
else i = 2; |
| 433 |
|
| 434 |
return(i); |
| 435 |
} |
| 436 |
|
| 437 |
/*==========================================================================*/ |
| 438 |
static int getlen(str, len) |
| 439 |
/*+++ |
| 440 |
.PURPOSE Get length of text |
| 441 |
.RETURNS Length of text, also edited in len_text |
| 442 |
.REMARKS Parameter is rescanned. |
| 443 |
---*/ |
| 444 |
char *str; /* IN: Text string */ |
| 445 |
int len; /* IN: Length of text */ |
| 446 |
{ |
| 447 |
int count, i, n; |
| 448 |
SDV sdv; |
| 449 |
|
| 450 |
sdv.pos = -1, sdv.offset = 0, sdv.len = len, sdv.text = str; |
| 451 |
count = 0; |
| 452 |
alen = &count; |
| 453 |
|
| 454 |
tex_rescan(&sdv, tex_lout, tex_lact, error_opt); |
| 455 |
|
| 456 |
/* Edit the resulting count */ |
| 457 |
|
| 458 |
for (i = sizeof(len_text), n = count; --i >= 0; n /= 10) |
| 459 |
len_text[i] = '0' + n%10; |
| 460 |
|
| 461 |
return(count); |
| 462 |
} |
| 463 |
|
| 464 |
/*==========================================================================*/ |
| 465 |
static int tex_init(htex) |
| 466 |
/*+++ |
| 467 |
.PURPOSE Initialize TeX parsing |
| 468 |
.RETURNS OK / NOK |
| 469 |
.REMARKS Copies locally parts of TeX structure for easier communication with |
| 470 |
subroutines. TeX structure also installed if not yet done. |
| 471 |
---*/ |
| 472 |
TeX *htex; /* IN: The TeX header */ |
| 473 |
{ |
| 474 |
int status, id, ld, ieq, leq; |
| 475 |
/* Note: sequence \05\03mask for Set Mode Mask, |
| 476 |
\05\04mask for Clear Mode Mask |
| 477 |
*/ |
| 478 |
MID_STATIC char definitions[] = {"\ |
| 479 |
\\begin:1\01#1\0\ |
| 480 |
\\end:1\02#1\0\ |
| 481 |
\\iffalse:0\021\0\ |
| 482 |
\\iftrue:0\021\01\0\ |
| 483 |
\\else:0\021\02\0\ |
| 484 |
\\fi:0\021\03\0\ |
| 485 |
\\action:1\03\0\ |
| 486 |
\\ifdef:0\06\0\ |
| 487 |
\\def:1\06\0\ |
| 488 |
\\defenv:2\06\0\ |
| 489 |
\01verbatim:0\05\01\0\ |
| 490 |
\02verbatim:0\05\02\0\ |
| 491 |
\\obeylines:0\05\03\101\0\ |
| 492 |
\\obeyspaces:0\05\03\103\0\ |
| 493 |
\\noobey:0\05\04\103\0\ |
| 494 |
\\b:0\05\\\0\ |
| 495 |
\\\\:0\03\r\04\0\ |
| 496 |
\\len:1\022#1\022\0\ |
| 497 |
\\today:0\023A\0\ |
| 498 |
\\now:0\023.\0\ |
| 499 |
\\time:0\023B\0\ |
| 500 |
\\inputverbatim:1\07\05\0\ |
| 501 |
\\input:1\07\01"}; |
| 502 |
|
| 503 |
ENTER("tex_init"); |
| 504 |
|
| 505 |
status = NOK; |
| 506 |
|
| 507 |
sdvp = ssdvp; |
| 508 |
stex = htex; |
| 509 |
macros = htex->macros; |
| 510 |
fout = htex->output; |
| 511 |
fact = htex->action; |
| 512 |
mode = &(htex->mode); |
| 513 |
out_buf = (OUT_BUF *)(&(htex->nbuf)); |
| 514 |
|
| 515 |
if_not(fout) |
| 516 |
{ ERROR("No output function"); |
| 517 |
FINISH; |
| 518 |
} |
| 519 |
if_not(fact) |
| 520 |
{ ERROR("No Action function"); |
| 521 |
FINISH; |
| 522 |
} |
| 523 |
|
| 524 |
/* Look if contents of TeX header is OK */ |
| 525 |
|
| 526 |
if_not(substitute) |
| 527 |
if_not(substitute = BUF_Open(char,1024,1024)) |
| 528 |
FINISH; |
| 529 |
|
| 530 |
if_not(param) |
| 531 |
if_not(param = BUF_Open(char,128,128)) |
| 532 |
FINISH; |
| 533 |
|
| 534 |
if_not(macros) macros = H_Create(HSIZE); |
| 535 |
if_not(macros) FINISH; /* Failed to allocate */ |
| 536 |
htex->macros = macros; |
| 537 |
|
| 538 |
if(macros->symbols == 0) /* Insert Basic Macros */ |
| 539 |
{ atex_(); /* Ensures the inclusion of atex module */ |
| 540 |
for (id=0; id<sizeof(definitions)-1; ) |
| 541 |
{ ld = oscloc(&definitions[id], sizeof(definitions), ':'); |
| 542 |
ieq = id + ld + 1; |
| 543 |
leq = strlen(&definitions[ieq]); |
| 544 |
h_add(macros, &definitions[id], ld, &definitions[ieq], leq); |
| 545 |
id = ieq + leq + 1; |
| 546 |
} |
| 547 |
h_add(macros,"\\EOF",4,EOF_marker,2); |
| 548 |
} |
| 549 |
|
| 550 |
status = OK; |
| 551 |
|
| 552 |
FIN: |
| 553 |
EXIT(status); |
| 554 |
} |
| 555 |
|
| 556 |
/*==========================================================================*/ |
| 557 |
static int tex_subs() |
| 558 |
/*+++ |
| 559 |
.PURPOSE Substitute a macro |
| 560 |
.RETURNS OK / NOK |
| 561 |
.REMARKS verbatim mode not checked. |
| 562 |
---*/ |
| 563 |
{ |
| 564 |
SDV *sdvin, *sdvx; |
| 565 |
SDV sdv0, sdva; |
| 566 |
H_ITEM *pi; |
| 567 |
char *p, *eq, *string, x, y, npdef; |
| 568 |
int i, lm, np, status; |
| 569 |
char blank; |
| 570 |
static char defaction[2] = {_TeX_ACTION_, _TeX_ENDACTION_}; |
| 571 |
static char ifdef[] = "\021\01"; /* If true */ |
| 572 |
|
| 573 |
#define LENGTH 12000 |
| 574 |
|
| 575 |
npdef = ' '; |
| 576 |
ENTER("tex_subs"); |
| 577 |
|
| 578 |
sdvin = sdvc; |
| 579 |
string = GetString(sdvin), p = Text(string, sdvin), lm = Length(sdvin); |
| 580 |
|
| 581 |
status = OK; |
| 582 |
|
| 583 |
/* 1. Fetch Macro Name, and save position as param. 0 */ |
| 584 |
|
| 585 |
sdv0 = *sdvin, sdv0.pos = -1; |
| 586 |
lm = tex_macro(p, lm); |
| 587 |
sdvin->offset += lm; |
| 588 |
|
| 589 |
TRACE_ED_STR2("Substitute: ", p, lm); |
| 590 |
|
| 591 |
if_not(pi = h_look(macros, p, lm)) /* Macro Not Found */ |
| 592 |
{ if ((*ifc) && (error_opt)) |
| 593 |
{ if (*p == '\\') ERR_ED_STR2("Undefined Macro: ", p, lm); |
| 594 |
else ERR_ED_STR2("Unknown Environment: ", p+1,lm-1); |
| 595 |
} |
| 596 |
status = NOK; |
| 597 |
FINISH; |
| 598 |
} |
| 599 |
sdv0.offset= 0; |
| 600 |
sdv0.len = lm; |
| 601 |
sdv0.text = &pi->strings[0]; |
| 602 |
|
| 603 |
eq = &(pi->strings[1+lm]); /* Equivalence text of macro */ |
| 604 |
|
| 605 |
GobbleSpaces(string,sdvin); |
| 606 |
|
| 607 |
/* 2. Allocate new SDV */ |
| 608 |
|
| 609 |
StackSDV(); |
| 610 |
sdvc->pos = -1, sdvc->offset = 0, sdvc->len = 0, sdvc->text = NULL_PTR(char); |
| 611 |
|
| 612 |
|
| 613 |
/* 3. If the macro has no parameter, exit here */ |
| 614 |
|
| 615 |
np = *(eq++) - '0'; |
| 616 |
if ((np == 0) && (*eq != _TeX_DEF_)) /* No parameters. Simply give the string */ |
| 617 |
{ sdvc->text = eq; |
| 618 |
sdvc->len = strlen(eq); |
| 619 |
*sdvp = sdv0; /* Save macro name */ |
| 620 |
FINISH; |
| 621 |
} |
| 622 |
/* 3'. If Text to be ignored, exit here */ |
| 623 |
if (*ifc == 0) { status = NOK; FINISH; } |
| 624 |
|
| 625 |
/* 4. In case of \def: get name of macro to define */ |
| 626 |
|
| 627 |
string = GetString(sdvin); |
| 628 |
|
| 629 |
if (*eq == _TeX_DEF_) |
| 630 |
{ p = Text(string, sdvin); |
| 631 |
sdv0 = *sdvin, sdv0.pos = -1; |
| 632 |
lm = tex_macro(p, Length(sdvin)); |
| 633 |
sdvin->offset += lm; |
| 634 |
sdv0.len = sdv0.offset + lm; |
| 635 |
|
| 636 |
/* 4': Ifdef macro */ |
| 637 |
if (np == 0) |
| 638 |
{ ifdef[1] = (h_look(macros, p, lm) ? '\01' : '\0'); |
| 639 |
sdvc->text = ifdef, sdvc->len = 2; |
| 640 |
FINISH; |
| 641 |
} |
| 642 |
|
| 643 |
/* 4": Retrieve number of parameters in \def, |
| 644 |
* form #1#2... |
| 645 |
*/ |
| 646 |
GobbleSpaces(string,sdvin); |
| 647 |
for (npdef='0'; Length(sdvin)>0; ) |
| 648 |
{ p = Text(string, sdvin); |
| 649 |
if (*p != '#') break; |
| 650 |
p++, sdvin->offset += 1; |
| 651 |
if (isTeX(*p, _TeX_DIGIT_)) |
| 652 |
npdef = *p; |
| 653 |
else ERR_ED_STR2("Bad # in \\def\\", GetText(&sdv0), |
| 654 |
Length(&sdv0)), |
| 655 |
status = NOK; |
| 656 |
sdvin->offset += 1; |
| 657 |
} |
| 658 |
} |
| 659 |
/* 5: Get addresses of parameters */ |
| 660 |
|
| 661 |
for (i = 1; i <= np; i++) |
| 662 |
{ sdvx = (*eq == _TeX_ACTION_ ? &sdva : sdvp+i); |
| 663 |
GobbleNewlines(string, sdvin, &blank); |
| 664 |
sdvx->pos = -1, sdvx->offset = 0, sdvx->len = 0; |
| 665 |
|
| 666 |
while ((Length(sdvin) <= 0) && (sdvin > sdv_min)) |
| 667 |
/* Try to get previous */ |
| 668 |
sdvin--, |
| 669 |
string = GetString(sdvin), |
| 670 |
GobbleNewlines(string, sdvin, &blank); |
| 671 |
|
| 672 |
if ( (Length(sdvin) <= 0) || (string[sdvin->offset] != '{')) |
| 673 |
{ ERR_ED_STR2("Missing parameter(s) in ", |
| 674 |
&pi->strings[0], (int)pi->ls); |
| 675 |
status = NOK; |
| 676 |
continue; |
| 677 |
} |
| 678 |
|
| 679 |
sdvin->offset += 1; /* Skip the { */ |
| 680 |
*sdvx = *sdvin, sdvx->pos = -1; |
| 681 |
lm = tex_unit(Text(string, sdvin), Length(sdvin)); |
| 682 |
sdvin->offset += lm; |
| 683 |
sdvx->len = sdvx->offset + lm; |
| 684 |
if ( string[sdvin->offset] != '}') |
| 685 |
ERR_ED_STR2("Missing } in ", &pi->strings[0], (int)pi->ls), |
| 686 |
status = NOK; |
| 687 |
else sdvin->offset += 1; /* Skip the } */ |
| 688 |
} |
| 689 |
|
| 690 |
/* 6. If \def, insert now definition in table */ |
| 691 |
|
| 692 |
if (*eq == _TeX_DEF_) |
| 693 |
{ string = GetText(&sdv0), lm = Length(&sdv0); |
| 694 |
x = *string; |
| 695 |
for (i=1; i <= np; i++, npdef = '0') |
| 696 |
{ sdvx = sdvp + i; |
| 697 |
sdvx->offset -= 1; /* Get 1 byte for parameters */ |
| 698 |
if (np > 1) *string = i; |
| 699 |
p = GetText(sdvx); |
| 700 |
y = *p, *p = npdef; |
| 701 |
h_add(macros, string, lm, p, Length(sdvx)); |
| 702 |
*p = y; |
| 703 |
} |
| 704 |
*string = x; |
| 705 |
UnstackSDV(); |
| 706 |
FINISH; |
| 707 |
} |
| 708 |
|
| 709 |
sdvc->pos = substitute->used; /* To compute length of substituted string */ |
| 710 |
sdvc->offset = sdvc->pos; |
| 711 |
|
| 712 |
|
| 713 |
/* 7. If \action, rescan text to get correct action. |
| 714 |
*/ |
| 715 |
|
| 716 |
if (*eq == _TeX_ACTION_) |
| 717 |
{ i = param->used; |
| 718 |
tex_rescan(&sdva, tex_pout, tex_pout, error_opt); |
| 719 |
BUF_AppendItems(substitute, char, &defaction[0], 1); |
| 720 |
BUF_AppendItems(substitute, char, param->buf + i, param->used - i); |
| 721 |
BUF_AppendItems(substitute, char, &defaction[1], 1); |
| 722 |
param->used = i; |
| 723 |
sdvc->len = substitute->used; |
| 724 |
FINISH; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
/* 8. Copy equivalence text, with substitution of #'s by parameters |
| 729 |
* Note that parameters are preceded / followed with nuls |
| 730 |
* to avoid concatenation in names |
| 731 |
*/ |
| 732 |
|
| 733 |
*sdvp = sdv0; /* Save macro name */ |
| 734 |
|
| 735 |
for ( ; *eq; eq += i) |
| 736 |
{ i = 0; |
| 737 |
switch(*eq) |
| 738 |
{ case '\\': /* Just in case of \ followed by EOS */ |
| 739 |
i = (*(eq+1) ? 2 : 1); |
| 740 |
break; |
| 741 |
|
| 742 |
case '#': /* Substitute parameter */ |
| 743 |
eq++, i = 1; |
| 744 |
if_not(isTeX(*eq,_TeX_DIGIT_)) |
| 745 |
{ ERR_ED_STR2("Bad #-parameter(s) in ", &pi->strings[0], |
| 746 |
(int)pi->ls); |
| 747 |
status = NOK; |
| 748 |
} |
| 749 |
else |
| 750 |
{ sdvx = sdvp + (*eq - '0'); |
| 751 |
p = substitute->buf + substitute->used - 1; |
| 752 |
if (isTeX(*p,_TeX_GRAPH_)) |
| 753 |
BUF_AppendItem(substitute, char, &char0); |
| 754 |
BUF_AppendItems(substitute, char, |
| 755 |
GetText(sdvx), Length(sdvx)); |
| 756 |
p = substitute->buf + substitute->used - 1; |
| 757 |
if (isTeX(*p,_TeX_GRAPH_)) |
| 758 |
BUF_AppendItem(substitute, char, &char0); |
| 759 |
} |
| 760 |
continue; |
| 761 |
} |
| 762 |
if (i == 0) i = TeX_span(eq, LENGTH, _TeX_NORMAL_); |
| 763 |
if (i == 0) i = 1; |
| 764 |
BUF_AppendItems(substitute, char, eq, i); |
| 765 |
} |
| 766 |
sdvc->len = substitute->used; |
| 767 |
|
| 768 |
FIN: |
| 769 |
EXIT(status); |
| 770 |
} |
| 771 |
|
| 772 |
/*==========================================================================*/ |
| 773 |
static int getparm(np) |
| 774 |
/*+++ |
| 775 |
.PURPOSE Get parameter #np in TeX structure |
| 776 |
.RETURNS OK / NOK (np outside range 0..9) |
| 777 |
.REMARKS Parameter is rescanned. |
| 778 |
---*/ |
| 779 |
int np; /* IN: Parameter number, range [0...9] */ |
| 780 |
{ |
| 781 |
int status, old_offset; |
| 782 |
|
| 783 |
status = OK; |
| 784 |
old_offset = param->offset; |
| 785 |
param->offset = param->used; |
| 786 |
|
| 787 |
if ((np>0) && (np <= 9)) |
| 788 |
{ tex_rescan(sdvp+np, tex_pout, tex_pout, error_opt); |
| 789 |
BUF_AppendItem(param, char, &char0); /* Terminate with EOS */ |
| 790 |
stex->ap = param->buf + old_offset; |
| 791 |
} |
| 792 |
else if (np == 0) /* Don't rescan parameter 0 = macro name */ |
| 793 |
stex->ap = GetText(sdvp); |
| 794 |
else stex->ap = &char0, status = NOK; |
| 795 |
|
| 796 |
param->used = param->offset; |
| 797 |
param->offset = old_offset; |
| 798 |
|
| 799 |
return(status); |
| 800 |
} |
| 801 |
|
| 802 |
/*==========================================================================*/ |
| 803 |
static int act9(sdv, len) |
| 804 |
/*+++ |
| 805 |
.PURPOSE Internal action routine, to substitute parameters |
| 806 |
.RETURNS OK / NOK / EOF |
| 807 |
.REMARKS |
| 808 |
---*/ |
| 809 |
SDV *sdv; /* IN: Action string */ |
| 810 |
int len; /* IN: Length of string */ |
| 811 |
{ |
| 812 |
int i; |
| 813 |
char *str; |
| 814 |
|
| 815 |
str = GetText(sdv); |
| 816 |
if ((out_buf) && (out_buf->len)) /* First, output what remains in buffer */ |
| 817 |
if_not(act0()) return(0); |
| 818 |
|
| 819 |
if_not(*ifc) return(len); /* Nothing iffalse */ |
| 820 |
|
| 821 |
i = TeX_scan(str, len, _TeX_DIGIT_); /* Digit action => get parameters */ |
| 822 |
if (i < len) |
| 823 |
{ i = *(str + i) - '0'; |
| 824 |
getparm(i); |
| 825 |
str = GetText(sdv); |
| 826 |
} |
| 827 |
|
| 828 |
return((*fact)(str,len)); |
| 829 |
} |
| 830 |
|
| 831 |
/*==========================================================================*/ |
| 832 |
static int out9(str, len) |
| 833 |
/*+++ |
| 834 |
.PURPOSE Internal output routine. |
| 835 |
.RETURNS Length processed. |
| 836 |
.REMARKS Copied to tex buffer. |
| 837 |
---*/ |
| 838 |
char *str; /* IN: Action string */ |
| 839 |
int len; /* IN: Length of string */ |
| 840 |
{ |
| 841 |
int i; |
| 842 |
|
| 843 |
i = 0; |
| 844 |
|
| 845 |
if_not(*ifc) return(len); /* Nothing iffalse */ |
| 846 |
|
| 847 |
if_not(out_buf) goto Immediate_Output; |
| 848 |
|
| 849 |
if (*mode == _TeX_VERBATIM_) /* Output immediately */ |
| 850 |
{ if (out_buf->len) /* First, output what remains in buffer */ |
| 851 |
if_not(act0()) FINISH; |
| 852 |
} |
| 853 |
|
| 854 |
if ((out_buf->len + len) > TeX_BUF) |
| 855 |
if (out_buf->len) |
| 856 |
if_not(act0()) FINISH; |
| 857 |
|
| 858 |
if (len > TeX_BUF) |
| 859 |
goto Immediate_Output; |
| 860 |
else i = oscopy(&out_buf->buf[out_buf->len], str, len), |
| 861 |
out_buf->len += i; |
| 862 |
FINISH; |
| 863 |
|
| 864 |
Immediate_Output: |
| 865 |
i = (*fout)(str,len); |
| 866 |
|
| 867 |
FIN: |
| 868 |
return(i); |
| 869 |
} |
| 870 |
|
| 871 |
/*==========================================================================*/ |
| 872 |
static int load1(fn, pos, len, opt) |
| 873 |
/*+++ |
| 874 |
.PURPOSE Loads len bytes from file fid. |
| 875 |
.RETURNS Number of bytes loaded. |
| 876 |
.REMARKS The file is closed... |
| 877 |
---*/ |
| 878 |
char *fn; /* IN: File Name */ |
| 879 |
long pos; /* IN: Position to start loading */ |
| 880 |
int len; /* IN: Number of bytes to load */ |
| 881 |
int opt; /* IN: Option _TeX_VERBATIM_ for inputverbatim */ |
| 882 |
{ |
| 883 |
char *b; |
| 884 |
int status, l; |
| 885 |
|
| 886 |
status = 0; |
| 887 |
l = len; |
| 888 |
if (opt == _TeX_VERBATIM_) l += 4; |
| 889 |
StackSDV(); |
| 890 |
sdvc->pos = substitute->used; |
| 891 |
sdvc->offset = sdvc->pos; |
| 892 |
sdvc->len = sdvc->pos; |
| 893 |
sdvc->text = NULL_PTR(char); |
| 894 |
if_not (BUF_AllocateItems(substitute, char, l)) |
| 895 |
FINISH; |
| 896 |
|
| 897 |
b = substitute->buf + sdvc->pos; |
| 898 |
|
| 899 |
if (opt == _TeX_VERBATIM_) /* Add begin{verbatim} */ |
| 900 |
*b = '\05', *(b+1) = '\01', b += 2; |
| 901 |
|
| 902 |
b += fi_load(NameFile(fn,DEFAULT_EXTENSION), pos, b, len); |
| 903 |
|
| 904 |
if (opt == _TeX_VERBATIM_) /* Add end{verbatim} */ |
| 905 |
*b = '\05', *(b+1) = '\02', b += 2; |
| 906 |
|
| 907 |
sdvc->len = (b - substitute->buf); /* Final Length */ |
| 908 |
|
| 909 |
status = Length(sdvc); |
| 910 |
TRACE_ED_STR2("Input: ", substitute->buf + sdvc->offset, status); |
| 911 |
|
| 912 |
FIN: |
| 913 |
return(status); |
| 914 |
} |
| 915 |
|
| 916 |
/*==========================================================================*/ |
| 917 |
static int input1(opt) |
| 918 |
/*+++ |
| 919 |
.PURPOSE Includes a file in the stack (macro \input{}) |
| 920 |
.RETURNS OK / NOK |
| 921 |
.REMARKS Parameter is sdvp[1]. |
| 922 |
---*/ |
| 923 |
int opt; /* IN: Option _TeX_VERBATIM_ for inputverbatim */ |
| 924 |
{ |
| 925 |
char *endfile, *b, x; |
| 926 |
SDV *sdvx; |
| 927 |
int status; |
| 928 |
long int fs; |
| 929 |
|
| 930 |
sdvx = sdvp + 1; |
| 931 |
b = GetText(sdvx); |
| 932 |
endfile = b + Length(sdvx); |
| 933 |
x = *endfile; /* Save ending character */ |
| 934 |
*endfile = EOS; |
| 935 |
status = NOK; |
| 936 |
|
| 937 |
fs = fi_size(NameFile(b,DEFAULT_EXTENSION)); /* Mod. 28-Mar-89 Size of File */ |
| 938 |
|
| 939 |
load1(b, 0L, (int)fs, opt); |
| 940 |
|
| 941 |
*endfile = x; /* Restore last byte */ |
| 942 |
return(status); |
| 943 |
} |
| 944 |
|
| 945 |
/*==========================================================================*/ |
| 946 |
static int tex_1exec(sdv_stop) |
| 947 |
/*+++ |
| 948 |
.PURPOSE Transform the string |
| 949 |
.RETURNS OK / NOK (not finished) |
| 950 |
.REMARKS |
| 951 |
---*/ |
| 952 |
SDV *sdv_stop; /* IN: Ending position */ |
| 953 |
{ |
| 954 |
char *p; |
| 955 |
int i, l, n, status, returned_status; |
| 956 |
char blank; |
| 957 |
SDV sdvb; /* Blank descriptor */ |
| 958 |
|
| 959 |
returned_status = NOK; |
| 960 |
status = NOK; |
| 961 |
sdvb = sdvb0; |
| 962 |
sdvb.text = ␣ |
| 963 |
|
| 964 |
if ((out_buf) && (out_buf->len)) /* Output what's in the buffer */ |
| 965 |
if_not(act0()) FINISH; |
| 966 |
|
| 967 |
for( ; sdvc >= sdv_stop; sdvc->offset += i) |
| 968 |
{ i = 0; |
| 969 |
if (sdvc->offset >= sdvc->len) /* This piece is exhausted */ |
| 970 |
{ UnstackSDV(); |
| 971 |
continue; |
| 972 |
} |
| 973 |
|
| 974 |
p = GetText(sdvc), l = Length(sdvc); |
| 975 |
if (status == EOF) /* \EOF was encountered */ |
| 976 |
{ i = l, status = NOK; /* To force unstack */ |
| 977 |
continue; |
| 978 |
} |
| 979 |
|
| 980 |
switch(*p) |
| 981 |
{ case _TeX_VERBATIM_: /* Simply switch state */ |
| 982 |
i = 2; |
| 983 |
switch(*++p) |
| 984 |
{ case _TeX_BEGIN_: *mode = _TeX_VERBATIM_; break; |
| 985 |
case _TeX_END_: *mode = 0; break; |
| 986 |
case _TeX_ACTION_: i = 3; *mode |= p[1]; break; |
| 987 |
case _TeX_ENDACTION_: i = 3; *mode &= ~p[1]; break; |
| 988 |
default: |
| 989 |
if_not(out9(p,1)) FINISH; |
| 990 |
} |
| 991 |
continue; |
| 992 |
|
| 993 |
case _TeX_IF_: /* Test */ |
| 994 |
switch(*++p) |
| 995 |
{ case 0: case 1: /* New if */ |
| 996 |
StackIFS(); |
| 997 |
*ifc &= * (unsigned char *)p; |
| 998 |
break; |
| 999 |
case 2: /* Else */ |
| 1000 |
if (ifc == ifs_min) |
| 1001 |
ERROR("\\else outside \\if ... \\fi"); |
| 1002 |
else *ifc = *(ifc-1) & (*ifc ^ 1); |
| 1003 |
break; |
| 1004 |
default: /* Endif */ |
| 1005 |
UnstackIFS(); |
| 1006 |
break; |
| 1007 |
} |
| 1008 |
i = 2; |
| 1009 |
continue; |
| 1010 |
|
| 1011 |
case -1: /* EOF */ |
| 1012 |
if ((out_buf) && (out_buf->len)) |
| 1013 |
if_not(act0()) FINISH; |
| 1014 |
status = EOF; |
| 1015 |
i = l; |
| 1016 |
continue; |
| 1017 |
|
| 1018 |
case 0: case _TeX_ENDACTION_: |
| 1019 |
i = 1; |
| 1020 |
continue; |
| 1021 |
|
| 1022 |
case '~': /* Non-breakable space */ |
| 1023 |
blank = (*mode == _TeX_VERBATIM_ ? '~' : ' '); |
| 1024 |
if_not(i = out9(&blank, 1)) FINISH; |
| 1025 |
continue; |
| 1026 |
|
| 1027 |
case '\\': |
| 1028 |
if (*mode == _TeX_VERBATIM_) |
| 1029 |
{ if (l < 14) break; |
| 1030 |
if (oscomp(p,"\\end{verbatim}", 14) != 0) |
| 1031 |
break; /* Not the \end{verbatim} */ |
| 1032 |
} |
| 1033 |
blank = *(p+1); /* Char following escape */ |
| 1034 |
if (isTeX(blank, _TeX_DIGIT_)) |
| 1035 |
{ status = act9(&sdvb, 1); |
| 1036 |
if(status == 0) FINISH; |
| 1037 |
i = 2; |
| 1038 |
continue; |
| 1039 |
} |
| 1040 |
if (isTeX(blank, _TeX_SPACE_|_TeX_ESCAPE_)) |
| 1041 |
{ if(blank != '\\') |
| 1042 |
{ if (isTeX(blank, _TeX_SPACE_)) blank = ' '; |
| 1043 |
if_not(out9(&blank,1)) FINISH; |
| 1044 |
i = 2; |
| 1045 |
continue; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
case _TeX_BEGIN_: case _TeX_END_: /* Also macro and \\ */ |
| 1050 |
tex_subs(); |
| 1051 |
continue; |
| 1052 |
|
| 1053 |
case '%': /* Fetch the next newline */ |
| 1054 |
i = skip_comment(p, l); |
| 1055 |
if (*mode == _TeX_VERBATIM_) break; |
| 1056 |
continue; |
| 1057 |
|
| 1058 |
case _TeX_INCLUDE_: /* Special Action */ |
| 1059 |
sdvc->offset += 2; /* ... will change SDV */ |
| 1060 |
if (*ifc) input1(*++p); |
| 1061 |
continue; |
| 1062 |
|
| 1063 |
case _TeX_LEN_: /* Special Action */ |
| 1064 |
i = oscloc(++p, l-1, _TeX_LEN_); |
| 1065 |
n = getlen(p, i); |
| 1066 |
i += 2; /* Skip beginning / end of string */ |
| 1067 |
if_not(out9(len_text, sizeof(len_text))) FINISH; |
| 1068 |
continue; |
| 1069 |
|
| 1070 |
case _TeX_TIME_: /* Special Action */ |
| 1071 |
sdvc->offset += 2; /* ... will change SDV */ |
| 1072 |
ed_t(time_buf, time_pic, oshtime()); |
| 1073 |
StackSDV(); |
| 1074 |
*sdvc = sdv_time; |
| 1075 |
|
| 1076 |
switch(*++p) |
| 1077 |
{ case 'A': /* Date only */ |
| 1078 |
sdvc->len = o_time - 2; break; |
| 1079 |
case 'B': /* Time only */ |
| 1080 |
sdvc->offset = o_time; break; |
| 1081 |
default : /* Both */ break; |
| 1082 |
} |
| 1083 |
continue; |
| 1084 |
|
| 1085 |
case _TeX_ACTION_: /* Special Action */ |
| 1086 |
n = oscskip(p, l, _TeX_ACTION_); |
| 1087 |
p += n, l -= n; |
| 1088 |
i = oscloc(p, l, _TeX_ENDACTION_); |
| 1089 |
if (i) /* There is an action ... */ |
| 1090 |
{ sdvc->offset += n; |
| 1091 |
status = act9(sdvc, i); |
| 1092 |
sdvc->offset -= n; |
| 1093 |
if (status == 0) FINISH; |
| 1094 |
} |
| 1095 |
i += n+1; /* Skip Action */ |
| 1096 |
continue; |
| 1097 |
} |
| 1098 |
|
| 1099 |
if (*mode == _TeX_VERBATIM_) |
| 1100 |
{ if (i == 0) i = TeX_span(p, l, _TeX_NORMAL_); |
| 1101 |
if (i == 0) i = 1; |
| 1102 |
if_not(i = out9(p,i)) FINISH; |
| 1103 |
continue; |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (isTeX(*p,_TeX_GRAPH_)) /* Scan Normal Text */ |
| 1107 |
{ i = TeX_span(p, l, _TeX_GRAPH_); |
| 1108 |
if_not(i = out9(p, i)) FINISH; |
| 1109 |
continue; |
| 1110 |
} |
| 1111 |
|
| 1112 |
if (isTeX(*p,_TeX_SPACE_)) { /* Scan Blank Text */ |
| 1113 |
if (*mode & 0100) { /* Obey spaces... */ |
| 1114 |
i = skip_spaces(p, l, (char *)0); |
| 1115 |
if (i && (*mode & 02)) { /* ALL SPACES */ |
| 1116 |
if_not(i = out9(p, i)) FINISH; |
| 1117 |
continue; |
| 1118 |
} |
| 1119 |
if (i == 0) blank = '\r', i = 1; /* Next Line */ |
| 1120 |
else blank = ' '; |
| 1121 |
} |
| 1122 |
else i = skip_spaces(p, l, &blank); |
| 1123 |
status = act9(&sdvb, 1); |
| 1124 |
if(status == 0) FINISH; |
| 1125 |
continue; |
| 1126 |
} |
| 1127 |
|
| 1128 |
if_not(isTeX(*p, _TeX_ESCAPE_)) /* Bad Character... */ |
| 1129 |
{ ERR_ED_STR2("Bad character: ", p, l); |
| 1130 |
i = 1; |
| 1131 |
continue; |
| 1132 |
} |
| 1133 |
|
| 1134 |
i = 1; /* Escape */ |
| 1135 |
status = act9(sdvc,1); |
| 1136 |
if(status == 0) FINISH; |
| 1137 |
} |
| 1138 |
|
| 1139 |
if ((out_buf) && (out_buf->len)) |
| 1140 |
if_not(act0()) FINISH; |
| 1141 |
|
| 1142 |
returned_status = OK; |
| 1143 |
|
| 1144 |
FIN: |
| 1145 |
return(returned_status); |
| 1146 |
} |
| 1147 |
|
| 1148 |
/*==========================================================================*/ |
| 1149 |
static int tex_rescan(sdv, f_out, f_act, er_opt) |
| 1150 |
/*+++ |
| 1151 |
.PURPOSE Rescan text, storing result via supplied output / action routines |
| 1152 |
.RETURNS OK / NOK |
| 1153 |
.REMARKS |
| 1154 |
---*/ |
| 1155 |
SDV *sdv; /* IN: String to rescan */ |
| 1156 |
int (*f_out)(); /* IN: The output routine to use */ |
| 1157 |
int (*f_act)(); /* IN: The action routine to use */ |
| 1158 |
int er_opt; /* IN: Error report option: 0=no report */ |
| 1159 |
{ |
| 1160 |
int status, old_eropt; |
| 1161 |
int (*old_act)(), (*old_out)(); |
| 1162 |
OUT_BUF *old_buf; |
| 1163 |
SDV sdvp1[10], *old_parm; |
| 1164 |
|
| 1165 |
old_eropt = 0; |
| 1166 |
status = NOK; |
| 1167 |
StackSDV(); |
| 1168 |
*sdvc = *sdv; |
| 1169 |
sdvc->pos = -1; /* Don't free */ |
| 1170 |
param->offset = param->used; |
| 1171 |
old_eropt = error_opt; |
| 1172 |
error_opt = er_opt; |
| 1173 |
|
| 1174 |
old_parm = sdvp; |
| 1175 |
sdvp = &sdvp1[0]; |
| 1176 |
old_act = fact, old_out = fout, old_buf = out_buf; |
| 1177 |
fact = f_act, fout = f_out, out_buf = (OUT_BUF *)0; |
| 1178 |
|
| 1179 |
status = tex_1exec(sdvc); |
| 1180 |
|
| 1181 |
fout = old_out, fact = old_act, out_buf = old_buf; |
| 1182 |
sdvp = old_parm; |
| 1183 |
|
| 1184 |
FIN: |
| 1185 |
error_opt = old_eropt; |
| 1186 |
return(status); |
| 1187 |
} |
| 1188 |
|
| 1189 |
/*==========================================================================*/ |
| 1190 |
int tex_unit(str, len) |
| 1191 |
/*+++ |
| 1192 |
.PURPOSE Scan for a closing brace } |
| 1193 |
.RETURNS Offset of matching } |
| 1194 |
.REMARKS Not traced. |
| 1195 |
---*/ |
| 1196 |
char *str; /* IN: String to scan; points to char following { */ |
| 1197 |
int len; /* IN: Length of String */ |
| 1198 |
{ |
| 1199 |
char *p, *pe; |
| 1200 |
int lb; |
| 1201 |
|
| 1202 |
for (lb = 1, p = str, pe = p+len; p < pe; p++) |
| 1203 |
{ switch(*p) |
| 1204 |
{ case '\\': p++; continue; |
| 1205 |
case '{': lb++; continue; |
| 1206 |
case '}': lb--; break; |
| 1207 |
case '%': p += skip_comment(p, pe-p) - 1; |
| 1208 |
continue; |
| 1209 |
default: continue; |
| 1210 |
} |
| 1211 |
if (lb == 0) break; |
| 1212 |
} |
| 1213 |
|
| 1214 |
if (lb != 0) |
| 1215 |
ERR_ED_STR2("Missing Right Brace in {",str, p-str); |
| 1216 |
|
| 1217 |
return(p-str); |
| 1218 |
} |
| 1219 |
|
| 1220 |
/*==========================================================================*/ |
| 1221 |
int tex_input(htex, filename, opt) |
| 1222 |
/*+++ |
| 1223 |
.PURPOSE Loads a file, |
| 1224 |
.RETURNS Number of bytes loaded. |
| 1225 |
.REMARKS |
| 1226 |
---*/ |
| 1227 |
TeX *htex; /* IN: TeX structure */ |
| 1228 |
char *filename; /* IN: Name of file to load. */ |
| 1229 |
int opt; /* IN: Option _TeX_VERBATIM_ for inputverbatim */ |
| 1230 |
{ |
| 1231 |
long int fs; |
| 1232 |
int status; |
| 1233 |
|
| 1234 |
ENTER("tex_input"); |
| 1235 |
|
| 1236 |
status = NOK; |
| 1237 |
|
| 1238 |
if_not(tex_init(htex)) FINISH; |
| 1239 |
|
| 1240 |
fs = fi_size(NameFile(filename,DEFAULT_EXTENSION)); /* Size of File */ |
| 1241 |
|
| 1242 |
status = load1(filename, 0L, (int)fs, opt); |
| 1243 |
|
| 1244 |
FIN: |
| 1245 |
EXIT(status); |
| 1246 |
} |
| 1247 |
|
| 1248 |
/*==========================================================================*/ |
| 1249 |
int tex_load(htex, fid, len, opt) |
| 1250 |
/*+++ |
| 1251 |
.PURPOSE Load n bytes from a file, |
| 1252 |
.RETURNS Number of bytes loaded. |
| 1253 |
.REMARKS File is closed... |
| 1254 |
---*/ |
| 1255 |
TeX *htex; /* IN: TeX structure */ |
| 1256 |
int fid; /* IN: File identifier */ |
| 1257 |
int len; /* IN: Number of bytes to load */ |
| 1258 |
int opt; /* IN: Option _TeX_VERBATIM_ for inputverbatim */ |
| 1259 |
{ |
| 1260 |
int status; |
| 1261 |
char *fn; |
| 1262 |
long pos; |
| 1263 |
|
| 1264 |
ENTER("tex_load"); |
| 1265 |
|
| 1266 |
status = NOK; |
| 1267 |
|
| 1268 |
if_not(tex_init(htex)) FINISH; |
| 1269 |
|
| 1270 |
fn = fi_name(fid), pos = fi_tell(fid); |
| 1271 |
close(fid); |
| 1272 |
status = load1(fn, pos, len, opt); |
| 1273 |
|
| 1274 |
FIN: |
| 1275 |
EXIT(status); |
| 1276 |
} |
| 1277 |
|
| 1278 |
/*==========================================================================*/ |
| 1279 |
int tex_list(htex) |
| 1280 |
/*+++ |
| 1281 |
.PURPOSE Display stored macros in Log file. |
| 1282 |
.RETURNS Free space for symbol definitions. |
| 1283 |
.REMARKS |
| 1284 |
---*/ |
| 1285 |
TeX *htex; /* IN: TeX structure */ |
| 1286 |
{ |
| 1287 |
int n, i; |
| 1288 |
H_ITEM *pi; |
| 1289 |
|
| 1290 |
|
| 1291 |
n = 0; |
| 1292 |
ENTER("+tex_list"); |
| 1293 |
if_not(tex_init(htex)) FINISH; |
| 1294 |
n = h_log(macros); /* Log the size / collisions / free */ |
| 1295 |
|
| 1296 |
for (i=0; i < macros->size; i++) |
| 1297 |
{ if_not(macros->start[i]) continue; |
| 1298 |
for (pi = macros->start[i]; pi; pi = pi->next) |
| 1299 |
LOG_ED_STRING((pi->next ? "+" : " "), &pi->strings[0]); |
| 1300 |
} |
| 1301 |
|
| 1302 |
FIN: |
| 1303 |
EXIT(n); |
| 1304 |
} |
| 1305 |
|
| 1306 |
/*==========================================================================*/ |
| 1307 |
int tex_getvparm(np) |
| 1308 |
/*+++ |
| 1309 |
.PURPOSE Get ``verbatim'' parameter #np in TeX structure |
| 1310 |
.RETURNS Length of the parameter. The address is in the TeX structure. |
| 1311 |
-1 is returned in case of error. |
| 1312 |
.REMARKS Parameter is NOT rescanned. |
| 1313 |
---*/ |
| 1314 |
int np; /* IN: Parameter number, range [0...9] */ |
| 1315 |
{ |
| 1316 |
int status; |
| 1317 |
SDV *p; |
| 1318 |
|
| 1319 |
ENTER("+tex_getvparm"); |
| 1320 |
|
| 1321 |
if ((np>0) && (np <= 9)) |
| 1322 |
{ p = sdvp + np; |
| 1323 |
stex->ap = GetText(p); |
| 1324 |
status = Length(p); |
| 1325 |
} |
| 1326 |
else status = -1; |
| 1327 |
|
| 1328 |
EXIT(status); |
| 1329 |
} |
| 1330 |
|
| 1331 |
/*==========================================================================*/ |
| 1332 |
int tex_getparm(np) |
| 1333 |
/*+++ |
| 1334 |
.PURPOSE Get parameter #np in TeX structure |
| 1335 |
.RETURNS OK / NOK (np outside range 0..9) |
| 1336 |
.REMARKS Parameter is rescanned. |
| 1337 |
---*/ |
| 1338 |
int np; /* IN: Parameter number, range [0...9] */ |
| 1339 |
{ |
| 1340 |
int status; |
| 1341 |
|
| 1342 |
ENTER("tex_getparm"); |
| 1343 |
|
| 1344 |
BUF_Clear(param); |
| 1345 |
|
| 1346 |
status = getparm(np); |
| 1347 |
|
| 1348 |
EXIT(status); |
| 1349 |
} |
| 1350 |
|
| 1351 |
/*==========================================================================*/ |
| 1352 |
int tex_exec(htex, str, len) |
| 1353 |
/*+++ |
| 1354 |
.PURPOSE Transform the string |
| 1355 |
.RETURNS OK / NOK (not finished) |
| 1356 |
.REMARKS |
| 1357 |
---*/ |
| 1358 |
TeX *htex; /* IN: The TeX header */ |
| 1359 |
char *str; /* IN: String to scan; NULL to continue */ |
| 1360 |
int len; /* IN: Length of string */ |
| 1361 |
{ |
| 1362 |
int status; |
| 1363 |
|
| 1364 |
ENTER("tex_exec"); |
| 1365 |
|
| 1366 |
status = NOK; |
| 1367 |
if_not(tex_init(htex)) FINISH; |
| 1368 |
|
| 1369 |
if(str) /* Start a new set of substitutions */ |
| 1370 |
{ BUF_Clear(substitute), BUF_Clear(param); |
| 1371 |
htex->nbuf = 0; |
| 1372 |
ifc = ifs_min; |
| 1373 |
*ifc = 1; /* TRUE */ |
| 1374 |
sdvc = sdv_min; |
| 1375 |
sdvc->pos = -1; |
| 1376 |
sdvc->offset = 0; |
| 1377 |
sdvc->len = len; |
| 1378 |
sdvc->text = str; |
| 1379 |
error_opt = 1; /* Error report is ON */ |
| 1380 |
} |
| 1381 |
|
| 1382 |
status = tex_1exec(sdv_min); |
| 1383 |
|
| 1384 |
FIN: |
| 1385 |
EXIT(status); |
| 1386 |
} |
| 1387 |
|
| 1388 |
/*==========================================================================*/ |
| 1389 |
int tex_mexec(htex, str, nstrings) |
| 1390 |
/*+++ |
| 1391 |
.PURPOSE Multiple transformations |
| 1392 |
.RETURNS OK / NOK (not finished) |
| 1393 |
.REMARKS |
| 1394 |
---*/ |
| 1395 |
TeX *htex; /* IN: The TeX header */ |
| 1396 |
char **str; /* IN: Array of strings to scan */ |
| 1397 |
int nstrings; /* IN: Number of strings */ |
| 1398 |
{ |
| 1399 |
int status; |
| 1400 |
char **s; |
| 1401 |
|
| 1402 |
ENTER("tex_mexec"); |
| 1403 |
|
| 1404 |
status = NOK; |
| 1405 |
if_not(tex_init(htex)) FINISH; |
| 1406 |
|
| 1407 |
if (nstrings >= SUBSTITUTE_MAX - 2) |
| 1408 |
{ ERR_ED_I("To many strings to substitute: ", nstrings); |
| 1409 |
FINISH; |
| 1410 |
} |
| 1411 |
|
| 1412 |
if(nstrings > 0) /* Start a new set of substitutions */ |
| 1413 |
{ BUF_Clear(substitute), BUF_Clear(param); |
| 1414 |
htex->nbuf = 0; |
| 1415 |
ifc = ifs_min; |
| 1416 |
*ifc = 1; /* TRUE */ |
| 1417 |
error_opt = 1; /* Error report is ON */ |
| 1418 |
for (s = str + 2*nstrings, sdvc = sdv_min; s > str; sdvc++) |
| 1419 |
{ s -= 2; |
| 1420 |
sdvc->pos = -1; |
| 1421 |
sdvc->offset = 0; |
| 1422 |
sdvc->text = *s; |
| 1423 |
sdvc->len = *(s+1) - sdvc->text; |
| 1424 |
} |
| 1425 |
sdvc--; |
| 1426 |
} |
| 1427 |
|
| 1428 |
status = tex_1exec(sdv_min); |
| 1429 |
|
| 1430 |
FIN: |
| 1431 |
EXIT(status); |
| 1432 |
} |
| 1433 |
|
| 1434 |
/*==========================================================================*/ |
| 1435 |
int tex_tell() |
| 1436 |
/*+++ |
| 1437 |
.PURPOSE Tell where we are in the TeX processing |
| 1438 |
.RETURNS Offset from last string passed to tex_exec / -1 if terminated |
| 1439 |
.REMARKS |
| 1440 |
---*/ |
| 1441 |
{ |
| 1442 |
int status; |
| 1443 |
|
| 1444 |
ENTER("+tex_tell"); |
| 1445 |
|
| 1446 |
status = (sdvc >= sdv_min ? (sdv_min)->offset : -1); |
| 1447 |
|
| 1448 |
EXIT(status); |
| 1449 |
} |
| 1450 |
|
| 1451 |
/*==========================================================================*/ |
| 1452 |
char *tex_symbol(str) |
| 1453 |
/*+++ |
| 1454 |
.PURPOSE Retrieve a symbol |
| 1455 |
.RETURNS Address of processed symbol / NULL |
| 1456 |
.REMARKS Uses the last TeX structure |
| 1457 |
---*/ |
| 1458 |
char *str; /* IN: String to substitute */ |
| 1459 |
{ |
| 1460 |
char *p; |
| 1461 |
int old_offset; |
| 1462 |
SDV sdvl; |
| 1463 |
|
| 1464 |
ENTER("*tex_symbol"); |
| 1465 |
|
| 1466 |
sdvl.pos = -1; |
| 1467 |
sdvl.offset = 0; |
| 1468 |
sdvl.len = strlen(str); |
| 1469 |
sdvl.text = str; |
| 1470 |
old_offset = param->offset; /* Save */ |
| 1471 |
param->offset = param->used; |
| 1472 |
|
| 1473 |
tex_rescan(&sdvl, tex_pout, tex_pout, 0); |
| 1474 |
BUF_AppendItem(param, char, &char0); /* Terminate with EOS */ |
| 1475 |
|
| 1476 |
param->used = param->offset; /* Restore */ |
| 1477 |
param->offset = old_offset; |
| 1478 |
|
| 1479 |
p = param->buf + param->offset; |
| 1480 |
EXITp(p); |
| 1481 |
} |
| 1482 |
|