| 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 |
.MODULE atex.c |
| 30 |
.AUTHOR Francois Ochsenbein [ESO-IPG] |
| 31 |
.LANGUAGE C |
| 32 |
.CATEGORY TeX character classification |
| 33 |
.ENVIRONMENT ASCII |
| 34 |
.COMMENTS This file includes the definition of TeX attributes. |
| 35 |
Characters flagged _TeX_ESCAPE are |
| 36 |
\ # & { } % ~ $ ^ _ |
| 37 |
.VERSION 1.0 19-Apr-1988: Creation |
| 38 |
.VERSION 1.1 19-Mar-1989: Added ed_tex function |
| 39 |
.VERSION 1.2 27-Oct-1990: Added ATEX_C conditional. |
| 40 |
Added atex_ entry point to force inclusion of this module |
| 41 |
for this *&^$%CRAZY*&$% gcc compiler on VAX |
| 42 |
|
| 43 |
090831 last modif |
| 44 |
-------------------------------------------*/ |
| 45 |
|
| 46 |
#define ATEX_C 1 /* To avoid insertion of MID_EXTERN main_TeX */ |
| 47 |
#include <atex.h> |
| 48 |
#include <string.h> |
| 49 |
|
| 50 |
#define ___ 0x00 |
| 51 |
#define _U_ _TeX_UPPER_ /* UPPER-case letter A...Z */ |
| 52 |
#define _L_ _TeX_LOWER_ /* lower-case letter a...z */ |
| 53 |
#define _D_ _TeX_DIGIT_ /* digit 0...9 */ |
| 54 |
#define _S_ _TeX_SPACE_ /* space */ |
| 55 |
#define _P_ _TeX_PUNCT_ /* punctuation */ |
| 56 |
#define _M_ _TeX_MATH_ /* Math env. */ |
| 57 |
#define _Z_ _TeX_NULL_ /* Null character */ |
| 58 |
#define _E_ _TeX_ESCAPE_ /* \ # & { } % ~ */ |
| 59 |
|
| 60 |
/* Attributes of TeX characters */ |
| 61 |
MID_GLOBAL unsigned char main_TeX[256] = /* TeX attributes */ |
| 62 |
{ /* UPP|low|dig|sp |pun|ctr|ign|escape |
| 63 |
* | | | | | | | */ |
| 64 |
___|___|___|___|___|___|_Z_|___, /* NUL */ |
| 65 |
___|___|___|___|___|___|___|___, /* SOH */ |
| 66 |
___|___|___|___|___|___|___|___, /* STX */ |
| 67 |
___|___|___|___|___|___|___|___, /* ETX */ |
| 68 |
___|___|___|___|___|___|___|___, /* EOT */ |
| 69 |
___|___|___|___|___|___|___|___, /* ENQ */ |
| 70 |
___|___|___|___|___|___|___|___, /* ACK */ |
| 71 |
___|___|___|___|___|___|___|___, /* BEL */ |
| 72 |
___|___|___|___|___|___|___|___, /* BS */ |
| 73 |
___|___|___|_S_|___|___|___|___, /* HTab */ |
| 74 |
___|___|___|_S_|___|___|___|___, /* LF */ |
| 75 |
___|___|___|_S_|___|___|___|___, /* VTab */ |
| 76 |
___|___|___|_S_|___|___|___|___, /* FF */ |
| 77 |
___|___|___|_S_|___|___|___|___, /* CR */ |
| 78 |
___|___|___|___|___|___|___|___, /* SO */ |
| 79 |
___|___|___|___|___|___|___|___, /* SI */ |
| 80 |
___|___|___|___|___|___|___|___, /* DLE */ |
| 81 |
___|___|___|___|___|___|___|___, /* DC1 */ |
| 82 |
___|___|___|___|___|___|___|___, /* DC2 */ |
| 83 |
___|___|___|___|___|___|___|___, /* DC3 */ |
| 84 |
___|___|___|___|___|___|___|___, /* DC4 */ |
| 85 |
___|___|___|___|___|___|___|___, /* NAK */ |
| 86 |
___|___|___|___|___|___|___|___, /* SYN */ |
| 87 |
___|___|___|___|___|___|___|___, /* ETB */ |
| 88 |
___|___|___|___|___|___|___|___, /* CAN */ |
| 89 |
___|___|___|___|___|___|___|___, /* EM */ |
| 90 |
___|___|___|___|___|___|___|___, /* SUB */ |
| 91 |
___|___|___|___|___|___|___|___, /* ESC */ |
| 92 |
___|___|___|___|___|___|___|___, /* FS */ |
| 93 |
___|___|___|___|___|___|___|___, /* GS */ |
| 94 |
___|___|___|___|___|___|___|___, /* RS */ |
| 95 |
___|___|___|___|___|___|___|___, /* US */ |
| 96 |
___|___|___|_S_|___|___|___|___, /* sp */ |
| 97 |
___|___|___|___|_P_|___|___|___, /* ! */ |
| 98 |
___|___|___|___|_P_|___|___|___, /* " */ |
| 99 |
___|___|___|___|___|___|___|_E_, /* # */ |
| 100 |
___|___|___|___|___|___|___|_E_, /* $ */ |
| 101 |
___|___|___|___|___|___|___|_E_, /* % */ |
| 102 |
___|___|___|___|___|___|___|_E_, /* & */ |
| 103 |
___|___|___|___|_P_|___|___|___, /* ' */ |
| 104 |
___|___|___|___|_P_|___|___|___, /* ( */ |
| 105 |
___|___|___|___|_P_|___|___|___, /* ) */ |
| 106 |
___|___|___|___|_P_|_M_|___|___, /* * */ |
| 107 |
___|___|___|___|_P_|___|___|___, /* + */ |
| 108 |
___|___|___|___|_P_|___|___|___, /* , */ |
| 109 |
___|___|___|___|_P_|___|___|___, /* - */ |
| 110 |
___|___|___|___|_P_|___|___|___, /* . */ |
| 111 |
___|___|___|___|_P_|___|___|___, /* / */ |
| 112 |
___|___|_D_|___|___|___|___|___, /* 0 */ |
| 113 |
___|___|_D_|___|___|___|___|___, /* 1 */ |
| 114 |
___|___|_D_|___|___|___|___|___, /* 2 */ |
| 115 |
___|___|_D_|___|___|___|___|___, /* 3 */ |
| 116 |
___|___|_D_|___|___|___|___|___, /* 4 */ |
| 117 |
___|___|_D_|___|___|___|___|___, /* 5 */ |
| 118 |
___|___|_D_|___|___|___|___|___, /* 6 */ |
| 119 |
___|___|_D_|___|___|___|___|___, /* 7 */ |
| 120 |
___|___|_D_|___|___|___|___|___, /* 8 */ |
| 121 |
___|___|_D_|___|___|___|___|___, /* 9 */ |
| 122 |
___|___|___|___|_P_|___|___|___, /* : */ |
| 123 |
___|___|___|___|_P_|___|___|___, /* ; */ |
| 124 |
___|___|___|___|_P_|_M_|___|___, /* < */ |
| 125 |
___|___|___|___|_P_|___|___|___, /* = */ |
| 126 |
___|___|___|___|_P_|_M_|___|___, /* > */ |
| 127 |
___|___|___|___|_P_|___|___|___, /* ? */ |
| 128 |
___|___|___|___|_P_|___|___|___, /* @ */ |
| 129 |
_U_|___|___|___|___|___|___|___, /* A */ |
| 130 |
_U_|___|___|___|___|___|___|___, /* B */ |
| 131 |
_U_|___|___|___|___|___|___|___, /* C */ |
| 132 |
_U_|___|___|___|___|___|___|___, /* D */ |
| 133 |
_U_|___|___|___|___|___|___|___, /* E */ |
| 134 |
_U_|___|___|___|___|___|___|___, /* F */ |
| 135 |
_U_|___|___|___|___|___|___|___, /* G */ |
| 136 |
_U_|___|___|___|___|___|___|___, /* H */ |
| 137 |
_U_|___|___|___|___|___|___|___, /* I */ |
| 138 |
_U_|___|___|___|___|___|___|___, /* J */ |
| 139 |
_U_|___|___|___|___|___|___|___, /* K */ |
| 140 |
_U_|___|___|___|___|___|___|___, /* L */ |
| 141 |
_U_|___|___|___|___|___|___|___, /* M */ |
| 142 |
_U_|___|___|___|___|___|___|___, /* N */ |
| 143 |
_U_|___|___|___|___|___|___|___, /* O */ |
| 144 |
_U_|___|___|___|___|___|___|___, /* P */ |
| 145 |
_U_|___|___|___|___|___|___|___, /* Q */ |
| 146 |
_U_|___|___|___|___|___|___|___, /* R */ |
| 147 |
_U_|___|___|___|___|___|___|___, /* S */ |
| 148 |
_U_|___|___|___|___|___|___|___, /* T */ |
| 149 |
_U_|___|___|___|___|___|___|___, /* U */ |
| 150 |
_U_|___|___|___|___|___|___|___, /* V */ |
| 151 |
_U_|___|___|___|___|___|___|___, /* W */ |
| 152 |
_U_|___|___|___|___|___|___|___, /* X */ |
| 153 |
_U_|___|___|___|___|___|___|___, /* Y */ |
| 154 |
_U_|___|___|___|___|___|___|___, /* Z */ |
| 155 |
___|___|___|___|_P_|___|___|___, /* [ */ |
| 156 |
___|___|___|___|___|___|___|_E_, /* \ */ |
| 157 |
___|___|___|___|_P_|___|___|___, /* ] */ |
| 158 |
___|___|___|___|___|___|___|_E_, /* ^ */ |
| 159 |
___|___|___|___|___|___|___|_E_, /* _ */ |
| 160 |
___|___|___|___|_P_|___|___|___, /* ` */ |
| 161 |
___|_L_|___|___|___|___|___|___, /* a */ |
| 162 |
___|_L_|___|___|___|___|___|___, /* b */ |
| 163 |
___|_L_|___|___|___|___|___|___, /* c */ |
| 164 |
___|_L_|___|___|___|___|___|___, /* d */ |
| 165 |
___|_L_|___|___|___|___|___|___, /* e */ |
| 166 |
___|_L_|___|___|___|___|___|___, /* f */ |
| 167 |
___|_L_|___|___|___|___|___|___, /* g */ |
| 168 |
___|_L_|___|___|___|___|___|___, /* h */ |
| 169 |
___|_L_|___|___|___|___|___|___, /* i */ |
| 170 |
___|_L_|___|___|___|___|___|___, /* j */ |
| 171 |
___|_L_|___|___|___|___|___|___, /* k */ |
| 172 |
___|_L_|___|___|___|___|___|___, /* l */ |
| 173 |
___|_L_|___|___|___|___|___|___, /* m */ |
| 174 |
___|_L_|___|___|___|___|___|___, /* n */ |
| 175 |
___|_L_|___|___|___|___|___|___, /* o */ |
| 176 |
___|_L_|___|___|___|___|___|___, /* p */ |
| 177 |
___|_L_|___|___|___|___|___|___, /* q */ |
| 178 |
___|_L_|___|___|___|___|___|___, /* r */ |
| 179 |
___|_L_|___|___|___|___|___|___, /* s */ |
| 180 |
___|_L_|___|___|___|___|___|___, /* t */ |
| 181 |
___|_L_|___|___|___|___|___|___, /* u */ |
| 182 |
___|_L_|___|___|___|___|___|___, /* v */ |
| 183 |
___|_L_|___|___|___|___|___|___, /* w */ |
| 184 |
___|_L_|___|___|___|___|___|___, /* x */ |
| 185 |
___|_L_|___|___|___|___|___|___, /* y */ |
| 186 |
___|_L_|___|___|___|___|___|___, /* z */ |
| 187 |
___|___|___|___|___|___|___|_E_, /* { */ |
| 188 |
___|___|___|___|_P_|_M_|___|___, /* | */ |
| 189 |
___|___|___|___|___|___|___|_E_, /* } */ |
| 190 |
___|___|___|___|___|___|___|_E_, /* ~ */ |
| 191 |
___|___|___|___|___|___|___|___, /* DEL */ |
| 192 |
|
| 193 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x8. */ |
| 194 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x9. */ |
| 195 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xA. */ |
| 196 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xB. */ |
| 197 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xC. */ |
| 198 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xD. */ |
| 199 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xE. */ |
| 200 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* 0xF. */ |
| 201 |
}; |
| 202 |
|
| 203 |
/*===========================================================================*/ |
| 204 |
static char *sub(c) |
| 205 |
/*+++++++++++++++++++++++ |
| 206 |
.PURPOSE Check special TeX char, and provides substitution string |
| 207 |
.RETURNS Address of special string (NULL) |
| 208 |
.REMARKS |
| 209 |
------------------------*/ |
| 210 |
char c; /* IN: The character */ |
| 211 |
{ |
| 212 |
static char qesc[] = "\\q"; |
| 213 |
static char qmath[] = "$q$"; |
| 214 |
char *q; /* The returned pointer */ |
| 215 |
|
| 216 |
if (isTeX(c, _TeX_ESCAPE_)) |
| 217 |
{ if (c == '\\') q = "\\b{}"; |
| 218 |
else if (c == '^') q = "\\^{ }"; |
| 219 |
else if (c == '~') q = "\\~{ }"; |
| 220 |
else qesc[1] = c, q = qesc; |
| 221 |
} |
| 222 |
else if (isTeX(c, _TeX_MATH_)) |
| 223 |
qmath[1] = c, q = qmath; |
| 224 |
else if (c == '\n') q = "\\\\"; |
| 225 |
else q = (char *)0; |
| 226 |
|
| 227 |
return(q); |
| 228 |
} |
| 229 |
|
| 230 |
/*===========================================================================*/ |
| 231 |
int ed_tex (dest, size, source) |
| 232 |
/*+++++++++++++++++++++++ |
| 233 |
.PURPOSE Perform transformations of special TeX characters: |
| 234 |
add the `\' escape in front of characters { } $ & # % _ , |
| 235 |
replace EOL by \\, |
| 236 |
and replace \ by `\b ' |
| 237 |
assuming non-overlapping strings. |
| 238 |
.RETURNS Length of new string (== 0 if too small) |
| 239 |
.REMARKS Result is correct if strings overlap. |
| 240 |
Destination is "%" if dest too small. |
| 241 |
------------------------*/ |
| 242 |
char *dest; /* OUT: destination string */ |
| 243 |
int size; /* IN: size of dest */ |
| 244 |
char *source; /* IN: source string */ |
| 245 |
{ |
| 246 |
char *p1, *p2; |
| 247 |
char *p1e, *p2e, *q; |
| 248 |
int i; |
| 249 |
char *sub(); |
| 250 |
|
| 251 |
p1 = source; |
| 252 |
|
| 253 |
/* Compute number of required characters */ |
| 254 |
|
| 255 |
for (i = 0, p1e = p1; *p1e; p1e++) |
| 256 |
{ |
| 257 |
if (isTeX(*p1e, _TeX_ALNUM_)) continue; |
| 258 |
if ((p2 = sub(*p1e))) i += strlen(p2) - 1; |
| 259 |
} |
| 260 |
|
| 261 |
p2 = dest; |
| 262 |
p2e = p2 + (p1e-p1) + i; |
| 263 |
|
| 264 |
/* Check if destination is large enough */ |
| 265 |
|
| 266 |
if (p2e >= (p2 + size)) /* Too long */ |
| 267 |
{ |
| 268 |
*(p2++) = '%', *p2 = '\0'; |
| 269 |
return(0); |
| 270 |
} |
| 271 |
|
| 272 |
if ((p2 == p1) && (i == 0)) ; /* No Modif */ |
| 273 |
else if ((p2 >= p1) && (p2 <= p1e)) for (p2=p2e; p1e>=p1; p1e--) |
| 274 |
{ /* Overlap */ |
| 275 |
if (isTeX(*p1e, _TeX_ALNUM_)) q = (char *)0; |
| 276 |
else q = sub(*p1e); |
| 277 |
if (q) for (i=strlen(q), q+=i; --i >= 0; ) *(p2--) = *(--q); |
| 278 |
else *(p2--) = *p1e; |
| 279 |
} |
| 280 |
else for (; p1 <= p1e; p1++) |
| 281 |
{ /* Normal Left to Right */ |
| 282 |
if (isTeX(*p1, _TeX_ALNUM_)) q = (char *)0; |
| 283 |
else q = sub(*p1); |
| 284 |
if (q) while (*q) *(p2++) = *(q++); |
| 285 |
else *(p2++) = *p1; |
| 286 |
} |
| 287 |
|
| 288 |
return(p2e - dest); |
| 289 |
} |
| 290 |
|
| 291 |
unsigned char *atex_() |
| 292 |
/*+++++++++++++++++++++++ |
| 293 |
.PURPOSE Force the inclusion of this module. This is to get around a problem |
| 294 |
that occurs with the Gnu compiler on VAX/VMS (bad attributes of |
| 295 |
external variables) |
| 296 |
.RETURNS The address of the table characteristics |
| 297 |
------------------------*/ |
| 298 |
{ |
| 299 |
return(main_TeX); |
| 300 |
} |