File: tclXcmdloop.c

package info (click to toggle)
tclx8.4 8.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,800 kB
  • sloc: ansic: 14,863; tcl: 2,090; sh: 265; makefile: 159
file content (793 lines) | stat: -rw-r--r-- 27,119 bytes parent folder | download | duplicates (9)
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
/* 
 * tclXcmdloop --
 *
 *   Interactive command loop, C and Tcl callable.
 *-----------------------------------------------------------------------------
 * Copyright 1991-1999 Karl Lehenbauer and Mark Diekhans.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies.  Karl Lehenbauer and
 * Mark Diekhans make no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 *-----------------------------------------------------------------------------
 * $Id: tclXcmdloop.c,v 1.3 2002/09/26 00:19:18 hobbs Exp $
 *-----------------------------------------------------------------------------
 */

#include "tclExtdInt.h"

/*
 * Client data entry for asynchronous command reading.  This is associated
 * with a given instance of a async command loop.  I allows for recursive
 * commands loops on the same channel (and even multiple, but the results
 * out be unpredicatable).
 */
typedef struct {
    Tcl_Interp  *interp;       /* Interp for command eval.            */
    Tcl_Channel  channel;      /* Input channel.                      */
    int          options;      /* Command loop options.               */
    Tcl_DString  command;      /* Buffer for command being read.      */
    int          partial;      /* Partial command in buffer?          */
    char        *endCommand;   /* Command to execute at end of loop.  */
    char        *prompt1;      /* Prompts to use.                     */
    char        *prompt2;
} asyncLoopData_t;


/*
 * Prototypes of internal functions.
 */
static int
IsSetVarCmd _ANSI_ARGS_((char  *command));

static void
OutputPrompt _ANSI_ARGS_((Tcl_Interp *interp,
                          int         topLevel,
                          char       *prompt1,
                          char       *prompt2));

static int
AsyncSignalErrorHandler _ANSI_ARGS_((Tcl_Interp *interp,
                                     ClientData  clientData,
                                     int         background,
                                     int         signalNum));


static void
AsyncCommandHandler _ANSI_ARGS_((ClientData clientData,
                                 int        mask));

static int
SyncSignalErrorHandler _ANSI_ARGS_((Tcl_Interp *interp,
                                    ClientData  clientData,
                                    int         background,
                                    int         signalNum));

static void
AsyncCommandHandlerDelete _ANSI_ARGS_((ClientData clientData));

static int 
TclX_CommandloopObjCmd _ANSI_ARGS_((ClientData clientData, 
                                    Tcl_Interp *interp,
                                    int objc,
                                    Tcl_Obj *CONST objv[]));

/*-----------------------------------------------------------------------------
 * IsSetVarCmd --
 *    Determine if a command is a `set' command that sets a variable
 * (i.e. two arguments).
 *
 * Parameters:
 *   o command (I) - Command to check.
 * Returns:
 *   TRUE if it is a set that sets a variable, FALSE if its some other command.
 *-----------------------------------------------------------------------------
 */
static int
IsSetVarCmd (command)
    char  *command;
{
    Tcl_Parse tclParse;
    int numWords;

    if ((!STRNEQU (command, "set", 3)) || (!ISSPACE (command [3])))
        return FALSE;  /* Quick check */

    Tcl_ParseCommand(NULL, command, -1, 1, &tclParse);
    numWords = tclParse.numWords;
    Tcl_FreeParse(&tclParse);
    return numWords > 2 ? TRUE : FALSE;
}

/*-----------------------------------------------------------------------------
 * TclX_PrintResult --
 *   Print the result of a Tcl_Eval.  It can optionally not echo "set" commands
 * that successfully set a variable.
 *
 * Parameters:
 *   o interp (I) - A pointer to the interpreter.  Result of command should be
 *     in interp result.
 *   o intResult (I) - The integer result returned by Tcl_Eval.
 *   o checkCmd (I) - If not NULL and the command was sucessful, check to
 *     set if this is a "set" command setting a variable.  If so, don't echo
 *     the result. 
 *-----------------------------------------------------------------------------
 */
