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
|
/* Copyright (c) 1995 by jfw. All Rights Reserved */
/***
NAME
procchar.c
PURPOSE
charater handling for typeset.
NOTES
get's imported from chrproc.scm
There is some more less used handling defined. This is only for speed.
HISTORY
jw - 31. Mar 1995 Created.
jw - 29. Okt 1995 Changed to return buffers instead of writing ports
***/
extern char * GC_malloc(int);
extern char * GC_realloc(char*, int);
#define malloc(a) GC_malloc(a)
#define realloc(a,b) GC_realloc((a),(b))
#define INT(a) (((int)(a))&0xff)
/* The first version happend to import write_out_str and write the
* result of translations instead of giving the translated string back
*
extern int write_out_str (char *);
*
*/
static int syschar = 0;
/* collect escaped characters */
int proc_esc_char ( unsigned char c)
{
static int escape = 0;
static int ccode = 0;
if( escape ) {
switch(c) {
case 'n': escape = 0; return '\n';
case '\\': escape = 0; return '\\';
case '|': escape = 0; syschar = !syschar; return - (syschar + 2);
default:
if( (c >= '0') && (c <= '9')) {
ccode = (ccode << 3) | (c - '0');
}
if( escape < 3 ) {
++escape;
return -1;
} else {
escape=0;
return ccode;
}
}
} else {
if( c == '\\' ) {
escape=1;
ccode=0;
return -1;
} else {
return (int) c;
}
}
}
/* use c_buf to return characters to be written out */
static unsigned char c_buf [2] = "\0";
static unsigned char * plain_proc_char ( unsigned char c)
{
int ci = proc_esc_char(c);
if( ci < 0 ) return 0;
*c_buf=(unsigned char)ci;
return c_buf;
}
static unsigned char * info_proc_char ( unsigned char c)
{
int ci = proc_esc_char(c);
if( ci < 0 ) return 0;
switch (ci) {
case '\n': case '\t':
*c_buf = (unsigned char) ' ';
return c_buf;
default:
*c_buf=(unsigned char)ci;
return c_buf;
}
}
static unsigned char * html_proc_char ( unsigned char c)
{
int ci = proc_esc_char(c);
if( ci < 0 ) return 0;
switch( ci ) {
case '>': return ">";
case '&': return "&";
case '<': return "<";
case INT(''): return "ä";
case INT(''): return "ö";
case INT(''): return "ü";
case INT(''): return "Ä";
case INT(''): return "Ö";
case INT(''): return "Ü";
case INT(''): return "ß";
case '"': return """;
default:
*c_buf = (unsigned char) ci;
return c_buf;
}
}
static unsigned char * lout_proc_char ( unsigned char c)
{
int ci = proc_esc_char(c);
if( ci < 0 ) return 0;
switch(ci) {
case '&': return "\"&\"";
case '"': return "\"\\\"\"";
case '{': return "\"{\"";
case '}': return "\"}\"";
case '|': return "\"|\"";
case '\\': return "\"\\\\\"";
case '/': return "\"/\"";
case '#': return "\"#\"";
case '@': return "\"@\"";
case '^': return "\"^\"";
default: *c_buf=(unsigned char)ci; return c_buf;
}
}
static unsigned char * latex_proc_char( unsigned char c)
{
int ci = proc_esc_char(c);
if( ci < 0 ) return 0;
switch( ci ) {
case '\\': return "$\\backslash$";
case '%': return "\\%";
case '&': return "\\&";
case '$': return "\\$";
case '#': return "\\#";
case '_': return "\\_";
case '{': return "\\{";
case '}': return "\\}";
case '<': return "{\\tt<}";
case '>': return "{\\tt>}";
case '~': return "\\~{ }";
case '^': return "\\^{ }";
case INT(''): return "\\\"A";
case INT(''): return "\\\"O";
case INT(''): return "\\\"U";
case INT(''): return "\\\"a";
case INT(''): return "\\\"o";
case INT(''): return "\\\"u";
case INT(''): return "\\ss{}";
default: *c_buf=(unsigned char)ci; return c_buf;
}
}
static unsigned char * latex_math_proc_char( unsigned char c)
{
int ci = proc_esc_char(c);
if( ci < 0 ) return 0;
switch( ci ) {
case '\\': return "\\backslash ";
case '%': return "\\%";
case '&': return "\\&";
case '$': return "\\$";
case '#': return "\\#";
case '_': return "\\_";
case '{': return "\\{";
case '}': return "\\}";
case '~': return "\\~{ }";
case '^': return "\\^{ }";
case INT(''): return "\\\"A";
case INT(''): return "\\\"O";
case INT(''): return "\\\"U";
case INT(''): return "\\\"a";
case INT(''): return "\\\"o";
case INT(''): return "\\\"u";
case INT(''): return "\\ss{}";
default: *c_buf=(unsigned char)ci; return c_buf;
}
}
static unsigned char * man_proc_char( unsigned char c )
{
static int cc = 0;
int ci = proc_esc_char(c);
if( ci < 0 ) return 0;
switch (ci) {
case '\n':
cc=0; *c_buf='\n'; return c_buf;
case '-':
++cc; return "\\-";
case '\\':
++cc; return "\\e";
case '.':
++cc; *c_buf=ci;
if(cc==1) return "\\&."; else return c_buf;
case '\'':
++cc; *c_buf=ci;
if(cc==1) return "\\\'"; else return c_buf;
default:
++cc;
*c_buf = ci;
return c_buf;
}
}
/* ---------------------------------------------------------------------- */
typedef unsigned char* chrp;
typedef unsigned char* (*chrprocp) (unsigned char);
static unsigned char * tr_string( chrprocp f, unsigned char * line)
{
int ii=0, bi=0, ll, sl, bs, ci;
unsigned char *buf;
unsigned char * r,c;
ll = strlen(line);
sl = ll > 16 ? ll/8 : 2; /* 10% grow expected, at least 2 chars */
bs = ll + sl; /* buffer size */
buf = (chrp) malloc(bs*sizeof(unsigned char));
while((c=line[ii++])) {
if( syschar ) {
ci = proc_esc_char(c);
if( ci >= 0 ) buf[bi++]=(unsigned char)ci;
} else {
r=(*f)(c);
if( r == c_buf ) {
buf[bi++]=*r;
} else if (r) {
while(*r) {
buf[bi++] = *r++;
if(bi==bs) {
bs += sl;
buf = (chrp) realloc(buf, bs);
}
}
}
}
if( bi == bs) { /* No space left */
bs += sl;
buf = (chrp) realloc(buf, bs);
}
}
buf[bi]=0;
return buf;
}
unsigned char * plain_tr_string( unsigned char * line )
{
return tr_string( plain_proc_char, line);
}
unsigned char * html_tr_string( unsigned char * line)
{
return tr_string( html_proc_char, line);
}
unsigned char * man_tr_string( unsigned char * line)
{
return tr_string( man_proc_char, line);
}
unsigned char * lout_tr_string( unsigned char * line)
{
return tr_string( lout_proc_char, line);
}
unsigned char * latex_tr_string( unsigned char * line)
{
return tr_string( latex_proc_char, line);
}
static unsigned char * latex_condens_math( unsigned char* line )
{
int i, j;
for( i=0, j=0; line[i];) {
if(line[i] == '\\') { /* buggy if the last char was a \ */
/* but this *should* be impossible */
line[j++]=line[i++];
} else {
if( line[i] == '$') {
++i;
continue;
}
}
line[j++]=line[i++];
}
line[j]='\0';
return line;
}
unsigned char * latex_math_tr_string( unsigned char * line)
{
return latex_condens_math( tr_string( latex_math_proc_char, line));
}
unsigned char * info_tr_string( unsigned char * line)
{
return tr_string( info_proc_char, line);
}
|