-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.c
1748 lines (1565 loc) · 48.7 KB
/
script.c
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* script.c
*
* qodem - Qodem Terminal Emulator
*
* Written 2003-2017 by Kevin Lamonte
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along
* with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "qcurses.h"
#include "common.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#ifdef Q_PDCURSES_WIN32
# include <windows.h>
#else
# include <signal.h>
# include <sys/ioctl.h>
# include <sys/poll.h>
# include <sys/wait.h>
# include <unistd.h>
/* Find the right header for forkpty() */
# ifdef __APPLE__
# include <util.h>
# else
# if defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__)
# include <sys/types.h>
# include <util.h>
# else
# include <pty.h>
# endif
# endif
#endif /* Q_PDCURSES_WIN32 */
#include <ctype.h>
#include "screen.h"
#include "qodem.h"
#include "options.h"
#include "states.h"
#include "dialer.h"
#include "console.h"
#include "forms.h"
#include "translate.h"
#include "keyboard.h"
#include "script.h"
/* Set this to a not-NULL value to enable debug log. */
/* static const char * DLOGNAME = "script"; */
static const char * DLOGNAME = NULL;
/**
* Status of the running script.
*/
Q_SCRIPT q_running_script;
/*
* Buffer of UTF-8 encoded printable characters to send to the script's
* stdin. This is space for at least 128 Unicode code points that could each
* take up to 4 bytes to encode.
*/
static char print_buffer[128 * 4];
static int print_buffer_n;
#ifdef Q_PDCURSES_WIN32
/*
* Win32 case: we have to create pipes that are connected to the script
* process' streams. These are initialized in spawn_process().
*/
HANDLE q_script_stdin = NULL;
HANDLE q_script_stdout = NULL;
HANDLE q_script_stderr = NULL;
HANDLE q_script_process = NULL;
HANDLE q_script_thread = NULL;
#endif
/*
* Time when the script process was spawned.
*/
static time_t script_start_time;
/*
* Script stdout buffer.
*/
static unsigned char stdout_buffer[Q_BUFFER_SIZE];
static int stdout_buffer_n = 0;
/*
* Script stderr buffer, both in the raw byte stream form and in the decoded
* wide char form.
*/
static unsigned char stderr_buffer[Q_BUFFER_SIZE];
static int stderr_buffer_n = 0;
static wchar_t stderr_utf8_buffer[Q_BUFFER_SIZE];
static int stderr_utf8_buffer_n = 0;
static struct q_scrolline_struct * stderr_lines = NULL;
static struct q_scrolline_struct * stderr_last = NULL;
/* UTF-8 decoder support */
uint32_t stdout_utf8_state;
uint32_t stderr_utf8_state;
/* The final return code retrieved when the script exited. */
int script_rc = -1;
/**
* Figure out the appropriate full and empty print buffer state exposed to
* the global script status.
*/
static void update_print_buffer_flags() {
if (print_buffer_n >= sizeof(print_buffer) - 4) {
/*
* No more room for another character, we are full
*/
q_running_script.print_buffer_full = Q_TRUE;
} else {
q_running_script.print_buffer_full = Q_FALSE;
}
if (print_buffer_n == 0) {
q_running_script.print_buffer_empty = Q_TRUE;
} else {
q_running_script.print_buffer_empty = Q_FALSE;
}
}
/**
* Called by print_character() in scrollback.c to pass printable characters
* to the running script's stdin.
*
* @param ch the character
*/
void script_print_character(const wchar_t ch) {
int rc;
if (q_running_script.paused == Q_TRUE) {
/*
* Drop characters when the script is paused.
*/
return;
}
if (q_running_script.running == Q_FALSE) {
/*
* Drop characters when the script is dead.
*/
print_buffer_n = 0;
update_print_buffer_flags();
return;
}
if (q_running_script.print_buffer_full == Q_TRUE) {
/*
* Drop characters when the print buffer is full.
*/
return;
}
/*
* Encode the character to UTF-8.
*/
rc = utf8_encode(ch, &print_buffer[print_buffer_n]);
print_buffer_n += rc;
/*
* Fix the full/empty flags
*/
update_print_buffer_flags();
}
/**
* Send a script message (a line from its stderr) to the log file.
*
* @param line the message from the script
*/
static void log_line(struct q_scrolline_struct * line) {
char buffer[Q_MAX_LINE_LENGTH + 1];
int n;
/*
* Terminate wcs string
*/
if (stderr_last->length == Q_MAX_LINE_LENGTH) {
stderr_last->length--;
}
stderr_last->chars[stderr_last->length] = 0;
n = wcstombs(NULL, stderr_last->chars, 0) + 1;
wcstombs(buffer, stderr_last->chars, n);
qlog(_("Script message: %s\n"), buffer);
}
/**
* Record whatever the script emitted to its stderr to our variable
* stderr_lines.
*/
static void print_stderr() {
struct q_scrolline_struct * new_line;
int i, j;
if (stderr_lines == NULL) {
/*
* Allocate first line
*/
new_line =
(struct q_scrolline_struct *)
Xmalloc(sizeof(struct q_scrolline_struct), __FILE__, __LINE__);
memset(new_line, 0, sizeof(struct q_scrolline_struct));
for (i = 0; i < Q_MAX_LINE_LENGTH; i++) {
new_line->chars[i] = ' ';
}
new_line->prev = NULL;
new_line->next = NULL;
stderr_lines = new_line;
stderr_last = stderr_lines;
}
for (i = 0; i < stderr_utf8_buffer_n; i++) {
if (((stderr_utf8_buffer[i] == '\r') ||
(stderr_utf8_buffer[i] == '\n') ||
(stderr_last->length == WIDTH)) && (stderr_last->length > 0)
) {
/*
* New line
*/
new_line =
(struct q_scrolline_struct *)
Xmalloc(sizeof(struct q_scrolline_struct), __FILE__, __LINE__);
memset(new_line, 0, sizeof(struct q_scrolline_struct));
for (j = 0; j < Q_MAX_LINE_LENGTH; j++) {
new_line->chars[j] = ' ';
}
new_line->prev = stderr_last;
new_line->next = NULL;
new_line->length = 0;
stderr_last->next = new_line;
log_line(stderr_last);
stderr_last = new_line;
/*
* Fall through...
*/
}
if ((stderr_utf8_buffer[i] != '\r') &&
(stderr_utf8_buffer[i] != '\n')) {
stderr_last->chars[stderr_last->length] = stderr_utf8_buffer[i];
stderr_last->length++;
}
}
/*
* Clear pending data
*/
stderr_utf8_buffer_n = 0;
/*
* Refresh
*/
q_screen_dirty = Q_TRUE;
}
/**
* Process raw bytes from the remote side through the script. See also
* console_process_incoming_data().
*
* @param input the bytes from the remote side
* @param input_n the number of bytes in input_n
* @param remaining the number of un-processed bytes that should be sent
* through a future invocation of script_process_data()
* @param output a buffer to contain the bytes to send to the remote side
* @param output_n the number of bytes that this function wrote to output
* @param output_max the maximum number of bytes this function may write to
* output
*/
void script_process_data(unsigned char * input, const unsigned int input_n,
int * remaining, unsigned char * output,
unsigned int * output_n,
const unsigned int output_max) {
char notify_message[DIALOG_MESSAGE_SIZE];
int rc = 0;
int n;
int i;
uint32_t utf8_char;
uint32_t last_utf8_state;
#ifdef Q_PDCURSES_WIN32
DWORD actual_bytes = 0;
DWORD bytes_read = 0;
#else
struct pollfd pfd;
#endif
Q_BOOL check_stderr = Q_FALSE;
DLOG(("script.c: buffer_full %s buffer_empty %s running %s paused %s\n",
(q_running_script.print_buffer_full == Q_TRUE ? "true" : "false"),
(q_running_script.print_buffer_empty == Q_TRUE ? "true" : "false"),
(q_running_script.running == Q_TRUE ? "true" : "false"),
(q_running_script.paused == Q_TRUE ? "true" : "false")));
DLOG(("script.c: stdin %s stdout %s buffer_n %d\n",
(q_running_script.stdin_writeable == Q_TRUE ? "true" : "false"),
(q_running_script.stdout_readable == Q_TRUE ? "true" : "false"),
print_buffer_n));
/*
* -----------------------------------------------------------------
* Dispatch things in print_buffer to script stdin
* -----------------------------------------------------------------
*/
if (q_running_script.stdin_writeable == Q_TRUE) {
/*
* Write the data to q_running_script.script_tty_fd
*/
#ifdef Q_PDCURSES_WIN32
if (print_buffer_n > 0) {
DWORD bytes_written = 0;
if (WriteFile(q_script_stdin, print_buffer,
print_buffer_n, &bytes_written, NULL) == TRUE) {
rc = bytes_written;
/*
* Force this sucker to flush
*/
FlushFileBuffers(q_script_stdin);
} else {
errno = GetLastError();
rc = -1;
}
} else {
/*
* NOP, pretend it's EAGAIN
*/
errno = EAGAIN;
rc = -1;
}
#else
rc = write(q_running_script.script_tty_fd, print_buffer,
print_buffer_n);
#endif
if (rc < 0) {
switch (errno) {
#ifdef Q_PDCURSES_WIN32
case ERROR_NO_DATA:
/*
* Other side is closing, give up here and the stdout read
* should return EOF.
*/
break;
#endif
case EAGAIN:
/*
* Outgoing buffer is full, wait for the next round.
*/
break;
default:
/*
* Uh-oh, error
*/
snprintf(notify_message, sizeof(notify_message),
_("Call to write() failed: %d %s"), errno,
strerror(errno));
notify_form(notify_message, 0);
return;
}
} else {
/*
* Hang onto the difference for the next round.
*/
assert(rc <= print_buffer_n);
if (rc < print_buffer_n) {
memmove(print_buffer, print_buffer + rc, print_buffer_n - rc);
}
print_buffer_n -= rc;
update_print_buffer_flags();
}
}
/*
* -----------------------------------------------------------------
* Read bytes from remote side, and pass through
* console_process_incoming_data()
* -----------------------------------------------------------------
*/
if ((input_n > 0) &&
(((q_running_script.print_buffer_full == Q_FALSE) &&
(q_running_script.running == Q_TRUE) &&
(q_running_script.paused == Q_FALSE)) ||
(q_running_script.running == Q_FALSE) ||
(q_running_script.paused == Q_TRUE))
) {
console_process_incoming_data(input, input_n, remaining);
}
/*
* -----------------------------------------------------------------
* Read bytes from script stderr, decode from UTF-8, and display on
* screen.
*
* Since stderr is a named pipe, poll it explicitly. select() and named
* pipes are broken on some platforms.
* -----------------------------------------------------------------
*/
#ifdef Q_PDCURSES_WIN32
if (PeekNamedPipe(q_script_stderr, NULL, 0, NULL, &actual_bytes,
NULL) == 0) {
/*
* Error peeking. It's either EOF or something else. Ignore it, the
* stdout check will catch the EOF.
*/
check_stderr = Q_FALSE;
} else {
if (actual_bytes > 0) {
/*
* Data is available for reading
*/
check_stderr = Q_TRUE;
} else {
/*
* Do not access stderr
*/
check_stderr = Q_FALSE;
}
}
#else
if (q_running_script.script_stderr_fd != -1) {
pfd.fd = q_running_script.script_stderr_fd;
pfd.events = POLLIN;
rc = poll(&pfd, 1, 0);
if (rc > 0) {
/*
* Data is available for reading
*/
check_stderr = Q_TRUE;
} else {
/*
* Do not access stderr
*/
check_stderr = Q_FALSE;
}
} else {
check_stderr = Q_FALSE;
}
#endif /* Q_PDCURSES_WIN32 */
if (check_stderr == Q_TRUE) {
n = Q_BUFFER_SIZE - stderr_buffer_n;
/*
* Make sure at least 4 bytes are available for 1 UTF-8 character
*/
if ((n > 0) && (output_max - *output_n > 4)) {
/*
* Clear errno
*/
errno = 0;
#ifdef Q_PDCURSES_WIN32
if (PeekNamedPipe(q_script_stderr, NULL, 0, NULL, &actual_bytes,
NULL) == 0) {
/*
* Error peeking
*/
rc = 0;
} else {
if (actual_bytes == 0) {
rc = 0;
} else if (actual_bytes > n) {
actual_bytes = n;
}
if (ReadFile(q_script_stderr,
stderr_buffer + stderr_buffer_n,
actual_bytes, &bytes_read, NULL) == TRUE) {
rc = bytes_read;
} else {
/*
* Error in read
*/
rc = 0;
}
}
#else
rc = read(q_running_script.script_stderr_fd,
stderr_buffer + stderr_buffer_n, n);
#endif /* Q_PDCURSES_WIN32 */
if (rc < 0) {
if (errno == EIO) {
/*
* This is EOF
*/
rc = 0;
} else {
qlog(_("Script stderr read() failed: %s\n"),
strerror(errno));
}
}
if (rc == 0) {
/*
* EOF - close stderr. Only the stdout EOF officially kills
* the script.
*/
#ifdef Q_PDCURSES_WIN32
CloseHandle(q_script_stderr);
q_script_stderr = NULL;
#else
close(q_running_script.script_stderr_fd);
q_running_script.script_stderr_fd = -1;
#endif
}
if (rc > 0) {
/*
* Record # of new bytes in
*/
stderr_buffer_n += rc;
}
} /* if ((n > 0) && (output_max - *output_n > 4)) */
last_utf8_state = stderr_utf8_state;
n = 0;
for (i = 0; i < stderr_buffer_n; i++) {
utf8_decode(&stderr_utf8_state, &utf8_char, stderr_buffer[i]);
n++;
if ((last_utf8_state == stderr_utf8_state) &&
(stderr_utf8_state != UTF8_ACCEPT)) {
/*
* Bad character, reset UTF8 decoder state
*/
stderr_utf8_state = 0;
/*
* Discard character
*/
continue;
}
if (stderr_utf8_state != UTF8_ACCEPT) {
/*
* Not enough characters to convert yet
*/
continue;
}
stderr_utf8_buffer[stderr_utf8_buffer_n] = (wchar_t) utf8_char;
stderr_utf8_buffer_n++;
if (stderr_utf8_buffer_n == Q_BUFFER_SIZE) {
break;
}
}
/*
* Discard processed bytes
*/
if (n < stderr_buffer_n) {
memmove(stderr_buffer, stderr_buffer + n, stderr_buffer_n - n);
}
stderr_buffer_n -= n;
print_stderr();
} /* if (check_stderr == Q_TRUE) */
/*
* -----------------------------------------------------------------
* Read bytes from script stdout, decode from UTF-8, and send to remote
* side
* -----------------------------------------------------------------
*/
if ((q_running_script.stdout_readable == Q_TRUE) &&
(q_running_script.paused == Q_FALSE)) {
n = Q_BUFFER_SIZE - stdout_buffer_n;
/*
* Make sure at least 4 bytes are available for 1 UTF-8 character
*/
if ((n > 0) && (output_max - *output_n > 4)) {
/*
* Clear errno
*/
errno = 0;
#ifdef Q_PDCURSES_WIN32
if (PeekNamedPipe(q_script_stdout, NULL, 0, NULL, &actual_bytes,
NULL) == 0) {
/*
* Error peeking
*/
rc = 0;
} else {
if (actual_bytes == 0) {
rc = 0;
} else if (actual_bytes > n) {
actual_bytes = n;
}
if (ReadFile(q_script_stdout,
stdout_buffer + stdout_buffer_n,
actual_bytes, &bytes_read, NULL) == TRUE) {
rc = bytes_read;
} else {
/*
* Error in read
*/
rc = 0;
}
}
#else
rc = read(q_running_script.script_tty_fd,
stdout_buffer + stdout_buffer_n, n);
#endif /* Q_PDCURSES_WIN32 */
DLOG(("read() rc: %d\n", rc));
for (i = 0; i < rc; i++) {
DLOG((" %c\n", stdout_buffer[stdout_buffer_n + i]));
}
if (rc < 0) {
if (errno == EIO) {
/*
* This is EOF
*/
rc = 0;
} else {
qlog(_("Script stdout read() failed: %s"), strerror(errno));
}
}
if (rc == 0) {
/*
* EOF
*/
script_stop();
return;
}
if (rc > 0) {
/*
* Record # of new bytes in
*/
stdout_buffer_n += rc;
}
} /* if ((n > 0) && (output_max - *output_n > 4)) */
/*
* Decode UTF-8 and post to remote side
*/
last_utf8_state = stdout_utf8_state;
n = 0;
for (i = 0; i < stdout_buffer_n; i++) {
utf8_decode(&stdout_utf8_state, &utf8_char, stdout_buffer[i]);
n++;
if ((last_utf8_state == stdout_utf8_state)
&& (stdout_utf8_state != UTF8_ACCEPT)) {
/*
* Bad character, reset UTF8 decoder state
*/
stdout_utf8_state = 0;
/*
* Discard character
*/
continue;
}
if (stdout_utf8_state != UTF8_ACCEPT) {
/*
* Not enough characters to convert yet
*/
continue;
}
switch (q_status.emulation) {
case Q_EMUL_TTY:
case Q_EMUL_ANSI:
case Q_EMUL_AVATAR:
case Q_EMUL_PETSCII:
case Q_EMUL_ATASCII:
case Q_EMUL_DEBUG:
case Q_EMUL_VT52:
case Q_EMUL_VT100:
case Q_EMUL_VT102:
case Q_EMUL_VT220:
case Q_EMUL_LINUX:
case Q_EMUL_XTERM:
/*
* 8-bit emulations: try to backmap to the codepage,
* including allowing Unicode synonyms.
*/
output[*output_n] = translate_unicode_to_8bit(
(wchar_t) utf8_char, q_status.codepage);
/*
* 8-bit emulations
*/
output[*output_n] = (unsigned char) (utf8_char & 0xFF);
rc = 1;
break;
case Q_EMUL_LINUX_UTF8:
case Q_EMUL_XTERM_UTF8:
/*
* UTF-8 emulations: encode outbound "keystroke", after
* running it through the direct Unicode translation map.
*/
utf8_char = translate_unicode_out((wchar_t) utf8_char);
/*
* Now re-encode on the wire.
*/
rc = utf8_encode((wchar_t) utf8_char,
(char *) &output[*output_n]);
break;
}
*output_n += rc;
if (output_max - *output_n < 4) {
/*
* No room for more characters
*/
break;
}
}
/*
* Discard processed bytes
*/
if (n < stdout_buffer_n) {
memmove(stdout_buffer, stdout_buffer + n, stdout_buffer_n - n);
}
stdout_buffer_n -= n;
}
}
/**
* Spawn a new script process and start it.
*
* @param script_filename the filename to execute
*/
void script_start(const char * script_filename) {
#ifdef Q_PDCURSES_WIN32
PROCESS_INFORMATION process_info;
STARTUPINFOA startup_info;
HANDLE q_script_stdin_2 = NULL;
HANDLE q_script_stdout_2 = NULL;
HANDLE q_script_stderr_2 = NULL;
SECURITY_ATTRIBUTES security_attr;
char buffer[32];
int columns;
DWORD pipe_flags = PIPE_NOWAIT;
#else
char path_string[COMMAND_LINE_SIZE];
Q_BOOL use_stderr = Q_TRUE;
char * stderr_filename;
char * env_string = getenv("HOME");
pid_t child_pid = -1;
char ** target_argv;
char ttyname_buffer[FILENAME_SIZE];
#endif
char command_line[COMMAND_LINE_SIZE];
struct q_scrolline_struct * line;
struct q_scrolline_struct * line2;
assert(q_status.read_only == Q_FALSE);
qlog(_("Executing script %s...\n"), script_filename);
/*
* Initial state
*/
q_running_script.running = Q_FALSE;
q_running_script.paused = Q_FALSE;
#ifdef Q_PDCURSES_WIN32
assert(q_script_stdin == NULL);
assert(q_script_stdout == NULL);
assert(q_script_stderr == NULL);
assert(q_script_process == NULL);
assert(q_script_thread == NULL);
#else
q_running_script.script_pid = -1;
q_running_script.script_tty_fd = -1;
q_running_script.script_tty_name = NULL;
q_running_script.script_stderr_fd = -1;
#endif
/*
* Script filename needs to be on the display even after script_stop()
* has been called, so reset it here.
*/
if (q_running_script.filename != NULL) {
Xfree(q_running_script.filename, __FILE__, __LINE__);
}
q_running_script.filename = Xstrdup(script_filename, __FILE__, __LINE__);
q_running_script.stdin_writeable = Q_FALSE;
q_running_script.stdout_readable = Q_FALSE;
memset(print_buffer, 0, sizeof(print_buffer));
print_buffer_n = 0;
update_print_buffer_flags();
#ifndef Q_PDCURSES_WIN32
/*
* Open stderr
*/
stderr_filename =
substitute_string(get_option(Q_OPTION_SCRIPTS_STDERR_FIFO), "$HOME",
env_string);
if (access(stderr_filename, F_OK) != 0) {
use_stderr = Q_FALSE;
}
#endif
/*
* Clear stderr output window lines
*/
line = stderr_lines;
while (line != NULL) {
line2 = line;
line = line2->next;
Xfree(line2, __FILE__, __LINE__);
}
stderr_lines = line;
stderr_last = stderr_lines;
#ifdef Q_PDCURSES_WIN32
/*
* Modeled after example on MSDN:
* http://msdn.microsoft.com/en-us/library/ms682499%28v=VS.85%29.aspx
*/
security_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
security_attr.bInheritHandle = TRUE;
security_attr.lpSecurityDescriptor = NULL;
/*
* Create pipes as needed to communicate with script process
*/
if (!CreatePipe(&q_script_stdout, &q_script_stdout_2, &security_attr, 0)) {
#ifdef DEBUG_SPAWNPROCESS
fprintf(stderr, "CreatePipe() 1 failed: %d %sn",
GetLastError(), strerror(GetLastError()));
fflush(stderr);
#endif
/*
* Error, bail out
*/
return;
}
if (!SetHandleInformation(q_script_stdout, HANDLE_FLAG_INHERIT, 0)) {
/*
* Error, bail out
*/
CloseHandle(q_script_stdout);
CloseHandle(q_script_stdout_2);
q_script_stdout = NULL;
q_script_stdout_2 = NULL;
#ifdef DEBUG_SPAWNPROCESS
fprintf(stderr, "SetHandleInformation() 1 failed: %d %sn",
GetLastError(), strerror(GetLastError()));
fflush(stderr);
#endif
return;
}
if (!CreatePipe(&q_script_stderr, &q_script_stderr_2, &security_attr, 0)) {
#ifdef DEBUG_SPAWNPROCESS
fprintf(stderr, "CreatePipe() 2 failed: %d %sn",
GetLastError(), strerror(GetLastError()));
fflush(stderr);
#endif
/*
* Error, bail out
*/
return;
}
if (!SetHandleInformation(q_script_stderr, HANDLE_FLAG_INHERIT, 0)) {
/*
* Error, bail out
*/
CloseHandle(q_script_stdout);
CloseHandle(q_script_stdout_2);
q_script_stdout = NULL;
q_script_stdout_2 = NULL;
CloseHandle(q_script_stderr);
CloseHandle(q_script_stderr_2);
q_script_stderr = NULL;
q_script_stderr_2 = NULL;
#ifdef DEBUG_SPAWNPROCESS
fprintf(stderr, "SetHandleInformation() 2 failed: %d %sn",
GetLastError(), strerror(GetLastError()));
fflush(stderr);
#endif
return;
}
/*
* This stdin must NOT be bufferred.
*/
if (!CreatePipe(&q_script_stdin_2, &q_script_stdin, &security_attr, 0)) {
#ifdef DEBUG_SPAWNPROCESS
fprintf(stderr, "CreatePipe() 3 failed: %d %sn",
GetLastError(), strerror(GetLastError()));
fflush(stderr);
#endif
/*
* Error, bail out
*/
return;
}
if (!SetHandleInformation(q_script_stdin, HANDLE_FLAG_INHERIT, 0)) {
/*
* Error, bail out
*/
CloseHandle(q_script_stdout);
CloseHandle(q_script_stdout_2);
q_script_stdout = NULL;
q_script_stdout_2 = NULL;
CloseHandle(q_script_stderr);
CloseHandle(q_script_stderr_2);
q_script_stderr = NULL;
q_script_stderr_2 = NULL;
CloseHandle(q_script_stdin);
CloseHandle(q_script_stdin_2);
q_script_stdin = NULL;
q_script_stdin_2 = NULL;
#ifdef DEBUG_SPAWNPROCESS
fprintf(stderr, "SetHandleInformation() 3 failed: %d %sn",
GetLastError(), strerror(GetLastError()));
fflush(stderr);
#endif
return;
}
/*
* Don't block on writes to q_script_stdin
*/
if (SetNamedPipeHandleState(q_script_stdin, &pipe_flags, NULL, NULL) ==
FALSE) {
/*
* Error, bail out
*/
CloseHandle(q_script_stdout);
CloseHandle(q_script_stdout_2);
q_script_stdout = NULL;
q_script_stdout_2 = NULL;
CloseHandle(q_script_stderr);
CloseHandle(q_script_stderr_2);
q_script_stderr = NULL;
q_script_stderr_2 = NULL;
CloseHandle(q_script_stdin);
CloseHandle(q_script_stdin_2);
q_script_stdin = NULL;
q_script_stdin_2 = NULL;
#ifdef DEBUG_SPAWNPROCESS
fprintf(stderr, "SetNamedPipeHandleState() 3 failed: %d %sn",
GetLastError(), strerror(GetLastError()));
fflush(stderr);
#endif
return;
}
/*
* Set my TERM variable
*/
if (strlen(emulation_term(q_status.emulation)) > 0) {
SetEnvironmentVariableA("TERM", emulation_term(q_status.emulation));
}
/*