File: siod.cc

package info (click to toggle)
speech-tools 1:2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (541 lines) | stat: -rw-r--r-- 11,349 bytes parent folder | download | duplicates (3)
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
/* Scheme In One Defun, but in C this time.
 
 *                    COPYRIGHT (c) 1988-1994 BY                            *
 *        PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
 *        See the source file SLIB.C for more information.                  *

*/

/*

gjc@paradigm.com or gjc@mitech.com or gjc@world.std.com

Paradigm Associates Inc          Phone: 617-492-6079
29 Putnam Ave, Suite 6
Cambridge, MA 02138

  */

/***************************************************************/
/* This has been modified to act as an interface to siod as an */
/* embedded Lisp module.                                       */
/* Also a (large) number of other functions have been added    */
/*                                                             */
/*    Alan W Black (awb@cstr.ed.ac.uk) 8th April 1996          */
/***************************************************************/
#include <cstdio>
#include "EST_unix.h"
#include <cstdlib>
#include <cstring>
#include "EST_String.h"
#include "EST_THash.h"
#include "EST_StringTrie.h"
#include "EST_cutils.h"
#include "EST_strcasecmp.h"
#include "siod.h"
#include "siodp.h"
#include "siodeditline.h"

#ifdef EST_SIOD_ENABLE_PYTHON
#include "slib_python.h"
#endif

extern "C" const char * repl_prompt;

template <> EST_String EST_THash<EST_String, EST_Regex *>::Dummy_Key = "DUMMY";
template <> EST_Regex *EST_THash<EST_String, EST_Regex *>::Dummy_Value = NULL;

#if defined(INSTANTIATE_TEMPLATES)
#include "../base_class/EST_THash.cc"

  Instantiate_TStringHash_T(EST_Regex *, hash_string_regex)
#endif

static EST_TStringHash<EST_Regex *> regexes(100);

int siod_init(int heap_size)
{
    /* Initialize siod */
    int actual_heap_size;

    if (heap_size == -1)  // unspecified by user
    {
	char *char_heap_size=getenv("SIODHEAPSIZE");
	if ((char_heap_size == 0) ||
	    (atoi(char_heap_size) < 1000))
	    actual_heap_size=ACTUAL_DEFAULT_HEAP_SIZE;
	else
	    actual_heap_size=atoi(char_heap_size);
    }
    else
	actual_heap_size = heap_size;

    init_storage(actual_heap_size);
    init_subrs();

    #ifdef EST_SIOD_ENABLE_PYTHON
    init_subrs_python();
    #endif

    return 0;
}

void siod_tidy_up()
{
    #ifdef EST_SIOD_ENABLE_PYTHON
    python_tidy_up();
    #endif

    close_open_files();
}

LISP siod_get_lval(const char *name,const char *message)
{
    // returns value of variable name.  If not set gives an error 
    LISP iii, rval=NIL;

    iii = rintern(name);

    // value or NIL if unset
    if (symbol_boundp(iii,current_env) == NIL)
    {
	if (message != NULL)
	    err(message,iii);
    }
    else
	rval = symbol_value(iii, current_env);

    return rval;
}

LISP siod_set_lval(const char *name,LISP val)
{
    // set variable name to val
    LISP iii, rval;
    
    iii = rintern(name);

    rval = setvar(iii,val,current_env);

    return rval;
}

LISP siod_assoc_str(const char *key,LISP alist)
{
    // assoc without going through LISP atoms 
    // made get_c_string inline for optimization
    LISP l,lc,lcc;

    for (l=alist; CONSP(l); l=CDR(l))
    {
	lc = CAR(l);
	if (CONSP(lc))
	{
	    lcc = CAR(lc);
	    if (NULLP(lcc)) continue;
	    else if TYPEP(lcc,tc_symbol)
	    {
		if (strcmp(key,PNAME(lcc))==0)
		    return lc;
	    }
	    else if TYPEP(lcc,tc_flonum)
	    {
		if (FLONMPNAME(lcc) == NULL)
		{
		    char b[TKBUFFERN];
		    sprintf(b,"%g",FLONM(lcc));
		    FLONMPNAME(lcc) = (char *)must_malloc(strlen(b)+1);
		    sprintf(FLONMPNAME(lcc),"%s",b);
		}
		if (strcmp(key,FLONMPNAME(lcc))==0)
		    return lc;
	    }
	    else if TYPEP(lcc,tc_string)
	    {
		if (strcmp(key,lcc->storage_as.string.data)==0)
		    return lc;
	    }
	    else
		continue;
	}
    }
    return NIL;
}