void
TclX_PrintResult (interp, intResult, checkCmd)
    Tcl_Interp *interp;
    int         intResult;
    char       *checkCmd;
{
    Tcl_Channel stdoutChan,  stderrChan;
    char *resultStr;

    /*
     * If the command was supplied and it was a successful set of a variable,
     * don't output the result.
     */
    if ((checkCmd != NULL) && (intResult == TCL_OK) && IsSetVarCmd (checkCmd))
        return;

    stdoutChan = Tcl_GetStdChannel(TCL_STDOUT);
    stderrChan = Tcl_GetStdChannel(TCL_STDERR);

    if (intResult == TCL_OK) {
        if (stdoutChan == NULL)
            return;
        resultStr = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), NULL);
        if (resultStr [0] != '\0') {
            if (stderrChan != NULL)
                Tcl_Flush (stderrChan);
            Tcl_WriteChars(stdoutChan, resultStr, -1);
            TclX_WriteNL(stdoutChan);
            Tcl_Flush(stdoutChan);
        }
    } else {
        char msg [64];

        if (stderrChan == NULL)
            return;
        if (stdoutChan != NULL)
            Tcl_Flush (stdoutChan);

        if (intResult == TCL_ERROR) {
            strcpy(msg, "Error: ");
        } else {
            sprintf(msg, "Bad return code (%d): ", intResult);
        }
        resultStr = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), NULL);
        Tcl_WriteChars(stderrChan, msg, -1);
        Tcl_WriteChars(stderrChan, resultStr, -1);
        TclX_WriteNL(stderrChan);
        Tcl_Flush(stderrChan);
    }
}

/*-----------------------------------------------------------------------------
 * OutputPrompt --
 *   Outputs a prompt by executing either the command string in tcl_prompt1 or
 * tcl_prompt2 or a specified prompt string.  Also involkes any pending async
 * handlers, as these need to be done before the eval of the prompt, or they
 * might result in an error in the prompt.
 *
 * Parameters:
 *   o interp (I) - A pointer to the interpreter.
 *   o topLevel (I) - If TRUE, output the top level prompt (tcl_prompt1).
 *   o prompt1 (I) - If not NULL, use this command instead of the value of
 *     tcl_prompt1.  In this case, the result of the command is used rather
 *     than the output.
 *   o prompt2 (I) - If not NULL, use this command instead of the value of
 *     tcl_prompt2.  In this case, the result of the command is used rather
 *     than the output.
 *-----------------------------------------------------------------------------
 */
static void
OutputPrompt (interp, topLevel, prompt1, prompt2)
    Tcl_Interp *interp;
    int         topLevel;
    char       *prompt1;
    char       *prompt2;
{
    char *promptHook;
    char *resultStr;
    int result, useResult, promptDone = FALSE;
    Tcl_Channel stdoutChan, stderrChan;

    stdoutChan = Tcl_GetStdChannel (TCL_STDOUT);
    stderrChan = Tcl_GetStdChannel (TCL_STDERR);

    /*
     * If a signal came in, process it.  This prevents signals that are queued
     * from generating prompt hook errors.
     */
    if (Tcl_AsyncReady ()) {
        Tcl_AsyncInvoke (interp, TCL_OK); 
    }

    if (stderrChan != NULL)
        Tcl_Flush (stderrChan);

    /*
     * Determine prompt command to evaluate.
     */
    if (topLevel) {
        if (prompt1 != NULL) {
            promptHook = prompt1;
            useResult = TRUE;
        } else {
            promptHook = (char *) Tcl_GetVar (interp, "tcl_prompt1",
		    TCL_GLOBAL_ONLY);
            useResult = FALSE;
        }
    } else {
        if (prompt2 != NULL) {
            promptHook = prompt2;
            useResult = TRUE;
        } else {
            promptHook = (char *) Tcl_GetVar (interp, "tcl_prompt2",
		    TCL_GLOBAL_ONLY);
            useResult = FALSE;
        }
    }

    if (promptHook != NULL) {
        result = Tcl_Eval (interp, promptHook);
        resultStr = Tcl_GetStringFromObj (Tcl_GetObjResult (interp), NULL);
        if (result == TCL_ERROR) {
            if (stderrChan != NULL) {
                Tcl_WriteChars(stderrChan, "Error in prompt hook: ", -1);
                Tcl_WriteChars(stderrChan, resultStr, -1);
                TclX_WriteNL (stderrChan);
            }
        } else {
            if (useResult && (stdoutChan != NULL))
                Tcl_WriteChars(stdoutChan, resultStr, -1);
            promptDone = TRUE;
        }
    } 

    if (stdoutChan != NULL) {
        if (!promptDone)
            Tcl_Write (stdoutChan, topLevel ? "%" : ">", 1);
        Tcl_Flush (stdoutChan);
    }
    Tcl_ResetResult (interp);
}

