File: script.c

package info (click to toggle)
pcb-rnd 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,260 kB
  • sloc: ansic: 198,059; sh: 5,767; yacc: 5,568; makefile: 2,519; awk: 1,737; lex: 1,073; python: 519; lisp: 169; tcl: 67; xml: 40; perl: 34; ruby: 5
file content (410 lines) | stat: -rw-r--r-- 9,852 bytes parent folder | download
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
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 2018 Tibor 'Igor2' Palinkas
 *
 *  This module, debug, was written and is Copyright (C) 2016 by Tibor Palinkas
 *  this module is also subject to the GNU GPL as described below
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Contact:
 *    Project page: http://repo.hu/projects/pcb-rnd
 *    lead developer: http://repo.hu/projects/pcb-rnd/contact.html
 *    mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
 */
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <genht/htsp.h>
#include <genht/hash.h>
#include <puplug/puplug.h>
#include <genregex/regex_se.h>

#include "actions.h"
#include "plugins.h"
#include "error.h"
#include "compat_misc.h"
#include "compat_fs.h"
#include "safe_fs.h"
#include "pcb-printf.h"
#include "globalconst.h"

typedef struct {
	char *id, *fn, *lang;
	pup_plugin_t *pup;
	fgw_obj_t *obj;
} script_t;

static htsp_t scripts; /* ID->script_t */

static pup_context_t script_pup;

#include "c_script.c"
#include "conf_core.h"
#include "compat_fs.h"

/* dir name under dotdir for saving script persistency data */
#define SCRIPT_PERS "script_pers"

/* allow only 1 megabyte of script persistency to be loaded */
#define PERS_MAX_SIZE 1024*1024

static const char *script_pup_paths[] = {
	"/usr/local/lib/puplug",
	"/usr/lib/puplug",
	NULL
};

static int script_save_preunload(script_t *s, const char *data)
{
	FILE *f;
	gds_t fn;

	gds_init(&fn);
	gds_append_str(&fn, conf_core.rc.path.home);
	gds_append(&fn, PCB_DIR_SEPARATOR_C);
	gds_append_str(&fn, DOT_PCB_RND);
	pcb_mkdir(fn.array, 0755);

	gds_append(&fn, PCB_DIR_SEPARATOR_C);
	gds_append_str(&fn, SCRIPT_PERS);
	pcb_mkdir(fn.array, 0750);

	gds_append(&fn, PCB_DIR_SEPARATOR_C);
	gds_append_str(&fn, s->obj->name);

	f = pcb_fopen(fn.array, "w");
	if (f != NULL) {
		gds_uninit(&fn);
		fputs(data, f);
		fclose(f);
		return 0;
	}

	gds_uninit(&fn);
	return -1;
}

/* auto-unregister from any central infra the script may have regitered in using
   the standard script cookie */
static void script_unreg(const char *cookie)
{
	if ((pcb_gui != NULL) && (pcb_gui->remove_menu != NULL))
		pcb_gui->remove_menu(cookie);
}

/* unload a script, free all memory and remove it from all lists/hashes.
   If preunload is not NULL, it's the unload reason the script's own
   preunload() function is called and its return is saved. */
static void script_free(script_t *s, const char *preunload, const char *cookie)
{
	if ((preunload != NULL) && (s->obj != NULL)) {
		fgw_func_t *f;
		fgw_arg_t res, argv[2];

		f = fgw_func_lookup(s->obj, "preunload");
		if (f != NULL) {
			argv[0].type = FGW_FUNC;
			argv[0].val.func = f;
			argv[1].type = FGW_STR;
			argv[1].val.cstr = preunload;
			res.type = FGW_INVALID;
			if (f->func(&res, 2, argv) == 0) {
				if ((fgw_arg_conv(&pcb_fgw, &res, FGW_STR) == 0) && (res.val.str != NULL) && (*res.val.str != '\0'))
					script_save_preunload(s, res.val.str);
			}
			fgw_arg_free(&pcb_fgw, &res);
		}
	}

	if (cookie != NULL)
		script_unreg(cookie);

	if (s->obj != NULL)
		fgw_obj_unreg(&pcb_fgw, s->obj);
	if (s->pup != NULL)
		pup_unload(&script_pup, s->pup, NULL);
	free(s->id);
	free(s->fn);
	free(s);
}

