1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * BeBox port Copyright 1997 by Olaf Seibert.
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
11 * os_beos.c Additional stuff for BeOS (rest is in os_unix.c)
16 #include <kernel/OS.h>
19 #if USE_THREAD_FOR_INPUT_WITH_TIMEOUT
21 #ifdef PROTO /* making prototypes on Unix */
27 signed char charcount
;
28 sem_id character_present
;
29 sem_id character_wanted
;
30 thread_id read_thread_id
;
32 #define TRY_ABORT 0 /* This code does not work so turn it off. */
36 mostly_ignore(int sig
)
42 read_thread(void *dummy
)
44 signal(SIGINT
, SIG_IGN
);
45 signal(SIGQUIT
, SIG_IGN
);
47 signal(SIGUSR1
, mostly_ignore
);
51 if (acquire_sem(character_wanted
) != B_NO_ERROR
)
53 charcount
= read(read_cmd_fd
, &charbuf
, 1);
54 release_sem(character_present
);
61 beos_cleanup_read_thread(void)
63 if (character_present
> 0)
64 delete_sem(character_present
);
65 character_present
= 0;
66 if (read_thread_id
> 0)
67 kill_thread(read_thread_id
);
74 * select() emulation. Hopefully, in DR9 there will be something
75 * useful supplied by the system. ... Alas, not. Not in AAPR, nor
76 * in PR or even PR2... R3 then maybe? I don't think so!
80 beos_select(int nbits
,
84 struct timeval
*timeout
)
89 /* select is purely being used for delay */
90 snooze(timeout
->tv_sec
* 1e6
+ timeout
->tv_usec
);
95 * This does not seem to work either. Reads here are not supposed to
96 * block indefinitely, yet they do. This is most annoying.
98 if (FD_ISSET(0, rbits
)) {
105 tnew
.c_lflag
&= ~ICANON
;
107 tnew
.c_cc
[VTIME
] = timeout
->tv_sec
* 10 + timeout
->tv_usec
/ 100000;
108 tcsetattr(0, TCSANOW
, &tnew
);
110 count
= read(0, &cbuf
, sizeof(cbuf
));
111 tcsetattr(0, TCSANOW
, &told
);
113 add_to_input_buf(&cbuf
[0], count
);
119 #if USE_THREAD_FOR_INPUT_WITH_TIMEOUT
121 * Check if the operation is really on stdin...
123 if (FD_ISSET(read_cmd_fd
, rbits
))
128 * Is this the first time through?
129 * Then start up the thread and initialise the semaphores.
131 if (character_present
== 0) {
132 character_present
= create_sem(0, "vim character_present");
133 character_wanted
= create_sem(1, "vim character_wanted");
134 read_thread_id
= spawn_thread(read_thread
, "vim async read",
135 B_NORMAL_PRIORITY
, NULL
);
136 atexit(beos_cleanup_read_thread
);
137 resume_thread(read_thread_id
);
140 /* timeout == NULL means "indefinitely" */
142 tmo
= timeout
->tv_sec
* 1e6
+ timeout
->tv_usec
;
143 /* 0 means "don't wait, which is impossible to do exactly. */
148 release_sem(character_wanted
);
151 acquired
= acquire_sem_etc(character_present
, 1, B_TIMEOUT
, tmo
);
153 acquired
= acquire_sem(character_present
);
154 if (acquired
== B_NO_ERROR
) {
156 add_to_input_buf(&charbuf
, 1);
158 release_sem(character_wanted
);
164 release_sem(character_wanted
);
173 * Timeout occurred. Break the read() call by sending
174 * a signal. Problem: it may be just read()ing it now.
175 * Therefore we still have to finish the handshake with
176 * the thread and maybe remember the character.
178 kill(read_thread_id
, SIGUSR1
);
180 * If some other error occurred, don't hang now.
181 * (We will most likely hang later anyway...)
183 if (acquired
== B_TIMED_OUT
)
184 acquire_sem(character_present
);
186 add_to_input_buf(&charbuf
, 1);