/*-----------------------------------------------------------------------------
 * AsyncSignalErrorHandler --
 *   Handler for signals that generate errors.   If no code is currently
 * executing (i.e, it the event loop), we want the input buffer to be
 * cleared on SIGINT.
 *
 * Parameters:
 *   o interp (I) - The interpreter used to process the signal.  The error
 *     message is in the result.
 *   o clientData (I) - Pointer to the asyncLoopData structure.
 *   o background (I) - TRUE if signal was handled in the background (i.e
 *     the event loop) rather than in an interp.
 * Returns:
 *  The Tcl result code to continue with.   TCL_OK if we have handled the
 * signal, TCL_ERROR if not.
 *-----------------------------------------------------------------------------
 */
static int
AsyncSignalErrorHandler (interp, clientData, background, signalNum)
    Tcl_Interp *interp;
    ClientData  clientData;
    int         background;
    int         signalNum;
{
    if (background & (signalNum == SIGINT)) {
        asyncLoopData_t *dataPtr = (asyncLoopData_t *) clientData;
        Tcl_Channel stdoutChan = Tcl_GetStdChannel (TCL_STDOUT);

        Tcl_DStringFree (&dataPtr->command);
        dataPtr->partial = FALSE;

        Tcl_ResetResult (interp);
        
        if (dataPtr->options & TCLX_CMDL_INTERACTIVE) {
            if (stdoutChan != NULL)
                TclX_WriteNL (stdoutChan);
            OutputPrompt (dataPtr->interp, !dataPtr->partial,
                          dataPtr->prompt1, dataPtr->prompt2);
        }
        return TCL_OK;
    }
    return TCL_ERROR;
}

/*-----------------------------------------------------------------------------
 * AsyncCommandHandler --
 *   Handler for async command reading. This procedure is invoked by the event
 * dispatcher whenever the input becomes readable.  It grabs the next line of
 * input characters, adds them to a command being assembled, and executes the
 * command if it's complete.
 *
 * Parameters:
 *   o clientData (I) - Pointer to the asyncLoopData structure.
 *   o mask (I) - Not used.
 *-----------------------------------------------------------------------------
 */