LISP siod_member_str(const char *key,LISP list)
{
    // member without going through LISP atoms 
    LISP l;

    for (l=list; CONSP(l); l=CDR(l))
	if (strcmp(key,get_c_string(CAR(l))) == 0)
	    return l;

    return NIL;
}

LISP siod_regex_member_str(const EST_String &key,LISP list)
{
    // Check the regexs in LIST against key
    LISP l;

    for (l=list; CONSP(l); l=CDR(l))
	if (key.matches(make_regex(get_c_string(CAR(l)))))
	    return l;

    return NIL;
}

LISP siod_member_int(const int key,LISP list)
{
    // member without going through LISP atoms 
    LISP l;

    for (l=list; CONSP(l); l=CDR(l))
	if (key == get_c_int(CAR(l)))
	    return l;
    return NIL;
}
	 
int siod_llength(LISP list)
{
    // length of string;
    int len;
    LISP l;

    for (len=0,l=list; CONSP(l); l=CDR(l),len++);
	
    return len;

}

LISP siod_nth(int n,LISP list)
{
    // nth member -- first member is 0;
    int i;
    LISP l;

    for (i=0,l=list; CONSP(l); l=CDR(l),i++)
	if (i == n)
	    return car(l);
	
    return NIL;

}

int siod_atomic_list(LISP list)
{
    // TRUE is list only contains atoms
    LISP p;

    for (p=list; p != NIL; p=cdr(p))
	if (CONSP(car(p)))
	    return FALSE;

    return TRUE;
}

int siod_eof(LISP item)
{
    // TRUE if item is what siod denotes as eof
    if (CONSP(item) &&
	(cdr(item) == NIL) &&
	(SYMBOLP(car(item))) &&
	(strcmp("eof",get_c_string(car(item))) == 0))
	return TRUE;
    else
	return FALSE;
}

LISP quote(LISP l)
{
    // Add quote round a Lisp expression
    return cons(rintern("quote"),cons(l,NIL));
}

LISP siod_last(LISP list)
{
    LISP l;

    if ((list == NIL) || (NCONSP(list)))
	return NIL;
    else
    {
	for (l=list; cdr(l) != NIL; l=cdr(l));
	return l;
    }
}

int get_param_int(const char *name, LISP params, int defval)
{
    // Look up name in params and return value if present or 
    // defval if not present 
    LISP pair;

    pair = siod_assoc_str(name,params);

    if (pair == NIL)
	return defval;
    else  if FLONUMP(car(cdr(pair)))
	return (int)FLONM(car(cdr(pair)));
    else
    {
	cerr << "param " << name << " not of type int" << endl;
	err("",NIL);
	return -1;
    }

}

float get_param_float(const char *name, LISP params, float defval)
{
    // Look up name in params and return value if present or 
    // defval if not present 
    LISP pair;

    pair = siod_assoc_str(name,params);

    if (pair == NIL)
	return defval;
    else  if (FLONUMP(car(cdr(pair))))
	return (float)FLONM(car(cdr(pair)));
    else
    {
	cerr << "param " << name << " not of type float" << endl;
	err("",NIL);
	return -1;
    }

}

const char *get_param_str(const char *name, LISP params, const char *defval)
{
    // Look up name in params and return value if present or 
    // defval if not present
    LISP pair;

    pair = siod_assoc_str(name,params);

    if (pair == NIL)
	return defval;
    else
	return get_c_string(car(cdr(pair)));
}

LISP get_param_lisp(const char *name, LISP params, LISP defval)
{
    // Look up name in params and return value if present or 
    // defval if not present 
    LISP pair;

    pair = siod_assoc_str(name,params);

    if (pair == NIL)
	return defval;
    else
	return car(cdr(pair));
}

LISP make_param_str(const char *name,const char *val)
{
    return cons(rintern(name),cons(rintern(val),NIL));
}

LISP make_param_int(const char *name, int val)
{
    return cons(rintern(name),cons(flocons(val),NIL));
}

LISP make_param_float(const char *name, float val)
{
    return cons(rintern(name),cons(flocons(val),NIL));
}

LISP make_param_lisp(const char *name,LISP val)
{
    return cons(rintern(name),cons(val,NIL));
}

EST_Regex &make_regex(const char *r)
{
    // Return pointer to existing regex if its already been created
    // otherwise create a new one for this r.
    EST_Regex *rx;
    EST_String sr = r;
    int found;

    rx = regexes.val(sr,found);
    if (!found)
    {
	rx = new EST_Regex(r);
	regexes.add_item(sr,rx);
    }

    return *rx;
}

