summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/fonts/malayalam/preproc/patc/patc.c
blob: c3c641851b12418af51b4ed67dce106d76f94db7 (plain)
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
#define version "patc v1.1c (c) Jeroen Hellingman 01-MAY-1993\n"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "PSTree.h"

#define TRUE    (1==1)
#define FALSE   (1==0)

#define NUMPATS 10  /* number of pattern trees */
#define PATLEN  50  /* maximum length of pattern */
#define BUFSIZE 512 /* pushback buffer size (BUFSIZE >= PATLEN) */

/* datatypen */

typedef struct patterntree
{   PSTree *t;      /* pattern tree for this node */
    int     r;      /* restrictive or not? */
} patterntree;

/* globals */

FILE *infile;
FILE *outfile;
patterntree pat[NUMPATS];
char *progname = "patc";
char *patfilename = "patc.pat";
char *infilename;
char *outfilename;
static int quiet = TRUE;            /* be quiet */
static int linenumber = 1;          /* current line in infile */

/* prototypes */

void parsetables(FILE *patfile);
void patc(void);
void processflags(int argc, char** argv);
void usage(void);
void copytexcommand(void);
void skiptexcommand(void);
void skiptillmatchingbrace(void);
void skiptillchar(char c1);
void copycomment(void);
void skipcomment(void);
int readchar(void);
void unreadchar(int);
int what_escape(const char *s, char *result);

/***********************************************************************/

void PUSHBACK(char *c)
/* push the characters in string c back into the inputstream, works
 * with the pair of functions readchar() and unreadchar()
 */
{   int i = (int)strlen(c)-1;
    for( ;i >= 0; i--) unreadchar((int)c[i]);
}

void main(int argc, char** argv)
/* check arguments, intialize tables, open files
 */
{   FILE *patfile;

    processflags(argc, argv);
    if(!quiet) fputs(version, stderr);
    
    patfile = fopen(patfilename, "r");
    if(patfile==NULL)
    {   fprintf(stderr, "%s: can't open %s\n", progname, patfilename);
        exit(2);
    }
    parsetables(patfile);
    fclose(patfile);
    
    infile = fopen(infilename, "r");
    if(infile==NULL)
    {   fprintf(stderr, "%s: can't open %s\n", progname, infilename);
        exit(2);
    }
    outfile = fopen(outfilename, "w");
    if(outfile==NULL)
    {   fprintf(stderr, "%s: can't create %s\n", progname, outfilename);
        exit(2);
    }    
    patc();
    fclose(infile);
    fclose(outfile);
    exit(0);
}

void processflags(int argc, char** argv)
{   int nextoption = FALSE;
    int i = 1;
    
    if(argc < i+2) usage();
    if(argv[i][0] == '-') nextoption = TRUE;
    
    while(nextoption)
    {   switch(argv[i][1])
        {   case 'v':   quiet = FALSE;      break;
            case 'p':   patfilename = argv[i+1]; i++; break;
            default:    usage();
        }
        i++;
        if(argc < i+2) usage();
        if(argv[i][0] != '-') nextoption = FALSE;
    }
    
    infilename  = argv[i];
    outfilename = argv[i+1];
}

void usage()
{   fprintf(stderr, "usage: %s [-v] [-p patfile] infile outfile\n", progname);
    exit(1);
}

/***********************************************************************/

int readline(char *l, FILE* infile)
{   int i = 0;
    int c = fgetc(infile);
    while(isspace(c) && c != '\n' && c != EOF) c = fgetc(infile); /* skip whitespace */
    if(c == '%') while(c != '\n' && c != EOF) c = fgetc(infile); /* skip comments */
    while(c != '\n' && c != EOF && i < BUFSIZE)
    {   l[i] = (c == EOF) ? '\0' : (int) c;
        i++;
        c = getc(infile);
    }
    l[i] = '\0';    /* NULL-terminate */
    return (c!=EOF);
}

int getword(char *s, char *d)
{   int i = 0, j = 0;
    while(isspace(s[i]) && s[i] != '\0') i++;
    while(isalnum(s[i])) { d[j] = s[i]; j++; i++; }
    d[j] = '\0';
    return i;
}

int getquotedstring(const char *s, char *d)
{   int i = 0;  /* no of chars read in source */
    int j = 0;  /* no of chars inserted in destination */
    while(isspace(s[i]) && s[i] != '\0') i++;

    if(s[i] == '"')
    {   i++;
        while(s[i] != '"')
        {   if(s[i] == '\\') /* escape char */
                i += what_escape(&s[i], &d[j]);
            else
            {   d[j] = s[i];
            }
            j++; i++;
        }
    }
    d[j] = '\0';    /* NULL-terminate */
    i++;            /* skip final " */
    return i;
}