static void
AsyncCommandHandler (clientData, mask)
    ClientData clientData;
    int        mask;
{
    asyncLoopData_t *dataPtr = (asyncLoopData_t *) clientData;
    int code;
    char *cmd, *resultStr;

    /*
     * Make sure that we are the current signal error handler.  This
     * handles recusive event loop calls.
     */
    TclX_SetAppSignalErrorHandler (AsyncSignalErrorHandler, clientData);

    if (Tcl_Gets (dataPtr->channel, &dataPtr->command) < 0) {
        /*
         * Handler EINTR error special.
         */
        if (!(Tcl_Eof (dataPtr->channel) ||
              Tcl_InputBlocked (dataPtr->channel)) &&
            (Tcl_GetErrno () == EINTR)) {
            if (Tcl_AsyncReady ()) {
                Tcl_AsyncInvoke (NULL, TCL_OK); 
            }
            return;  /* Let the event loop call us again. */
        }

        /*
         * Handle EOF or error.
         */
        if (dataPtr->options & TCLX_CMDL_EXIT_ON_EOF) {
            Tcl_Exit (0);
        } else {
            AsyncCommandHandlerDelete (clientData);
        }
        return;
    }
 
   cmd = Tcl_DStringAppend (&dataPtr->command, "\n", -1);
    
    if (!Tcl_CommandComplete (cmd)) {
        dataPtr->partial = TRUE;
        goto prompt;
    }
    dataPtr->partial = FALSE;

    /*
     * Disable the stdin channel handler while evaluating the command;
     * otherwise if the command re-enters the event loop we might process
     * commands from stdin before the current command is finished.  Among
     * other things, this will trash the text of the command being evaluated.
     */

    Tcl_CreateChannelHandler (dataPtr->channel, 0,
                              AsyncCommandHandler, clientData);
    code = Tcl_RecordAndEval (dataPtr->interp, cmd, TCL_EVAL_GLOBAL);
    Tcl_CreateChannelHandler (dataPtr->channel, TCL_READABLE,
                              AsyncCommandHandler, clientData);

    resultStr = Tcl_GetStringFromObj (Tcl_GetObjResult (dataPtr->interp),
                                      NULL);
    if (resultStr [0] != '\0') {
        if (dataPtr->options & TCLX_CMDL_INTERACTIVE) {
            TclX_PrintResult (dataPtr->interp, code, cmd);
        }
    }
    Tcl_DStringFree (&dataPtr->command);

    /*
     * Output a prompt.
     */
  prompt:
    if (dataPtr->options & TCLX_CMDL_INTERACTIVE) {
        OutputPrompt (dataPtr->interp, !dataPtr->partial,
                      dataPtr->prompt1, dataPtr->prompt2);
    }
    Tcl_ResetResult (dataPtr->interp);
}

/*-----------------------------------------------------------------------------
 * AsyncCommandHandlerDelete --
 *   Delete an async command handler.
 *
 * Parameters:
 *   o clientData (I) - Pointer to the asyncLoopData structure for the
 *     handler being deleted.
 *-----------------------------------------------------------------------------
 */
static void
AsyncCommandHandlerDelete (clientData)
    ClientData clientData;
{
    asyncLoopData_t *dataPtr = (asyncLoopData_t *) clientData;

    /*
     * Remove handlers from system.
     */
    Tcl_DeleteChannelHandler (dataPtr->channel, AsyncCommandHandler, 
                              clientData);
    Tcl_DeleteCloseHandler (dataPtr->channel, AsyncCommandHandlerDelete,
                            clientData);
    TclX_SetAppSignalErrorHandler (NULL, NULL);
    
    /*
     * If there is an end command, eval it.
     */
    if (dataPtr->endCommand != NULL) {
        if (Tcl_GlobalEval (dataPtr->interp, dataPtr->endCommand) != TCL_OK)
            Tcl_BackgroundError (dataPtr->interp);
        Tcl_ResetResult (dataPtr->interp);
    }

    /*
     * Free resources.
     */          
    Tcl_DStringFree (&dataPtr->command);
    if (dataPtr->endCommand != NULL)
        ckfree (dataPtr->endCommand);
    if (dataPtr->prompt1 != NULL)
        ckfree (dataPtr->prompt1);
    if (dataPtr->prompt2 != NULL)
        ckfree (dataPtr->prompt2);
    ckfree ((char *) dataPtr);
}

