File: rc.c

package info (click to toggle)
euler 1.61.0-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 5,164 kB
  • sloc: ansic: 24,761; sh: 8,314; makefile: 141; cpp: 47; php: 1
file content (349 lines) | stat: -rw-r--r-- 10,644 bytes parent folder | download | duplicates (7)
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
/*
 * Preferences for Euler
 *
 * Copyright (C) 2001 Eric Bouchar <bouchare.eric@wanadoo.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>       /* for strchr */
#include "rc.h"

static short colors[3][MAX_COLORS] = {
	{255,0,100,0  ,0  ,0  ,100,150,100,50,220 ,80  ,80  ,80  ,140,190},
	{255,0,0  ,100,0  ,100,100,150,100,50,80  ,220 ,80  ,140 ,140,190},
	{255,0,0  ,0  ,100,100,  0,150,100,50,80  ,80  ,220 ,140 , 80,190}
};

/* define enumeration values to be returned for specific symbols */
typedef enum {
	E_TOKEN_FIRST = G_TOKEN_LAST,
	E_TOKEN_SAVEATEXIT,
	E_TOKEN_BROWSER,
	E_TOKEN_TFONT,
	E_TOKEN_PFONT,
	E_TOKEN_TWIDTH,
	E_TOKEN_THEIGHT,
	E_TOKEN_GWIDTH,
	E_TOKEN_GHEIGHT,
	E_TOKEN_ESTACK,
	E_TOKEN_GSTACK,
	E_TOKEN_GLINES,
	E_TOKEN_GCOLORS,
	E_TOKEN_LAST
} EulerRcTokenType;


/* symbol array */
static const struct {
	gchar *name;
	guint  token;
} symbols[] = {
	{ "saveatexit",	E_TOKEN_SAVEATEXIT },
	{ "browser",	E_TOKEN_BROWSER },
	{ "tfont",		E_TOKEN_TFONT },
	{ "pfont",		E_TOKEN_PFONT },
	{ "twidth",		E_TOKEN_TWIDTH },
	{ "theight",	E_TOKEN_THEIGHT },
	{ "gwidth",		E_TOKEN_GWIDTH },
	{ "gheight",	E_TOKEN_GHEIGHT },
	{ "estack",		E_TOKEN_ESTACK },
	{ "gstack",		E_TOKEN_GSTACK },
	{ "glines",		E_TOKEN_GLINES },
	{ "colors",		E_TOKEN_GCOLORS }
};

static const guint n_symbols = sizeof (symbols) / sizeof (symbols[0]);