/* Find out what escape sequence is used. If non can be found, we just 
   forget about the backslash. Interprete numbers up to 255/177/FF
*/

#define UNSIGNED(t) (char)(((t) < 0) ? (t) + 256 : (t))

int what_escape(const char *s, char *result)
{   int i = 1;      /* length of escape sequence read */
    int ok = TRUE;
    int t = 0;      /* temporary result */

    switch(s[1])
    {   case '"':   *result = '"';  break;
        case '\\':  *result = '\\'; break;
        case 't':   *result = '\t'; break;
        case 'n':   *result = '\n'; break;
        case 'b':   *result = '\b'; break;
        case 'h':   /* hexadecimal */
            while(i < 3 && ok)
            {   i++;
                if(s[i]>='0' && s[i]<='9') t = t * 16 + (s[i] - '0');
                else if(s[i]>='A' && s[i]<='F') t = t * 16 + (s[i] - 'A') + 10;
                else if(s[i]>='a' && s[i]<='f') t = t * 16 + (s[i] - 'a') + 10;
                else
                {   if(i==2) /* no number after \h */
                        *result = 'h';
                    else /* short number after \h */
                        *result = UNSIGNED(t);
                    i--;
                    ok = FALSE; 
                }
            }
            if(ok) *result = UNSIGNED(t);
            break;
        case 'd':   /* decimal */
            while(i < 4 && ok)
            {   i++;
                if(s[i]>='0' && s[i]<='9') t = t * 10 + (s[i] - '0');
                else
                {   if(i==2) /* no number after \d */
                        *result = 'd';
                    else /* short number after \d */
                        *result = UNSIGNED(t);
                    i--;
                    ok = FALSE; 
                }
            }
            if(ok) *result = UNSIGNED(t);
            break;
        default:    /* try octal interpretation */
            i--;
            while(i < 3 && ok)
            {   i++;
                if(s[i]>='0' && s[i]<='7') t = t * 8 + (s[i] - '0');
                else
                {   if(i==1) /* no number after \ */
                        *result = s[i];
                    else /* short number after \ */
                    {   *result = UNSIGNED(t);
                        i--;
                    }
                    ok = FALSE; 
                }
            }
            if(ok) *result = UNSIGNED(t);
    }
    return i;
}

void parsetables(FILE *patfile)
{   char line[BUFSIZE];
    char command[BUFSIZE];
    char pattern[BUFSIZE];
    char action[BUFSIZE];
    char *tmp;
    int pos = 1;
    char notEOF = TRUE;
    int currentpat = 0; /* current patterntree under construction */

    while(notEOF)
    {   notEOF = readline(line, patfile);
        pos = 0;
        switch(line[0])
        {   case '\0':  /* empty line */    break;
            case '@':   /* command */
                        pos++;
                        pos += tolower(getword(&line[1], command));
                        if(strcmp(command, "patterns") == 0)
                        {   pos += getword(&line[pos], command);
                            currentpat = atoi(command);
                            pat[currentpat].r = FALSE;
                        }
                        else if(strcmp(command, "rpatterns") == 0)
                        {   pos += getword(&line[pos], command);
                            currentpat = atoi(command);
                            pat[currentpat].r = TRUE;
                        }
                        else if(strcmp(command, "end") == 0) return;
                        else
                        {   fprintf(stderr, "Error: unknown command %s\n", command);
                            exit(1);
                        }
                        break;
            case '"':   /* action */
                        pos += getquotedstring(line, pattern);
                        pos += getword(&line[pos], command);
                        pos += getquotedstring(&line[pos], &action[1]);
                        action[0] = command[0];
                        tmp = malloc(strlen(action) + 1);
                        if(tmp == NULL)
                        {   fprintf(stderr, "Error: cannot allocate\n");
                            exit(3);
                        }
                        strcpy(tmp, action);
                        PSTinsert(&pat[currentpat].t, pattern, tmp);
                        break;
            default:    fprintf(stderr, "Error: illegal line '%s' in %s\n",
                                    line, patfilename);
                        fprintf(stderr, "I will forget it\n");
        }
    }
}

/***********************************************************************/