/*-----------------------------------------------------------------------------
 * TclX_AsyncCommandLoop --
 *   Establish an async command handler on stdin.
 *
 * Parameters:
 *   o interp (I) - A pointer to the interpreter
 *   o options (I) - Async command loop options:
 *     o TCLX_CMDL_INTERACTIVE - Print a prompt and the result of command
 *       execution.
 *     o TCLX_CMDL_EXIT_ON_EOF - Exit when an EOF is encountered.
 *   o endCommand (I) - If not NULL, a command to evaluate when the command
 *     handler is removed, either by closing the channel or hitting EOF.
 *   o prompt1 (I) - If not NULL, the command to evalute get the main prompt.
 *     If NULL, the current value of tcl_prompt1 is evaluted to output the
 *     main prompt.  NOTE: prompt1 returns a result while tcl_prompt1
 *     outputs a result.
 *   o prompt2 (I) - If not NULL, the command to evalute get the secondary
 *     prompt.  If NULL, the current value of tcl_prompt is evaluted to
 *     output the secondary prompt.  NOTE: prompt2 returns a result while
 *     tcl_prompt2 outputs a result.
 * Returns:
 *   TCL_OK or TCL_ERROR;
 *-----------------------------------------------------------------------------
 */
int
TclX_AsyncCommandLoop (interp, options, endCommand, prompt1, prompt2)
    Tcl_Interp *interp;
    int         options;
    char       *endCommand;
    char       *prompt1;
    char       *prompt2;
{
    Tcl_Channel stdinChan;
    asyncLoopData_t *dataPtr;

    stdinChan = TclX_GetOpenChannel (interp, "stdin", TCL_READABLE);
    if (stdinChan == NULL)
        return TCL_ERROR;

    dataPtr = (asyncLoopData_t *) ckalloc (sizeof (asyncLoopData_t));
    
    dataPtr->interp = interp;
    dataPtr->channel = stdinChan;
    dataPtr->options = options;
    Tcl_DStringInit (&dataPtr->command);
    dataPtr->partial = FALSE;
    if (endCommand == NULL)
        dataPtr->endCommand = NULL;
    else
        dataPtr->endCommand = ckstrdup (endCommand);
    if (prompt1 == NULL)
        dataPtr->prompt1 = NULL;
    else
        dataPtr->prompt1 = ckstrdup (prompt1);
    if (prompt2 == NULL)
        dataPtr->prompt2 = NULL;
    else
        dataPtr->prompt2 = ckstrdup (prompt2);

    Tcl_DeleteCloseHandler (stdinChan, AsyncCommandHandlerDelete,
                            (ClientData) dataPtr);
    Tcl_CreateChannelHandler (stdinChan, TCL_READABLE,
                              AsyncCommandHandler, (ClientData) dataPtr);
    TclX_SetAppSignalErrorHandler (AsyncSignalErrorHandler,
                                   (ClientData) dataPtr);

    /*
     * Output initial prompt.
     */
    if (dataPtr->options & TCLX_CMDL_INTERACTIVE) {
        OutputPrompt (dataPtr->interp, !dataPtr->partial,
                      dataPtr->prompt1, dataPtr->prompt2);
    }
    return TCL_OK;
}

/*-----------------------------------------------------------------------------
 * SyncSignalErrorHandler --
 *   Handler for signals that generate errors.  We want to clear the input
 * buffer on SIGINT.
 *
 * Parameters:
 *   o interp (I) - The interpreter used to process the signal.  The error
 *     message is in the result.
 *   o clientData (I) - Pointer to a int to set to TRUE if SIGINT occurs.
 *   o background (I) - Ignored.
 * Returns:
 *  The Tcl result code to continue with.   TCL_OK if we have handled the
 * signal, TCL_ERROR if not.
 *-----------------------------------------------------------------------------
 */
static int
SyncSignalErrorHandler (interp, clientData, background, signalNum)
    Tcl_Interp *interp;
    ClientData  clientData;
    int         background;
    int         signalNum;
{
    if (signalNum == SIGINT) {
        *((int *) clientData) = TRUE;
    }
    return TCL_ERROR;
}

