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
|
/*
* Copyright (c) 2004 Smithsonian Astrophysical Observatory
*/
/*
* mainlib.c -- support for main routines called as library subroutines
*
*/
#include <mainlib.h>
extern char *optarg;
extern int optind;
/*
*
* private routines
*
*/
/*
*----------------------------------------------------------------------------
*
* Routine: Pipe2Buf
*
* Purpose: read pipe til EOF and fill buffer with results
*
* Results: number of bytes read (and buffer in passed argument)
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
Pipe2Buf(int fd, char **buf)
#else
static int Pipe2Buf(fd, buf)
int fd;
char **buf;
#endif
{
int got;
int clen=0;
int blen=0;
/* initial buffer size */
blen = SZ_LINE*10;
*buf = xmalloc(blen);
/* read til EOF and fill buffer */
while( 1 ){
/* reallocate as needed */
if( (clen + SZ_LINE) >= blen ){
blen += SZ_LINE*10;
*buf = xrealloc(*buf, blen);
}
/* read */
got=read(fd, *buf+clen, SZ_LINE);
/* got data */
if( got > 0 ){
clen += got;
continue;
}
/* EOF */
else if( got == 0 ){
/* reallocate to actual size, but null terminate as a courtesy */
blen = clen+1;
*buf = xrealloc(*buf, blen);
*(*buf+clen) = '\0';
break;
}
/* error */
else{
xfree(*buf);
blen = -1;
break;
}
}
/* return the news */
return blen;
}
/*
*---------------------------------------------------------------------------
*
* Routine: MainLibListAdd
*
* Purpose: add a member of an mainlib list
*
* Results: 1 on success, 0 for failure
*
*---------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
MainLibListAdd (MainLib ml, MainLibEntry mle)
#else
static void MainLibListAdd(ml, mle)
MainLib ml;
MainLibEntry mle;
#endif
{
MainLibEntry cur;
if( ml->head == NULL ){
ml->head = mle;
}
else{
for(cur=ml->head; cur->next!=NULL; cur=cur->next)
;
cur->next = mle;
}
}
/*
*---------------------------------------------------------------------------
*
* Routine: MainLibListDel
*
* Purpose: remove a member of an mainlib list
*
* Results: 1 on success, 0 for failure
*
*---------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
MainLibListDel (MainLib ml, MainLibEntry mle)
#else
static void MainLibListDel(ml, mle)
MainLib ml;
MainLibEntry mle;
#endif
{
MainLibEntry cur;
/* remove from list of mainlibs */
if( ml->head ){
if( ml->head == mle ){
ml->head = mle->next;
}
else{
for(cur=ml->head; cur!=NULL; cur=cur->next){
if( cur->next == mle ){
cur->next = mle->next;
break;
}
}
}
}
}
/*
*
* semi-public routines
*
*/
/*
*---------------------------------------------------------------------------
*
* Routine: MainLibLookup
*
* Purpose: lookup a mainlib procedure
*
* Results: MainLibEntry record on success, NULL on failure
*
*---------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
MainLibEntry
MainLibLookup (MainLib ml, char *xclass, char *name)
#else
MainLibEntry MainLibLookup(ml, xclass, name)
MainLib ml;
char *xclass;
char *name;
#endif
{
MainLibEntry cur;
if( !ml || (!xclass && !name) ) return NULL;
for(cur=ml->head; cur!=NULL; cur=cur->next){
if( (!xclass || !strcmp(xclass, cur->xclass)) &&
(!name || !strcmp(name, cur->name)) ){
return cur;
}
}
return NULL;
}
/*
*
* public routines
*
*/
/*
*----------------------------------------------------------------------------
*
* Routine: MainLibNew
*
* Purpose: create mainlib struct
*
* Returns: mainlib list handle or NULL
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
MainLib
MainLibNew (void)
#else
MainLib MainLibNew()
#endif
{
MainLib ml;
/* allocate struct */
if( !(ml = (MainLib)xcalloc(1, sizeof(MainLibRec))) )
return NULL;
return ml;
}
/*
*----------------------------------------------------------------------------
*
* Routine: MainLibAdd
*
* Purpose: add a mainlib name to allow external processes to be called
* as subroutines
*
* Returns: mainlib handle associated with this name or NULL
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
MainLibEntry
MainLibAdd (MainLib ml, char *xclass, char *name, MainLibProc proc, int type)
#else
MainLibEntry MainLibAdd(ml, xclass, name, proc, type)
MainLib ml;
char *xclass;
char *name;
MainLibProc proc;
int type;
#endif
{
MainLibEntry mle;
/* sanity check */
if( !ml ) return NULL;
/* allocate struct */
if( !(mle = (MainLibEntry)xcalloc(1, sizeof(MainLibEntryRec))) ) return NULL;
/* fill in the blanks */
mle->xclass = xstrdup(xclass);
mle->name = xstrdup(name);
mle->proc = proc;
mle->type = type;
/* add to list */
MainLibListAdd(ml, mle);
return mle;
}
/*
*----------------------------------------------------------------------------
*
* Routine: MainLibProcess
*
* Purpose: call external processes as subroutines
*
* Results: number of bytes in return buffer (buffer in passed argument)
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int MainLibProcess(MainLib ml, char *cmd, char **buf, char *mode)
#else
int MainLibProcess(ml, cmd, buf, mode)
MainLib ml;
char *cmd;
char **buf;
char *mode;
#endif
{
int i, j;
int got;
int ncmd=0;
int ip=0;
int ifd=-1;
int ofd=-1;
int efd=-1;
int fillbuf=1;
int debug=0;
int use_ifd=0;
int use_ofd=0;
int use_efd=0;
#if HAVE_TCL
int use_tcl=0;
#endif
int nargs[MAINLIB_CMDS+1];
int pipes[MAINLIB_CMDS+1][2];
char *c, *s, *t;
char *undef=NULL;
char tbuf[SZ_LINE];
char ibuf[SZ_LINE];
char ibuf2[SZ_LINE];
char *args[(MAINLIB_CMDS+1)*MAINLIB_ARGS];
char *cmds[MAINLIB_CMDS+1];
#if HAVE_TCL
void *tcl=NULL;
#endif
MainLibEntry mles[MAINLIB_CMDS+1];
static int mainlibinit=0;
static int mainlib_debug=0;
static char *mainlib_path=NULL;
/* sanity check */
if( !ml ){
gerror(stderr, "invalid or missing command list for mainlib\n");
got = -1;
goto done;
}
/* initialization */
if( !mainlibinit ){
if( (s = (char *)getenv("MAINLIB_DEBUG")) )
mainlib_debug = istrue(s);
mainlib_path = (char *)getenv("PATH");
mainlibinit = 1;
}
/* clear arrays */
memset(cmds, 0, sizeof(char *)*(MAINLIB_CMDS+1));
memset(mles, 0, sizeof(MainLibEntry)*(MAINLIB_CMDS+1));
memset(args, 0, sizeof(char *)*(MAINLIB_CMDS+1)*MAINLIB_ARGS);
/* separate commands */
c = xstrdup(cmd);
for(s=c; (t=strchr(s, '|'))!=NULL; s=t+1){
if( ncmd >= MAINLIB_CMDS ){
gerror(stderr, "max cmds exceeded (%d) for mainlib\n", ncmd);
got = -1;
goto done;
}
*t = '\0';
cmds[++ncmd] = xstrdup(s);
}
/* get last command */
cmds[++ncmd] = xstrdup(s);
if( c ) xfree(c);
/* seed the debug flag (override with switch) */
debug = mainlib_debug;
/* check mode string */
if( (t = xstrdup(mode)) ){
if( keyword(t, "stdin", tbuf, SZ_LINE) ) ifd = atoi(tbuf);
if( keyword(t, "stdout", tbuf, SZ_LINE) ) ofd = atoi(tbuf);
if( keyword(t, "stderr", tbuf, SZ_LINE) ) efd = atoi(tbuf);
if( keyword(t, "undef", tbuf, SZ_LINE) ) undef = xstrdup(tbuf);
if( keyword(t, "fillbuf", tbuf, SZ_LINE) ) fillbuf = istrue(tbuf);
if( keyword(t, "debug", tbuf, SZ_LINE) ) debug = istrue(tbuf);
#if HAVE_TCL
if( keyword(t, "tcl", tbuf, SZ_LINE) ){
sscanf(tbuf, "%p", &tcl);
use_tcl=1;
}
#endif
xfree(t);
}
/* make sure unef is defined */
if( !undef ){
undef = xstrdup("extn");
}
/* pipe for top level */
pipe(pipes[0]);
/* package up arguments for each command */
for(i=1; i<=ncmd; i++){
if( pipe(pipes[i]) < 0 ){
gerror(stderr, "can't set up pipes for mainlib\n");
got = -1;
goto done;
}
ip = 0;
/* make sure we have a command */
if( !word(cmds[i], ibuf, &ip) ){
gerror(stderr, "invalid or missing command for mainlib\n");
got = -1;
goto done;
}
/* make sure its a valid command */
nowhite(ibuf, ibuf2);
if( !(mles[i] = MainLibLookup(ml, NULL, ibuf2)) ){
if( undef ){
ip = 0;
while( word(undef, tbuf, &ip) ){
if( !strcasecmp(tbuf, "error") ){
break;
}
#if HAVE_TCL
else if( use_tcl && ml->tcllookup && ml->tcllookup(tcl, ibuf2) ){
mles[i] = MainLibAdd(ml, cmds[i], ibuf2, NULL, MAINLIB_TCL);
break;
}
#endif
else if( (s=Find(ibuf2, "x", NULL, mainlib_path)) != NULL ){
mles[i] = MainLibAdd(ml, s, ibuf2, NULL, MAINLIB_EXTN);
xfree(s);
break;
}
}
if( !mles[i] ){
gerror(stderr, "can't locate procedure '%s' for mainlib\n", ibuf);
got = -1;
goto done;
}
}
#ifdef OLD
switch(mainlibundef){
case MAINLIB_ERROR:
gerror(stderr, "can't locate procedure '%s' for mainlib\n", ibuf);
got = -1;
goto done;
case MAINLIB_EXTN:
if( (s=Find(ibuf2, "x", NULL, mainlib_path)) != NULL ){
mles[i] = MainLibAdd(ml, s, ibuf2, NULL, MAINLIB_EXTN);
xfree(s);
}
else
gerror(stderr, "can't locate procedure '%s' for mainlib\n", ibuf);
break;
default:
gerror(stderr, "can't locate procedure '%s' for mainlib\n", ibuf);
got = -1;
goto done;
}
#endif
}
/* package up the arguments */
ip = 0;
nargs[i] = 0;
while( word(cmds[i], ibuf, &ip) ){
nowhite(ibuf, ibuf2);
if( *ibuf2 ){
if( nargs[i] >= (MAINLIB_ARGS-1) ){
gerror(stderr, "max args exceeded (%d) for mainlib\n", nargs[i]);
got = -1;
goto done;
}
args[i*MAINLIB_ARGS+nargs[i]] = xstrdup(ibuf2);
nargs[i]++;
args[i*MAINLIB_ARGS+nargs[i]] = NULL;
}
}
}
/* execute all commands */
for(i=1; i<=ncmd; i++){
/* fork new process */
if( (ml->pids[i] = fork()) == -1 ){
gerror(stderr, "can't fork()\n");
got = -1;
goto done;
}
/* child will call mainlib routine */
if( ml->pids[i] == 0 ){
/* reset optind in case its being used by child */
optind = 1;
/* stdin for this new process is */
if( i == 1 ){
/* first process either reads stdin from specified ifd,
or else we leave it alone */
if( ifd >= 0 ){
if( ifd != 0 ){
close(0); dup(ifd);
}
else{
use_ifd = 1;
}
}
}
/* all other processes read stdin from previous process stdout */
else{
close(0); dup(pipes[i][0]);
}
/* set up stdout for this new process */
if( i < ncmd ){
close(1); dup(pipes[i+1][1]);
}
else{
/* last process either writes stdout or to specified ofd */
if( ofd >= 0 ){
if( ofd != 1 ){
close(1); dup(ofd);
}
else{
use_ofd = 1;
}
}
/* or sends stdout to main process */
else{
close(1); dup(pipes[0][1]);
}
}
/* all processes redirect stderr, if required */
if( efd >= 0 ){
if( efd != 2 ){
close(2); dup(efd);
}
else{
use_efd = 1;
}
}
/* close all pipes */
for(j=0; j<=ncmd; j++){
close(pipes[j][0]); close(pipes[j][1]);
}
/* including user-specified pipes */
if( (ifd >=0) && !use_ifd ) close(ifd);
if( (ofd >=0) && !use_ofd ) close(ofd);
if( (efd >=0) && !use_efd ) close(efd);
/* call specified subroutine */
switch(mles[i]->type){
case MAINLIB_ARGV:
if( debug )
fprintf(stderr, "Executing MainLib: %s\n", args[i*MAINLIB_ARGS]);
mles[i]->proc(nargs[i], &args[i*MAINLIB_ARGS]);
break;
case MAINLIB_EXTN:
if( debug )
fprintf(stderr, "Executing external program: %s\n", mles[i]->xclass);
if( mles[i]->xclass )
execvp(mles[i]->xclass, (void *)&args[i*MAINLIB_ARGS]);
else
execvp(mles[i]->name, (void *)&args[i*MAINLIB_ARGS]);
break;
#if HAVE_TCL
case MAINLIB_TCL:
if( debug )
fprintf(stderr, "Executing Tcl proc: %s\n", mles[i]->xclass);
if( use_tcl && ml->tcleval){
ml->tcleval(tcl, mles[i]->xclass);
}
else{
gerror(stderr, "Tcl support missing for mainlib\n");
}
break;
#endif
default:
gerror(stderr, "unknown program type for mainlib\n");
break;
}
_exit(0);
}
}
/* close all pipes except the final input pipe */
close(pipes[0][1]);
for(i=1; i<=ncmd; i++){
close(pipes[i][0]);
close(pipes[i][1]);
}
/* if not filling buffer, just return the pipe (user calls cleanup) */
if( !fillbuf ){
got = pipes[0][0];
ml->npid = ncmd;
goto done2;
}
/* wait for output from last process */
got = Pipe2Buf(pipes[0][0], buf);
/* and now close final input pipe */
close(pipes[0][0]);
done:
/* done with processing, wait for children to finish */
MainLibProcessCleanup(ml);
done2:
/* free up space */
for(i=1; i<=ncmd; i++){
for(j=0; j<nargs[i]; j++){
if( args[i*MAINLIB_ARGS+j] ) xfree(args[i*MAINLIB_ARGS+j]);
}
if( cmds[i] ) xfree(cmds[i]);
}
if( undef ) xfree(undef);
/* return: error (-1), number of bytes in buffer, or pipe fd */
return got;
}
/*
*---------------------------------------------------------------------------
*
* Routine: MainLibProcessCleanup
*
* Purpose: cleanup after mainlib processing
*
* Results: 1 on success, 0 for failure
*
*---------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
MainLibProcessCleanup (MainLib ml)
#else
int MainLibProcessCleanup(ml)
MainLib ml;
#endif
{
int i;
int status;
for(i=1; i<=ml->npid; i++){
if( ml->pids[i] ){
#if HAVE_MINGW32==0
while( waitpid(ml->pids[i], &status, 0) < 0 ){
if( errno != EINTR ) break;
}
#endif
ml->pids[i] = 0;
}
}
return 1;
}
/*
*---------------------------------------------------------------------------
*
* Routine: MainLibDel
*
* Purpose: delete a MainLibEntry record structure
*
* Results: 0 on success, -1 for failure
*
*---------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
MainLibDel (MainLib ml, MainLibEntry mle)
#else
int MainLibDel(ml, mle)
MainLib ml;
MainLibEntry mle;
#endif
{
/* make sure we have something to do */
if( !ml || !mle ) return -1;
/* free up space */
if( mle->xclass ) xfree(mle->xclass);
if( mle->name ) xfree(mle->name);
/* remove from list of mainlibs */
MainLibListDel(ml, mle);
/* free up record struct */
xfree((char *)mle);
return 0;
}
/*
*---------------------------------------------------------------------------
*
* Routine: MainLibFree
*
* Purpose: free all members of this mainlib list
*
* Results: 1 on success, 0 for failure
*
*---------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
MainLibFree (MainLib ml)
#else
int MainLibFree(ml)
MainLib ml;
#endif
{
MainLibEntry mle, tmle;
/* sanity check */
if( !ml ) return 0;
/* free all commands */
for(mle=ml->head; mle!=NULL; ){
tmle = mle->next;
MainLibDel(ml, mle);
mle = tmle;
}
#if HAVE_DLFCN_H
/* release dynamic library */
if( ml->dl ) dlclose(ml->dl);
#endif
/* free global struct */
xfree(ml);
return 1;
}
/*
*----------------------------------------------------------------------------
*
* Routine: MainLibLoad
*
* Purpose: load shared library and execute init function
*
* Results: 0=>OK, -1=> no shared obj, -2=>no init function
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int MainLibLoad(char *name, char *shlib, void **ml, char **ermsg)
#else
int MainLibLoad(name, shlib, ml, ermsg)
char *name;
char *shlib;
void **ml;
char **ermsg;
#endif
{
#if HAVE_DLFCN_H
char tbuf[SZ_LINE];
MainLibInitCall irtn=NULL;
MainLib tml=NULL;
void *tdl=NULL;
/* sanity checks */
if( !name ){
if( ermsg ) *ermsg = "no package name specified";
return -3;
}
if( !ml ){
if( ermsg ) *ermsg = "no return ml struct specified";
return -3;
}
/* get name of init call */
snprintf(tbuf, SZ_LINE-1, "%sMainLibInit", name);
/* load shared library */
if( !(tdl=dlopen(shlib, RTLD_LAZY)) ){
if( ermsg ) *ermsg = (char *)dlerror();
return -1;
}
/* look up init funtion */
if( !(irtn=(MainLibInitCall)dlsym(tdl, tbuf)) ){
if( ermsg ) *ermsg = (char *)dlerror();
return -2;
}
/* execute init function */
tml = (*irtn)();
/* store values */
tml->dl = tdl;
/* look up and return MainLibProcess funtion, if it exists */
tml->mainlibprocess=(MainLibProcessCall)dlsym(tml->dl, "MainLibProcess");
/* store MainLib record */
*ml = tml;
/* if we got here, its OK */
return 0;
#else
/* avoid -W unused parameter warning */
if( 0 ){
name = name;
shlib = shlib;
ml = ml;
}
/* didn't find dlfcn.h */
if( ermsg ) *ermsg = "dynamic load supported not found";
return -1;
#endif
}
|