static void script_unload_entry(htsp_entry_t *e, const char *preunload, const char *cookie)
{
	script_t *s = (script_t *)e->value;
	script_free(s, preunload, cookie);
	e->key = NULL;
	htsp_delentry(&scripts, e);
}

static const char *script_persistency_id = NULL;
static int script_persistency(fgw_arg_t *res, const char *cmd)
{
	char *fn;

	if (script_persistency_id == NULL) {
		pcb_message(PCB_MSG_ERROR, "ScriptPersistency may be called only from the init part of a script\n");
		goto err;
	}

	fn = pcb_concat(conf_core.rc.path.home, PCB_DIR_SEPARATOR_S, DOT_PCB_RND, PCB_DIR_SEPARATOR_S, SCRIPT_PERS, PCB_DIR_SEPARATOR_S, script_persistency_id, NULL);

	if (strcmp(cmd, "remove") == 0) {
		PCB_ACT_IRES(pcb_remove(fn));
		goto succ;
	}

	if (strcmp(cmd, "read") == 0) {
		FILE *f;
		long fsize = pcb_file_size(fn);
		char *data;

		if ((fsize < 0) || (fsize > PERS_MAX_SIZE))
			goto err;

		data = malloc(fsize+1);
		if (data == NULL)
			goto err;

		f = pcb_fopen(fn, "r");
		if (f == NULL) {
			free(data);
			goto err;
		}

		if (fread(data, 1, fsize, f) != fsize) {
			free(data);
			fclose(f);
			goto err;
		}

		fclose(f);
		data[fsize] = '\0';
		res->type = FGW_STR | FGW_DYN;
		res->val.str = data;
		goto succ;
	}

	pcb_message(PCB_MSG_ERROR, "Unknown command for ScriptPersistency\n");

	err:;
	PCB_ACT_IRES(-1);
	return 0;

	succ:
	free(fn);
	return 0; /* assume IRES is set already */
}

static char *script_gen_cookie(const char *force_id)
{
	if (force_id == NULL) {
		if (script_persistency_id == NULL) {
			pcb_message(PCB_MSG_ERROR, "ScriptCookie called from outside of script init, can not generate the cookie\n");
			return NULL;
		}
		force_id = script_persistency_id;
	}
	return pcb_concat("script::fungw::", force_id, NULL);
}

static int script_unload(const char *id, const char *preunload)
{
	char *cookie;
	htsp_entry_t *e = htsp_getentry(&scripts, id);
	if (e == NULL)
		return -1;

	cookie = script_gen_cookie(id);
	script_unload_entry(e, preunload, cookie);
	free(cookie);
	return 0;
}

static int script_load(const char *id, const char *fn, const char *lang)
{
	char name[PCB_PATH_MAX];
	pup_plugin_t *pup;
	script_t *s;
	int st;
	const char *old_id;

	if (htsp_has(&scripts, id)) {
		pcb_message(PCB_MSG_ERROR, "Can not load script %s from file %s: ID already in use\n", id, fn);
		return -1;
	}

	if (lang == NULL) {
TODO(": guess")
		pcb_message(PCB_MSG_ERROR, "Can not load script %s from file %s: failed to guess language from file name\n", id, fn);
		return -1;
	}

	if (strcmp(lang, "c") != 0) {
		pcb_snprintf(name, sizeof(name), "fungw_%s", lang);

		old_id = script_persistency_id;
		script_persistency_id = id;
		pup = pup_load(&script_pup, script_pup_paths, name, 0, &st);
		script_persistency_id = old_id;
		if (pup == NULL) {
			pcb_message(PCB_MSG_ERROR, "Can not load script engine %s for language %s\n", name, lang);
			return -1;
		}
	}
	else {
		lang = "pcb_rnd_c";
		pup = NULL;
	}

	s = calloc(1, sizeof(script_t));
	s->pup = pup;
	s->id = pcb_strdup(id);
	s->fn = pcb_strdup(fn);
	s->lang = pcb_strdup(lang);

	old_id = script_persistency_id;
	script_persistency_id = id;
	s->obj = fgw_obj_new(&pcb_fgw, s->id, s->lang, s->fn, NULL);
	script_persistency_id = old_id;

	if (s->obj == NULL) {
		script_free(s, NULL, NULL);
		pcb_message(PCB_MSG_ERROR, "Failed to parse/execute %s script from file %s\n", id, fn);
		return -1;
	}

	htsp_set(&scripts, s->id, s);
	return 0;
}