/*-----------------------------------------------------------------------------
 * TclX_CommandLoop --
 *   Run a syncronous Tcl command loop.  EOF terminates the loop.
 *
 * Parameters:
 *   o interp (I) - A pointer to the interpreter
 *   o options (I) - Command loop options:
 *     o TCLX_CMDL_INTERACTIVE - Print a prompt and the result of command
 *       execution.
 *   o prompt1 (I) - If not NULL, the command to evalute get the main prompt.
 *     If NULL, the current value of tcl_prompt1 is evaluted to output the
 *     main prompt.  NOTE: prompt1 returns a result while tcl_prompt1
 *     outputs a result.
 *   o prompt2 (I) - If not NULL, the command to evalute get the secondary
 *     prompt.  If NULL, the current value of tcl_prompt is evaluted to
 *     output the secondary prompt.  NOTE: prompt2 returns a result while
 *     tcl_prompt2 outputs a result.
 * Returns:
 *   TCL_OK or TCL_ERROR;
 *-----------------------------------------------------------------------------
 */
int
TclX_CommandLoop (interp, options, endCommand, prompt1, prompt2)
    Tcl_Interp *interp;
    int         options;
    char       *endCommand;
    char       *prompt1;
    char       *prompt2;
{
    Tcl_DString command;
    int result, partial = FALSE, gotSigIntError = FALSE,
      gotInterrupted = FALSE;
    Tcl_Channel stdinChan, stdoutChan;

    Tcl_DStringInit (&command);

    while (TRUE) {
        /*
         * Always set signal error handler so recursive command loops work.
         */
        TclX_SetAppSignalErrorHandler (SyncSignalErrorHandler,
                                       &gotSigIntError);

        /*
         * If a signal handlers are pending, process them.
         */
        if (Tcl_AsyncReady ()) {
            result = Tcl_AsyncInvoke (interp, TCL_OK); 
            if ((result != TCL_OK) && !gotSigIntError)
                TclX_PrintResult (interp, result, NULL);
        }

        /*
         * Drop any pending command if SIGINT occured since the last time we
         * were through here, event if its already been processed.
         */
        if (gotSigIntError) {
            Tcl_DStringFree (&command);
            partial = FALSE;
            stdoutChan = Tcl_GetStdChannel (TCL_STDOUT);
            if (stdoutChan != NULL)
                TclX_WriteNL (stdoutChan);
        }

        /*
         * Output a prompt and input a command.
         */
        stdinChan = Tcl_GetStdChannel (TCL_STDIN);
        if (stdinChan == NULL)
            goto endOfFile;

        /*
         * Only ouput prompt if we didn't get interrupted or if the
         * interruption was SIGINT
         */
        if ((options & TCLX_CMDL_INTERACTIVE) &&
            (!gotInterrupted || gotSigIntError)) {
            OutputPrompt (interp, !partial, prompt1, prompt2);
        }

        /*
         * Reset these flags for the next round
         */
        gotSigIntError = FALSE;
        gotInterrupted = FALSE; 

        result = Tcl_Gets (stdinChan, &command);
        if (result < 0) {
            if (Tcl_Eof (stdinChan) || Tcl_InputBlocked (stdinChan))
                goto endOfFile;
            if (Tcl_GetErrno () == EINTR) {
                gotInterrupted = TRUE; 
                continue;  /* Process signals above */
            }
            TclX_AppendObjResult (interp, "command input error on stdin: ",
                                  Tcl_PosixError (interp), (char *) NULL);
            return TCL_ERROR;
        }

        /*
         * Newline was stripped by Tcl_DStringGets, but is needed for
         * command-complete checking, add it back in.  If the command is
         * not complete, get the next line.
         */
        Tcl_DStringAppend (&command, "\n", 1);

        if (!Tcl_CommandComplete (command.string)) {
            partial = TRUE;
            continue;  /* Next line */
        }

        /*
         * Finally have a complete command, go eval it and maybe output the
         * result.
         */
        result = Tcl_RecordAndEval (interp, command.string, 0);

        if ((options & TCLX_CMDL_INTERACTIVE) || (result != TCL_OK))
            TclX_PrintResult (interp, result, command.string);

        partial = FALSE;
        Tcl_DStringFree (&command);
    }
  endOfFile:
    Tcl_DStringFree (&command);
    if (endCommand != NULL) {
        if (Tcl_Eval (interp, endCommand) == TCL_ERROR) {
            return TCL_ERROR;
        }
    }
    return TCL_OK;
}

