[go: up one dir, main page]

File: cmd_br.c

package info (click to toggle)
libforms 1.2.3-1.7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,904 kB
  • sloc: ansic: 97,669; sh: 11,156; makefile: 951
file content (757 lines) | stat: -rw-r--r-- 17,833 bytes parent folder | download | duplicates (5)
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
/*
 *  This file is part of the XForms library package.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file cmd_br.c
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1997-2002  T.C. Zhao
 *  All rights reserved.
 *
 * Run a command and have its output re-directed to a browser.
 * Not very robust as signals are not handled.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include/forms.h"
#include "flinternal.h"
#include "private/flsnprintf.h"
#include "private/flvasprintf.h"
#include "fd/cmdbr.h"

#ifndef FL_WIN32
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#else
#include <process.h>
#include <io.h>
#include <errno.h>
#include <fcntl.h>
#define waitpid( pid,pstat, code )  _cwait( pstat, pid, code )
#define pid_t                     HANDLE
#endif /* FL_WIN32 */


/* Structures that hold the current running processes */

typedef struct pidlist
{
    struct pidlist * next;
    pid_t            pid;
    int              fd_out,
                     fd_err,
                     fd_user;
} PIDList;

static PIDList *pidlist = NULL;
static FD_cmd *logger = NULL;


/***************************************
 ***************************************/

static void
io_cb( int    fd,
       void * data  FL_UNUSED_ARG )
{
    char buf[ 1024 ];
    int n;

#ifndef FL_WIN32
    if ( ( n = read( fd, buf, sizeof buf - 1 ) ) > 0 )
    {
        buf[ n ] = '\0';
        fl_addto_browser_chars( logger->browser, buf );
    }
    else if ( n <= 0 )
    {               /* 0: EOF   <0: error. */
        if ( n < 0 )
            M_err( "io_cb", "read returns %d", n );
        fl_remove_io_callback( fd, FL_READ, io_cb );
        close( fd );
    }
#else
    ReadFile( ( HANDLE ) fd, buf, sizeof buf - 1, &n, NULL );
    if ( n > 0 )
    {
        buf[ n ] = '\0';
        fl_addto_browser_chars( logger->browser, buf );
    }
    else if ( n <= 0 )
    {               /* 0: EOF   <0: error. */
        if ( n < 0 )
            M_err( "io_cb", "read returns %d", n );
        fl_remove_io_callback( fd, FL_READ, io_cb );
        CloseHandle( ( HANDLE ) fd );
    }
#endif
}


/***************************************
 ***************************************/

static void
check_for_activity( PIDList * cur )
{
    XEvent xev;

    /* We must read until the pipe is closed, otherwise waitpid will hang */

    do
    {
        if ( fl_check_forms( ) == FL_EVENT )
            fl_XNextEvent( &xev );
    } while (    fli_is_watched_io( cur->fd_out )
              || fli_is_watched_io( cur->fd_err ) );

    fl_update_display( 1 );
}


/***************************************
 ***************************************/

void
fl_cmdbr_close_cb( FL_OBJECT * ob,
                   long        data  FL_UNUSED_ARG )
{
    fl_hide_form( ( ( FD_cmd * ) ob->form->fdui )->cmd );
}


/***************************************
 ***************************************/

void
fl_cmdbr_clear_cb( FL_OBJECT * ob,
                   long        data  FL_UNUSED_ARG )
{
    fl_clear_browser( ( ( FD_cmd * ) ob->form->fdui )->browser );
}


/***************************************
 ***************************************/

static int
atclose( FL_FORM * form  FL_UNUSED_ARG,
         void    * data  FL_UNUSED_ARG )
{
    return FL_OK;
}


/***************************************
 ***************************************/

static void
create_logger( void )
{
    if ( ! logger )
    {
        int oldy = fli_inverted_y;
        int oldu = fl_get_coordunit( );

        fli_inverted_y = 0;
        fl_set_coordunit( FL_COORD_PIXEL );

        logger = create_form_cmd( );
        fl_set_object_bw( logger->backface, 2 );
        fl_set_form_minsize( logger->cmd, 250, 100 );
        fl_set_form_atclose( logger->cmd, atclose, 0 );
        fl_set_form_position( logger->cmd, -logger->cmd->w - 5, 3 );

        fli_inverted_y = oldy;
        fl_set_coordunit( oldu );
    }
}


/***************************************
 ***************************************/

void
fl_addto_command_log( const char * s )
{
    create_logger( );
    fl_addto_browser_chars( logger->browser, s );
}


/***************************************
 ***************************************/

void
fl_addto_command_log_f( const char * fmt,
                        ...)
{
    char *buf;

    EXPAND_FORMAT_STRING( buf, fmt );
    fl_addto_command_log( buf );
    fl_free( buf );
}


