File: test.lex

package info (click to toggle)
abiword 0.7.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 20,604 kB
  • ctags: 18,358
  • sloc: cpp: 88,791; ansic: 66,296; sh: 7,777; makefile: 3,397; xml: 687; perl: 361; awk: 273; sed: 36; csh: 28
file content (324 lines) | stat: -rw-r--r-- 8,605 bytes parent folder | download | duplicates (6)
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
%{
#include <stdlib.h>
#include <stdio.h>
#include "config.h"
#include <string.h>
#include "mswordview.h"

int noconfig_styles=0;

enum tokens {ENDOFINPUT=0, ELEMENTtoken,STYLEtoken,LEFTBRACEtoken,RIGHTBRACEtoken,
	STARTtoken,ENDtoken,PRESPACEtoken,POSTSPACEtoken,BOLDtoken,
	ITALICtoken,FONTtoken,DEFAULTtoken,IDENTIFIERtoken};

%}
whitespace          [ \t\n]
alpha               [[:alnum:]><{}/]
%s instring
%%
<INITIAL>\"     {  /* start of string */
         BEGIN instring;
         yymore();
        }
<instring>\\\"    {
         yymore();
        }
<instring>\"    {  /* end of string */
         BEGIN INITIAL;
		 return(IDENTIFIERtoken);
        }
<instring>.     {
         yymore();
        }
<instring>\n    {
         printf("Error, new line in string\n");
         BEGIN INITIAL;
        }
<INITIAL>"#".*                          ;
<INITIAL>{whitespace}+                  ;
<INITIAL>Style							return(STYLEtoken);
<INITIAL>Start							return(STARTtoken);
<INITIAL>End							return(ENDtoken);
<INITIAL>PreSpace						return(PRESPACEtoken);
<INITIAL>PostSpace						return(POSTSPACEtoken);
<INITIAL>Element						return(ELEMENTtoken);
<INITIAL>"{"                 			return(LEFTBRACEtoken);
<INITIAL>"}"                 			return(RIGHTBRACEtoken);
<INITIAL>"Bold"                 		return(BOLDtoken);
<INITIAL>"Italic"                 		return(ITALICtoken);
<INITIAL>"Font"                 		return(FONTtoken);
<INITIAL>"Default"                 		return(DEFAULTtoken);
<INITIAL>{alpha}{alpha}*             	return(IDENTIFIERtoken);
%%

int main(void)
	{
	FILE *in = fopen("testconfig","rb");
	config_style *in_style=NULL;
	config_style *ctemp;
	Parse(in,&in_style);
	ctemp = in_style;
	while(ctemp != NULL)
		{
		fprintf(stderr,"the style name is %s, the beginning is %s, the end is %s\n",ctemp->name,ctemp->begin,ctemp->end);
		ctemp = ctemp->next;
		}
	}

int lennum(int num)
	{
	char buffer[128];
	sprintf(buffer,"%d",num);
	return(strlen(buffer));
	}

char *expand_variables(char *in, pap *apap)
	{
	int hack=0;
	char *test,*oldtest,*temp;
	char buffer[4096];
	char *variable;
	char *replacement=NULL;
	char *begin=(char *)malloc(strlen(in)+1);
	strcpy(begin,in);

	test = oldtest = strchr(begin,'$');

	while (test != NULL)
		{
		replacement=NULL;
		variable = buffer;
		test++;
		while(legal_variable_char(*test))
			*variable++ = *test++;
		*variable= '\0';

		if (!(strcmp(buffer,"patterndir")))
			{
			replacement = (char *)malloc(
				strlen(begin)+1-strlen(buffer)+strlen(patterndir()));
			strncpy(replacement,begin,oldtest-begin);
			strcpy(replacement+(oldtest-begin),patterndir());
			strcpy(replacement+(oldtest-begin)+strlen(patterndir()),
				begin+(oldtest-begin)+(test-oldtest));
			fprintf(stderr,"replacement is %s\n",replacement);
			}
		else if (!(strcmp(buffer,"pixelsafter")))
			{
			if (!apap)
				{
				fprintf(stderr,"invalid pap for variable request pixelsafter\n");
				apap = (pap *)malloc(sizeof(pap));
				apap->dyaAfter = 0;
				apap->dyaBefore = 0;
				}
				
			replacement = (char *)malloc(
				strlen(begin)+1-strlen(buffer)+lennum(apap->dyaAfter/TWIRPS_PER_V_PIXEL));
			strncpy(replacement,begin,oldtest-begin);
			
			sprintf(replacement+(oldtest-begin),"%d",apap->dyaAfter/TWIRPS_PER_V_PIXEL);
			
			strcpy(replacement+(oldtest-begin)+lennum(apap->dyaAfter/TWIRPS_PER_V_PIXEL),
				begin+(oldtest-begin)+(test-oldtest));
			fprintf(stderr,"replacement is %s\n",replacement);

			if (hack)
				free(apap);
			}
		else if (!(strcmp(buffer,"pixelsbefore")))
			{
			if (!apap)
				{
				fprintf(stderr,"invalid pap for variable request pixelsbefore\n");
				apap = (pap *)malloc(sizeof(pap));
				apap->dyaAfter = 0;
				}
				
			replacement = (char *)malloc(
				strlen(begin)+1-strlen(buffer)+lennum(apap->dyaBefore/TWIRPS_PER_V_PIXEL));
			strncpy(replacement,begin,oldtest-begin);
			
			sprintf(replacement+(oldtest-begin),"%d",apap->dyaBefore/TWIRPS_PER_V_PIXEL);
			
			strcpy(replacement+(oldtest-begin)+lennum(apap->dyaBefore/TWIRPS_PER_V_PIXEL),
				begin+(oldtest-begin)+(test-oldtest));
			fprintf(stderr,"replacement is %s\n",replacement);

			if (hack)
				free(apap);
			}
		else
			fprintf(stderr,"unrecognized variable $%s\n",buffer);

		if (NULL != replacement)
			{
			free(begin);
			begin = replacement;
			}

		test = oldtest = strchr(begin,'$');
		}
	return(begin);
	}