/*-----------------------------------------------------------------------------
 * Tcl_CommandloopObjCmd --
 *    Implements the commandloop command:
 *       commandloop -async -interactive on|off|tty -prompt1 cmd
 *                   -prompt2 cmd -endcommand cmd
 * Results:
 *   Standard TCL results.
 *-----------------------------------------------------------------------------
 */
static int
TclX_CommandloopObjCmd (clientData, interp, objc, objv)
    ClientData  clientData;
    Tcl_Interp *interp;
    int         objc;
    Tcl_Obj    *CONST objv[];
{
    int options = 0, async = FALSE, argIdx, interactive;
    char *argStr,  *endCommand = NULL;
    char *prompt1 = NULL, *prompt2 = NULL;

    interactive = isatty (0);
    for (argIdx = 1; argIdx < objc; argIdx++) {
        argStr = Tcl_GetStringFromObj (objv [argIdx], NULL);
        if (argStr [0] != '-')
            break;
        if (STREQU (argStr, "-async")) {
            async = TRUE;
        } else if (STREQU (argStr, "-prompt1")) {
            if (argIdx == objc - 1)
                goto argRequired;
            prompt1 = Tcl_GetStringFromObj (objv [++argIdx], NULL);;
        } else if (STREQU (argStr, "-prompt2")) {
            if (argIdx == objc - 1)
                goto argRequired;
            prompt2 = Tcl_GetStringFromObj (objv [++argIdx], NULL);
        } else if (STREQU (argStr, "-interactive")) {
            if (argIdx == objc - 1)
                goto argRequired;
            argIdx++;
            argStr = Tcl_GetStringFromObj (objv [argIdx], NULL);
            if (STREQU (argStr, "tty")) {
                interactive = TRUE;
            } else {
                if (Tcl_GetBooleanFromObj (interp, objv [argIdx],
                                           &interactive) != TCL_OK)
                    return TCL_ERROR;
            }
        } else if (STREQU (argStr, "-endcommand")) {
            if (argIdx == objc - 1)
                goto argRequired;
            endCommand = Tcl_GetStringFromObj (objv [++argIdx], NULL);
        } else {
            goto unknownOption;
        }
    }
    if (argIdx != objc)
        goto wrongArgs;

    if (interactive)
        options |= TCLX_CMDL_INTERACTIVE;

    if (async) {
        return TclX_AsyncCommandLoop (interp,
                                      options,
                                      endCommand,
                                      prompt1,
                                      prompt2);
    } else {
        return TclX_CommandLoop (interp,
                                 options,
                                 endCommand,
                                 prompt1,
                                 prompt2);
    }


    /*
     * Argument error message generation.  argStr should contain the
     * option being processed.
     */
  argRequired:
    TclX_AppendObjResult (interp, "argument required for ", argStr,
                          " option", (char *) NULL);
    return TCL_ERROR;

  unknownOption:
    TclX_AppendObjResult (interp, "unknown option \"", argStr,
                          "\", expected one of \"-async\", ",
                          "\"-interactive\", \"-prompt1\", \"-prompt2\", ",
                          " or \"-endcommand\"", (char *) NULL);
    return TCL_ERROR;
    
  wrongArgs:
    TclX_WrongArgs (interp, objv [0],
                    "?-async? ?-interactive on|off|tty? ?-prompt1 cmd? ?-prompt2 cmd? ?-endcommand cmd?");
    return TCL_ERROR;
}

/*-----------------------------------------------------------------------------
 * TclX_CmdloopInit --
 *     Initialize the coommandloop command.
 *-----------------------------------------------------------------------------
 */
void
TclX_CmdloopInit (interp)
    Tcl_Interp *interp;
{
    Tcl_CreateObjCommand (interp,
                          "commandloop",
                          TclX_CommandloopObjCmd, 
                          (ClientData) NULL,
                          (Tcl_CmdDeleteProc*) NULL);
    
}