Disable tests for strdup/strndup on __hpux__
[official-gcc.git] / gcc / ada / terminals.c
blob21784b1202b8801ab2e191df6a6884e91b706594
1 /****************************************************************************
2 * *
3 * GNAT RUN-TIME COMPONENTS *
4 * *
5 * T E R M I N A L S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2008-2023, AdaCore *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
32 #define ATTRIBUTE_UNUSED __attribute__((unused))
34 /* First all unsupported platforms. Add stubs for exported routines. */
36 #if defined (VMS) || defined (__vxworks) || defined (__Lynx__) \
37 || defined (__ANDROID__) || defined (__PikeOS__) || defined(__DJGPP__)
39 void *
40 __gnat_new_tty (void)
42 return (void*)0;
45 char *
46 __gnat_tty_name (void* t ATTRIBUTE_UNUSED)
48 return (char*)0;
51 int
52 __gnat_interrupt_pid (int pid ATTRIBUTE_UNUSED)
54 return -1;
57 int
58 __gnat_interrupt_process (void* desc ATTRIBUTE_UNUSED)
60 return -1;
63 int
64 __gnat_setup_communication (void** desc ATTRIBUTE_UNUSED)
66 return -1;
69 void
70 __gnat_setup_parent_communication (void *d ATTRIBUTE_UNUSED,
71 int *i ATTRIBUTE_UNUSED,
72 int *o ATTRIBUTE_UNUSED,
73 int *e ATTRIBUTE_UNUSED,
74 int *p ATTRIBUTE_UNUSED)
78 int
79 __gnat_setup_child_communication (void *d ATTRIBUTE_UNUSED,
80 char **n ATTRIBUTE_UNUSED,
81 int u ATTRIBUTE_UNUSED)
83 return -1;
86 int
87 __gnat_terminate_process (void *desc ATTRIBUTE_UNUSED)
89 return -1;
92 int
93 __gnat_terminate_pid (int pid ATTRIBUTE_UNUSED)
95 return -1;
98 int
99 __gnat_tty_fd (void* t ATTRIBUTE_UNUSED)
101 return -1;
105 __gnat_tty_supported (void)
107 return 0;
111 __gnat_tty_waitpid (void *desc ATTRIBUTE_UNUSED, int blocking)
113 return 1;
116 void
117 __gnat_close_tty (void* t ATTRIBUTE_UNUSED)
121 void
122 __gnat_free_process (void** process ATTRIBUTE_UNUSED)
126 void
127 __gnat_reset_tty (void* t ATTRIBUTE_UNUSED)
131 void
132 __gnat_send_header (void* d ATTRIBUTE_UNUSED,
133 char h[5] ATTRIBUTE_UNUSED,
134 int s ATTRIBUTE_UNUSED,
135 int *r ATTRIBUTE_UNUSED)
139 void
140 __gnat_setup_winsize (void *desc ATTRIBUTE_UNUSED,
141 int rows ATTRIBUTE_UNUSED,
142 int columns ATTRIBUTE_UNUSED)
146 /* For Windows platforms. */
148 #elif defined(_WIN32)
150 #include <errno.h>
151 #include <stdio.h>
152 #include <stdlib.h>
154 #define WIN32_LEAN_AND_MEAN
155 #include <windows.h>
156 #include <winternl.h>
157 #include <io.h>
159 #define MAXPATHLEN 1024
161 #define NILP(x) ((x) == 0)
162 #define Qnil 0
163 #define report_file_error(x, y) fprintf (stderr, "Error: %s\n", x);
164 #define INTEGERP(x) 1
165 #define XINT(x) x
167 struct TTY_Process {
168 int pid; /* Number of this process */
169 PROCESS_INFORMATION procinfo;
170 HANDLE w_infd, w_outfd;
171 HANDLE w_forkin, w_forkout;
172 BOOL usePipe;
175 /* Control whether create_child cause the process to inherit GNAT Studio'
176 error mode setting. The default is 1, to minimize the possibility of
177 subprocesses blocking when accessing unmounted drives. */
178 static int Vw32_start_process_inherit_error_mode = 1;
180 /* Control whether spawnve quotes arguments as necessary to ensure
181 correct parsing by child process. Because not all uses of spawnve
182 are careful about constructing argv arrays, we make this behavior
183 conditional (off by default, since a similar operation is already done
184 in g-expect.adb by calling Normalize_Argument). */
185 static int Vw32_quote_process_args = 0;
187 static DWORD AbsoluteSeek(HANDLE, DWORD);
188 static VOID ReadBytes(HANDLE, LPVOID, DWORD);
190 #define XFER_BUFFER_SIZE 2048
192 /* This tell if the executable we're about to launch uses a GUI interface. */
193 /* if we can't determine it, we will return true */
194 static int
195 is_gui_app (char *exe)
197 HANDLE hImage;
199 DWORD CoffHeaderOffset;
200 DWORD MoreDosHeader[16];
201 CHAR *file;
202 size_t nlen;
204 ULONG ntSignature;
206 IMAGE_DOS_HEADER image_dos_header;
207 IMAGE_FILE_HEADER image_file_header;
208 IMAGE_OPTIONAL_HEADER image_optional_header;
211 * Open the reference file.
213 nlen = strlen (exe);
214 file = exe;
215 if (nlen > 2) {
216 if (exe[0] == '"') {
217 /* remove quotes */
218 nlen -= 2;
219 file = malloc ((nlen + 1) * sizeof (char));
220 memcpy (file, &exe[1], nlen);
221 file [nlen] = '\0';
224 hImage = CreateFile(file,
225 GENERIC_READ,
226 FILE_SHARE_READ,
227 NULL,
228 OPEN_EXISTING,
229 FILE_ATTRIBUTE_NORMAL,
230 NULL);
232 if (file != exe) {
233 free (file);
236 if (INVALID_HANDLE_VALUE == hImage)
238 report_file_error ("Could not open exe: ", Qnil);
239 report_file_error (exe, Qnil);
240 report_file_error ("\n", Qnil);
241 CloseHandle (hImage);
242 return -1;
246 * Read the MS-DOS image header.
248 ReadBytes(hImage, &image_dos_header, sizeof(IMAGE_DOS_HEADER));
250 if (IMAGE_DOS_SIGNATURE != image_dos_header.e_magic)
252 report_file_error("Sorry, I do not understand this file.\n", Qnil);
253 CloseHandle (hImage);
254 return -1;
258 * Read more MS-DOS header. */
259 ReadBytes(hImage, MoreDosHeader, sizeof(MoreDosHeader));
261 * Get actual COFF header.
263 CoffHeaderOffset = AbsoluteSeek(hImage, image_dos_header.e_lfanew) +
264 sizeof(ULONG);
265 if (CoffHeaderOffset == (DWORD) -1) {
266 CloseHandle (hImage);
267 return -1;
270 ReadBytes (hImage, &ntSignature, sizeof(ULONG));
272 if (IMAGE_NT_SIGNATURE != ntSignature)
274 report_file_error ("Missing NT signature. Unknown file type.\n", Qnil);
275 CloseHandle (hImage);
276 return -1;
279 ReadBytes(hImage, &image_file_header, IMAGE_SIZEOF_FILE_HEADER);
282 * Read optional header.
284 ReadBytes(hImage,
285 &image_optional_header,
286 IMAGE_SIZEOF_NT_OPTIONAL_HEADER);
288 CloseHandle (hImage);
290 switch (image_optional_header.Subsystem)
292 case IMAGE_SUBSYSTEM_UNKNOWN:
293 return 1;
295 case IMAGE_SUBSYSTEM_NATIVE:
296 return 1;
298 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
299 return 1;
301 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
302 return 0;
304 case IMAGE_SUBSYSTEM_OS2_CUI:
305 return 0;
307 case IMAGE_SUBSYSTEM_POSIX_CUI:
308 return 0;
310 default:
311 /* Unknown, return GUI app to be preservative: if yes, it will be
312 correctly launched, if no, it will be launched, and a console will
313 be also displayed, which is not a big deal */
314 return 1;
319 static DWORD
320 AbsoluteSeek (HANDLE hFile, DWORD offset)
322 DWORD newOffset;
324 newOffset = SetFilePointer (hFile, offset, NULL, FILE_BEGIN);
326 if (newOffset == 0xFFFFFFFF)
327 return -1;
328 else
329 return newOffset;
332 static VOID
333 ReadBytes (HANDLE hFile, LPVOID buffer, DWORD size)
335 DWORD bytes;
337 if (!ReadFile(hFile, buffer, size, &bytes, NULL))
339 size = 0;
340 return;
342 else if (size != bytes)
344 return;
348 static int
349 nt_spawnve (char *exe ATTRIBUTE_UNUSED, char **argv, char *env,
350 struct TTY_Process *process)
352 STARTUPINFO start;
353 SECURITY_ATTRIBUTES sec_attrs;
354 SECURITY_DESCRIPTOR sec_desc;
355 DWORD flags;
356 int pid;
357 int is_gui, use_cmd;
358 char *cmdline, *parg, **targ;
359 int do_quoting = 0;
360 char escape_char = 0;
361 int arglen;
363 /* we have to do some conjuring here to put argv and envp into the
364 form CreateProcess wants... argv needs to be a space separated/null
365 terminated list of parameters, and envp is a null
366 separated/double-null terminated list of parameters.
368 Additionally, zero-length args and args containing whitespace or
369 quote chars need to be wrapped in double quotes - for this to work,
370 embedded quotes need to be escaped as well. The aim is to ensure
371 the child process reconstructs the argv array we start with
372 exactly, so we treat quotes at the beginning and end of arguments
373 as embedded quotes.
375 Note that using backslash to escape embedded quotes requires
376 additional special handling if an embedded quote is already
377 preceded by backslash, or if an arg requiring quoting ends with
378 backslash. In such cases, the run of escape characters needs to be
379 doubled. For consistency, we apply this special handling as long
380 as the escape character is not quote.
382 Since we have no idea how large argv and envp are likely to be we
383 figure out list lengths on the fly and allocate them. */
385 if (!NILP (Vw32_quote_process_args))
387 do_quoting = 1;
388 /* Override escape char by binding w32-quote-process-args to
389 desired character, or use t for auto-selection. */
390 if (INTEGERP (Vw32_quote_process_args))
391 escape_char = XINT (Vw32_quote_process_args);
392 else
393 escape_char = '\\';
396 /* do argv... */
397 arglen = 0;
398 targ = argv;
399 while (*targ)
401 char *p = *targ;
402 int need_quotes = 0;
403 int escape_char_run = 0;
405 if (*p == 0)
406 need_quotes = 1;
407 for ( ; *p; p++)
409 if (*p == '"')
411 /* allow for embedded quotes to be escaped */
412 arglen++;
413 need_quotes = 1;
414 /* handle the case where the embedded quote is already escaped */
415 if (escape_char_run > 0)
417 /* To preserve the arg exactly, we need to double the
418 preceding escape characters (plus adding one to
419 escape the quote character itself). */
420 arglen += escape_char_run;
423 else if (*p == ' ' || *p == '\t')
425 need_quotes = 1;
428 if (*p == escape_char && escape_char != '"')
429 escape_char_run++;
430 else
431 escape_char_run = 0;
433 if (need_quotes)
435 arglen += 2;
436 /* handle the case where the arg ends with an escape char - we
437 must not let the enclosing quote be escaped. */
438 if (escape_char_run > 0)
439 arglen += escape_char_run;
441 arglen += strlen (*targ) + 1;
442 targ++;
445 is_gui = is_gui_app (argv[0]);
446 use_cmd = FALSE;
448 if (is_gui == -1) {
449 /* could not determine application type. Try launching with "cmd /c" */
450 is_gui = FALSE;
451 arglen += 7;
452 use_cmd = TRUE;
455 cmdline = (char*)malloc (arglen + 1);
456 targ = argv;
457 parg = cmdline;
459 if (use_cmd == TRUE) {
460 strcpy (parg, "cmd /c ");
461 parg += 7;
464 while (*targ)
466 char * p = *targ;
467 int need_quotes = 0;
469 if (*p == 0)
470 need_quotes = 1;
472 if (do_quoting)
474 for ( ; *p; p++)
475 if (*p == ' ' || *p == '\t' || *p == '"')
476 need_quotes = 1;
478 if (need_quotes)
480 int escape_char_run = 0;
482 p = *targ;
483 *parg++ = '"';
484 for ( ; *p; p++)
486 if (*p == '"')
488 /* double preceding escape chars if any */
489 while (escape_char_run > 0)
491 *parg++ = escape_char;
492 escape_char_run--;
494 /* escape all quote chars, even at beginning or end */
495 *parg++ = escape_char;
497 *parg++ = *p;
499 if (*p == escape_char && escape_char != '"')
500 escape_char_run++;
501 else
502 escape_char_run = 0;
504 /* double escape chars before enclosing quote */
505 while (escape_char_run > 0)
507 *parg++ = escape_char;
508 escape_char_run--;
510 *parg++ = '"';
512 else
514 strcpy (parg, *targ);
515 parg += strlen (*targ);
517 *parg++ = ' ';
518 targ++;
520 *--parg = '\0';
522 memset (&start, 0, sizeof (start));
523 start.cb = sizeof (start);
525 if (process->usePipe == TRUE) {
526 start.dwFlags = STARTF_USESTDHANDLES;
527 start.hStdInput = process->w_forkin;
528 start.hStdOutput = process->w_forkout;
529 /* child's stderr is always redirected to outfd */
530 start.hStdError = process->w_forkout;
531 } else {
532 start.dwFlags = STARTF_USESTDHANDLES;
533 /* We only need to redirect stderr/stdout here. Stdin will be forced to
534 the spawned process console by explaunch */
535 start.hStdInput = NULL;
536 start.hStdOutput = process->w_forkout;
537 start.hStdError = process->w_forkout;
540 /* Explicitly specify no security */
541 if (!InitializeSecurityDescriptor (&sec_desc, SECURITY_DESCRIPTOR_REVISION))
542 goto EH_Fail;
543 if (!SetSecurityDescriptorDacl (&sec_desc, TRUE, NULL, FALSE))
544 goto EH_Fail;
545 sec_attrs.nLength = sizeof (sec_attrs);
546 sec_attrs.lpSecurityDescriptor = &sec_desc;
547 sec_attrs.bInheritHandle = FALSE;
549 /* creating a new console allow easier close. Do not use
550 CREATE_NEW_PROCESS_GROUP as this results in disabling Ctrl+C */
551 flags = CREATE_NEW_CONSOLE;
552 if (NILP (Vw32_start_process_inherit_error_mode))
553 flags |= CREATE_DEFAULT_ERROR_MODE;
555 /* if app is not a gui application, hide the console */
556 if (is_gui == FALSE) {
557 start.dwFlags |= STARTF_USESHOWWINDOW;
558 start.wShowWindow = SW_HIDE;
561 /* Set initial directory to null character to use current directory */
562 if (!CreateProcess (NULL, cmdline, &sec_attrs, NULL, TRUE,
563 flags, env, NULL, &start, &process->procinfo))
564 goto EH_Fail;
566 pid = (int) (intptr_t) process->procinfo.hProcess;
567 process->pid = pid;
569 return pid;
571 EH_Fail:
572 return -1;
575 /*************************
576 ** __gnat_send_header ()
577 *************************/
579 #define EXP_SLAVE_CREATE 'c'
580 #define EXP_SLAVE_KEY 'k'
581 #define EXP_SLAVE_MOUSE 'm'
582 #define EXP_SLAVE_WRITE 'w'
583 #define EXP_SLAVE_KILL 'x'
585 #define EXP_KILL_TERMINATE 0x1
586 #define EXP_KILL_CTRL_C 0x2
587 #define EXP_KILL_CTRL_BREAK 0x4
589 void
590 __gnat_send_header (struct TTY_Process* p, char header[5], int size, int *ret)
592 if (p->usePipe == FALSE) {
593 header[0] = EXP_SLAVE_WRITE;
594 header[1] = size & 0xff;
595 header[2] = (size & 0xff00) >> 8;
596 header[3] = (size & 0xff0000) >> 16;
597 header[4] = (size & 0xff000000) >> 24;
598 *ret = 1;
599 } else {
600 *ret = 0;
604 /**********************************
605 ** __gnat_setup_communication ()
606 **********************************/
609 __gnat_setup_communication (struct TTY_Process** process_out) /* output param */
611 struct TTY_Process* process;
613 process = (struct TTY_Process*)malloc (sizeof (struct TTY_Process));
614 ZeroMemory (process, sizeof (struct TTY_Process));
615 *process_out = process;
617 return 0;
620 #define EXP_PIPE_BASENAME "\\\\.\\pipe\\ExpectPipe"
623 __gnat_setup_child_communication
624 (struct TTY_Process* process,
625 char** argv,
626 int Use_Pipes)
628 int cpid;
629 SECURITY_ATTRIBUTES sec_attrs;
630 char slavePath [MAX_PATH];
631 char **nargv;
632 int argc;
633 int i;
634 char pipeNameIn[100];
635 HANDLE hSlaveInDrv = NULL; /* Handle to communicate with slave driver */
637 /* Set inheritance for the pipe handles */
638 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
639 sec_attrs.bInheritHandle = TRUE;
640 sec_attrs.lpSecurityDescriptor = NULL;
642 if (Use_Pipes) {
643 /* Create in and out pipes */
644 if (!CreatePipe (&process->w_forkin, &process->w_infd, &sec_attrs, 0))
645 report_file_error ("Creation of child's IN handle", Qnil);
646 if (!CreatePipe (&process->w_outfd, &process->w_forkout, &sec_attrs, 0))
647 report_file_error ("Creation of child's OUT handle", Qnil);
649 /* Do not inherit the parent's side of the pipes */
650 SetHandleInformation (&process->w_infd, HANDLE_FLAG_INHERIT, 0);
651 SetHandleInformation (&process->w_outfd, HANDLE_FLAG_INHERIT, 0);
653 /* use native argv */
654 nargv = argv;
655 process->usePipe = TRUE;
657 } else {
658 static int pipeNameId = 0;
660 process->w_infd = NULL;
662 /* We create a named pipe for Input, as we handle input by sending special
663 commands to the explaunch process, that uses it to feed the actual input
664 of the process */
665 sprintf(pipeNameIn, "%sIn%08lx_%08x", EXP_PIPE_BASENAME,
666 GetCurrentProcessId(), pipeNameId);
667 pipeNameId++;
669 hSlaveInDrv = CreateNamedPipe(pipeNameIn,
670 PIPE_ACCESS_OUTBOUND,
671 PIPE_TYPE_BYTE | PIPE_WAIT, 1, 8192, 8192,
672 20000, NULL);
673 if (hSlaveInDrv == NULL) goto end;
675 if (!CreatePipe (&process->w_outfd, &process->w_forkout, &sec_attrs, 0))
676 report_file_error ("Creation of child's OUT handle", Qnil);
678 if (SearchPath (NULL, "explaunch.exe", NULL,
679 MAX_PATH, slavePath, NULL) == 0) goto end;
681 for (argc=0; argv[argc] != NULL; argc++) ;
682 nargv = (char **) malloc (sizeof (char*) * (argc + 3));
683 nargv[0] = slavePath;
684 nargv[1] = pipeNameIn;
686 for (i = 0; i <= argc; i++) nargv[i + 2] = argv[i];
687 process->usePipe = FALSE;
690 /* Spawn the child. */
691 cpid = nt_spawnve (nargv[0], nargv, NULL, process);
693 /* close the duplicated handles passed to the child */
694 CloseHandle (process->w_forkout);
696 if (process->usePipe == TRUE) {
697 CloseHandle (process->w_forkin);
699 } else {
700 UCHAR buf[8]; /* enough space for child status info */
701 DWORD count;
702 BOOL bRet;
703 DWORD dwRet;
706 * Wait for connection with the slave driver
708 bRet = ConnectNamedPipe(hSlaveInDrv, NULL);
709 if (bRet == FALSE) {
710 dwRet = GetLastError();
711 if (dwRet == ERROR_PIPE_CONNECTED) {
713 } else {
714 goto end;
718 process->w_infd = hSlaveInDrv;
721 * wait for slave driver to initialize before allowing user to send to it
723 bRet = ReadFile(process->w_outfd, buf, 8, &count, NULL);
724 if (bRet == FALSE) {
725 cpid = -1;
728 dwRet = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
729 if (dwRet != 0) {
730 cpid = -1;
733 cpid = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24);
734 process->pid = cpid;
737 if (cpid == -1)
738 /* An error occurred while trying to spawn the process. */
739 report_file_error ("Spawning child process", Qnil);
741 return cpid;
742 end:
743 if (hSlaveInDrv != NULL)
744 CloseHandle (hSlaveInDrv);
745 return -1;
748 void
749 __gnat_setup_parent_communication
750 (struct TTY_Process* process,
751 int* in,
752 int* out,
753 int* err,
754 int* pid)
756 *in = _open_osfhandle ((intptr_t) process->w_infd, 0);
757 *out = _open_osfhandle ((intptr_t) process->w_outfd, 0);
758 /* child's stderr is always redirected to outfd */
759 *err = *out;
760 *pid = process->pid;
763 typedef struct _child_process
765 HWND hwnd;
766 PROCESS_INFORMATION *procinfo;
767 } child_process;
769 /* The major and minor versions of NT. */
770 static int w32_major_version;
771 static int w32_minor_version;
773 /* Distinguish between Windows NT and Windows 95. */
774 static enum {OS_UNKNOWN, OS_WIN95, OS_NT} os_subtype = OS_UNKNOWN;
776 /* Cache information describing the NT system for later use. */
777 static void
778 cache_system_info (void)
780 union
782 struct info
784 char major;
785 char minor;
786 short platform;
787 } info;
788 DWORD data;
789 } version;
791 /* Cache the version of the operating system. */
792 version.data = GetVersion ();
793 w32_major_version = version.info.major;
794 w32_minor_version = version.info.minor;
796 if (version.info.platform & 0x8000)
797 os_subtype = OS_WIN95;
798 else
799 os_subtype = OS_NT;
802 static WINBOOL CALLBACK
803 find_child_console (HWND hwnd, LPARAM param)
805 child_process *cp = (child_process *) param;
806 DWORD process_id;
808 (void) GetWindowThreadProcessId (hwnd, &process_id);
809 if (process_id == cp->procinfo->dwProcessId)
811 char window_class[32];
813 GetClassName (hwnd, window_class, sizeof (window_class));
814 if (strcmp (window_class,
815 (os_subtype == OS_WIN95)
816 ? "tty"
817 : "ConsoleWindowClass") == 0)
819 cp->hwnd = hwnd;
820 return FALSE;
823 /* keep looking */
824 return TRUE;
828 __gnat_interrupt_pid (int pid)
830 volatile child_process cp;
831 int rc = 0;
833 cp.procinfo = (LPPROCESS_INFORMATION) malloc (sizeof (PROCESS_INFORMATION));
834 cp.procinfo->dwProcessId = pid;
836 if (os_subtype == OS_UNKNOWN)
837 cache_system_info ();
839 /* Try to locate console window for process. */
840 EnumWindows ((WNDENUMPROC) find_child_console, (LPARAM) &cp);
842 if (cp.hwnd)
844 BYTE control_scan_code = (BYTE) MapVirtualKey (VK_CONTROL, 0);
845 /* Retrieve Ctrl-C scancode */
846 BYTE vk_break_code = 'C';
847 BYTE break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
848 HWND foreground_window;
850 foreground_window = GetForegroundWindow ();
851 if (foreground_window)
853 /* NT 5.0, and apparently also Windows 98, will not allow
854 a Window to be set to foreground directly without the
855 user's involvement. The workaround is to attach
856 ourselves to the thread that owns the foreground
857 window, since that is the only thread that can set the
858 foreground window. */
859 DWORD foreground_thread, child_thread;
861 foreground_thread =
862 GetWindowThreadProcessId (foreground_window, NULL);
863 if (foreground_thread == GetCurrentThreadId ()
864 || !AttachThreadInput (GetCurrentThreadId (),
865 foreground_thread, TRUE))
866 foreground_thread = 0;
868 child_thread = GetWindowThreadProcessId (cp.hwnd, NULL);
869 if (child_thread == GetCurrentThreadId ()
870 || !AttachThreadInput (GetCurrentThreadId (),
871 child_thread, TRUE))
872 child_thread = 0;
874 /* Set the foreground window to the child. */
875 if (SetForegroundWindow (cp.hwnd))
877 /* Generate keystrokes as if user had typed Ctrl-Break or
878 Ctrl-C. */
879 keybd_event (VK_CONTROL, control_scan_code, 0, 0);
880 keybd_event (vk_break_code, break_scan_code,
881 (vk_break_code == 'C' ? 0 : KEYEVENTF_EXTENDEDKEY), 0);
882 keybd_event (vk_break_code, break_scan_code,
883 (vk_break_code == 'C' ? 0 : KEYEVENTF_EXTENDEDKEY)
884 | KEYEVENTF_KEYUP, 0);
885 keybd_event (VK_CONTROL, control_scan_code, KEYEVENTF_KEYUP, 0);
887 /* Sleep for a bit to give time for the main frame to respond
888 to focus change events. */
889 Sleep (100);
891 SetForegroundWindow (foreground_window);
893 /* Detach from the foreground and child threads now that
894 the foreground switching is over. */
895 if (foreground_thread)
896 AttachThreadInput (GetCurrentThreadId (), foreground_thread, FALSE);
897 if (child_thread)
898 AttachThreadInput (GetCurrentThreadId (), child_thread, FALSE);
901 /* Ctrl-Break is NT equivalent of SIGINT. */
902 else if (!GenerateConsoleCtrlEvent
903 (CTRL_BREAK_EVENT, cp.procinfo->dwProcessId))
905 errno = EINVAL;
906 rc = -1;
909 free (cp.procinfo);
910 return rc;
914 __gnat_interrupt_process (struct TTY_Process* p)
916 char buf[2];
917 DWORD written;
918 BOOL bret;
920 if (p->usePipe == TRUE) {
921 bret = FALSE;
922 } else {
923 buf[0] = EXP_SLAVE_KILL;
924 buf[1] = EXP_KILL_CTRL_C;
925 bret = WriteFile (p->w_infd, buf, 2, &written, NULL);
928 if (bret == FALSE) {
929 return __gnat_interrupt_pid (p->procinfo.dwProcessId);
931 return 0;
934 /* kill a process, as this implementation use CreateProcess on Win32 we need
935 to use Win32 TerminateProcess API */
937 __gnat_terminate_process (struct TTY_Process* p)
939 char buf[2];
940 DWORD written;
941 BOOL bret;
943 if (p->usePipe == TRUE) {
944 bret = FALSE;
945 } else {
946 buf[0] = EXP_SLAVE_KILL;
947 buf[1] = EXP_KILL_TERMINATE;
948 bret = WriteFile (p->w_infd, buf, 2, &written, NULL);
951 if (bret == FALSE) {
952 if (!TerminateProcess (p->procinfo.hProcess, 1))
953 return -1;
954 else
955 return 0;
956 } else
957 return 0;
960 typedef struct {
961 DWORD dwProcessId;
962 HANDLE hwnd;
963 } pid_struct;
965 static WINBOOL CALLBACK
966 find_process_handle (HWND hwnd, LPARAM param)
968 pid_struct *ps = (pid_struct *) param;
969 DWORD process_id;
971 (void) GetWindowThreadProcessId (hwnd, &process_id);
972 if (process_id == ps->dwProcessId)
974 ps->hwnd = hwnd;
975 return FALSE;
977 /* keep looking */
978 return TRUE;
982 __gnat_terminate_pid (int pid)
984 pid_struct ps;
986 ps.dwProcessId = pid;
987 ps.hwnd = 0;
988 EnumWindows ((WNDENUMPROC) find_process_handle, (LPARAM) &ps);
990 if (ps.hwnd)
992 if (!TerminateProcess (ps.hwnd, 1))
993 return -1;
994 else
995 return 0;
998 return -1;
1001 /* wait for process pid to terminate and return the process status. This
1002 implementation is different from the adaint.c one for Windows as it uses
1003 the Win32 API instead of the C one. */
1006 __gnat_tty_waitpid (struct TTY_Process* p, int blocking)
1008 DWORD exitcode;
1009 HANDLE hprocess = p->procinfo.hProcess;
1011 if (blocking) {
1012 /* Wait is needed on Windows only in blocking mode. */
1013 WaitForSingleObject (hprocess, 0);
1016 GetExitCodeProcess (hprocess, &exitcode);
1018 if (exitcode == STILL_ACTIVE) {
1019 /* If process is still active return -1. */
1020 exitcode = -1;
1021 } else {
1022 /* Process is dead, so handle to process and main thread can be closed. */
1023 CloseHandle (p->procinfo.hThread);
1024 CloseHandle (hprocess);
1027 /* No need to close the handles: they were closed on the ada side */
1028 return (int) exitcode;
1031 /********************************
1032 ** __gnat_free_process ()
1033 ********************************/
1035 void
1036 __gnat_free_process (struct TTY_Process** process)
1038 free (*process);
1039 *process = NULL;
1042 /* TTY handling */
1044 typedef struct {
1045 int tty_fd; /* descriptor for the tty */
1046 char tty_name[24]; /* Name of TTY device */
1047 } TTY_Handle;
1050 __gnat_tty_supported (void)
1052 return 0;
1055 /* Return the tty name associated with p */
1057 char *
1058 __gnat_tty_name (TTY_Handle* t)
1060 return t->tty_name;
1064 __gnat_tty_fd (TTY_Handle* t)
1066 return t->tty_fd;
1069 TTY_Handle*
1070 __gnat_new_tty (void)
1072 return (TTY_Handle*)0;
1075 void
1076 __gnat_reset_tty (TTY_Handle* t ATTRIBUTE_UNUSED)
1080 void
1081 __gnat_close_tty (TTY_Handle* t)
1083 free (t);
1086 void
1087 __gnat_setup_winsize (void *desc ATTRIBUTE_UNUSED,
1088 int rows ATTRIBUTE_UNUSED, int columns ATTRIBUTE_UNUSED)
1092 #else /* defined(_WIN32, implementation for all UNIXes */
1094 /* First defined some macro to identify easily some systems */
1095 #if defined (__FreeBSD__) \
1096 || defined (__OpenBSD__) \
1097 || defined (__NetBSD__) \
1098 || defined (__DragonFly__)
1099 # define BSD
1100 #endif
1102 /* Include every system header we need */
1103 #define _GNU_SOURCE
1104 #include <errno.h>
1105 #include <stdio.h>
1106 #include <stdlib.h>
1107 #include <signal.h>
1108 #include <sys/ioctl.h>
1109 #include <termios.h>
1110 #include <fcntl.h>
1111 #include <string.h>
1112 #include <sys/stat.h>
1113 #include <sys/types.h>
1114 #include <sys/wait.h>
1115 #include <unistd.h>
1116 #if defined (__sun__)
1117 # include <sys/stropts.h>
1118 #endif
1119 #if defined (BSD) || defined (__sun__)
1120 # include <sys/signal.h>
1121 #endif
1122 #if defined (__hpux__)
1123 # include <sys/stropts.h>
1124 #endif
1125 #if defined (__APPLE__)
1126 # include <util.h>
1127 #endif
1128 #if defined (__FreeBSD__)
1129 # include <libutil.h>
1130 #endif
1132 #define CDISABLE _POSIX_VDISABLE
1134 /* POSIX does not specify how to open the master side of a terminal.Several
1135 methods are available (system specific):
1136 1- using a cloning device (USE_CLONE_DEVICE)
1137 2- getpt (USE_GETPT)
1138 3- openpty (USE_OPENPTY)
1140 When using the cloning device method, the macro USE_CLONE_DEVICE should
1141 contains a full path to the adequate device.
1143 When a new system is about to be supported, one of the previous macro should
1144 be set otherwise allocate_pty_desc will return an error
1147 /* Configurable part */
1148 #if defined (__APPLE__) || defined (BSD)
1149 #define USE_OPENPTY
1150 #elif defined (__linux__)
1151 #define USE_GETPT
1152 #elif defined (__sun__)
1153 #define USE_CLONE_DEVICE "/dev/ptmx"
1154 #elif defined (_AIX)
1155 #define USE_CLONE_DEVICE "/dev/ptc"
1156 #elif defined (__hpux__)
1157 /* On HP-UX we use the streamed version. Using the non streamed version is not
1158 recommanded (through "/dev/ptym/clone"). Indeed it seems that there are
1159 issues to detect process terminations. */
1160 #define USE_CLONE_DEVICE "/dev/ptmx"
1161 #endif
1163 /* structure that holds information about the terminal used and the process
1164 connected on the slave side */
1165 typedef struct pty_desc_struct {
1166 int master_fd; /* fd of the master side if the terminal */
1167 int slave_fd; /* fd of the slave side */
1168 char slave_name[32]; /* filename of the slave side */
1169 int child_pid; /* PID of the child process connected to the slave side
1170 of the terminal */
1171 } pty_desc;
1173 /* allocate_pty_desc - allocate a pseudo terminal
1175 * PARAMETERS
1176 * out desc returned pointer to a pty_desc structure containing information
1177 * about the opened pseudo terminal
1178 * RETURN VALUE
1179 * -1 if failed
1180 * 0 if ok
1181 * COMMENTS
1182 * If the function is successful we should have at least the master side fd
1183 * and the slave side filename. On some system, the slave side will also be
1184 * opened. If this is not the case the slave side will be open once we are in
1185 * the child process (note that opening the slave side at this stage will
1186 * failed...).
1189 extern char* ptsname (int);
1191 static int
1192 allocate_pty_desc (pty_desc **desc) {
1194 pty_desc *result;
1195 int status = 0;
1196 int slave_fd = -1;
1197 int master_fd = -1;
1198 char *slave_name = NULL;
1200 #ifdef USE_GETPT
1201 master_fd = getpt ();
1202 #elif defined (USE_OPENPTY)
1203 status = openpty (&master_fd, &slave_fd, NULL, NULL, NULL);
1204 #elif defined (USE_CLONE_DEVICE)
1205 master_fd = open (USE_CLONE_DEVICE, O_RDWR | O_NONBLOCK, 0);
1206 #else
1207 printf ("[error]: terminal support is not configured\n");
1208 return -1;
1209 #endif
1211 /* at this stage we should have the master side fd and status should be 0 */
1212 if (status != 0 || master_fd < 0)
1214 /* If this is not the case close all opened files and return -1 */
1215 printf ("[error]: cannot allocate master side of the pty\n");
1216 if (master_fd >= 0) close (master_fd);
1217 if (slave_fd >= 0) close (slave_fd);
1218 *desc = NULL;
1219 return -1;
1222 /* retrieve the file name of the slave side if necessary */
1223 if (slave_name == NULL) slave_name = (char *) ptsname (master_fd);
1225 /* Now we should have slave file name */
1226 if (slave_name == NULL)
1228 /* If not the case close any opened file and return - 1 */
1229 printf ("[error]: cannot allocate slave side of the pty\n");
1230 if (master_fd >= 0) close (master_fd);
1231 if (slave_fd >= 0) close (slave_fd);
1232 *desc = NULL;
1233 return -1;
1236 #if !defined(__rtems__)
1237 /* grant access to the slave side */
1238 grantpt (master_fd);
1239 /* unlock the terminal */
1240 unlockpt (master_fd);
1241 #endif
1243 /* set desc and return 0 */
1244 result = malloc (sizeof (pty_desc));
1245 result->master_fd = master_fd;
1246 result->slave_fd = slave_fd;
1247 /* the string returned by ptsname or _getpty is a static allocated string. So
1248 we should make a copy */
1249 strncpy (result->slave_name, slave_name, sizeof (result->slave_name) - 1);
1250 result->slave_name[sizeof (result->slave_name) - 1] = '\0';
1251 result->child_pid = -1;
1252 *desc=result;
1253 return 0;
1256 /* some utility macro that make the code of child_setup_tty easier to read */
1257 #define __enable(a, b) ((a) |= (b))
1258 #define __disable(a, b) ((a) &= ~(b))
1260 /* some properties do not exist on all systems. Set their value to 0 in that
1261 case */
1262 #ifndef IUCLC
1263 #define IUCLC 0
1264 #endif
1265 #ifndef OLCUC
1266 #define OLCUC 0
1267 #endif
1268 #ifndef NLDLY
1269 #define NLDLY 0
1270 #define CRDLY 0
1271 #define BSDLY 0
1272 #define VTDLY 0
1273 #define FFDLY 0
1274 #endif
1275 #ifndef TABDLY
1276 #define TABDLY 0
1277 #endif
1279 /* child_setup_tty - set terminal properties
1281 * PARAMETERS
1282 * file descriptor of the slave side of the terminal
1284 * RETURN VALUE
1285 * 0 if success, any other value if failed.
1287 * COMMENTS
1288 * None
1290 static int
1291 child_setup_tty (int fd)
1293 struct termios s;
1294 int status;
1296 /* Ensure that s is filled with 0.
1298 Note that we avoid using bzero for a few reasons:
1299 - On HP-UX and Sun system, there is a bzero function but with
1300 a different signature, thus making the use of bzero more
1301 complicated on these platforms (we used to define a bzero
1302 macro that rewrote calls to bzero into calls to memset);
1303 - bzero is deprecated (marked as LEGACY in POSIX.1-2001). */
1304 memset (&s, 0, sizeof (s));
1306 /* Get the current terminal settings */
1307 status = tcgetattr (fd, &s);
1308 if (status != 0) return -1;
1310 /* Adjust input modes */
1311 __disable (s.c_iflag, IUCLC); /* don't transform to lower case */
1312 __disable (s.c_iflag, ISTRIP); /* don't delete 8th bit */
1314 /* Adjust output modes */
1315 __enable (s.c_oflag, OPOST); /* enable postprocessing */
1316 __disable (s.c_oflag, ONLCR); /* don't map LF to CR-LF */
1317 __disable (s.c_oflag, NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
1318 /* disable delays */
1319 __disable (s.c_oflag, OLCUC); /* don't transform to upper case */
1321 /* Adjust control modes */
1322 s.c_cflag = (s.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
1324 /* Adjust local modes */
1325 __disable (s.c_lflag, ECHO); /* disable echo */
1326 __enable (s.c_lflag, ISIG); /* enable signals */
1327 __enable (s.c_lflag, ICANON); /* erase/kill/eof processing */
1329 /* Adjust control characters */
1330 /* IMPORTANT: we need to ensure that Ctrl-C will trigger an interrupt signal
1331 otherwise send_signal_via_characters will fail */
1332 s.c_cc[VEOF] = 04; /* insure that EOF is Control-D */
1333 s.c_cc[VERASE] = CDISABLE; /* disable erase processing */
1334 s.c_cc[VKILL] = CDISABLE; /* disable kill processing */
1335 s.c_cc[VQUIT] = 28; /* Control-\ */
1336 s.c_cc[VINTR] = 03; /* Control-C */
1337 s.c_cc[VEOL] = CDISABLE;
1338 s.c_cc[VSUSP] = 26; /* Control-Z */
1340 /* push our changes */
1341 status = tcsetattr (fd, TCSADRAIN, &s);
1342 return status;
1345 /* __gnat_setup_communication - interface to the external world. Should be
1346 * called before forking. On Unixes this function only call allocate_pty_desc.
1347 * The Windows implementation (in different part of this file) is very
1348 * different.
1350 * PARAMETERS
1351 * out desc returned pointer to a pty_desc structure
1352 * RETURN VALUE
1353 * 0 if success, -1 otherwise
1355 int __gnat_setup_communication (pty_desc** desc) {
1356 return allocate_pty_desc (desc);
1359 /* __gnat_setup_parent_communication - interface to the external world. Should
1360 * be called after forking in the parent process
1362 * PARAMETERS
1363 * out in_fd
1364 out out_fd
1365 out err_fd fds corresponding to the parent side of the
1366 terminal
1367 in pid_out child process pid
1368 * RETRUN VALUE
1371 void
1372 __gnat_setup_parent_communication
1373 (pty_desc *desc,
1374 int* in_fd, /* input */
1375 int* out_fd, /* output */
1376 int* err_fd, /* error */
1377 int* pid_out)
1380 *in_fd = desc->master_fd;
1381 *out_fd= desc->master_fd;
1382 *err_fd= desc->master_fd;
1383 desc->child_pid = *pid_out;
1386 /* __gnat_setup_winsize - Sets up the size of the terminal
1387 * This lets the process know the size of the terminal
1390 void __gnat_setup_winsize (pty_desc *desc, int rows, int columns) {
1391 #ifdef TIOCGWINSZ
1392 struct winsize s;
1393 s.ws_row = (unsigned short)rows;
1394 s.ws_col = (unsigned short)columns;
1395 s.ws_xpixel = 0;
1396 s.ws_ypixel = 0;
1397 ioctl (desc->master_fd, TIOCSWINSZ, &s);
1398 #ifdef SIGWINCH
1399 if (desc->child_pid > 0) {
1400 /* Let the process know about the change in size */
1401 kill (desc->child_pid, SIGWINCH);
1403 #endif
1404 #endif
1407 /* __gnat_setup_child_communication - interface to external world. Should be
1408 * called after forking in the child process. On Unixes, this function
1409 * first adjust the line setting, set standard output, input and error and
1410 * then spawn the program.
1412 * PARAMETERS
1413 * desc a pty_desc structure containing the pty parameters
1414 * new_argv argv of the program to be spawned
1415 * RETURN VALUE
1416 * this function should not return
1419 __gnat_setup_child_communication
1420 (pty_desc *desc,
1421 char **new_argv,
1422 int Use_Pipes ATTRIBUTE_UNUSED)
1424 int status;
1425 int pid = getpid ();
1427 setsid ();
1429 /* open the slave side of the terminal if necessary */
1430 if (desc->slave_fd == -1)
1431 #if defined (_AIX)
1432 /* On AIX, if the slave process is not opened with O_NDELAY or O_NONBLOCK
1433 then we might have some processes hanging on I/O system calls. Not sure
1434 we can do that for all platforms so do it only on AIX for the moment.
1435 On AIX O_NONBLOCK and O_NDELAY have slightly different meanings. When
1436 reading on the slave fd, in case there is no data available, if O_NDELAY
1437 is set then 0 is returned. If O_NON_BLOCK is -1 is returned. It seems
1438 that interactive programs such as GDB prefer the O_NDELAY behavior.
1439 We chose O_NONBLOCK because it allows us to make the distinction
1440 between a true EOF and an EOF returned because there is no data
1441 available to be read. */
1442 desc->slave_fd = open (desc->slave_name, O_RDWR | O_NONBLOCK, 0);
1443 #else
1444 desc->slave_fd = open (desc->slave_name, O_RDWR, 0);
1445 #endif
1447 #if defined (__sun__) || defined (__hpux__)
1448 /* On systems such as Solaris we are using stream. We need to push the right
1449 "modules" in order to get the expected terminal behaviors. Otherwise
1450 functionalities such as termios are not available. */
1451 ioctl (desc->slave_fd, I_PUSH, "ptem");
1452 ioctl (desc->slave_fd, I_PUSH, "ldterm");
1453 ioctl (desc->slave_fd, I_PUSH, "ttcompat");
1454 #endif
1456 #ifdef TIOCSCTTY
1457 /* make the tty the controlling terminal */
1458 if ((status = ioctl (desc->slave_fd, TIOCSCTTY, 0)) == -1)
1459 _exit (1);
1460 #endif
1462 /* adjust tty settings */
1463 child_setup_tty (desc->slave_fd);
1464 __gnat_setup_winsize (desc, 24, 80); /* To prevent errors in some shells */
1466 /* stdin, stdout and stderr should be now our tty */
1467 dup2 (desc->slave_fd, 0);
1468 dup2 (desc->slave_fd, 1);
1469 dup2 (desc->slave_fd, 2);
1470 if (desc->slave_fd > 2) close (desc->slave_fd);
1472 /* adjust process group settings */
1473 /* ignore failures of the following two commands as the context might not
1474 * allow making those changes. */
1475 setpgid (pid, pid);
1476 tcsetpgrp (0, pid);
1478 /* launch the program */
1479 execvp (new_argv[0], new_argv);
1481 _exit (1);
1484 /* send_signal_via_characters - Send a characters that will trigger a signal
1485 * in the child process.
1487 * PARAMETERS
1488 * desc a pty_desc structure containing terminal information
1489 * int a signal number
1490 * RETURN VALUE
1491 * None
1493 static void
1494 send_signal_via_characters
1495 (pty_desc *desc,
1496 int signal_number)
1498 char ctrl_c = 03;
1499 char ctrl_backslash = 28;
1500 char ctrl_Z = 26;
1502 switch (signal_number)
1504 case SIGINT:
1505 write (desc->master_fd, &ctrl_c, 1); return;
1506 case SIGQUIT:
1507 write (desc->master_fd, &ctrl_backslash, 1); return;
1508 case SIGTSTP:
1509 write (desc->master_fd, &ctrl_Z, 1); return;
1513 /* __gnat_interrupt_process - interrupt the child process
1515 * PARAMETERS
1516 * desc a pty_desc structure
1519 __gnat_interrupt_process (pty_desc *desc)
1521 send_signal_via_characters (desc, SIGINT);
1522 return 0;
1525 /* __gnat_interrupt_pid - interrupt a process group
1527 * PARAMETERS
1528 * pid pid of the process to interrupt
1531 __gnat_interrupt_pid (int pid)
1533 kill (-pid, SIGINT);
1534 return 0;
1537 /* __gnat_terminate_process - kill a child process
1539 * PARAMETERS
1540 * desc pty_desc structure
1542 int __gnat_terminate_process (pty_desc *desc)
1544 return kill (desc->child_pid, SIGKILL);
1547 /* __gnat_terminate_pid - kill a process
1549 * PARAMETERS
1550 * pid unix process id
1553 __gnat_terminate_pid (int pid)
1555 return kill (pid, SIGKILL);
1558 /* __gnat_tty_waitpid - wait for the child process to die
1560 * PARAMETERS
1561 * desc pty_desc structure
1562 * RETURN VALUE
1563 * exit status of the child process
1566 __gnat_tty_waitpid (pty_desc *desc, int blocking)
1568 int status = -1;
1569 int options = 0;
1571 if (blocking) {
1572 options = 0;
1573 } else {
1574 options = WNOHANG;
1576 waitpid (desc->child_pid, &status, options);
1577 if WIFEXITED (status) {
1578 status = WEXITSTATUS (status);
1580 return status;
1583 /* __gnat_tty_supported - Are tty supported ?
1585 * RETURN VALUE
1586 * always 1 on Unix systems
1589 __gnat_tty_supported (void)
1591 return 1;
1594 /* __gnat_free_process - free a pty_desc structure
1596 * PARAMETERS
1597 * in out desc: a pty desc structure
1599 void
1600 __gnat_free_process (pty_desc** desc)
1602 free (*desc);
1603 *desc = NULL;
1606 /* __gnat_send_header - dummy function. this interface is only used on Windows */
1607 void
1608 __gnat_send_header (pty_desc* desc ATTRIBUTE_UNUSED,
1609 char header[5] ATTRIBUTE_UNUSED,
1610 int size ATTRIBUTE_UNUSED,
1611 int *ret ATTRIBUTE_UNUSED)
1613 *ret = 0;
1616 /* __gnat_reset_tty - reset line setting
1618 * PARAMETERS
1619 * desc: a pty_desc structure
1621 void
1622 __gnat_reset_tty (pty_desc* desc)
1624 child_setup_tty (desc->master_fd);
1627 /* __gnat_new_tty - allocate a new terminal
1629 * RETURN VALUE
1630 * a pty_desc structure
1632 pty_desc *
1633 __gnat_new_tty (void)
1635 int status;
1636 pty_desc* desc = NULL;
1637 if ((status = allocate_pty_desc (&desc)))
1638 child_setup_tty (desc->master_fd);
1639 return desc;
1642 /* __gnat_close_tty - close a terminal
1644 * PARAMETERS
1645 * desc a pty_desc strucure
1647 void __gnat_close_tty (pty_desc* desc)
1649 if (desc->master_fd >= 0) { close (desc->master_fd); desc->master_fd = -1; }
1650 if (desc->slave_fd >= 0) { close (desc->slave_fd); desc->slave_fd = -1; }
1653 /* __gnat_tty_name - return slave side device name
1655 * PARAMETERS
1656 * desc a pty_desc strucure
1657 * RETURN VALUE
1658 * a string
1660 char *
1661 __gnat_tty_name (pty_desc* desc)
1663 return desc->slave_name;
1666 /* __gnat_tty_name - return master side fd
1668 * PARAMETERS
1669 * desc a pty_desc strucure
1670 * RETURN VALUE
1671 * a fd
1674 __gnat_tty_fd (pty_desc* desc)
1676 return desc->master_fd;
1679 #endif /* WIN32 */