char *argument(void)
	{
	char *test;
	char *begin;
	int i;
	begin = yytext;
	if (*begin == '\"')
		begin++;
	if (begin[strlen(begin)-1] == '\"')
		begin[strlen(begin)-1] = '\0';
    while ((isspace(*begin)) && (yytext+strlen(yytext)-begin > 0))
    	begin++;
    for (i=0;i<strlen(begin);i++)
    	begin[i] = tolower(begin[i]);
	return(begin);
	}

int Parse (FILE * in,config_style **in_style)
	{
	config_style *current;
	config_style *temp;
	config_style *start=NULL;
	enum tokens CurrentToken;
	char *begin="test";
	int i;
	yyin = in;
	do
		{
		CurrentToken = yylex();
		switch (CurrentToken)
			{
			case STYLEtoken:
				fprintf(stderr,"Style Token named ");
				noconfig_styles++;
				CurrentToken = yylex ();
				begin = argument();
				temp = (config_style *)malloc(sizeof(config_style));
				if (start == NULL)
					{
					start = temp;
					current = temp;
					fprintf(stderr,"first time\n");
					}
				else
					{
					current->next = temp;
					current = current->next;
					fprintf(stderr,"next time\n");
					}
				current->next = NULL;
				current->begin=NULL;
				current->end=NULL;
				current->Default=NULL;
				current->bold=NULL;
				current->font=NULL;
				current->italic=NULL;
				current->name = (char *)malloc(strlen(begin)+1);
				strcpy(current->name,begin);
				fprintf(stderr,"%s\n",current->name);
				CurrentToken = yylex ();
				if (CurrentToken != LEFTBRACEtoken)
					fprintf(stderr,"hmm, got it wrogn, got %s\n",yytext);
				do
					{
					CurrentToken = yylex ();
					switch(CurrentToken)
						{
						case STARTtoken:
							CurrentToken = yylex ();
							begin = argument();
							current->begin = (char *)malloc(strlen(begin)+1);
							strcpy(current->begin,begin);
							fprintf(stderr,"start string is %s\n",current->begin);
							break;
						case ENDtoken:
							CurrentToken = yylex ();
							begin = argument();
							current->end = (char *)malloc(strlen(begin)+1);
							strcpy(current->end,begin);
							fprintf(stderr,"end string is %s\n",current->end);
							break;
						case BOLDtoken:
							CurrentToken = yylex ();
							begin = argument();
							current->bold = (char *)malloc(strlen(begin)+1);
							strcpy(current->bold,begin);
							fprintf(stderr,"bold string is %s\n",current->bold);
							break;
						case ITALICtoken:
							CurrentToken = yylex ();
							begin = argument();
							current->italic = (char *)malloc(strlen(begin)+1);
							strcpy(current->italic,begin);
							fprintf(stderr,"italic string is %s\n",current->italic);
							break;
						case FONTtoken:
							CurrentToken = yylex ();
							begin = argument();
							current->font = (char *)malloc(strlen(begin)+1);
							strcpy(current->font,begin);
							fprintf(stderr,"font string is %s\n",current->font);
							break;
						case DEFAULTtoken:
							CurrentToken = yylex ();
							begin = argument();
							current->Default = (char *)malloc(strlen(begin)+1);
							strcpy(current->Default,begin);
							fprintf(stderr,"default string is %s\n",current->Default);
							break;
						case PRESPACEtoken:
							CurrentToken = yylex ();
							begin = argument();
							current->prespace = (char *)malloc(strlen(begin)+1);
							strcpy(current->prespace,begin);
							fprintf(stderr,"prespace string is %s\n",current->prespace);
							break;
						case POSTSPACEtoken:
							CurrentToken = yylex ();
							begin = argument();
							current->postspace = (char *)malloc(strlen(begin)+1);
							strcpy(current->postspace,begin);
							fprintf(stderr,"portspace string is %s\n",current->postspace);
							break;
						default:
							break;
						}
					}
				while(CurrentToken != RIGHTBRACEtoken);
				break;
			case ELEMENTtoken:
				fprintf(stderr,"Element Token named ");
				CurrentToken = yylex ();
				begin = argument();
				fprintf(stderr,"%s\n",begin);
				CurrentToken = yylex ();
				if (CurrentToken != LEFTBRACEtoken)
					fprintf(stderr,"hmm, got it wrogn\n");
				do
					{
					CurrentToken = yylex ();
					fprintf(stderr,"parsing style code %s\n",yytext);
					}
				while(CurrentToken != RIGHTBRACEtoken);

				break;

			}
		}
	while (CurrentToken != ENDOFINPUT);
	*in_style = start;
	}

#ifdef yywrap
#undef yywrap
#endif

int yywrap ()
    {
    return (1);
    }