/***************************************
 ***************************************/

void
fl_clear_command_log( void )
{
    create_logger( );
    fl_clear_browser( logger->browser );
}


/***************************************
 ***************************************/

void
fl_show_command_log( int border )
{
    create_logger( );
    fl_fit_object_label( logger->close_br, 2, 1 );
    fl_fit_object_label( logger->clear_br, 2, 1 );
    fl_show_form( logger->cmd, FL_PLACE_POSITION, border, "MessageLog" );
}


/***************************************
 ***************************************/

void
fl_hide_command_log( void )
{
    create_logger( );

    if ( logger->cmd->visible )
        fl_hide_form( logger->cmd );
}



/*
 * VMS support is from the patch by
 * David Mathog (mathog@seqaxp.bio.caltech.edu)
 */


/* OpenVMS < 7.0 is missing some key pieces here, notably waitpid, so
   it uses wait instead.  Also, vfork instead of fork (see local.h). */

#ifdef __VMS
#include <processes.h>      /* for wait(),vfork() */


/***************************************
 ***************************************/

static void
fl_execl( const char * path,
          const char * arg0,
          const char * arg1,
          const char * arg2,
          const char * arg3 )
{
    char cmd_tmp[ 1024 ];
    char *cmd_token[ 16 ];
    int i;

    /* VMS needs the tokens as tokens, not in one big command line. The
       following code is really simple minded, and will screw up if one of
       the command line arguments includes double quotes around spaces. A
       maximum of 16 tokens can exist in the command line. */

    for ( i = 0; i < 16; i++ )
        cmd_token[ i ] = NULL;

    strncpy( cmd_tmp, arg2, 1024 );
    cmd_token[ 0 ]  = strtok( cmd_tmp, " " );
    cmd_token[ 15 ] = NULL;

    for ( i = 1; i < 15, ( cmd_token[ i - 1 ] != NULL ); i++ )
        cmd_token[ i ] = strtok( NULL, " " );

    execl( cmd_token[ 0 ],
           cmd_token[ 0 ],  cmd_token[ 1 ],  cmd_token[ 2 ],  cmd_token[ 3 ],
           cmd_token[ 4 ],  cmd_token[ 5 ],  cmd_token[ 6 ],  cmd_token[ 7 ],
           cmd_token[ 8 ],  cmd_token[ 9 ],  cmd_token[ 10 ], cmd_token[ 11 ],
           cmd_token[ 12 ], cmd_token[ 13 ], cmd_token[ 14 ], cmd_token[ 15 ],
           ( char * ) 0);
}


/***************************************
 ***************************************/

#if __VMS_VER < 70000000
pid_t
waitpid( pid_t pid,
         int * status,
         int   options )
{
    return wait( status );
}
#endif /* VMS_VER < 7.0 */
#else
/* on all other platforms use native execl */
#define  fl_execl    execl
#endif /* VMS */

#ifndef FL_WIN32

FL_PID_T
fl_exe_command( const char * cmd,
                int          block )
{
    pid_t pid;
    static int p_err[ 2 ] = { -1, -1 },
               p_inout[ 2 ];
    char buf[ 256 ];

    create_logger( );

    if ( pipe( p_err ) < 0 || pipe( p_inout ) < 0 )
    {
        fli_snprintf( buf, sizeof buf,
                      "Can't create pipe - %s", fli_get_syserror_msg( ) );
        fprintf( stderr, "%s\n", buf );
        fl_addto_browser( logger->browser, buf );
        if ( p_err[ 0 ] > 0 )
        {
            close( p_err[ 0 ] );
            close( p_err[ 1 ] );
        }

        return -1;
    }

    if ( ( pid = fork( ) ) < 0 )
    {
        fli_snprintf( buf, sizeof buf, "fork failed: %s",
                      fli_get_syserror_msg( ) );
        fl_addto_browser( logger->browser, buf );
        perror( "fork" );
        close( p_inout[ 0 ] );
        close( p_inout[ 1 ] );
        close( p_err[ 0 ] );
        close( p_err[ 1 ] );
        return -1;
    }

    if ( pid == 0 )
    {
        dup2( p_inout[ 1 ], fileno( stdout ) );
        close( p_inout[ 1 ] );
        close( p_inout[ 0 ] );
        dup2( p_err[ 1 ], fileno( stderr ) );
        close( p_err[ 1 ] );
        close( p_err[ 0 ] );
        fl_execl( "/bin/sh", "sh", "-c", cmd, ( char * ) 0 );
        perror( "execle" );
        _exit( 127 );
    }
    else
    {
        PIDList *cur = fl_malloc( sizeof *cur );

        cur->next    = pidlist;
        cur->pid     = pid;
        cur->fd_user = -1;

        pidlist = cur;

        /* close write end of the pipe, only child process uses them */

        close( p_inout[ 1 ] );
        close( p_err[ 1 ] );
        cur->fd_out = p_inout[ 0 ];
        cur->fd_err = p_err[ 0 ];

        /* add_io prepends. handle stdout first when data is present */

        fl_add_io_callback( cur->fd_err,
                            FL_READ,
                            io_cb,
                            ( void * ) ( long ) cur->pid );
        fl_add_io_callback( cur->fd_out,
                            FL_READ,
                            io_cb,
                            ( void * ) ( long ) cur->pid );
    }

    return block ? ( FL_PID_T ) fl_end_command( pid ) : pid;
}