static int script_reload(const char *id)
{
	int ret;
	char *fn, *lang, *cookie;
	script_t *s;
	htsp_entry_t *e = htsp_getentry(&scripts, id);

	if (e == NULL)
		return -1;

	s = e->value;
	fn = pcb_strdup(s->fn);
	lang = pcb_strdup(s->lang);

	cookie = script_gen_cookie(id);
	script_unload_entry(e, "reload", cookie);
	free(cookie);

	ret = script_load(id, fn, lang);
	free(fn);
	free(lang);
	return ret;
}

void script_list(const char *pat)
{
	htsp_entry_t *e;
	re_se_t *r = NULL;

	if ((pat != NULL) && (*pat != '\0'))
		r = re_se_comp(pat);

	for(e = htsp_first(&scripts); e; e = htsp_next(&scripts, e)) {
		script_t *s = (script_t *)e->value;
		if ((r == NULL) || (re_se_exec(r, s->id)) || (re_se_exec(r, s->fn)) || (re_se_exec(r, s->lang)))
			pcb_message(PCB_MSG_INFO, "id=%s fn=%s lang=%s\n", s->id, s->fn, s->lang);
	}
	
	if (r != NULL)
		re_se_free(r);
}

static void oneliner_boilerplate(FILE *f, const char *lang, int pre)
{
	if (strcmp(lang, "mawk") == 0) {
		if (pre)
			fputs("BEGIN {\n", f);
		else
			fputs("}\n", f);
	}
}

int script_oneliner(const char *lang, const char *src)
{
	FILE *f;
	char *fn;
	int res = 0;

	fn = pcb_tempfile_name_new("oneliner");
	f = pcb_fopen(fn, "w");
	if (f == NULL) {
		pcb_tempfile_unlink(fn);
		pcb_message(PCB_MSG_ERROR, "script oneliner: can't open temp file for write\n");
		return -1;
	}
	oneliner_boilerplate(f, lang, 1);
	fputs(src, f);
	fputs("\n", f);
	oneliner_boilerplate(f, lang, 0);
	fclose(f);

	if (script_load("__oneliner", fn, lang) != 0) {
		pcb_message(PCB_MSG_ERROR, "script oneliner: can't load/parse the script\n");
		res = -1;
	}
	script_unload("__oneliner", NULL);

	pcb_tempfile_unlink(fn);
	return res;
}

#include "timer.c"
#include "script_act.c"

char *script_cookie = "script plugin";

PCB_REGISTER_ACTIONS(script_action_list, script_cookie)

int pplg_check_ver_script(int ver_needed) { return 0; }

void pplg_uninit_script(void)
{
	htsp_entry_t *e;
	pcb_remove_actions_by_cookie(script_cookie);
	for(e = htsp_first(&scripts); e; e = htsp_next(&scripts, e)) {
		script_t *script = e->value;
		char *cookie = script_gen_cookie(script->id);
		script_unload_entry(e, "unload", cookie);
		free(cookie);
	}

	htsp_uninit(&scripts);
	pup_uninit(&script_pup);
}

#include "dolists.h"
int pplg_init_script(void)
{
	PCB_API_CHK_VER;
	PCB_REGISTER_ACTIONS(script_action_list, script_cookie);
	pcb_c_script_init();
	htsp_init(&scripts, strhash, strkeyeq);
	pup_init(&script_pup);
	return 0;
}