1 |
/* |
/* |
2 |
* Copyright (C) 2003-2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2003-2006 Anders Gavare. All rights reserved. |
3 |
* |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
* modification, are permitted provided that the following conditions are met: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: console.c,v 1.8 2005/05/29 17:51:45 debug Exp $ |
* $Id: console.c,v 1.16 2006/05/05 21:28:09 debug Exp $ |
29 |
* |
* |
30 |
* Generic console support functions. |
* Generic console support functions. |
31 |
* |
* |
32 |
* This is used by individual device drivers, for example serial controllers, |
* This module is used by individual device drivers, for example serial |
33 |
* to attach stdin/stdout of the host system to a specific device. |
* controllers, keyboards, or other devices which need to attach to the |
34 |
|
* emulator's real stdin/stdout. |
35 |
* |
* |
36 |
* NOTE: This stuff is non-reentrant. |
* The idea is that several input and output streams (console handles) are |
37 |
|
* allowed. As long as the number of input streams is <= 1, then everything |
38 |
|
* can be done in the emulator's default terminal window. |
39 |
|
* |
40 |
|
* If the number of inputs is more than 1, it is necessary to open up slave |
41 |
|
* xterm windows for each input. (Otherwise the behaviour is undefined; i.e. |
42 |
|
* which of two emulated serial controllers would get keyboard input?) |
43 |
|
* |
44 |
|
* (If the -x command line option is NOT used, then slaves are not opened up. |
45 |
|
* Instead, a warning message is printed, and input is not allowed.) |
46 |
|
* |
47 |
|
* Note that console handles that _allow_ input but are not yet used for |
48 |
|
* output are not counted. This allows a machine to have, say, 2 serial ports |
49 |
|
* which can be used for both input and output, and it will still be possible |
50 |
|
* to run in the default terminal window as long as only one of those serial |
51 |
|
* ports is actually used. |
52 |
|
* |
53 |
|
* xterms are opened up "on demand", when output is sent to them. |
54 |
|
* |
55 |
|
* The MAIN console handle (fixed as handle nr 0) is the one used by the |
56 |
|
* default terminal window. A machine which registers a serial controller, |
57 |
|
* which should be used as the main way of communicating with guest operating |
58 |
|
* systems running on that machine, should set machine->main_console_handle |
59 |
|
* to the handle of the correct port on that controller. |
60 |
|
* |
61 |
|
* |
62 |
|
* NOTE: The code in this module is mostly non-reentrant. |
63 |
*/ |
*/ |
64 |
|
|
65 |
#include <errno.h> |
#include <errno.h> |
76 |
#include "machine.h" |
#include "machine.h" |
77 |
#include "memory.h" |
#include "memory.h" |
78 |
#include "misc.h" |
#include "misc.h" |
79 |
|
#include "settings.h" |
80 |
|
|
81 |
|
|
82 |
extern char *progname; |
extern char *progname; |
83 |
|
extern int verbose; |
84 |
|
extern struct settings *global_settings; |
85 |
|
|
86 |
|
|
87 |
static struct termios console_oldtermios; |
static struct termios console_oldtermios; |
112 |
|
|
113 |
struct console_handle { |
struct console_handle { |
114 |
int in_use; |
int in_use; |
115 |
|
int in_use_for_input; |
116 |
int using_xterm; |
int using_xterm; |
117 |
int inputonly; |
int inputonly; |
118 |
|
int outputonly; |
119 |
|
int warning_printed; |
120 |
|
|
121 |
|
char *machine_name; |
122 |
char *name; |
char *name; |
123 |
|
|
124 |
int w_descriptor; |
int w_descriptor; |
177 |
/* |
/* |
178 |
* start_xterm(): |
* start_xterm(): |
179 |
* |
* |
180 |
* When using X11, this routine tries to start up an xterm, with another |
* When using X11 (well, when allow_slaves is set), this routine tries to |
181 |
* copy of gxemul inside. The other gxemul copy is given arguments |
* start up an xterm, with another copy of gxemul inside. The other gxemul |
182 |
* that will cause it to run console_slave(). |
* copy is given arguments that will cause it to run console_slave(). |
183 |
|
* |
184 |
|
* TODO: This is ugly and hardcoded. Clean it up. |
185 |
*/ |
*/ |
186 |
static void start_xterm(int handle) |
static void start_xterm(int handle) |
187 |
{ |
{ |
188 |
int filedes[2]; |
int filedes[2]; |
189 |
int filedesB[2]; |
int filedesB[2]; |
190 |
int res; |
int res; |
191 |
|
size_t mlen; |
192 |
char **a; |
char **a; |
193 |
pid_t p; |
pid_t p; |
194 |
|
|
210 |
/* NOTE/warning: Hardcoded max nr of args! */ |
/* NOTE/warning: Hardcoded max nr of args! */ |
211 |
a = malloc(sizeof(char *) * 20); |
a = malloc(sizeof(char *) * 20); |
212 |
if (a == NULL) { |
if (a == NULL) { |
213 |
fprintf(stderr, "console_start_slave(): out of memory\n"); |
fprintf(stderr, "start_xterm(): out of memory\n"); |
214 |
exit(1); |
exit(1); |
215 |
} |
} |
216 |
|
|
220 |
a[1] = "-geometry"; |
a[1] = "-geometry"; |
221 |
a[2] = "80x25"; |
a[2] = "80x25"; |
222 |
a[3] = "-title"; |
a[3] = "-title"; |
223 |
a[4] = console_handles[handle].name; |
mlen = strlen(console_handles[handle].name) + |
224 |
|
strlen(console_handles[handle].machine_name) + 30; |
225 |
|
a[4] = malloc(mlen); |
226 |
|
snprintf(a[4], mlen, "GXemul: %s %s", |
227 |
|
console_handles[handle].machine_name, |
228 |
|
console_handles[handle].name); |
229 |
a[5] = "-e"; |
a[5] = "-e"; |
230 |
a[6] = progname; |
a[6] = progname; |
231 |
a[7] = malloc(50); |
a[7] = malloc(80); |
232 |
sprintf(a[7], "-WW@S%i,%i", filedes[0], filedesB[1]); |
snprintf(a[7], 80, "-WW@S%i,%i", filedes[0], filedesB[1]); |
233 |
a[8] = NULL; |
a[8] = NULL; |
234 |
|
|
235 |
p = fork(); |
p = fork(); |
236 |
if (p == -1) { |
if (p == -1) { |
237 |
printf("[ console_start_slave(): ERROR while trying to " |
printf("[ start_xterm(): ERROR while trying to " |
238 |
"fork(): %i ]\n", errno); |
"fork(): %i ]\n", errno); |
239 |
exit(1); |
exit(1); |
240 |
} else if (p == 0) { |
} else if (p == 0) { |
243 |
|
|
244 |
p = setsid(); |
p = setsid(); |
245 |
if (p < 0) |
if (p < 0) |
246 |
printf("[ console_start_slave(): ERROR while trying " |
printf("[ start_xterm(): ERROR while trying " |
247 |
"to do a setsid(): %i ]\n", errno); |
"to do a setsid(): %i ]\n", errno); |
248 |
|
|
249 |
res = execvp(a[0], a); |
res = execvp(a[0], a); |
250 |
printf("[ console_start_slave(): ERROR while trying to " |
printf("[ start_xterm(): ERROR while trying to " |
251 |
"execvp(\""); |
"execvp(\""); |
252 |
while (a[0] != NULL) { |
while (a[0] != NULL) { |
253 |
printf("%s", a[0]); |
printf("%s", a[0]); |
256 |
a++; |
a++; |
257 |
} |
} |
258 |
printf("\"): %i ]\n", errno); |
printf("\"): %i ]\n", errno); |
259 |
|
if (errno == ENOENT) |
260 |
|
printf("[ Most probably you don't have xterm" |
261 |
|
" in your PATH. Try again. ]\n"); |
262 |
exit(1); |
exit(1); |
263 |
} |
} |
264 |
|
|
323 |
*/ |
*/ |
324 |
static int console_stdin_avail(int handle) |
static int console_stdin_avail(int handle) |
325 |
{ |
{ |
326 |
|
if (!console_handles[handle].in_use_for_input) |
327 |
|
return 0; |
328 |
|
|
329 |
if (!allow_slaves) |
if (!allow_slaves) |
330 |
return d_avail(STDIN_FILENO); |
return d_avail(STDIN_FILENO); |
331 |
|
|
407 |
{ |
{ |
408 |
char buf[1]; |
char buf[1]; |
409 |
|
|
410 |
|
if (!console_handles[handle].in_use_for_input && |
411 |
|
!console_handles[handle].outputonly) |
412 |
|
console_change_inputability(handle, 1); |
413 |
|
|
414 |
if (!allow_slaves) { |
if (!allow_slaves) { |
415 |
/* stdout: */ |
/* stdout: */ |
416 |
putchar(ch); |
putchar(ch); |
660 |
memset(chp, 0, sizeof(struct console_handle)); |
memset(chp, 0, sizeof(struct console_handle)); |
661 |
|
|
662 |
chp->in_use = 1; |
chp->in_use = 1; |
663 |
|
chp->machine_name = ""; |
664 |
chp->name = strdup(name); |
chp->name = strdup(name); |
665 |
if (chp->name == NULL) { |
if (chp->name == NULL) { |
666 |
printf("console_new_handle(): out of memory\n"); |
printf("console_new_handle(): out of memory\n"); |
686 |
* |
* |
687 |
* consolename should be something like "serial 0". |
* consolename should be something like "serial 0". |
688 |
* |
* |
689 |
|
* If use_for_input is 1, input is allowed right from the start. (This |
690 |
|
* can be upgraded later from 0 to 1 using the console_change_inputability() |
691 |
|
* function.) |
692 |
|
* |
693 |
|
* If use_for_input is CONSOLE_OUTPUT_ONLY, then this is an output-only stream. |
694 |
|
* |
695 |
* On success, an integer >= 0 is returned. This can then be used as a |
* On success, an integer >= 0 is returned. This can then be used as a |
696 |
* 'handle' when writing to or reading from an emulated console. |
* 'handle' when writing to or reading from an emulated console. |
697 |
* |
* |
698 |
* On failure, -1 is returned. |
* On failure, -1 is returned. |
699 |
*/ |
*/ |
700 |
int console_start_slave(struct machine *machine, char *consolename) |
int console_start_slave(struct machine *machine, char *consolename, |
701 |
|
int use_for_input) |
702 |
{ |
{ |
|
int handle; |
|
703 |
struct console_handle *chp; |
struct console_handle *chp; |
704 |
|
int handle; |
705 |
|
|
706 |
if (machine == NULL || consolename == NULL) { |
if (machine == NULL || consolename == NULL) { |
707 |
printf("console_start_slave(): NULL ptr\n"); |
printf("console_start_slave(): NULL ptr\n"); |
709 |
} |
} |
710 |
|
|
711 |
chp = console_new_handle(consolename, &handle); |
chp = console_new_handle(consolename, &handle); |
712 |
|
chp->in_use_for_input = use_for_input; |
713 |
chp->name = malloc(strlen(machine->name) + strlen(consolename) + 100); |
if (use_for_input == CONSOLE_OUTPUT_ONLY) { |
714 |
if (chp->name == NULL) { |
chp->outputonly = 1; |
715 |
printf("out of memory\n"); |
chp->in_use_for_input = 0; |
|
exit(1); |
|
716 |
} |
} |
717 |
sprintf(chp->name, "GXemul: '%s' %s", machine->name, consolename); |
chp->machine_name = strdup(machine->machine_name); |
718 |
|
chp->name = strdup(consolename); |
719 |
|
|
720 |
#if 0 |
if (allow_slaves) |
721 |
if (!machine->use_x11) { |
chp->using_xterm = USING_XTERM_BUT_NOT_YET_OPEN; |
|
return handle; |
|
|
} |
|
|
#endif |
|
|
chp->using_xterm = USING_XTERM_BUT_NOT_YET_OPEN; |
|
722 |
|
|
723 |
return handle; |
return handle; |
724 |
} |
} |
736 |
* |
* |
737 |
* On failure, -1 is returned. |
* On failure, -1 is returned. |
738 |
*/ |
*/ |
739 |
int console_start_slave_inputonly(struct machine *machine, char *consolename) |
int console_start_slave_inputonly(struct machine *machine, char *consolename, |
740 |
|
int use_for_input) |
741 |
{ |
{ |
742 |
struct console_handle *chp; |
struct console_handle *chp; |
743 |
int handle; |
int handle; |
749 |
|
|
750 |
chp = console_new_handle(consolename, &handle); |
chp = console_new_handle(consolename, &handle); |
751 |
chp->inputonly = 1; |
chp->inputonly = 1; |
752 |
|
chp->in_use_for_input = use_for_input; |
753 |
|
chp->machine_name = strdup(machine->name); |
754 |
|
chp->name = strdup(consolename); |
755 |
|
|
756 |
chp->name = malloc(strlen(machine->name) + strlen(consolename) + 100); |
return handle; |
757 |
if (chp->name == NULL) { |
} |
758 |
printf("out of memory\n"); |
|
759 |
|
|
760 |
|
/* |
761 |
|
* console_change_inputability(): |
762 |
|
* |
763 |
|
* Sets whether or not a console handle can be used for input. Return value |
764 |
|
* is 1 if the change took place, 0 otherwise. |
765 |
|
*/ |
766 |
|
int console_change_inputability(int handle, int inputability) |
767 |
|
{ |
768 |
|
int old; |
769 |
|
|
770 |
|
if (handle < 0 || handle >= n_console_handles) { |
771 |
|
fatal("console_change_inputability(): bad handle %i\n", |
772 |
|
handle); |
773 |
exit(1); |
exit(1); |
774 |
} |
} |
|
sprintf(chp->name, "GXemul: '%s' %s", machine->name, consolename); |
|
775 |
|
|
776 |
return handle; |
old = console_handles[handle].in_use_for_input; |
777 |
|
console_handles[handle].in_use_for_input = inputability; |
778 |
|
|
779 |
|
if (inputability != 0) { |
780 |
|
if (console_warn_if_slaves_are_needed(0)) { |
781 |
|
console_handles[handle].in_use_for_input = old; |
782 |
|
if (!console_handles[handle].warning_printed) { |
783 |
|
fatal("%%\n%% WARNING! Input to console ha" |
784 |
|
"ndle \"%s\" wasn't enabled,\n%% because " |
785 |
|
"it", console_handles[handle].name); |
786 |
|
fatal(" would interfere with other inputs,\n" |
787 |
|
"%% and you did not use the -x command " |
788 |
|
"line option!\n%%\n"); |
789 |
|
} |
790 |
|
console_handles[handle].warning_printed = 1; |
791 |
|
return 0; |
792 |
|
} |
793 |
|
} |
794 |
|
|
795 |
|
return 1; |
796 |
} |
} |
797 |
|
|
798 |
|
|
799 |
/* |
/* |
800 |
* console_init_main(): |
* console_init_main(): |
801 |
* |
* |
802 |
* Put host's console into single-character (non-canonical) mode. |
* Puts the host's console into single-character (non-canonical) mode. |
803 |
*/ |
*/ |
804 |
void console_init_main(struct emul *emul) |
void console_init_main(struct emul *emul) |
805 |
{ |
{ |
849 |
|
|
850 |
|
|
851 |
/* |
/* |
852 |
|
* console_debug_dump(): |
853 |
|
* |
854 |
|
* Dump debug info, if verbose >= 2. |
855 |
|
*/ |
856 |
|
void console_debug_dump(struct machine *machine) |
857 |
|
{ |
858 |
|
int i, iadd = DEBUG_INDENTATION, listed_main = 0; |
859 |
|
|
860 |
|
if (verbose < 2) |
861 |
|
return; |
862 |
|
|
863 |
|
debug("console slaves (xterms): %s\n", allow_slaves? |
864 |
|
"yes" : "no"); |
865 |
|
|
866 |
|
debug("console handles:\n"); |
867 |
|
debug_indentation(iadd); |
868 |
|
|
869 |
|
for (i=0; i<n_console_handles; i++) { |
870 |
|
if (!console_handles[i].in_use) |
871 |
|
continue; |
872 |
|
debug("%i: \"%s\"", i, console_handles[i].name); |
873 |
|
if (console_handles[i].using_xterm) |
874 |
|
debug(" [xterm]"); |
875 |
|
if (console_handles[i].inputonly) |
876 |
|
debug(" [inputonly]"); |
877 |
|
if (console_handles[i].outputonly) |
878 |
|
debug(" [outputonly]"); |
879 |
|
if (i == machine->main_console_handle) { |
880 |
|
debug(" [MAIN CONSOLE]"); |
881 |
|
listed_main = 1; |
882 |
|
} |
883 |
|
debug("\n"); |
884 |
|
} |
885 |
|
|
886 |
|
debug_indentation(-iadd); |
887 |
|
|
888 |
|
if (!listed_main) |
889 |
|
fatal("WARNING! no main console handle?\n"); |
890 |
|
} |
891 |
|
|
892 |
|
|
893 |
|
/* |
894 |
* console_allow_slaves(): |
* console_allow_slaves(): |
895 |
* |
* |
896 |
* This function tells the console subsystem whether or not to open up |
* This function tells the console subsystem whether or not to open up |
903 |
|
|
904 |
|
|
905 |
/* |
/* |
906 |
|
* console_are_slaves_allowed(): |
907 |
|
* |
908 |
|
* Returns the value of allow_slaves. |
909 |
|
*/ |
910 |
|
int console_are_slaves_allowed(void) |
911 |
|
{ |
912 |
|
return allow_slaves; |
913 |
|
} |
914 |
|
|
915 |
|
|
916 |
|
/* |
917 |
|
* console_warn_if_slaves_are_needed(): |
918 |
|
* |
919 |
|
* Prints an error (during startup of the emulator) if slave xterms are needed |
920 |
|
* (i.e. there is more than one console handle in use which is used for |
921 |
|
* INPUT), but they are not currently allowed. |
922 |
|
* |
923 |
|
* This function should be called during startup (with init = 1), and every |
924 |
|
* time a console handle changes/upgrades its in_use_for_input from 0 to 1. |
925 |
|
* |
926 |
|
* If init is non-zero, this function doesn't return if there was a warning. |
927 |
|
* |
928 |
|
* If init is zero, no warning is printed. 1 is returned if there were more |
929 |
|
* than one input, 0 otherwise. |
930 |
|
*/ |
931 |
|
int console_warn_if_slaves_are_needed(int init) |
932 |
|
{ |
933 |
|
int i, n = 0; |
934 |
|
|
935 |
|
if (allow_slaves) |
936 |
|
return 0; |
937 |
|
|
938 |
|
for (i=MAIN_CONSOLE+1; i<n_console_handles; i++) |
939 |
|
if (console_handles[i].in_use && |
940 |
|
console_handles[i].in_use_for_input && |
941 |
|
!console_handles[i].using_xterm) |
942 |
|
n ++; |
943 |
|
|
944 |
|
if (n > 1) { |
945 |
|
if (init) { |
946 |
|
fatal("#\n# ERROR! More than one console input is " |
947 |
|
"in use,\n# but xterm slaves are not enabled.\n" |
948 |
|
"#\n"); |
949 |
|
fatal("# Use -x to enable slave xterms.)\n#\n"); |
950 |
|
for (i=MAIN_CONSOLE+1; i<n_console_handles; i++) |
951 |
|
if (console_handles[i].in_use && |
952 |
|
console_handles[i].in_use_for_input && |
953 |
|
!console_handles[i].using_xterm) |
954 |
|
fatal("# console handle %i: '%s'\n", |
955 |
|
i, console_handles[i].name); |
956 |
|
fatal("#\n"); |
957 |
|
exit(1); |
958 |
|
} |
959 |
|
return 1; |
960 |
|
} |
961 |
|
|
962 |
|
return 0; |
963 |
|
} |
964 |
|
|
965 |
|
|
966 |
|
/* |
967 |
* console_init(): |
* console_init(): |
968 |
* |
* |
969 |
* This function should be called before any other console_*() function |
* This function should be called before any other console_*() function |
973 |
{ |
{ |
974 |
int handle; |
int handle; |
975 |
struct console_handle *chp; |
struct console_handle *chp; |
976 |
|
struct settings *console_settings = settings_new(); |
977 |
|
|
978 |
|
settings_add(global_settings, "console", 1, |
979 |
|
SETTINGS_TYPE_SUBSETTINGS, 0, console_settings); |
980 |
|
|
981 |
|
settings_add(console_settings, "allow_slaves", 0, |
982 |
|
SETTINGS_TYPE_INT, SETTINGS_FORMAT_YESNO, (void *)&allow_slaves); |
983 |
|
|
984 |
chp = console_new_handle("MAIN", &handle); |
chp = console_new_handle("MAIN", &handle); |
985 |
if (handle != MAIN_CONSOLE) { |
if (handle != MAIN_CONSOLE) { |
987 |
" console 0: handle = %i\n", handle); |
" console 0: handle = %i\n", handle); |
988 |
exit(1); |
exit(1); |
989 |
} |
} |
990 |
|
|
991 |
|
chp->in_use_for_input = 1; |
992 |
} |
} |
993 |
|
|