static guint eulerrc_parse(GScanner *scanner, EulerPrefs *prefs)
{
	guint token;

	token = g_scanner_get_next_token(scanner);
	
	switch (token) {
		case E_TOKEN_SAVEATEXIT:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_INT) return G_TOKEN_INT;
			prefs->saveatexit = scanner->value.v_int;
			break;
		case E_TOKEN_BROWSER:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_STRING) return G_TOKEN_STRING;
			strcpy(prefs->browser, scanner->value.v_string);
			break;
		case E_TOKEN_TFONT:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_STRING) return G_TOKEN_STRING;
			strcpy(prefs->tfont, scanner->value.v_string);
			break;
		case E_TOKEN_PFONT:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_STRING) return G_TOKEN_STRING;
			strcpy(prefs->pfont, scanner->value.v_string);
			break;
		case E_TOKEN_TWIDTH:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_INT) return G_TOKEN_INT;
			prefs->twidth = scanner->value.v_int;
			break;
 		case E_TOKEN_THEIGHT:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_INT) return G_TOKEN_INT;
			prefs->theight = scanner->value.v_int;
			break;
		case E_TOKEN_GWIDTH:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_INT) return G_TOKEN_INT;
			prefs->gwidth = scanner->value.v_int;
			break;
 		case E_TOKEN_GHEIGHT:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_INT) return G_TOKEN_INT;
			prefs->gheight = scanner->value.v_int;
			break;
		case E_TOKEN_ESTACK:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_INT) return G_TOKEN_INT;
			prefs->estack = scanner->value.v_int;
			break;
		case E_TOKEN_GSTACK:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_INT) return G_TOKEN_INT;
			prefs->gstack = scanner->value.v_int;
			break;
		case E_TOKEN_GLINES:
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
			token = g_scanner_get_next_token(scanner);
			if (token != G_TOKEN_INT) return G_TOKEN_INT;
			prefs->glines = scanner->value.v_int;
			break;
		case E_TOKEN_GCOLORS:
			{
				int i, j;
				
				token = g_scanner_get_next_token(scanner);
				if (token != G_TOKEN_EQUAL_SIGN) return G_TOKEN_EQUAL_SIGN;
				token = g_scanner_get_next_token(scanner);
				if (token != G_TOKEN_LEFT_CURLY) return G_TOKEN_LEFT_CURLY;
				
				for (j=0 ; j<MAX_COLORS ;j++) {
					token = g_scanner_get_next_token(scanner);
					if (token != G_TOKEN_LEFT_CURLY) return G_TOKEN_LEFT_CURLY;
					for (i=0 ; i<3 ; i++) {
						token = g_scanner_get_next_token(scanner);
						if (token != G_TOKEN_INT) return G_TOKEN_INT;
						prefs->colors[i][j] = scanner->value.v_int;
						if (i!=2) {
							token = g_scanner_get_next_token(scanner);
							if (token != G_TOKEN_COMMA) return G_TOKEN_COMMA;
						}
					}
					token = g_scanner_get_next_token(scanner);
					if (token != G_TOKEN_RIGHT_CURLY) return G_TOKEN_RIGHT_CURLY;
				}
				token = g_scanner_get_next_token(scanner);
				if (token != G_TOKEN_RIGHT_CURLY) return G_TOKEN_RIGHT_CURLY;
			}
			break;
		default:
			return token;
	}
	
	return G_TOKEN_NONE;
}


static gint eulerrc_load(EulerPrefs *prefs)
{
	GScanner *scanner;
	gint fd;
	guint i, expected_token = G_TOKEN_NONE;
	gchar *filename;

	filename = g_strconcat(g_get_home_dir(),"/.euler/eulerrc",NULL);
	fd = open(filename, O_RDONLY);
	g_free(filename);
	if (fd < 0)	return 0;

	scanner = g_scanner_new(NULL);

	/* Don't return G_TOKEN_SYMBOL, but the symbol's value */
	scanner->config->symbol_2_token = TRUE;

	/* Don't return G_TOKEN_IDENTIFIER, but convert it to string */
	scanner->config->identifier_2_string = TRUE;

	g_scanner_freeze_symbol_table(scanner);
	for (i = 0; i < n_symbols; i++)
		g_scanner_add_symbol(scanner,symbols[i].name,GINT_TO_POINTER(symbols[i].token));
	g_scanner_thaw_symbol_table(scanner);

	g_scanner_input_file(scanner, fd);
	scanner->input_name = ".eulerrc";

	/*
	 * Scanning loop, we parse the input until it's end is reached,
	 * the scanner encountered a lexing error, or our sub routine came
	 * across invalid syntax
	 */
	while(1) {
		expected_token = eulerrc_parse(scanner,prefs);

		if (expected_token != G_TOKEN_NONE) {
			switch (expected_token) {
				case G_TOKEN_EQUAL_SIGN:
					g_scanner_unexp_token(scanner,expected_token,NULL,"=",NULL,NULL,TRUE);
					break;
				case G_TOKEN_EOF:
					break;
				default:
					g_scanner_unexp_token(scanner,expected_token,NULL,NULL,NULL,NULL,TRUE);
					break;
			}
			break;
		}
	}

	g_scanner_destroy(scanner);

	close(fd);

	return 1;
}


/*
 *	preference init
 */
