-
Notifications
You must be signed in to change notification settings - Fork 17
/
vmbiftio.cpp
2569 lines (2174 loc) · 71.8 KB
/
vmbiftio.cpp
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
#ifdef RCSID
static char RCSid[] =
"$Header$";
#endif
/*
* Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
vmbiftio.cpp - TADS Input/Output functions
Function
Notes
Modified
02/08/00 MJRoberts - Creation
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "t3std.h"
#include "os.h"
#include "utf8.h"
#include "charmap.h"
#include "vmbiftio.h"
#include "vmstack.h"
#include "vmerr.h"
#include "vmerrnum.h"
#include "vmglob.h"
#include "vmpool.h"
#include "vmobj.h"
#include "vmstr.h"
#include "vmlst.h"
#include "vmrun.h"
#include "vmfile.h"
#include "vmconsol.h"
#include "vmstrres.h"
#include "vmvsn.h"
#include "vmhost.h"
#include "vmpredef.h"
#include "vmcset.h"
#include "vmfilobj.h"
#include "vmfilnam.h"
#include "vmnetfil.h"
#include "vmnet.h"
/* ------------------------------------------------------------------------ */
/*
* Display a value
*/
void CVmBifTIO::say(VMG_ uint argc)
{
/* write the value to the main console */
say_to_console(vmg_ G_console, argc);
}
/*
* Display the value or values at top of stack to the given console
*/
void CVmBifTIO::say_to_console(VMG_ CVmConsole *console, uint argc)
{
vm_val_t val;
const char *str;
char buf[30];
size_t len;
vm_val_t new_str;
/* presume we won't need to create a new string value */
new_str.set_nil();
/* display each argument */
for ( ; argc != 0 ; --argc)
{
/* get our argument */
G_stk->pop(&val);
/* see what we have */
switch(val.typ)
{
case VM_SSTRING:
/* get the string */
str = G_const_pool->get_ptr(val.val.ofs);
/* the original value is our string */
new_str = val;
disp_str:
/* push the string to protect from garbage collection */
G_stk->push(&new_str);
/* display the string through the output formatter */
console->format_text(vmg_ str + 2, osrp2(str));
/* discard the saved string now that we no longer need it */
G_stk->discard();
/* done */
break;
case VM_OBJ:
/* convert it to a string */
str = vm_objp(vmg_ val.val.obj)
->cast_to_string(vmg_ val.val.obj, &new_str);
/* go display it */
goto disp_str;
case VM_LIST:
/* convert to a string */
str = val.cast_to_string(vmg_ &new_str);
goto disp_str;
case VM_INT:
/* convert it to a string */
sprintf(buf + 2, "%ld", (long)val.val.intval);
/* set its length */
len = strlen(buf + 2);
oswp2(buf, len);
/* display it */
str = buf;
goto disp_str;
case VM_NIL:
/* display nothing */
break;
default:
/* other types are invalid */
err_throw(VMERR_BAD_TYPE_BIF);
}
}
}
/* ------------------------------------------------------------------------ */
/*
* Logging - turn on or off output text capture
*/
#define LOG_SCRIPT 1
#define LOG_CMD 2
#define LOG_EVENT 3
void CVmBifTIO::logging(VMG_ uint argc)
{
/* presume success */
int ok = TRUE;
/* check arguments */
check_argc_range(vmg_ argc, 1, 2);
/* get the arguments: filename, log type */
const vm_val_t *filespec = G_stk->get(0);
int log_type = (argc >= 2 ? G_stk->get(1)->num_to_int(vmg0_) : LOG_SCRIPT);
/*
* if they passed us nil, turn off logging; otherwise, start logging
* to the filename given by the string
*/
if (filespec->typ == VM_NIL)
{
/* turn off the appropriate type of logging */
switch(log_type)
{
case LOG_SCRIPT:
G_console->close_log_file(vmg0_);
break;
case LOG_CMD:
case LOG_EVENT:
G_console->close_command_log(vmg0_);
break;
default:
err_throw(VMERR_BAD_VAL_BIF);
}
}
else
{
/* set up the recursive call context */
vm_rcdesc rc(vmg_ "setLogFile", bif_table, 1,
G_stk->get(0), argc);
/* open the appropriate log file */
switch(log_type)
{
case LOG_SCRIPT:
ok = !G_console->open_log_file(vmg_ filespec, &rc);
break;
case LOG_CMD:
case LOG_EVENT:
ok = !G_console->open_command_log(
vmg_ filespec, &rc, log_type == LOG_EVENT);
break;
default:
err_throw(VMERR_BAD_VAL_BIF);
}
}
/* discard arguments */
G_stk->discard(argc);
/* return true on success, nil on error */
retval_bool(vmg_ ok);
}
/* ------------------------------------------------------------------------ */
/*
* clearscreen - clear the main display screen
*/
void CVmBifTIO::clearscreen(VMG_ uint argc)
{
/* check arguments */
check_argc(vmg_ argc, 0);
/* ask the console to clear the screen */
G_console->clear_window(vmg0_);
}
/* ------------------------------------------------------------------------ */
/*
* more - show MORE prompt
*/
void CVmBifTIO::more(VMG_ uint argc)
{
/* check arguments */
check_argc(vmg_ argc, 0);
/* ignore in web host mode */
if (G_net_config != 0)
return;
/*
* if we're reading from a script, ignore this request - these types of
* interactive pauses are irrelevant when reading a script, since we're
* getting our input non-interactively
*/
if (G_console->is_reading_script())
return;
/* flush the display output */
G_console->flush_all(vmg_ VM_NL_NONE);
/* show the MORE prompt */
G_console->show_more_prompt(vmg0_);
}
/* ------------------------------------------------------------------------ */
/*
* input - get a line of input from the keyboard
*/
void CVmBifTIO::input(VMG_ uint argc)
{
/* check arguments */
check_argc(vmg_ argc, 0);
/* read a line of text from the keyboard */
char buf[256];
if (G_console->read_line(vmg_ buf, sizeof(buf)))
{
/* end of file - return nil */
retval_nil(vmg0_);
}
else
{
/* return the string */
retval_str(vmg_ buf);
}
}
/* ------------------------------------------------------------------------ */
/*
* inputkey - read a keystroke
*/
void CVmBifTIO::inputkey(VMG_ uint argc)
{
char buf[32];
char c[10];
size_t len;
int evt;
static const int filter[] = { OS_EVT_KEY };
/* check arguments */
check_argc(vmg_ argc, 0);
/* check for script input */
if (G_console->read_event_script(
vmg_ &evt, buf, sizeof(buf),
filter, sizeof(filter)/sizeof(filter[0]), 0))
{
/* we got a key from the script */
retval_ui_str(vmg_ buf);
/* log the event */
G_console->log_event(vmg_ OS_EVT_KEY, buf, strlen(buf), FALSE);
/* done */
return;
}
/* flush any output */
G_console->flush_all(vmg_ VM_NL_INPUT);
/* there's no console in web host mode - simply return eof */
if (G_net_config != 0)
{
retval_ui_str(vmg_ "[eof]");
return;
}
/* get a keystroke */
c[0] = (char)os_getc_raw();
len = 1;
/* if it's an extended key, map it specially */
if (c[0] == 0)
{
char extc;
/* get the second part of the sequence */
extc = (char)os_getc_raw();
/* map the extended key */
map_ext_key(vmg_ buf, (unsigned char)extc);
}
else
{
/* continue fetching bytes until we have a full character */
while (!raw_key_complete(vmg_ c, len) && len + 1 < sizeof(c))
{
/*
* We don't yet have enough bytes for a complete character, so
* read another raw byte. The keyboard driver should already
* have queued up all of the bytes we need to complete the
* character sequence, so there should never be a delay from
* os_getc_raw() here - it should simply return the next byte
* of the sequence immediately.
*/
c[len++] = (char)os_getc_raw();
}
c[len] = '\0';
/*
* translate the key from the local character set to UTF-8, and map
* the extended key code to the portable representation
*/
map_raw_key(vmg_ buf, c, len);
}
/* reset the [MORE] counter */
G_console->reset_line_count(FALSE);
/* log the event */
G_console->log_event(vmg_ OS_EVT_KEY, buf, strlen(buf), TRUE);
/* return the string */
retval_str(vmg_ buf);
}
/* ------------------------------------------------------------------------ */
/*
* inputevent - read an event
*/
void CVmBifTIO::inputevent(VMG_ uint argc)
{
int use_timeout;
unsigned long timeout;
os_event_info_t info;
int evt;
int ele_count;
vm_obj_id_t lst_obj;
CVmObjList *lst;
char keyname[32];
vm_val_t val;
int from_script = FALSE;
static const int filter[] =
{
OS_EVT_KEY, OS_EVT_TIMEOUT, OS_EVT_NOTIMEOUT,
OS_EVT_HREF, OS_EVT_EOF, OS_EVT_COMMAND
};
/* check arguments */
check_argc_range(vmg_ argc, 0, 1);
/* if there's a timeout argument, get it */
if (argc == 0)
{
/* there's no timeout */
use_timeout = FALSE;
timeout = 0;
}
else if (G_stk->get(0)->typ == VM_NIL)
{
/* the timeout is nil, which is the same as no timeout */
use_timeout = FALSE;
timeout = 0;
/* discard the nil timeout value */
G_stk->discard();
}
else
{
/* pop the timeout value */
timeout = pop_long_val(vmg0_);
/* note that we have a timeout to use */
use_timeout = TRUE;
}
/* check for script input */
if (G_console->read_event_script(
vmg_ &evt, info.href, sizeof(info.href),
filter, sizeof(filter)/sizeof(filter[0]), 0))
{
/* we got a script event - note it */
from_script = TRUE;
/* translate certain events */
switch (evt)
{
case OS_EVT_COMMAND:
/* read the numeric parameter */
info.cmd_id = atoi(info.href);
break;
}
}
else if (G_net_config != 0)
{
/* there's no console in web host mode - return eof */
evt = OS_EVT_EOF;
}
else
{
/* flush any buffered output */
G_console->flush_all(vmg_ VM_NL_INPUT);
/* reset the [MORE] counter */
G_console->reset_line_count(FALSE);
/* read an event from the OS layer */
evt = os_get_event(timeout, use_timeout, &info);
}
/* figure out how big a list we need to allocate */
switch(evt)
{
case OS_EVT_KEY:
/*
* we need two elements - one for the event type code, one for the
* keystroke string
*/
ele_count = 2;
break;
case OS_EVT_COMMAND:
/* we need a second element for the command ID */
ele_count = 2;
break;
case OS_EVT_HREF:
/*
* we need two elements - one for the event type code, one for the
* HREF string
*/
ele_count = 2;
break;
default:
/* for anything else, we need only the event type code element */
ele_count = 1;
break;
}
/* create the return list */
lst_obj = CVmObjList::create(vmg_ FALSE, ele_count);
lst = (CVmObjList *)vm_objp(vmg_ lst_obj);
lst->cons_clear();
/* save the list on the stack to protect against garbage collection */
val.set_obj(lst_obj);
G_stk->push(&val);
/* fill in the first element with the event type code */
val.set_int(evt);
lst->cons_set_element(0, &val);
/* set additional elements, according to the event type */
switch(evt)
{
case OS_EVT_KEY:
/* map the extended or ordinary key, as appropriate */
if (from_script)
{
/* we got a key from the script - it's in the 'href' field */
val.set_obj(str_from_ui_str(vmg_ info.href));
/* log the event */
G_console->log_event(
vmg_ OS_EVT_KEY, info.href, strlen(info.href), FALSE);
}
else if (info.key[0] == 0)
{
/* it's an extended key */
map_ext_key(vmg_ keyname, info.key[1]);
/* create a string for the key name */
val.set_obj(CVmObjString::create(
vmg_ FALSE, keyname, strlen(keyname)));
/* log the event */
G_console->log_event(
vmg_ OS_EVT_KEY, keyname, strlen(keyname), TRUE);
}
else
{
char c[4];
size_t len;
/* fetch more bytes until we have a complete character */
for (c[0] = (char)info.key[0], len = 1 ;
!raw_key_complete(vmg_ c, len) && len < sizeof(c) ; )
{
/*
* Read another input event. The keyboard driver should
* already have queued up all of the bytes needed to
* complete this character sequence, so there should never
* be a delay from os_get_event() here - it should simply
* return immediately with another OS_EVT_KEY event with
* the next byte of the sequence.
*/
evt = os_get_event(0, FALSE, &info);
/*
* if it's not a keystroke event, something's wrong -
* ignore the event and stop trying to read the remaining
* bytes of the character sequence
*/
if (evt != OS_EVT_KEY)
break;
/* store the next byte of the sequence */
c[len++] = (char)info.key[0];
}
/* it's an ordinary key - map it */
map_raw_key(vmg_ keyname, c, len);
/* create a string for the key name */
val.set_obj(CVmObjString::create(
vmg_ FALSE, keyname, strlen(keyname)));
/* log the event */
G_console->log_event(
vmg_ OS_EVT_KEY, keyname, strlen(keyname), TRUE);
}
/* add it to the list */
lst->cons_set_element(1, &val);
break;
case OS_EVT_HREF:
/* create the string for the href text */
val.set_obj(str_from_ui_str(vmg_ info.href));
/* add it to the list */
lst->cons_set_element(1, &val);
/* log it */
G_console->log_event(vmg_ OS_EVT_HREF,
info.href, strlen(info.href), FALSE);
break;
case OS_EVT_COMMAND:
/* the second element is the command ID code */
val.set_int(info.cmd_id);
lst->cons_set_element(1, &val);
/* log it */
{
char buf[20];
sprintf(buf, "%d", info.cmd_id);
G_console->log_event(vmg_ OS_EVT_COMMAND,
buf, strlen(buf), TRUE);
}
break;
default:
/* other event types have no extra data */
G_console->log_event(vmg_ evt);
break;
}
/* return the list */
retval_obj(vmg_ lst_obj);
/* we can drop the garbage collection protection now */
G_stk->discard();
}
/* ------------------------------------------------------------------------ */
/*
* Service routine: Map an "extended" keystroke from raw os_getc_raw()
* notation to a UTF-8 key name. The caller should pass the second byte of
* the extended two-byte raw sequence.
*/
int CVmBifTIO::map_ext_key(VMG_ char *namebuf, int extc)
{
/*
* Portable key names for the extended keystrokes. We map the extended
* key codes to these strings, so that the TADS code can access arrow
* keys and the like.
*/
static const char *ext_key_names[] =
{
"[up]", /* CMD_UP - 1 */
"[down]", /* CMD_DOWN - 2 */
"[right]", /* CMD_RIGHT - 3 */
"[left]", /* CMD_LEFT - 4 */
"[end]", /* CMD_END - 5 */
"[home]", /* CMD_HOME - 6 */
"[del-eol]", /* CMD_DEOL - 7 */
"[del-line]", /* CMD_KILL - 8 */
"[del]", /* CMD_DEL - 9 */
"[scroll]", /* CMD_SCR - 10 */
"[page up]", /* CMD_PGUP - 11 */
"[page down]", /* CMD_PGDN - 12 */
"[top]", /* CMD_TOP - 13 */
"[bottom]", /* CMD_BOT - 14 */
"[f1]", /* CMD_F1 - 15 */
"[f2]", /* CMD_F2 - 16 */
"[f3]", /* CMD_F3 - 17 */
"[f4]", /* CMD_F4 - 18 */
"[f5]", /* CMD_F5 - 19 */
"[f6]", /* CMD_F6 - 20 */
"[f7]", /* CMD_F7 - 21 */
"[f8]", /* CMD_F8 - 22 */
"[f9]", /* CMD_F9 - 23 */
"[f10]", /* CMD_F10 - 24 */
"[?]", /* invalid key - CMD_CHOME - 25 */
"[tab]", /* CMD_TAB - 26 */
"[?]", /* invalid key - shift-F2 - 27 */
"[?]", /* not used (obsoleted) - 28 */
"[word-left]", /* CMD_WORD_LEFT - 29 */
"[word-right]", /* CMD_WORD_RIGHT - 30 */
"[del-word]", /* CMD_WORDKILL - 31 */
"[eof]", /* CMD_EOF - 32 */
"[break]", /* CMD_BREAK - 33 */
"[insert]" /* CMD_INS - 34 */
};
/* if it's in the key name array, use the array entry */
if (extc >= 1
&& extc <= (int)sizeof(ext_key_names)/sizeof(ext_key_names[0]))
{
/* use the array name */
strcpy(namebuf, ext_key_names[extc - 1]);
return TRUE;
}
/* if it's in the ALT key range, generate an ALT key name */
if ((unsigned char)extc >= CMD_ALT && (unsigned char)extc <= CMD_ALT + 25)
{
/* generate an ALT key name */
strcpy(namebuf, "[alt-?]");
namebuf[5] = (char)(extc - CMD_ALT + 'a');
return TRUE;
}
/* it's not a valid key - use '[?]' as the name */
strcpy(namebuf, "[?]");
return FALSE;
}
/*
* Service routine: Map a keystroke from the raw notation, consisting of a
* normal keystroke in the local character set or an extended command key
* using a CMD_xxx code, to UTF-8. If the keystroke is a control character
* or any CMD_xxx code, we'll map the key to a high-level keystroke name
* enclosed in square brackets.
*/
int CVmBifTIO::map_raw_key(VMG_ char *namebuf, const char *c, size_t len)
{
size_t outlen;
/* if it's a control character, give it a portable key name */
if (len == 1 && ((c[0] >= 1 && c[0] <= 27) || c[0] == 127))
{
switch(c[0])
{
case 10:
case 13:
/*
* return an ASCII 10 (regardless of local newline conventions
* - this is the internal string representation, which we
* define to use ASCII 10 to represent a newline everywhere)
*/
namebuf[0] = 10;
namebuf[1] = '\0';
return TRUE;
case 9:
/* return ASCII 9 for TAB characters */
namebuf[0] = 9;
namebuf[1] = '\0';
return TRUE;
case 8:
case 127:
/* return '[bksp]' for backspace/del characters */
strcpy(namebuf, "[bksp]");
return TRUE;
case 27:
/* return '[esc]' for the escape key */
strcpy(namebuf, "[esc]");
return TRUE;
default:
/* return '[ctrl-X]' for other control characters */
strcpy(namebuf, "[ctrl-?]");
namebuf[6] = (char)(c[0] + 'a' - 1);
return TRUE;
}
}
/* map the character to wide Unicode */
outlen = 32;
G_cmap_from_ui->map(&namebuf, &outlen, c, len);
/* null-terminate the result */
*namebuf = '\0';
/* successfully mapped */
return TRUE;
}
/*
* Service routine: determine if a raw byte sequence forms a complete
* character in the local character set.
*/
int CVmBifTIO::raw_key_complete(VMG_ const char *c, size_t len)
{
/* ask the local character mapper if it's a complete character */
return G_cmap_from_ui->is_complete_char(c, len);
}
/* ------------------------------------------------------------------------ */
/*
* Standard system button labels for bifinpdlg()
*/
#define BIFINPDLG_LBL_OK 1
#define BIFINPDLG_LBL_CANCEL 2
#define BIFINPDLG_LBL_YES 3
#define BIFINPDLG_LBL_NO 4
/*
* inputdialog - run a dialog
*/
void CVmBifTIO::inputdialog(VMG_ uint argc)
{
int icon_id;
char prompt[256];
char label_buf[256];
vm_val_t label_val[10];
const char *labels[10];
int lst_cnt;
int std_btns;
int btn_cnt;
char *dst;
size_t dstrem;
int default_resp;
int cancel_resp;
int resp;
char numbuf[32];
/* check arguments */
check_argc(vmg_ argc, 5);
/* get the icon number */
icon_id = pop_int_val(vmg0_);
/* get the prompt string */
pop_str_val_ui(vmg_ prompt, sizeof(prompt));
/* there aren't any buttons yet */
btn_cnt = 0;
/* check for the button type */
if (G_stk->get(0)->typ == VM_INT)
{
/* get the standard button set ID */
std_btns = pop_int_val(vmg0_);
}
else if (G_stk->get(0)->is_listlike(vmg0_)
&& (lst_cnt = G_stk->get(0)->ll_length(vmg0_)) >= 0)
{
int i;
vm_val_t *valp;
/* we're not using any standard button set */
std_btns = 0;
/*
* run through the list and get the button items into our array
* (we do this rather than traversing the list directly so that
* we don't have to worry about a constant list's data being
* paged out)
*/
vm_val_t *lst = G_stk->get(0);
/* limit the number of elements to our private array size */
if (lst_cnt > (int)sizeof(label_val)/sizeof(label_val[0]))
lst_cnt = sizeof(label_val)/sizeof(label_val[0]);
/* copy the list */
for (i = 1, valp = label_val ; i <= lst_cnt ; ++i, ++valp)
lst->ll_index(vmg_ valp, i);
/* done with the list - discard it */
G_stk->discard();
/* set up to write into our label buffer */
dst = label_buf;
dstrem = sizeof(label_buf);
/* now build our internal button list from the array elements */
for (i = 0, valp = label_val ; i < lst_cnt ; ++i, ++valp)
{
const char *p;
/*
* We could have a number or a string in each element. If
* the element is a number, it refers to a standard label.
* If it's a string, use the string directly.
*/
if ((p = valp->get_as_string(vmg0_)) != 0)
{
size_t copy_len;
/*
* it's a string - make a copy in the label buffer,
* making sure to leave space for null termination
*/
copy_len = vmb_get_len(p);
if (copy_len > dstrem - 1)
copy_len = utf8_ptr::s_trunc(p + VMB_LEN, dstrem - 1);
memcpy(dst, p + VMB_LEN, copy_len);
/* null-terminate the buffer */
dst[copy_len++] = '\0';
/* set this button to point to the converted text */
labels[btn_cnt++] = dst;
/* skip past this label */
dst += copy_len;
dstrem -= copy_len;
}
else if (valp->typ == VM_INT)
{
int id;
int resid;
char rscbuf[128];
/* it's a standard system label ID - get the ID */
id = (int)valp->val.intval;
/* translate it to the appropriate string resource */
switch(id)
{
case BIFINPDLG_LBL_OK:
resid = VMRESID_BTN_OK;
break;
case BIFINPDLG_LBL_CANCEL:
resid = VMRESID_BTN_CANCEL;
break;
case BIFINPDLG_LBL_YES:
resid = VMRESID_BTN_YES;
break;
case BIFINPDLG_LBL_NO:
resid = VMRESID_BTN_NO;
break;
default:
resid = 0;
break;
}
/*
* if we got a valid resource ID, load the resource;
* otherwise, skip this button
*/
if (resid != 0
&& !os_get_str_rsc(resid, rscbuf, sizeof(rscbuf)))
{
/* set this button to point to the converted text */
labels[btn_cnt++] = dst;
/* convert the resource text to UTF-8 */
G_cmap_from_ui->map(&dst, &dstrem,
rscbuf, strlen(rscbuf));
/* null-terminate the converted text */
*dst++ = '\0';
--dstrem;
}
}
}
}
else
{
/* invalid button type */
err_throw(VMERR_BAD_TYPE_BIF);
}
/* get the default response */
if (G_stk->get(0)->typ == VM_NIL)
{
/* discard the nil argument */
G_stk->discard();
/* there's no default response */
default_resp = 0;
}
else
{
/* get the default response index */
default_resp = pop_int_val(vmg0_);
}
/* get the cancel response */
if (G_stk->get(0)->typ == VM_NIL)
{
/* discard the nil argument */
G_stk->discard();
/* there's no cancel response */
cancel_resp = 0;
}
else
{
/* get the cancel response index */
cancel_resp = pop_int_val(vmg0_);
}
/* check for script input */
static int filter[] = { VMCON_EVT_DIALOG };
int evt;
if (G_console->read_event_script(
vmg_ &evt, numbuf, sizeof(numbuf),
filter, sizeof(filter)/sizeof(filter[0]), 0))
{
/* we got a script response - no need to show the dialog */
resp = atoi(numbuf);
/* log the event */
G_console->log_event(vmg_ VMCON_EVT_DIALOG,
numbuf, strlen(numbuf), FALSE);
}
else if (G_net_config != 0)
{
/* there's no console in web host mode - return eof */
resp = 0;
}
else
{
/* flush output before showing the dialog */
G_console->flush_all(vmg_ VM_NL_INPUT);
/* show the dialog */
resp = G_console->input_dialog(vmg_ icon_id, prompt,
std_btns, labels, btn_cnt,
default_resp, cancel_resp);
/* log the event */
sprintf(numbuf, "%d", resp);
G_console->log_event(vmg_ VMCON_EVT_DIALOG,
numbuf, strlen(numbuf), TRUE);
}
/* return the result */
retval_int(vmg_ resp);
}
/* ------------------------------------------------------------------------ */