void patc()
{   char ps[PATLEN+1];      /* pattern to be search for */
    char *action;           /* action with pattern */
    int len = PATLEN;       /* length of found pattern; part of ps to be read */
    int current = 0;        /* current active patterntree */
    int i, j;               /* counters */

    while(TRUE)
    {
        /* fill pattern */
        for(i = 0, j = len; j < PATLEN; i++, j++) ps[i] = ps[j];
        for(i = PATLEN - len; i < PATLEN; i++)
        {   int c = readchar();
            ps[i] = (c == EOF) ? '\0' : c;
        }
        ps[PATLEN] = '\0'; /* NULL-terminate */
        if(ps[0] == '\0') break;
        
        /* find action */
    
        action = PSTmatch(pat[current].t, ps, &len);
        
        if(len == 0)
        {   if(pat[current].r) /* complain */
                fprintf(stderr, "Error: illegal character '%c' near line %d\n", ps[0], linenumber);
            else /* copy silently */
                fputc(ps[0], outfile);
            len = 1;
        }
        else /* do action */
        {   switch(action[0])
            {   case 'p':   fputs(&action[1], outfile);         break;
                case 'c':   PUSHBACK(ps); len = PATLEN; copycomment();      break;
                case 't':   PUSHBACK(ps); len = PATLEN; copytexcommand();       break;
                case 'T':   PUSHBACK(ps); len = PATLEN; skiptexcommand();       break;
                case 's':   PUSHBACK(ps); len = PATLEN; skipcomment();      break;
                case 'f':   /* forget */                        break;
                case 'e':   fprintf(stderr, "Error: %s near line %d\n", &action[1], linenumber);
                            break;
				case 'S':   PUSHBACK(ps); len = PATLEN; skiptillchar(action[1]);	break;
			    case 'B':   PUSHBACK(ps); len = PATLEN; skiptillmatchingbrace();	break;
                case '0':   current = 0; fputs(&action[1], outfile); break;
                case '1':   current = 1; fputs(&action[1], outfile); break;
                case '2':   current = 2; fputs(&action[1], outfile); break;
                case '3':   current = 3; fputs(&action[1], outfile); break;
                case '4':   current = 4; fputs(&action[1], outfile); break;
                case '5':   current = 5; fputs(&action[1], outfile); break;
                case '6':   current = 6; fputs(&action[1], outfile); break;
                case '7':   current = 7; fputs(&action[1], outfile); break;
                case '8':   current = 8; fputs(&action[1], outfile); break;
                case '9':   current = 9; fputs(&action[1], outfile); break;
                default:    fprintf(stderr, "Internal error: unknown action\n");
                            exit(10);
            } /* switch */
        } /* else */
    } /* while */
    if(current != 0)
        fprintf(stderr, "Warning: mode = %d at end of file\n", current);
} /* patc() */

/***********************************************************************/

void skiptillchar(char c1)
{	char c2;
	do
	{	c2 = readchar();
		if(c2 == EOF) return;
	} while(c2 != c1);
}

void skiptillmatchingbrace()
/* skip a TeX group enclosed in braces.
 * next brace on input opens the group to skip
 */
{	int i = 1;
	int c;	
	do
	{	c = readchar();
		if(c == EOF) return;
	} while(c != '{');
	while(i>0)
	{	c = readchar();
		if(c == '{') i++;
		if(c == '}') i--;
		if(c == EOF) return;
	}
}

void copytexcommand()
/* copy TeX commmand, including preceding \
 * this will work in plain TeX and LaTeX
 */
{   int c = readchar();
    if(c=='\\')
    {   fputc(c, outfile);
        c = readchar();
        if(isalpha(c))
        {   while(isalpha(c))
            {   fputc(c, outfile);
                c = readchar();
            }
            unreadchar(c);
        }
        else
            fputc(c, outfile);
    }
    else
        unreadchar(c);
}

void skiptexcommand()
/* skip TeX commmand, including preceding \
 * this will work in plain TeX and LaTeX
 */
{   int c = readchar();
    if(c=='\\')
    {   c = readchar();
        if(isalpha(c))
        {   while(isalpha(c))
            {   c = readchar();
            }
            unreadchar(c);
        }
    }
    else
        unreadchar(c);
}


void copycomment()
{   int c = readchar();
    if(c=='%')
    {   while(c != '\n' && c != EOF)
        {   fputc(c, outfile);
            c = readchar();
        }
        fputc('\n', outfile);
    }
    else
        unreadchar(c);
}

void skipcomment()
{   int c = readchar();
    if(c=='%')
    {   while(c != '\n' && c != EOF)
            c = readchar();
    }
    else
        unreadchar(c);
}

/***********************************************************************/
/* file access with buffer */

static int fbuffer[BUFSIZE];        /* buffer for file operations */
static int last = 0;                /* last + 1 used in fbuffer */

int readchar()
{   int result;

    if(last==0) /* niets in buffer */
        result = fgetc(infile);
    else /* pak first uit buffer */
    {   last--;
        result = fbuffer[last];
    }
    if(result == '\n') linenumber++;
    return result;
}

void unreadchar(int c)
{  
    if(last == BUFSIZE)
    {   fprintf(stderr, "%s: push-back file buffer full\n", progname);
        exit(1);
    }
    else /* voeg na last in buffer */
    {   fbuffer[last] = c;
        last++;
        if(c == '\n') linenumber--;
    }
}

/***********************************************************************/