EulerPrefs eulerrc_init()
{
	EulerPrefs prefs;
	int i,j;
	
	prefs.saveatexit = E_SAVEATEXIT_DEFAULT;
	prefs.twidth	 = E_TWIDTH_DEFAULT;
	prefs.theight	 = E_THEIGHT_DEFAULT;
	prefs.gwidth	 = E_GWIDTH_DEFAULT;
	prefs.gheight	 = E_GHEIGHT_DEFAULT;
	prefs.estack	 = E_ESTACK_DEFAULT;
	prefs.gstack	 = E_GSTACK_DEFAULT;
	prefs.glines	 = E_GLINES_DEFAULT;
	for (i=0 ; i<3 ; i++)
		for (j=0 ; j<MAX_COLORS ; j++)
			prefs.colors[i][j] = colors[i][j];
	strcpy(prefs.browser, E_BROWSER_DEFAULT);
	strcpy(prefs.tfont, E_TFONT_DEFAULT);
	strcpy(prefs.pfont, E_PFONT_DEFAULT);
	eulerrc_load(&prefs);
	return prefs;
}

static int is_dir(char *dirname)
{
	struct stat buf;
	
	if (stat(dirname,&buf)==0) {
		if (S_ISDIR(buf.st_mode))
			return 1;
	}
	return 0;
}


/*
 *  Preferences memory-deallocation
 *  (Call this one at exit time)
 */
void eulerrc_save(EulerPrefs *prefs)
{
	FILE *file;
	char *filename, *dirname;
	
	dirname = g_strconcat(g_get_home_dir(),"/.euler",NULL);
	if (!is_dir(dirname)) {
		mkdir(dirname,0755);
	}
	g_free(dirname);
	
	filename = g_strconcat(g_get_home_dir(),"/.euler/eulerrc",NULL);
	file = fopen(filename, "w");
	g_free(filename);

	if (file) {
		int i,j;
		
		fprintf(file,"#\n# Euler ressouce file\n#\n");
		fprintf(file,"#\n# flag to tell Euler if it must save the preferences when the program exits.\n#\n");
		fprintf(file,"saveatexit = %d\n",prefs->saveatexit);
		fprintf(file,"\n#\n# browser for documentation viewing\n#\n");
		fprintf(file,"browser = \"%s\"\n",prefs->browser);
		fprintf(file,"\n#\n# text window font\n#\n");
		fprintf(file,"tfont = \"%s\"\n",prefs->tfont);
		fprintf(file,"\n#\n# editor font\n#\n");
		fprintf(file,"pfont = \"%s\"\n",prefs->pfont);
		fprintf(file,"\n#\n# text window width and height (in chars)\n#\n");
		fprintf(file,"twidth = %d\ntheight = %d\n",prefs->twidth,prefs->theight);
		fprintf(file,"\n#\n# graphics window width and height (in pixels)\n#\n");
		fprintf(file,"gwidth = %d\ngheight = %d\n",prefs->gwidth,prefs->gheight);
		fprintf(file,"\n#\n# Euler stack size in kbyte\n#\n");
		fprintf(file,"estack = %d\n",prefs->estack);
		fprintf(file,"\n#\n# graphic stack in kbyte\n#\n");
		fprintf(file,"gstack = %d\n",prefs->gstack);
		fprintf(file,"\n#\n# glines fixes the font size for the graphics window\n# the font size is gheight / glines.\n#\n");
		fprintf(file,"glines = %d\n",prefs->glines);
		fprintf(file,"\n#\n# the 16 basic colors used for the graphics\n#\n");
		fprintf(file,"colors = {\n");
		for (j=0 ; j<MAX_COLORS ; j++) {
			fprintf(file,"    { ");
			for (i=0 ; i<3 ; i++) {
				fprintf(file,"%3d ",prefs->colors[i][j]);
				if (i!=2) fprintf(file,", ");
			}
			fprintf(file,"}\n");
		}
		fprintf(file,"}\n");
		fclose(file);
	}
}