#else /* Win/NT platform */

FL_PID_T
fl_exe_command( const char * cmd,
                int          block )
{
    SECURITY_ATTRIBUTES sa = { 0 };
    STARTUPINFO si = { 0 };
    PROCESS_INFORMATION pi = { 0 };
    HANDLE hPipeWrite = NULL;
    HANDLE hPipeRead = NULL;
    char buf[ 512 ];
    PIDList *cur;

    create_logger( );

    sa.nLength = sizeof sa;
    sa.bInheritHandle = FL_TRUE;
    sa.lpSecurityDescriptor = NULL;
    if ( ! CreatePipe( &hPipeRead, &hPipeWrite, &sa, 255 ) )
    {
        fli_snprintf( buf, sizeof buf, "Can't create pipe - %s",
                      fli_get_syserror_msg( ) );
        fprintf( stderr, "%s\n", buf );
        fl_addto_browser( logger->browser, buf );
        return ( FL_PID_T ) - 1;
    }

    /* Make child process use p_inout as standard out, and make sure it does
       not show on screen. */

    si.cb = sizeof si;
    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.wShowWindow = SW_HIDE;
    si.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
    si.hStdOutput = hPipeWrite;
    si.hStdError = hPipeWrite;

    if ( CreateProcess( NULL, ( LPTSTR ) cmd, NULL, NULL, FL_TRUE, 0,
                        NULL, NULL, &si, &pi ) )
    {
        /* close write end of the pipe, only child process uses them */

        CloseHandle( hPipeWrite );
        CloseHandle( pi.hThread );  /* close the primay thread handle */

        cur = fl_malloc( sizeof *cur );

        cur->next    = pidlist;
        cur->pid     = pi.hProcess;
        cur->fd_out  = hPipeRead;
        cur->fd_err  = 0;
        cur->fd_user = 0;

        pidlist = cur;

        /* add_io prepends. handle stdout first when data is present */

        fl_add_io_callback( ( int ) cur->fd_out, FL_READ, io_cb,
                            ( void * ) cur->pid );
    }
    else
    {
        fli_snprintf( buf,sizeof buf, "spawn failed: %s",
                      fli_get_syserror_msg( ) );
        fl_addto_browser( logger->browser, buf );
        perror( "spawn" );

        /* close the pipes */

        CloseHandle( hPipeRead );
        CloseHandle( hPipeWrite );
        return ( FL_PID_T ) - 1;
    }

    return block ? ( FL_PID_T ) fl_end_command( cur->pid ) : cur->pid;
}
#endif


/***************************************
 * essentially a bi-directional pipe
 ***************************************/

