summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/fonts/malayalam/preproc/trs.c
blob: 4805c35079afbfbeb80b286f931dc03472c6ec7a (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
/*

trs.c -- read transcription table from file

History:
  24-DEC-1992 added ability to look in directory given by home
  06-JUL-1992 Genesis (Jeroen Hellingman)

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "readfile.h"
#include "trs.h"
#include "mm.h"

extern char *progname;
extern int verbose;

static PSTree *read_table(FILE *f);

trs_table *read_trs(char *filename)
{   int stop = FALSE;
    char b[BUFSIZE];
    char *command=b;
    string_list *rest=NULL;
    trs_table *result = NULL;
    FILE *f;

    f = fopen(filename, "r");
    if(f==NULL)
    {   filename = prepend_home(filename);
        f = fopen(filename, "r");
    		if(f==NULL)
		{  	fprintf(stderr, "%s: can't open %s\n", progname, filename);
        	exit(1);
        }
    }
    if(verbose) printf("reading transcription from %s\n", filename);

    result=malloc(sizeof(trs_table));
    if(result==NULL){fprintf(stderr,"can't allocate\n");exit(0);}

    while(!stop)
    {   get_command(f, command, &rest);
        if(command == NULL || strcmp(command, "end")==0)
        {   if(result->p == NULL ||
               result->atbegin == NULL || 
               result->atend == NULL)
            {   fprintf(stderr, "%s: invalid transcription file &s:\n", progname, filename);
                if(result->p == NULL)
                    fprintf(stderr, "no @table given\n");
                if(result->atbegin == NULL)
                    fprintf(stderr, "no @atbegin given\n");
                if(result->atend == NULL)
                    fprintf(stderr, "no @atend given\n");
                exit(1);
            }
            stop = TRUE;
        }
        else
        {   if(strcmp(command, "atbegin")==0)
                result->atbegin = rest;
            else
            {   if(strcmp(command, "atend")==0)
                result->atend = rest;
                else
                {   if(strcmp(command, "table")==0)
                        result->p = read_table(f);
                }
            }
        }
    }
    return result;
}
    
static PSTree *read_table(FILE *f)
{   PSTree      *result = NULL;
    string_list *l, *n;
    char        *pattern;
    char        b[BUFSIZE];
    char        *action = b;
    char        *tmp = NULL;
    int         i = 1;              /* length of action string */
    int         no_parameters;

    while(TRUE)
    {   int ignore = FALSE;
        l = get_line(f);
        if(l==NULL)
            return result;
        else
        {   
            /* pattern at position 1 */
            pattern = l->s;
            n = l->n;
            free(l);
            if(n==NULL)
            {   fprintf(stderr, "incomplete line\n");
                ignore = TRUE;
            }
            else
            {   /* command pattern at position 2    */
                action[0] = (n->s)[0];  /* single letter commands */
                action[1] = '\0';       /* NULL terminate */
                switch(action[0])
                {   case 'f':
                    case 't':
                    case 's': no_parameters = 0; break;
                    default : no_parameters = 1;
                }
                /* parameters pattern at position 3 */
                while(no_parameters > 0)
                {   no_parameters--;
                    l = n->n;
                    free(n);
                    n = l;
                    if(l==NULL)
                    {   fprintf(stderr, "incomplete line\n");
                        ignore = TRUE;
                        break;
                    }
                    else
                    {   if (++i >= BUFSIZE)
                        {   fprintf(stderr, "line too long\n");
                            ignore = TRUE;
                            break;
                        }
                        if ((i+=(int)strlen(l->s)) >= BUFSIZE)
                        {   fprintf(stderr, "line too long\n");
                            ignore = TRUE;
                            break;
                        }
                        action = strcat(action, l->s);
                    }
                }
            }
        }
        if(!ignore)
        {   tmp = malloc(strlen(action));
            if(tmp==NULL){fprintf(stderr,"can't allocate\n");exit(1);}
            strcpy(tmp, action);
            PSTinsert(&result, pattern, tmp);
        }
    }
}