LISP apply_hooks(LISP hooks,LISP arg)
{
    //  Apply each function in hooks to arg returning value from 
    // final application (or arg itself)
    LISP h,r;

    r = arg;
    
    if (hooks && (!CONSP(hooks)))  // singleton
	r = leval(cons(hooks,cons(quote(arg),NIL)),NIL);
    else
	for (h=hooks; h != NIL; h=cdr(h))
	    r = leval(cons(car(h),cons(quote(arg),NIL)),NIL);
    return r;
}

LISP apply_hooks_right(LISP hooks,LISP args)
{
    // The above version neither quotes its arguments properly of deals
    // with lists of arguments so here's a better one
    //  Apply each function in hooks to arg returning value from 
    // final application (or arg itself)
    LISP h,r;

    if (hooks == NIL)
	r = args;
    else if (!CONSP(hooks))  // singleton
	r = apply(hooks,args);
    else
	for (r=args,h=hooks; h != NIL; h=cdr(h))
	    r = apply(car(h),r);
    return r;
}

LISP apply(LISP func,LISP args)
{
    LISP qa,a;

    for (qa=NIL,a=args; a; a=cdr(a))
	qa = cons(quote(car(a)),qa);
    return leval(cons(func,reverse(qa)),NIL);
}

LISP stringexplode(const char *str)
{
    // Explode character string into list of symbols one for each char
    LISP l=NIL;
    unsigned int i;
    char id[2];
    id[1] = '\0';

    for (i=0; i < strlen(str); i++)
    {
	id[0] = str[i];
	l = cons(rintern(id),l);
    }

    return reverse(l);
}

/* Editline completion functions */
    
char **siod_variable_generator(char *text,int length)
{
    LISP l,lmatches;
    const char *name;
    char **matches = NULL;
    int i;

    /* Return the next name which partially matches from the command list. */
    for(lmatches=NIL,l=oblistvar;CONSP(l);l=CDR(l))
    {
	if (VCELL(car(l)) == NIL) continue;
	switch(TYPE(VCELL(CAR(l))))
	{
	  case tc_subr_0:
	  case tc_subr_1:
	  case tc_subr_2:
	  case tc_subr_3:
	  case tc_subr_4:
	  case tc_lsubr:
	  case tc_fsubr:
	  case tc_msubr:
	  case tc_closure:
	    continue;
	  default:
            /* only return names of nonfunctions (sometimes too restrictive) */
	    name = PNAME(CAR(l));
	    if (strncmp(name, text, length) == 0)
		lmatches = cons(CAR(l),lmatches);
	}
    }

    /* Need to return the matches in a char** */
    matches = walloc(char *,siod_llength(lmatches)+1);
    for (l=lmatches,i=0; l; l=cdr(l),i++)
	matches[i] = wstrdup(PNAME(car(l)));
    matches[i] = NULL;

    return matches;
}

char **siod_command_generator (char *text,int length)
{
    LISP l,lmatches;
    const char *name;
    char **matches = NULL;
    int i;

    /* Return the next name which partially matches from the command list. */
    for(lmatches=NIL,l=oblistvar;CONSP(l);l=CDR(l))
    {
	if (VCELL(car(l)) == NIL) continue;
	switch(TYPE(VCELL(CAR(l))))
	{
	  case tc_subr_0:
	  case tc_subr_1:
	  case tc_subr_2:
	  case tc_subr_3:
	  case tc_subr_4:
	  case tc_lsubr:
	  case tc_fsubr:
	  case tc_msubr:
	  case tc_closure:
            /* only return names of functions */
	    name = PNAME(CAR(l));
	    if (strncmp(name, text, length) == 0)
		lmatches = cons(CAR(l),lmatches);
	  default: continue;
	}
    }

    /* Need to return the matches in a char** */
    matches = walloc(char *,siod_llength(lmatches)+1);
    for (l=lmatches,i=0; l; l=cdr(l),i++)
	matches[i] = wstrdup(PNAME(car(l)));
    matches[i] = NULL;

    return matches;
}

void siod_list_to_strlist(LISP l, EST_StrList &a)
{
    // copy l into a
    LISP b;

    a.clear();

    for (b=l; b != NIL; b=cdr(b))
	a.append(get_c_string(car(b)));

}

LISP siod_strlist_to_list(EST_StrList &a)
{
    // copy a into l
    LISP b=NIL;;
    EST_Litem *p;

    for (p=a.head(); p != 0; p=p->next())
	b = cons(rintern(a(p)),b);

    return reverse(b);
}