aboutsummaryrefslogtreecommitdiff
path: root/shared/toxy/scriptlet.c
blob: df94e90572b3c7cc465cf9cb950fc03f96aaff18 (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/* Copyright (c) 2003-2004 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include <stdio.h>
#include <string.h>
#ifdef UNIX
#include <unistd.h>
#endif
#ifdef NT
#include <io.h>
#endif
#include "m_pd.h"
#include "g_canvas.h"
#include "common/loud.h"
#include "common/grow.h"
#include "common/props.h"
#include "scriptlet.h"

//#define SCRIPTLET_DEBUG

#define SCRIPTLET_INISIZE   1024
#define SCRIPTLET_MARGIN      64
#define SCRIPTLET_MAXARGS      9  /* do not increase (parser's constraint) */
#define SCRIPTLET_MAXPUSH  20000  /* cf SOCKSIZE in t_tkcmd.c, LATER revisit */

enum { SCRIPTLET_CVOK, SCRIPTLET_CVUNKNOWN, SCRIPTLET_CVMISSING };

struct _scriptlet
{
    t_pd             *s_owner;
    t_glist          *s_glist;     /* containing glist (empty allowed) */
    t_symbol         *s_rptarget;  /* reply target */
    t_symbol         *s_cbtarget;  /* callback target */
    t_symbol         *s_item;
    t_scriptlet_cvfn  s_cvfn;      /* if empty, passing resolveall is a bug */
    t_canvas         *s_cv;        /* as returned by cvfn */
    int               s_cvstate;
    int     s_locked;  /* append lock, for filtering, when reading from file */
    int     s_size;
    char   *s_buffer;
    char    s_bufini[SCRIPTLET_INISIZE];
    char   *s_head;       /* ptr to the command part of a scriptlet */
    char   *s_tail;
    char    s_separator;  /* current separator, set before a new token */
    int     s_ac;                     /* the actual count */
    t_atom  s_av[SCRIPTLET_MAXARGS];  /* always padded with zeros (if used) */
};

static t_canvas *scriptlet_canvasvalidate(t_scriptlet *sp, int visedonly)
{
    t_canvas *cv;
    if (sp->s_cvstate == SCRIPTLET_CVUNKNOWN)
    {
	if (sp->s_cvfn)
	    cv = sp->s_cv = sp->s_cvfn(sp->s_owner);
	else
	{
	    bug("scriptlet_canvasvalidate");
	    return (0);
	}
	if (cv && (!visedonly || glist_isvisible(cv)))
	    sp->s_cvstate = SCRIPTLET_CVOK;
	else
	    sp->s_cvstate = SCRIPTLET_CVMISSING;
    }
    else cv = sp->s_cv;
    return (sp->s_cvstate == SCRIPTLET_CVOK ? cv : 0);
}

static int scriptlet_ready(t_scriptlet *sp)
{
    int len = sp->s_tail - sp->s_head;
    if (len > 0 && *sp->s_head && sp->s_cvstate != SCRIPTLET_CVMISSING)
    {
	if (len < SCRIPTLET_MAXPUSH)
	    return (1);
	else
	    loud_error(sp->s_owner,
		       "scriptlet too long to be pushed (%d bytes)", len);
    }
    return (0);
}

static int scriptlet_doappend(t_scriptlet *sp, char *buf)
{
    if (buf && !sp->s_locked)
    {
	int nprefix = sp->s_head - sp->s_buffer;
	int nused = sp->s_tail - sp->s_buffer;
	int newsize = nused + strlen(buf) + SCRIPTLET_MARGIN;
	if (newsize > sp->s_size)
	{
	    int nrequested = newsize;
	    sp->s_buffer = grow_withdata(&nrequested, &nused,
					 &sp->s_size, sp->s_buffer,
					 SCRIPTLET_INISIZE, sp->s_bufini,
					 sizeof(*sp->s_buffer));
	    if (nrequested != newsize)
	    {
		scriptlet_reset(sp);
		return (0);
	    }
	    sp->s_head = sp->s_buffer + nprefix;
	    sp->s_tail = sp->s_buffer + nused;
	}
	if (sp->s_separator && sp->s_tail > sp->s_head)
	    *sp->s_tail++ = sp->s_separator;
	*sp->s_tail = 0;
	strcpy(sp->s_tail, buf);
	sp->s_tail += strlen(sp->s_tail);
    }
    sp->s_separator = 0;
    return (1);
}

static char *scriptlet_dedot(t_scriptlet *sp, char *ibuf, char *obuf,
			     int resolveall, int visedonly,
			     int ac, t_atom *av, t_props *argprops)
{
    int len = 0;
    switch (*ibuf)
    {
    case '#':
	/* ac is ignored -- assuming av is padded to SCRIPTLET_MAXARGS atoms */
	if (resolveall)
	{
	    int which = ibuf[1] - '1';
	    if (which >= 0 && which < SCRIPTLET_MAXARGS)
	    {
		if (av)
		{
		    if (av[which].a_type == A_FLOAT)
		    {
			sprintf(obuf, "%g", av[which].a_w.w_float);
			len = 2;
		    }
		    else if (av[which].a_type == A_SYMBOL)
		    {
			strcpy(obuf, av[which].a_w.w_symbol->s_name);
			len = 2;
		    }
		}
	    }
	    else if (argprops)
	    {
		char *iptr, *optr, c;
		int cnt;
		for (iptr = ibuf + 1, c = *iptr, cnt = 1; c;
		     iptr++, c = *iptr, cnt++)
		{
		    if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z'))
		    {
			*iptr = 0;
			break;
		    }
		}
		if (optr = props_getvalue(argprops, ibuf + 1))
		{
		    strcpy(obuf, optr);
		    len = cnt;
		}
		if (c) *iptr = c;
	    }
	}
	break;
    case '-':
	if (resolveall && sp->s_item)
	{
	    t_canvas *cv;
	    if (cv = scriptlet_canvasvalidate(sp, visedonly))
	    {
		sprintf(obuf, ".x%x.c.%s%x", (int)cv, sp->s_item->s_name,
			(int)sp->s_owner);
		len = 1;
	    }
	}
	break;
    case '^':
	if (resolveall)
	{
	    t_canvas *cv;
	    if (cv = scriptlet_canvasvalidate(sp, visedonly))
	    {
		sprintf(obuf, ".x%x", (int)cv);
		len = 1;
	    }
	}
	break;
    case '|':
	if (resolveall)
	{
	    strcpy(obuf, sp->s_cbtarget->s_name);
	    len = 1;
	}
	break;
    case '~':  /* FIXME, the dot-tilde stuff is purely experimental. */
	if (resolveall)
	{
	    t_canvas *cv;
	    if (cv = scriptlet_canvasvalidate(sp, 0))
	    {
		t_glist *glist;
		if (!strncmp(&ibuf[1], "tag", 3))
		{
		    t_rtext *rt;
		    glist = cv->gl_owner;
		    if (glist && glist_isvisible(glist) && glist->gl_editor
			&& (rt = glist_findrtext(glist, (t_object *)cv)))
			sprintf(obuf, "%s", rtext_gettag(rt));
		    else
			obuf[0] = 0;
		    len = 4;
		}
		else if (!strncmp(&ibuf[1], "parent", 6))
		{
		    glist = cv->gl_owner;
		    if (glist && glist_isvisible(glist))
			sprintf(obuf, ".x%x", (int)glist);
		    else
			obuf[0] = 0;
		    len = 7;
		}
		else if (!strncmp(&ibuf[1], "root", 4))
		{
		    glist = canvas_getrootfor(cv);
		    if (glist && glist_isvisible(glist))
			sprintf(obuf, ".x%x", (int)glist);
		    else
			obuf[0] = 0;
		    len = 5;
		}
		else if (!strncmp(&ibuf[1], "owner", 5))
		{
		    if (glist = canvas_getrootfor(cv))
			glist = glist->gl_owner;
		    if (glist && glist_isvisible(glist))
			sprintf(obuf, ".x%x", (int)glist);
		    else
			obuf[0] = 0;
		    len = 6;
		}
		else if (!strncmp(&ibuf[1], "top", 3))
		{
		    glist = cv;
		    while (glist->gl_owner) glist = glist->gl_owner;
		    if (glist && glist_isvisible(glist))
			sprintf(obuf, ".x%x", (int)glist);
		    else
			obuf[0] = 0;
		    len = 4;
		}
		/* LATER find out when gl_<coords> are updated,
		   think how to better sync them to Tk. */
		else if (!strncmp(&ibuf[1], "x1", 2))
		{
		    sprintf(obuf, "%d", cv->gl_screenx1);
		    len = 3;
		}
		else if (!strncmp(&ibuf[1], "x2", 2))
		{
		    sprintf(obuf, "%d", cv->gl_screenx2);
		    len = 3;
		}
		else if (!strncmp(&ibuf[1], "y1", 2))
		{
		    sprintf(obuf, "%d", cv->gl_screeny1);
		    len = 3;
		}
		else if (!strncmp(&ibuf[1], "y2", 2))
		{
		    sprintf(obuf, "%d", cv->gl_screeny2);
		    len = 3;
		}
		else if (!strncmp(&ibuf[1], "edit", 4))
		{
		    sprintf(obuf, "%d", cv->gl_edit);
		    len = 5;
		}
		else if (!strncmp(&ibuf[1], "gop", 3))
		{
		    sprintf(obuf, "%d", glist_isgraph(cv));
		    len = 4;
		}
		else if (!strncmp(&ibuf[1], "dir", 3))
		{
		    sprintf(obuf, "%s", canvas_getdir(cv)->s_name);
		    len = 4;
		}
		else loud_error(sp->s_owner, "bad field '%s'", &ibuf[1]);
	    }
	}
	break;
    case '`':
	sprintf(obuf, "\\");
	len = 1;
	break;
    case ':':
	sprintf(obuf, ";");
	len = 1;
	break;
    case '(':
	sprintf(obuf, "{");
	len = 1;
	break;
    case ')':
	sprintf(obuf, "}");
	len = 1;
	break;
    case '<':
	if (resolveall)
	{
	    if (ibuf[1] == ':')
	    {
		sprintf(obuf, "{pd [concat ");
		len = 2;
	    }
	    else if (ibuf[1] == '|')
	    {
		sprintf(obuf, "{pd [concat %s ", sp->s_rptarget->s_name);
		len = 2;
	    }
	    else
	    {
		sprintf(obuf, "{pd [concat %s _cb ", sp->s_cbtarget->s_name);
		len = 1;
	    }
	}
	break;
    case '>':
	if (resolveall)
	{
	    sprintf(obuf, "\\;]}");
	    len = 1;
	}
	break;
    }
    return (len ? ibuf + len : 0);
}

int scriptlet_isempty(t_scriptlet *sp)
{
    return (!(sp->s_tail > sp->s_head && *sp->s_head));
}

void scriptlet_reset(t_scriptlet *sp)
{
    sp->s_cvstate = SCRIPTLET_CVUNKNOWN;
    sp->s_locked = 0;
    sp->s_separator = 0;
    strcpy(sp->s_buffer, "namespace eval ::toxy {\
 proc query {} {set ::toxy::reply [\n");
    sp->s_head = sp->s_tail = sp->s_buffer + strlen(sp->s_buffer);
}

void scriptlet_prealloc(t_scriptlet *sp, int sz, int mayshrink)
{
    if (sz < SCRIPTLET_INISIZE)
	sz = SCRIPTLET_INISIZE;
    if (sz < sp->s_size && mayshrink)
    {
	if (sp->s_buffer != sp->s_bufini)
	    freebytes(sp->s_buffer, sp->s_size * sizeof(*sp->s_buffer));
	else
	    bug("scriptlet_prealloc");
	sp->s_size = SCRIPTLET_INISIZE;
	sp->s_buffer = sp->s_bufini;
    }
    if (sz > sp->s_size)
	sp->s_buffer = grow_nodata(&sz, &sp->s_size, sp->s_buffer,
				   SCRIPTLET_INISIZE, sp->s_bufini,
				   sizeof(*sp->s_buffer));
    scriptlet_reset(sp);
}

static int scriptlet_addstring(t_scriptlet *sp, char *ibuf,
			       int resolveall, int visedonly,
			       int ac, t_atom *av, t_props *argprops)
{
    int result = 1;
    char *bp = ibuf, *ep = ibuf, *ep1;
    char dotbuf[256];  /* FIXME requires a growable per-scriptlet buffer. */
    if (!sp->s_separator)
	sp->s_separator = ' ';
    while (*ep)
    {
	if (*ep == '.'
	    && (ep1 = scriptlet_dedot(sp, ep + 1, dotbuf,
				      resolveall, visedonly, ac, av, argprops)))
	{
	    *ep = 0;
	    if (!(result = scriptlet_doappend(sp, bp)))
		break;
	    *ep = '.';
	    if (!(result = scriptlet_doappend(sp, dotbuf)))
		break;
	    bp = ep = ep1;
	}
	else ep++;
    }
    if (result)
	result = scriptlet_doappend(sp, bp);
    sp->s_separator = 0;
    return (result);
}

static int scriptlet_addfloat(t_scriptlet *sp, t_float f)
{
    char buf[64];
    if (!sp->s_separator)
	sp->s_separator = ' ';
    sprintf(buf, "%g ", f);
    return (scriptlet_doappend(sp, buf));
}

int scriptlet_add(t_scriptlet *sp,
		  int resolveall, int visedonly, int ac, t_atom *av)
{
    while (ac--)
    {
	int result = 1;
	if (av->a_type == A_SYMBOL)
	    result = scriptlet_addstring(sp, av->a_w.w_symbol->s_name,
					 resolveall, visedonly, 0, 0, 0);
	else if (av->a_type == A_FLOAT)
	    result = scriptlet_addfloat(sp, av->a_w.w_float);
	if (!result)
	    return (0);
	av++;
    }
    return (1);
}

void scriptlet_setseparator(t_scriptlet *sp, char c)
{
    sp->s_separator = c;
}

void scriptlet_push(t_scriptlet *sp)
{
    if (scriptlet_ready(sp))
    {
	char *tail = sp->s_tail;
	strcpy(tail, "\n");
	sys_gui(sp->s_head);
	*tail = 0;
    }
}

void scriptlet_qpush(t_scriptlet *sp)
{
    if (scriptlet_ready(sp))
    {
	char buf[MAXPDSTRING];
	char *tail = sp->s_tail;
	/* Could not find any other way, than to postpone processing of the
	   query, after everything which might have been glued to our tail,
	   is evaluated.  Otherwise, any command arriving later, during
	   blocking of the query (e.g. in a Tk dialog), would be evaluated
	   prior to our tail, via Tcl_DoOneEvent().  We postpone also the
	   setup step (defining the query proc), in order to be able to
	   handle several queries at once.  All this is far from ideal --
	   the sequence "query this, tot that", is swapped, unless written
	   as "query this, tot after 0 .(that.)", which is going to cause
	   much confusion...  LATER revisit.  Do not forget, that since
	   pd_readsocket() is not reentrant, sys_gui()d commands should never
	   enter event loop directly by blocking on a dialog, vwait, etc.,
	   because the pd_readsocket handler is event-driven on unix. */
	sys_gui("after 0 {\n");
	strcpy(tail, "]}}\n");
	sys_gui(sp->s_buffer);
	*tail = 0;
	sprintf(buf, "\
 if {[info tclversion] < 8.4} {\n\
  trace variable ::toxy::reply w \"::toxy::doreply %s\"\n\
 } else {\n\
  trace add variable ::toxy::reply write \"::toxy::doreply %s\"\n\
 }\n\
 ::toxy::query}\n", sp->s_rptarget->s_name, sp->s_rptarget->s_name);
	sys_gui(buf);
    }
}

/* Non-substituting -- LATER think if this is likely to cause any confusion.
   Especially, consider the widget_vis() vs. widget_update() case. */
void scriptlet_vpush(t_scriptlet *sp, char *varname)
{
    if (scriptlet_ready(sp))
    {
	char *tail = sp->s_tail;
	strcpy(tail, "}\n");
	sys_vgui("set ::toxy::%s { ", varname);
	sys_gui(sp->s_head);
	*tail = 0;
    }
}

int scriptlet_evaluate(t_scriptlet *insp, t_scriptlet *outsp, int visedonly,
		       int ac, t_atom *av, t_props *argprops)
{
    if (scriptlet_ready(insp))
    {
	t_atom *ap;
	int i;
	char *bp;
	char separator = 0;
	insp->s_ac = ac;
	for (i = 0, ap = insp->s_av; i < SCRIPTLET_MAXARGS; i++, ap++)
	{
	    if (ac)
	    {
		if (av->a_type == A_FLOAT ||
		    (av->a_type == A_SYMBOL && av->a_w.w_symbol))
		    *ap = *av;
		else
		    SETFLOAT(ap, 0);
		ac--; av++;
	    }
	    else SETFLOAT(ap, 0);
	}
	/* FIXME pregrowing of the transient scriptlet */
	scriptlet_reset(outsp);
	/* LATER abstract this into scriptlet_parse() */
	bp = insp->s_head;
	while (*bp)
	{
	    if (*bp == '\n')
		separator = '\n';
	    else if (*bp == ' ' || *bp == '\t')
	    {
		if (!separator) separator = ' ';
	    }
	    else
	    {
		int done = 1;
		char *ep = bp;
		char c = ' ';
		while (*++ep)
		{
		    if (*ep == ' ' || *bp == '\t' || *ep == '\n')
		    {
			done = 0;
			c = *ep;
			*ep = 0;
			break;
		    }
		}
		outsp->s_separator = separator;
		scriptlet_addstring(outsp, bp, 1, visedonly,
				    ac, insp->s_av, argprops);
		if (done)
		    break;
		*ep = c;
		bp = ep;
		separator = (c == '\t' ? ' ' : c);
	    }
	    bp++;
	}
	return (outsp->s_cvstate != SCRIPTLET_CVMISSING);
    }
    else return (0);
}

/* utility function to be used in a comment-parsing callback */
char *scriptlet_nextword(char *buf)
{
    while (*++buf)
    {
	if (*buf == ' ' || *buf == '\t')
	{
	    char *ptr = buf + 1;
	    while (*ptr == ' ' || *ptr == '\t') ptr++;
	    *buf = 0;
	    return (*ptr ? ptr : 0);
	}
    }
    return (0);
}

static int scriptlet_doread(t_scriptlet *sp, t_pd *caller, FILE *fp,
			    char *rc, char *builtin, t_scriptlet_cmntfn cmntfn)
{
    t_scriptlet *outsp = sp, *newsp;
    char buf[MAXPDSTRING];
    if (!caller) caller = sp->s_owner;
    while ((fp && !feof(fp) && fgets(buf, MAXPDSTRING - 1, fp))
	   || builtin)
    {
	char *ptr;
	if (builtin)
	{
	    int i;
	    for (i = 0, ptr = buf; i < MAXPDSTRING - 1; i++, ptr++)
	    {
		if ((*ptr = (*builtin ? *builtin : '\n')) == '\n')
		{
		    ptr[1] = 0;
		    if (*builtin) builtin++;
		    if (!*builtin) builtin = 0;
		    break;
		}
		else builtin++;
	    }
	}
	ptr = buf;
	while (*ptr == ' ' || *ptr == '\t') ptr++;
	if (*ptr == '#')
	{
	    if (cmntfn)
	    {
		char sel = *++ptr;
		if (sel && sel != '\n')
		{
		    ptr++;
		    while (*ptr == ' ' || *ptr == '\t') ptr++;
		    if (*ptr == '\n')
			*ptr = 0;
		    if (*ptr)
		    {
			char *ep = ptr + strlen(ptr) - 1;
			while (*ep == ' ' || *ep == '\t' || *ep == '\n')
			    ep--;
			ep[1] = 0;
		    }
		    newsp = cmntfn(caller, rc, sel, ptr);
		    if (newsp == SCRIPTLET_UNLOCK)
			outsp->s_locked = 0;
		    else if (newsp == SCRIPTLET_LOCK)
			outsp->s_locked = 1;
		    else if (newsp != outsp)
		    {
			outsp->s_locked = 0;
			scriptlet_reset(outsp = newsp);
		    }
		}
	    }
	}
	else if (*ptr && *ptr != '\n')
	    scriptlet_doappend(outsp, buf);
    }
    outsp->s_locked = 0;
    return (SCRIPTLET_OK);
}

/* Load particular section(s) from buffer (skip up to an unlocking comment,
   keep appending up to a locking comment, repeat). */
int scriptlet_rcparse(t_scriptlet *sp, t_pd *caller, char *rc, char *contents,
		      t_scriptlet_cmntfn cmntfn)
{
    int result;
    sp->s_locked = 1;  /* see scriptlet_doread() above for unlocking scheme */
    result = scriptlet_doread(sp, caller, 0, rc, contents, cmntfn);
    return (result);
}

int scriptlet_rcload(t_scriptlet *sp, t_pd *caller, char *rc, char *ext,
		     char *builtin, t_scriptlet_cmntfn cmntfn)
{
    int result;
    char filename[MAXPDSTRING], buf[MAXPDSTRING], *nameptr, *dir;
    int fd;
    if (sp->s_glist)
	dir = canvas_getdir(sp->s_glist)->s_name;
    else
	dir = "";  /* which means pwd, usually the same as at Pd startup... */
    if ((fd = open_via_path(dir, rc, ext, buf, &nameptr, MAXPDSTRING, 0)) < 0)
    {
	result = SCRIPTLET_NOFILE;
    }
    else
    {
	FILE *fp;
    	close(fd);
	if (nameptr != buf)
	{
	    strcpy(filename, buf);
	    strcat(filename, "/");
	    strcat(filename, nameptr);
	    sys_bashfilename(filename, filename);
	}
	else sys_bashfilename(nameptr, filename);
	if (fp = fopen(filename, "r"))
	{
	    result = scriptlet_doread(sp, caller, fp, rc, 0, cmntfn);
	    fclose(fp);
	}
	else
	{
	    bug("scriptlet_rcload");
	    result = SCRIPTLET_NOFILE;
	}
    }
    if (result != SCRIPTLET_OK)
    {
	scriptlet_doread(sp, caller, 0, rc, builtin, cmntfn);
    }
    return (result);
}

int scriptlet_read(t_scriptlet *sp, t_symbol *fn)
{
    int result;
    FILE *fp;
    char buf[MAXPDSTRING];
    post("loading scriptlet file \"%s\"", fn->s_name);
    if (sp->s_glist)
	canvas_makefilename(sp->s_glist, fn->s_name, buf, MAXPDSTRING);
    else
	strncpy(buf, fn->s_name, MAXPDSTRING);
    sys_bashfilename(buf, buf);
    if (fp = fopen(buf, "r"))
    {
	scriptlet_reset(sp);
	result = scriptlet_doread(sp, 0, fp, 0, 0, 0);
	fclose(fp);
    }
    else
    {
	loud_error(sp->s_owner, "error while loading file \"%s\"", fn->s_name);
    	result = SCRIPTLET_NOFILE;
    }
    return (result);
}

int scriptlet_write(t_scriptlet *sp, t_symbol *fn)
{
    int size = sp->s_tail - sp->s_head;
    if (size > 0 && *sp->s_head)
    {
	FILE *fp;
	char buf[MAXPDSTRING];
	post("saving scriptlet file \"%s\"", fn->s_name);
	if (sp->s_glist)
	    canvas_makefilename(sp->s_glist, fn->s_name, buf, MAXPDSTRING);
	else
	    strncpy(buf, fn->s_name, MAXPDSTRING);
	sys_bashfilename(buf, buf);
	if (fp = fopen(buf, "w"))
	{
	    int result = fwrite(sp->s_head, 1, size, fp);
	    fclose(fp);
	    if (result == size)
		return (SCRIPTLET_OK);
	}
	loud_error(sp->s_owner, "error while saving file \"%s\"", fn->s_name);
	return (fp ? SCRIPTLET_BADFILE : SCRIPTLET_NOFILE);
    }
    else
    {
	loud_warning(sp->s_owner, "scriptlet", "empty scriptlet not written");
	return (SCRIPTLET_IGNORED);
    }
}

char *scriptlet_getcontents(t_scriptlet *sp, int *lenp)
{
    *lenp = sp->s_tail - sp->s_head;
    return (sp->s_head);
}

char *scriptlet_getbuffer(t_scriptlet *sp, int *sizep)
{
    *sizep = sp->s_size;
    return (sp->s_buffer);
}

void scriptlet_setowner(t_scriptlet *sp, t_pd *owner)
{
    sp->s_owner = owner;
}

void scriptlet_clone(t_scriptlet *to, t_scriptlet *from)
{
    scriptlet_reset(to);
    to->s_separator = ' ';
    /* LATER add a flag to optionally use from's buffer with refcount */
    scriptlet_doappend(to, from->s_head);
}

void scriptlet_append(t_scriptlet *to, t_scriptlet *from)
{
    to->s_separator = ' ';
    scriptlet_doappend(to, from->s_head);
}

void scriptlet_free(t_scriptlet *sp)
{
    if (sp)
    {
	if (sp->s_buffer != sp->s_bufini)
	    freebytes(sp->s_buffer, sp->s_size * sizeof(*sp->s_buffer));
	freebytes(sp, sizeof(*sp));
    }
}

/* The parameter 'gl' (null accepted) is necessary, because the 's_glist'
   field, if implicitly set, would be dangerous (after a glist is gone)
   and confusing (current directory used for i/o of a global scriptlet). */
t_scriptlet *scriptlet_new(t_pd *owner, t_symbol *rptarget, t_symbol *cbtarget,
			   t_symbol *item, t_glist *gl, t_scriptlet_cvfn cvfn)
{
    t_scriptlet *sp = getbytes(sizeof(*sp));
    if (sp)
    {
	static int configured = 0;
	if (!configured)
	{
	    sys_gui("image create bitmap ::toxy::img::empty -data {}\n");
	    sys_gui("proc ::toxy::doreply {target vname vndx op} {\n");
	    sys_gui(" pd [concat $target _rp $::toxy::reply \\;]\n");
	    sys_gui(" unset ::toxy::reply\n");
	    sys_gui("}\n");
	}
	sp->s_owner = owner;
	sp->s_glist = gl;
	sp->s_rptarget = rptarget;
	sp->s_cbtarget = cbtarget;
	sp->s_item = item;
	sp->s_cvfn = cvfn;
	sp->s_size = SCRIPTLET_INISIZE;
	sp->s_buffer = sp->s_bufini;
	scriptlet_reset(sp);
    }
    return (sp);
}