File: tclmainlib.c

package info (click to toggle)
funtools 1.4.4+dfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,168 kB
  • ctags: 10,760
  • sloc: ansic: 87,238; sh: 9,727; lex: 4,595; asm: 3,281; ada: 1,681; makefile: 1,458; pascal: 1,089; cpp: 1,001; cs: 879; perl: 161; yacc: 64; sed: 32; csh: 10; tcl: 9
file content (504 lines) | stat: -rw-r--r-- 11,372 bytes parent folder | download | duplicates (2)
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
/*
 *	Copyright (c) 2004 Smithsonian Astrophysical Observatory
 */

#include <tclmainlib.h>

#if HAVE_TCL
/*
 *----------------------------------------------------------------------------
 *
 *
 * 			Private Routines and Data
 *
 *
 *----------------------------------------------------------------------------
 */

/*
 *----------------------------------------------------------------------------
 *
 *
 * 			Semi-Public Routines and Data
 *
 *
 *----------------------------------------------------------------------------
 */

#ifdef ANSI_FUNC
int
MainLibInit_Tcl (MainLib ml)
#else
int MainLibInit_Tcl (ml)
     MainLib ml;
#endif
{
  /* sanity check */
  if( !ml ) return 0;

  /* populate struct with Tcl routines */
  ml->tcllookup = MainLibLookup_Tcl;
  ml->tcleval = MainLibEval_Tcl;

  return 1;
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	MainLibLookup_Tcl
 *
 * Purpose:	lookup a Tcl command
 *
 * Tcl call:
 *
 *     NONE
 *
 * Returns:	1 if Tcl command, otherwise 0
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int
MainLibLookup_Tcl (void *vinterp, char *s)
#else
int MainLibLookup_Tcl (vinterp, s)
     void *vinterp;
     char *s;
#endif
{
  Tcl_Interp *interp = (Tcl_Interp *)vinterp;
  Tcl_CmdInfo info;
  char lbuf[SZ_LINE];
  char tbuf[SZ_LINE];
  int ip=0;
  int got=0;

  /* create interpreter, if necessary */
  if( !vinterp ){
    interp = Tcl_CreateInterp();
  }

  /* get first token  */
  strncpy(lbuf, s, SZ_LINE-1);
  lbuf[SZ_LINE-1] = '\0';
  if( !word(lbuf, tbuf, &ip) )
    return 0;
	  
  /* lookup Tcl command */
  if( Tcl_GetCommandInfo(interp, tbuf, &info) )
    got = 1;
  else
    got = 0;

  /* delete interpreter, if necessary */
  if( !vinterp ){
    Tcl_DeleteInterp(interp);
  }

  /* return the news */
  return got;
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	MainLibEval_Tcl
 *
 * Purpose:	lookup a Tcl command
 *
 * Tcl call:
 *
 *     NONE
 *
 * Returns:	1 if Tcl command, otherwise 0
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int
MainLibEval_Tcl (void *vinterp, char *s)
#else
int MainLibEval_Tcl (vinterp, s)
     void *vinterp;
     char *s;
#endif
{
  Tcl_Interp *interp = (Tcl_Interp *)vinterp;
  int got=0;

  /* create interpreter, if necessary */
  if( !vinterp ){
    interp = Tcl_CreateInterp();
  }

  /* lookup Tcl command */
  got = Tcl_EvalEx(interp, s, strlen(s), TCL_EVAL_GLOBAL);

  /* delete interpreter, if necessary */
  if( !vinterp ){
    Tcl_DeleteInterp(interp);
  }

  /* return the news */
  return got;
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	MainLibProcess_Tcl
 *
 * Purpose:	execute the mainlibprocess command
 *
 * Tcl call:
 *
 *     set result [mainlib $ml "command"]
 *
 * Returns:	Tcl error code
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int
MainLibProcess_Tcl(ClientData clientData, Tcl_Interp *interp,
	    int objc, Tcl_Obj *CONST objv[])
#else
int MainLibProcess_Tcl(clientData, interp, objc, objv)
     ClientData clientData;
     Tcl_Interp *interp;
     int objc;
     Tcl_Obj *CONST objv[];
#endif
{
  int len;
  int got;
  int fillbuf=1;
  void *tml;
  char *s=NULL;
  char *t=NULL;
  char *mode=NULL;
  char *name=NULL;
  char *cmd=NULL;
  char *args=NULL;
  char *buf=NULL;
  char tbuf[SZ_LINE];
  Tcl_Obj *resultPtr;
  MainLib ml;

  /* avoid -W unused parameter warning */
  if( 0 ){
    clientData = clientData;
  }

  /* make sure argument count is correct */
  if( (objc != 3) && (objc != 4) ){
    name = Tcl_GetStringFromObj(objv[0], NULL);
    if( !strcmp(name, TCL_MAINLIB_NAME) ){
      Tcl_WrongNumArgs(interp, 1, objv, "ml 'cmd args' [mode]");
    }
    else{
      snprintf(tbuf, SZ_LINE-1, "ml 'args' [mode]");
      Tcl_WrongNumArgs(interp, 1, objv, tbuf);
    }
    return(TCL_ERROR);
  }

  /* this will hold the result */
  resultPtr = Tcl_GetObjResult(interp);

  /* get name */
  name = Tcl_GetStringFromObj(objv[0], NULL);

  /* get mainlib handle */
  s = Tcl_GetStringFromObj(objv[1], NULL);
  if( (sscanf(s, "%p", &tml) != 1) ){
    Tcl_SetStringObj(resultPtr, "MAINLIB$ERROR: invalid mainlib handle", -1);
    return(TCL_ERROR);
  }
  ml = (MainLib)tml;

  /* make sure Tcl routines are in mainlib record */
  MainLibInit_Tcl(ml);
  
  /* if name is the default, then the full command is in argv[1] */
  name = Tcl_GetStringFromObj(objv[0], NULL);
  if( !strcmp(name, TCL_MAINLIB_NAME) ){
    cmd = xstrdup(Tcl_GetStringFromObj(objv[2], NULL));
  }
  /* otherwise, the command name is in argv[0] and arguments are in argv[1] */
  else{
    args = xstrdup(Tcl_GetStringFromObj(objv[2], NULL));
    len = strlen(name) + strlen(args) + 2;
    cmd = malloc(len);
    snprintf(cmd, len, "%s %s", name, args);
  }

  /* check for mode */
  if( objc == 4 ){
    mode = xstrdup(Tcl_GetStringFromObj(objv[3], NULL));
  }

  /* reset error/result condition */
  Tcl_ResetResult(interp);

  /* add tcl=[interp] to mode */
  snprintf(tbuf, SZ_LINE-1, "tcl=%p", (void *)interp);
  if( mode ){
    len = strlen(mode) + strlen(tbuf) + 2;
    mode = xrealloc(mode, len);
    snprintf(mode, len, "%s,%s", mode, tbuf);
  }
  else{
    mode = xstrdup(tbuf);
  }

  /* fillbuf determines meaning of MainLibProcess return value */
  if( (t = xstrdup(mode)) ){
    if( keyword(t, "fillbuf",  tbuf, SZ_LINE) ) fillbuf = istrue(tbuf);
    xfree(t);
  }

  /* call the mainlib process routine */
  got = MainLibProcess(ml, cmd, &buf, mode);

  /* free up space */
  if( args ) xfree(args);
  if( cmd )  xfree(cmd);
  if( mode ) xfree(mode);

  /* return buf as the result */
  if( got >= 0 ){
    if( fillbuf ){
      if( buf ){
	Tcl_SetStringObj(resultPtr, buf, got);
	xfree(buf);
      }
      return(TCL_OK);
    }
#if HAVE_CYGWIN==0
    else{
      Tcl_Channel chan;
      /* create a tcl channel corresponding to the pipe */
      if( !(chan = Tcl_MakeFileChannel((ClientData)(intptr_t)got, TCL_READABLE)) ){
	Tcl_SetResult(interp, "can't create Tcl chan for pipe", TCL_STATIC);
	return TCL_ERROR;
      }
      else{
	/* register this channel with tcl */
	Tcl_RegisterChannel(interp, chan);
	/* return name so that it can be used by tcl */
	Tcl_SetResult(interp,(char *)Tcl_GetChannelName(chan), TCL_VOLATILE);
	return(TCL_OK);
      }
    }
#else
    else{
      /* this probably is useless */
      snprintf(tbuf, SZ_LINE-1, "%d", got);
      Tcl_SetStringObj(resultPtr, tbuf, -1);
      return(TCL_OK);
    }
#endif
  }
  else{
    Tcl_SetStringObj(resultPtr, "unable to execute mainlib command", -1);
    return(TCL_ERROR);
  }
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	MainLibProcessCleanup_Tcl
 *
 * Purpose:	execute the mainlibprocesscleanup command
 *
 * Tcl call:
 *
 *     set result [mainlib ml]
 *
 * Returns:	Tcl error code
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int
MainLibProcessCleanup_Tcl(ClientData clientData, Tcl_Interp *interp,
	    int objc, Tcl_Obj *CONST objv[])
#else
int MainLibProcessCleanup_Tcl(clientData, interp, objc, objv)
     ClientData clientData;
     Tcl_Interp *interp;
     int objc;
     Tcl_Obj *CONST objv[];
#endif
{
  int got;
  char *s=NULL;
  void *tml;
  Tcl_Obj *resultPtr;
  MainLib ml;

  /* avoid -W unused parameter warning */
  if( 0 ){
    clientData = clientData;
  }

  /* make sure argument count is correct */
  if( objc != 2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "ml");
    return(TCL_ERROR);
  }

  /* this will hold the result */
  resultPtr = Tcl_GetObjResult(interp);

  /* get mainlib handle */
  s = Tcl_GetStringFromObj(objv[1], NULL);
  if( (sscanf(s, "%p", &tml) != 1) ){
    Tcl_SetStringObj(resultPtr, "MAINLIB$ERROR: invalid mainlib handle", -1);
    return(TCL_ERROR);
  }
  ml = (MainLib)tml;

  /* reset error/result condition */
  Tcl_ResetResult(interp);

  /* call the mainlib process routine */
  got = MainLibProcessCleanup(ml);

  /* return buf as the result */
  if( got == 1 ){
    return(TCL_OK);
  }
  else{
    Tcl_SetStringObj(resultPtr, "error on mainlibcleanup command", -1);
    return(TCL_ERROR);
  }

}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	MainLibLoad_Tcl
 *
 * Purpose:	load shared object and execute the mainlibinit
 *
 * Tcl call:
 *
 *     set result [mainlibload package sharedlib]
 *
 * Returns:	Tcl error code
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int
MainLibLoad_Tcl(ClientData clientData, Tcl_Interp *interp,
	    int objc, Tcl_Obj *CONST objv[])
#else
int MainLibLoad_Tcl(clientData, interp, objc, objv)
     ClientData clientData;
     Tcl_Interp *interp;
     int objc;
     Tcl_Obj *CONST objv[];
#endif
{
  char *package=NULL;
  char *shlib=NULL;
  char *ermsg;
  char tbuf[SZ_LINE];
  void *dl=NULL;
  void *ml=NULL;
  Tcl_Obj *resultPtr;

  /* avoid -W unused parameter warning */
  if( 0 ){
    clientData = clientData;
  }

  /* make sure argument count is correct */
  if( objc != 3 ){
    Tcl_WrongNumArgs(interp, 1, objv,  "package sharedlib"); 
    return(TCL_ERROR);
  }

  /* get arguments */
  package = Tcl_GetStringFromObj(objv[1], NULL);
  shlib = Tcl_GetStringFromObj(objv[2], NULL);

  /* this will hold the result */
  resultPtr = Tcl_GetObjResult(interp);

  /* load package and process result */
  switch(MainLibLoad(package, shlib, &ml, &ermsg)){
  case -1:
    snprintf(tbuf, SZ_LINE-1,
	     "MAINLIB$ERROR: could not load shared library %s (%s)",
	     shlib, ermsg);
    Tcl_SetStringObj(resultPtr, tbuf, -1);
    return(TCL_ERROR);
  case -2:
    snprintf(tbuf, SZ_LINE-1,
	     "MAINLIB$ERROR: could not initialize package %s (%s)",
	     package, ermsg);
    Tcl_SetStringObj(resultPtr, tbuf, -1);
    return(TCL_ERROR);
  default:
    snprintf(tbuf, SZ_LINE-1, "%p %p", ml, dl);
    Tcl_SetStringObj(resultPtr, tbuf, -1);
    return(TCL_OK);
    break;
  }
}

/*
 *----------------------------------------------------------------------------
 *
 *
 * 			Public Routines and Data
 *
 *
 *----------------------------------------------------------------------------
 */

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	Tclmainlib_Init
 *
 * Purpose:	initialize Tcl mainlib package
 *
 * Returns:	tcl return code
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int
Tclmainlib_Init (void *vinterp)
#else
int Tclmainlib_Init (vinterp)
     void *vinterp;
#endif
{
  Tcl_Interp *interp = (Tcl_Interp *)vinterp;

  /* add mainlib commands to this interpreter */
  Tcl_CreateObjCommand(interp, TCL_MAINLIB_NAME,
		       MainLibProcess_Tcl, 
		       (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

  Tcl_CreateObjCommand(interp, "mainlibcleanup",
		       MainLibProcessCleanup_Tcl,
		       (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

  Tcl_CreateObjCommand(interp, "mainlibload",
		       MainLibLoad_Tcl,
		       (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

  Tcl_PkgProvide(interp, "tclmainlib", "1.0");
  return(TCL_OK);
}
#endif