FILE *
fl_popen( const char * cmd,
          const char * type )
{
    pid_t pid;
    static int p_err[ 2 ] = { -1, -1 },
               p_p2c[ 2 ] = { -1, -1 },
               p_c2p[ 2 ] = { -1, -1 };
    char buf[ 512 ];
    int iswrite,
        i;
    FILE *fp = 0;

    if (    ! cmd
         || ! *cmd
         || ! type
         || ! *type
         || ( *type != 'r' && *type != 'w' ) )
        return 0;

    iswrite = *type == 'w';

    create_logger( );

    if ( pipe( p_err ) < 0 || pipe( p_p2c ) < 0 || pipe( p_c2p ) < 0 )
    {
        fli_snprintf( buf, sizeof buf,
                      "Can't create pipe - %s", fli_get_syserror_msg( ) );
        fprintf( stderr, "%s\n", buf );
        fl_addto_browser(logger->browser, buf );
        for ( i = 0; i < 2; i++ )
        {
            if ( p_err[ i ] >= 0 )
                close( p_err[ i ] );
            if (p_p2c[ i ] >= 0 )
                close( p_p2c[ i ] );
        }
        return NULL;
    }

    if ( ( pid = fork( ) ) < 0 )
    {
        fli_snprintf( buf,sizeof buf, "fork failed: %s",
                      fli_get_syserror_msg( ) );
        fl_addto_browser( logger->browser, buf );
        perror( "fork" );
        for ( i = 0; i < 2; i++ )
        {
            close( p_err[ i ] );
            close( p_p2c[ i ] );
            close( p_c2p[ i ] );
        }
        return NULL;
    }

    if ( pid == 0 )
    {
        dup2( p_p2c[ 0 ], fileno( stdin ) );
        dup2( p_c2p[ 1 ], fileno( stdout ) );
        dup2( p_err[ 1 ], fileno( stderr ) );

        close( p_p2c[ 0 ] );
        close( p_p2c[ 1 ] );
        close( p_c2p[ 0 ] );
        close( p_c2p[ 1 ] );
        close( p_err[ 0 ] );
        close( p_err[ 1 ] );

        fl_execl( "/bin/sh", "sh", "-c", cmd, ( char * ) 0 );

        perror( "execle" );
        _exit( 127 );
    }
    else
    {
        /* the parent process */

        PIDList *cur = fl_malloc( sizeof *cur );

        cur->next    = pidlist;
        cur->pid     = pid;

        pidlist = cur;

        close( p_p2c[ 0 ] );
        close( p_c2p[ 1 ] );
        close( p_err[ 1 ] );

        cur->fd_err = p_err[ 0 ];
        cur->fd_out = p_c2p[ 0 ];

        fl_add_io_callback( cur->fd_err,
                            FL_READ, io_cb,
                            ( void * ) ( long ) pid );

        if ( iswrite )
        {
            cur->fd_user = p_p2c[1];
            fl_add_io_callback( cur->fd_out,
                                FL_READ,
                                io_cb,
                                ( void * ) ( long ) pid );
        }
        else
            cur->fd_user = p_c2p[ 0 ];

        fp = fdopen( cur->fd_user, type );
    }

    return fp;
}


/***************************************
 * close pipe
 ***************************************/

int
fl_pclose( FILE * stream )
{
    PIDList *cur, *last;
    int fd;

    if ( ! stream || ( fd = fileno( stream ) ) < 0 )
        return -1;

    fclose( stream );

    for ( last = 0, cur = pidlist;
          cur && cur->fd_user != fd;
          last = cur, cur = last->next )
        /* empty */ ;

    if ( ! cur )
        return -1;

    check_for_activity( cur );

    return fl_end_command( cur->pid );
}


/***************************************
 ***************************************/

int
fl_end_command( FL_PID_T pid )
{
    PIDList *cur,
            *last;
    int pstat;

    for ( last = NULL, cur = pidlist;
          cur && cur->pid != pid;
          last = cur, cur = last->next )
        /* empty */ ;

    if ( ! cur )
        return -1;

    do
    {
        check_for_activity( cur );
        pid = waitpid( cur->pid, &pstat, 0 );
    } while ( pid == -1 && errno == EINTR );

    if ( last )
        last->next = cur->next;
    else
        pidlist = cur->next;

    fl_free( cur );

    return pid == -1 ? -1 : pstat;
}


/***************************************
 * check if PID exists or not. Return 0 if it does not, 1 if still
 * running, any other value indicates an error
 ***************************************/

int
fl_check_command( FL_PID_T pid )
{
#ifndef FL_WIN32
    if ( kill( pid, 0 ) == 0 )
    {
        waitpid( pid, 0, WNOHANG );
        return 1;
    }

    return errno == ESRCH ? 0 : -1;
#else
    DWORD ExitCode = 0;
    GetExitCodeProcess( pid, &ExitCode );

    if ( ExitCode == STILL_ACTIVE )
        return 1;
    return ( GetLastError( ) == ERROR_INVALID_HANDLE ) ? 0 : -1;
#endif
}


/***************************************
 * clean up all child processes
 ***************************************/

int
fl_end_all_command( void )
{
    PIDList *cur,
            *next;
    int pstat = 0;
    pid_t pid = 0;

    for ( cur = pidlist; cur; cur = next )
    {
        next = cur->next;
        check_for_activity( cur );
        pid = waitpid( cur->pid, &pstat, 0 );
        fl_free( cur );
    }

    pidlist = NULL;

    return pid == -1 ? pid : pstat;
}


/***************************************
 ***************************************/

FD_CMDLOG *
fl_get_command_log_fdstruct( void )
{
    static FD_CMDLOG ret;

    create_logger( );
    ret.form = logger->cmd;
    ret.browser = logger->browser;
    ret.close_browser = logger->close_br;
    ret.clear_browser = logger->clear_br;
    return &ret;
}


/***************************************
 ***************************************/

void
fl_set_command_log_position( int x,
                             int y )
{
    create_logger( );
    fl_set_form_position( logger->cmd, x, y );
}

#include "fd/cmdbr.c"       /* from directory fd */


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */