[4] | 1 | /* This file contains the terminal driver, both for the IBM console and regular
|
---|
| 2 | * ASCII terminals. It handles only the device-independent part of a TTY, the
|
---|
| 3 | * device dependent parts are in console.c, rs232.c, etc. This file contains
|
---|
| 4 | * two main entry points, tty_task() and tty_wakeup(), and several minor entry
|
---|
| 5 | * points for use by the device-dependent code.
|
---|
| 6 | *
|
---|
| 7 | * The device-independent part accepts "keyboard" input from the device-
|
---|
| 8 | * dependent part, performs input processing (special key interpretation),
|
---|
| 9 | * and sends the input to a process reading from the TTY. Output to a TTY
|
---|
| 10 | * is sent to the device-dependent code for output processing and "screen"
|
---|
| 11 | * display. Input processing is done by the device by calling 'in_process'
|
---|
| 12 | * on the input characters, output processing may be done by the device itself
|
---|
| 13 | * or by calling 'out_process'. The TTY takes care of input queuing, the
|
---|
| 14 | * device does the output queuing. If a device receives an external signal,
|
---|
| 15 | * like an interrupt, then it causes tty_wakeup() to be run by the CLOCK task
|
---|
| 16 | * to, you guessed it, wake up the TTY to check if input or output can
|
---|
| 17 | * continue.
|
---|
| 18 | *
|
---|
| 19 | * The valid messages and their parameters are:
|
---|
| 20 | *
|
---|
| 21 | * HARD_INT: output has been completed or input has arrived
|
---|
| 22 | * SYS_SIG: e.g., MINIX wants to shutdown; run code to cleanly stop
|
---|
| 23 | * DEV_READ: a process wants to read from a terminal
|
---|
| 24 | * DEV_WRITE: a process wants to write on a terminal
|
---|
| 25 | * DEV_IOCTL: a process wants to change a terminal's parameters
|
---|
| 26 | * DEV_OPEN: a tty line has been opened
|
---|
| 27 | * DEV_CLOSE: a tty line has been closed
|
---|
| 28 | * DEV_SELECT: start select notification request
|
---|
| 29 | * DEV_STATUS: FS wants to know status for SELECT or REVIVE
|
---|
| 30 | * CANCEL: terminate a previous incomplete system call immediately
|
---|
| 31 | *
|
---|
| 32 | * m_type TTY_LINE PROC_NR COUNT TTY_SPEK TTY_FLAGS ADDRESS
|
---|
| 33 | * ---------------------------------------------------------------------------
|
---|
| 34 | * | HARD_INT | | | | | | |
|
---|
| 35 | * |-------------+---------+---------+---------+---------+---------+---------|
|
---|
| 36 | * | SYS_SIG | sig set | | | | | |
|
---|
| 37 | * |-------------+---------+---------+---------+---------+---------+---------|
|
---|
| 38 | * | DEV_READ |minor dev| proc nr | count | O_NONBLOCK| buf ptr |
|
---|
| 39 | * |-------------+---------+---------+---------+---------+---------+---------|
|
---|
| 40 | * | DEV_WRITE |minor dev| proc nr | count | | | buf ptr |
|
---|
| 41 | * |-------------+---------+---------+---------+---------+---------+---------|
|
---|
| 42 | * | DEV_IOCTL |minor dev| proc nr |func code|erase etc| flags | |
|
---|
| 43 | * |-------------+---------+---------+---------+---------+---------+---------|
|
---|
| 44 | * | DEV_OPEN |minor dev| proc nr | O_NOCTTY| | | |
|
---|
| 45 | * |-------------+---------+---------+---------+---------+---------+---------|
|
---|
| 46 | * | DEV_CLOSE |minor dev| proc nr | | | | |
|
---|
| 47 | * |-------------+---------+---------+---------+---------+---------+---------|
|
---|
| 48 | * | DEV_STATUS | | | | | | |
|
---|
| 49 | * |-------------+---------+---------+---------+---------+---------+---------|
|
---|
| 50 | * | CANCEL |minor dev| proc nr | | | | |
|
---|
| 51 | * ---------------------------------------------------------------------------
|
---|
| 52 | *
|
---|
| 53 | * Changes:
|
---|
| 54 | * Jan 20, 2004 moved TTY driver to user-space (Jorrit N. Herder)
|
---|
| 55 | * Sep 20, 2004 local timer management/ sync alarms (Jorrit N. Herder)
|
---|
| 56 | * Jul 13, 2004 support for function key observers (Jorrit N. Herder)
|
---|
| 57 | */
|
---|
| 58 |
|
---|
| 59 | #include "../drivers.h"
|
---|
| 60 | #include "../drivers.h"
|
---|
| 61 | #include <termios.h>
|
---|
| 62 | #include <sys/ioc_tty.h>
|
---|
| 63 | #include <signal.h>
|
---|
| 64 | #include <minix/callnr.h>
|
---|
| 65 | #include <minix/keymap.h>
|
---|
| 66 | #include "tty.h"
|
---|
| 67 |
|
---|
| 68 | #include <sys/time.h>
|
---|
| 69 | #include <sys/select.h>
|
---|
| 70 |
|
---|
| 71 | extern int irq_hook_id;
|
---|
| 72 |
|
---|
| 73 | unsigned long kbd_irq_set = 0;
|
---|
| 74 | unsigned long rs_irq_set = 0;
|
---|
| 75 |
|
---|
| 76 | /* Address of a tty structure. */
|
---|
| 77 | #define tty_addr(line) (&tty_table[line])
|
---|
| 78 |
|
---|
| 79 | /* Macros for magic tty types. */
|
---|
| 80 | #define isconsole(tp) ((tp) < tty_addr(NR_CONS))
|
---|
| 81 | #define ispty(tp) ((tp) >= tty_addr(NR_CONS+NR_RS_LINES))
|
---|
| 82 |
|
---|
| 83 | /* Macros for magic tty structure pointers. */
|
---|
| 84 | #define FIRST_TTY tty_addr(0)
|
---|
| 85 | #define END_TTY tty_addr(sizeof(tty_table) / sizeof(tty_table[0]))
|
---|
| 86 |
|
---|
| 87 | /* A device exists if at least its 'devread' function is defined. */
|
---|
| 88 | #define tty_active(tp) ((tp)->tty_devread != NULL)
|
---|
| 89 |
|
---|
| 90 | /* RS232 lines or pseudo terminals can be completely configured out. */
|
---|
| 91 | #if NR_RS_LINES == 0
|
---|
| 92 | #define rs_init(tp) ((void) 0)
|
---|
| 93 | #endif
|
---|
| 94 | #if NR_PTYS == 0
|
---|
| 95 | #define pty_init(tp) ((void) 0)
|
---|
| 96 | #define do_pty(tp, mp) ((void) 0)
|
---|
| 97 | #endif
|
---|
| 98 |
|
---|
| 99 | FORWARD _PROTOTYPE( void tty_timed_out, (timer_t *tp) );
|
---|
| 100 | FORWARD _PROTOTYPE( void expire_timers, (void) );
|
---|
| 101 | FORWARD _PROTOTYPE( void settimer, (tty_t *tty_ptr, int enable) );
|
---|
| 102 | FORWARD _PROTOTYPE( void do_cancel, (tty_t *tp, message *m_ptr) );
|
---|
| 103 | FORWARD _PROTOTYPE( void do_ioctl, (tty_t *tp, message *m_ptr) );
|
---|
| 104 | FORWARD _PROTOTYPE( void do_open, (tty_t *tp, message *m_ptr) );
|
---|
| 105 | FORWARD _PROTOTYPE( void do_close, (tty_t *tp, message *m_ptr) );
|
---|
| 106 | FORWARD _PROTOTYPE( void do_read, (tty_t *tp, message *m_ptr) );
|
---|
| 107 | FORWARD _PROTOTYPE( void do_write, (tty_t *tp, message *m_ptr) );
|
---|
| 108 | FORWARD _PROTOTYPE( void do_select, (tty_t *tp, message *m_ptr) );
|
---|
| 109 | FORWARD _PROTOTYPE( void do_status, (message *m_ptr) );
|
---|
| 110 | FORWARD _PROTOTYPE( void in_transfer, (tty_t *tp) );
|
---|
| 111 | FORWARD _PROTOTYPE( int tty_echo, (tty_t *tp, int ch) );
|
---|
| 112 | FORWARD _PROTOTYPE( void rawecho, (tty_t *tp, int ch) );
|
---|
| 113 | FORWARD _PROTOTYPE( int back_over, (tty_t *tp) );
|
---|
| 114 | FORWARD _PROTOTYPE( void reprint, (tty_t *tp) );
|
---|
| 115 | FORWARD _PROTOTYPE( void dev_ioctl, (tty_t *tp) );
|
---|
| 116 | FORWARD _PROTOTYPE( void setattr, (tty_t *tp) );
|
---|
| 117 | FORWARD _PROTOTYPE( void tty_icancel, (tty_t *tp) );
|
---|
| 118 | FORWARD _PROTOTYPE( void tty_init, (void) );
|
---|
| 119 |
|
---|
| 120 | /* Default attributes. */
|
---|
| 121 | PRIVATE struct termios termios_defaults = {
|
---|
| 122 | TINPUT_DEF, TOUTPUT_DEF, TCTRL_DEF, TLOCAL_DEF, TSPEED_DEF, TSPEED_DEF,
|
---|
| 123 | {
|
---|
| 124 | TEOF_DEF, TEOL_DEF, TERASE_DEF, TINTR_DEF, TKILL_DEF, TMIN_DEF,
|
---|
| 125 | TQUIT_DEF, TTIME_DEF, TSUSP_DEF, TSTART_DEF, TSTOP_DEF,
|
---|
| 126 | TREPRINT_DEF, TLNEXT_DEF, TDISCARD_DEF,
|
---|
| 127 | },
|
---|
| 128 | };
|
---|
| 129 | PRIVATE struct winsize winsize_defaults; /* = all zeroes */
|
---|
| 130 |
|
---|
| 131 | /* Global variables for the TTY task (declared extern in tty.h). */
|
---|
| 132 | PUBLIC tty_t tty_table[NR_CONS+NR_RS_LINES+NR_PTYS];
|
---|
| 133 | PUBLIC int ccurrent; /* currently active console */
|
---|
| 134 | PUBLIC timer_t *tty_timers; /* queue of TTY timers */
|
---|
| 135 | PUBLIC clock_t tty_next_timeout; /* time that the next alarm is due */
|
---|
| 136 | PUBLIC struct machine machine; /* kernel environment variables */
|
---|
| 137 |
|
---|
| 138 | /*===========================================================================*
|
---|
| 139 | * tty_task *
|
---|
| 140 | *===========================================================================*/
|
---|
| 141 | PUBLIC void main(void)
|
---|
| 142 | {
|
---|
| 143 | /* Main routine of the terminal task. */
|
---|
| 144 |
|
---|
| 145 | message tty_mess; /* buffer for all incoming messages */
|
---|
| 146 | unsigned line;
|
---|
| 147 | int s;
|
---|
| 148 | char *types[] = {"task","driver","server", "user"};
|
---|
| 149 | register struct proc *rp;
|
---|
| 150 | register tty_t *tp;
|
---|
| 151 |
|
---|
| 152 | /* Initialize the TTY driver. */
|
---|
| 153 | tty_init();
|
---|
| 154 |
|
---|
| 155 | /* Get kernel environment (protected_mode, pc_at and ega are needed). */
|
---|
| 156 | if (OK != (s=sys_getmachine(&machine))) {
|
---|
| 157 | panic("TTY","Couldn't obtain kernel environment.", s);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /* Final one-time keyboard initialization. */
|
---|
| 161 | kb_init_once();
|
---|
| 162 |
|
---|
| 163 | printf("\n");
|
---|
| 164 |
|
---|
| 165 | while (TRUE) {
|
---|
| 166 |
|
---|
| 167 | /* Check for and handle any events on any of the ttys. */
|
---|
| 168 | for (tp = FIRST_TTY; tp < END_TTY; tp++) {
|
---|
| 169 | if (tp->tty_events) handle_events(tp);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /* Get a request message. */
|
---|
| 173 | receive(ANY, &tty_mess);
|
---|
| 174 |
|
---|
| 175 | /* First handle all kernel notification types that the TTY supports.
|
---|
| 176 | * - An alarm went off, expire all timers and handle the events.
|
---|
| 177 | * - A hardware interrupt also is an invitation to check for events.
|
---|
| 178 | * - A new kernel message is available for printing.
|
---|
| 179 | * - Reset the console on system shutdown.
|
---|
| 180 | * Then see if this message is different from a normal device driver
|
---|
| 181 | * request and should be handled separately. These extra functions
|
---|
| 182 | * do not operate on a device, in constrast to the driver requests.
|
---|
| 183 | */
|
---|
| 184 | switch (tty_mess.m_type) {
|
---|
| 185 | case SYN_ALARM: /* fall through */
|
---|
| 186 | expire_timers(); /* run watchdogs of expired timers */
|
---|
| 187 | continue; /* contine to check for events */
|
---|
| 188 | case HARD_INT: { /* hardware interrupt notification */
|
---|
| 189 | if (tty_mess.NOTIFY_ARG & kbd_irq_set)
|
---|
| 190 | kbd_interrupt(&tty_mess);/* fetch chars from keyboard */
|
---|
| 191 | #if NR_RS_LINES > 0
|
---|
| 192 | if (tty_mess.NOTIFY_ARG & rs_irq_set)
|
---|
| 193 | rs_interrupt(&tty_mess);/* serial I/O */
|
---|
| 194 | #endif
|
---|
| 195 | expire_timers(); /* run watchdogs of expired timers */
|
---|
| 196 | continue; /* contine to check for events */
|
---|
| 197 | }
|
---|
| 198 | case SYS_SIG: { /* system signal */
|
---|
| 199 | sigset_t sigset = (sigset_t) tty_mess.NOTIFY_ARG;
|
---|
| 200 |
|
---|
| 201 | if (sigismember(&sigset, SIGKSTOP)) {
|
---|
| 202 | cons_stop(); /* switch to primary console */
|
---|
| 203 | if (irq_hook_id != -1) {
|
---|
| 204 | sys_irqdisable(&irq_hook_id);
|
---|
| 205 | sys_irqrmpolicy(KEYBOARD_IRQ, &irq_hook_id);
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | if (sigismember(&sigset, SIGTERM)) cons_stop();
|
---|
| 209 | if (sigismember(&sigset, SIGKMESS)) do_new_kmess(&tty_mess);
|
---|
| 210 | continue;
|
---|
| 211 | }
|
---|
| 212 | case PANIC_DUMPS: /* allow panic dumps */
|
---|
| 213 | cons_stop(); /* switch to primary console */
|
---|
| 214 | do_panic_dumps(&tty_mess);
|
---|
| 215 | continue;
|
---|
| 216 | case DIAGNOSTICS: /* a server wants to print some */
|
---|
| 217 | do_diagnostics(&tty_mess);
|
---|
| 218 | continue;
|
---|
| 219 | case FKEY_CONTROL: /* (un)register a fkey observer */
|
---|
| 220 | do_fkey_ctl(&tty_mess);
|
---|
| 221 | continue;
|
---|
| 222 | default: /* should be a driver request */
|
---|
| 223 | ; /* do nothing; end switch */
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | /* Only device requests should get to this point. All requests,
|
---|
| 227 | * except DEV_STATUS, have a minor device number. Check this
|
---|
| 228 | * exception and get the minor device number otherwise.
|
---|
| 229 | */
|
---|
| 230 | if (tty_mess.m_type == DEV_STATUS) {
|
---|
| 231 | do_status(&tty_mess);
|
---|
| 232 | continue;
|
---|
| 233 | }
|
---|
| 234 | line = tty_mess.TTY_LINE;
|
---|
| 235 | if ((line - CONS_MINOR) < NR_CONS) {
|
---|
| 236 | tp = tty_addr(line - CONS_MINOR);
|
---|
| 237 | } else if (line == LOG_MINOR) {
|
---|
| 238 | tp = tty_addr(0);
|
---|
| 239 | } else if ((line - RS232_MINOR) < NR_RS_LINES) {
|
---|
| 240 | tp = tty_addr(line - RS232_MINOR + NR_CONS);
|
---|
| 241 | } else if ((line - TTYPX_MINOR) < NR_PTYS) {
|
---|
| 242 | tp = tty_addr(line - TTYPX_MINOR + NR_CONS + NR_RS_LINES);
|
---|
| 243 | } else if ((line - PTYPX_MINOR) < NR_PTYS) {
|
---|
| 244 | tp = tty_addr(line - PTYPX_MINOR + NR_CONS + NR_RS_LINES);
|
---|
| 245 | if (tty_mess.m_type != DEV_IOCTL) {
|
---|
| 246 | do_pty(tp, &tty_mess);
|
---|
| 247 | continue;
|
---|
| 248 | }
|
---|
| 249 | } else {
|
---|
| 250 | tp = NULL;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /* If the device doesn't exist or is not configured return ENXIO. */
|
---|
| 254 | if (tp == NULL || ! tty_active(tp)) {
|
---|
| 255 | printf("Warning, TTY got illegal request %d from %d\n",
|
---|
| 256 | tty_mess.m_type, tty_mess.m_source);
|
---|
| 257 | tty_reply(TASK_REPLY, tty_mess.m_source,
|
---|
| 258 | tty_mess.PROC_NR, ENXIO);
|
---|
| 259 | continue;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /* Execute the requested device driver function. */
|
---|
| 263 | switch (tty_mess.m_type) {
|
---|
| 264 | case DEV_READ: do_read(tp, &tty_mess); break;
|
---|
| 265 | case DEV_WRITE: do_write(tp, &tty_mess); break;
|
---|
| 266 | case DEV_IOCTL: do_ioctl(tp, &tty_mess); break;
|
---|
| 267 | case DEV_OPEN: do_open(tp, &tty_mess); break;
|
---|
| 268 | case DEV_CLOSE: do_close(tp, &tty_mess); break;
|
---|
| 269 | case DEV_SELECT: do_select(tp, &tty_mess); break;
|
---|
| 270 | case CANCEL: do_cancel(tp, &tty_mess); break;
|
---|
| 271 | default:
|
---|
| 272 | printf("Warning, TTY got unexpected request %d from %d\n",
|
---|
| 273 | tty_mess.m_type, tty_mess.m_source);
|
---|
| 274 | tty_reply(TASK_REPLY, tty_mess.m_source,
|
---|
| 275 | tty_mess.PROC_NR, EINVAL);
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /*===========================================================================*
|
---|
| 281 | * do_status *
|
---|
| 282 | *===========================================================================*/
|
---|
| 283 | PRIVATE void do_status(m_ptr)
|
---|
| 284 | message *m_ptr;
|
---|
| 285 | {
|
---|
| 286 | register struct tty *tp;
|
---|
| 287 | int event_found;
|
---|
| 288 | int status;
|
---|
| 289 | int ops;
|
---|
| 290 |
|
---|
| 291 | /* Check for select or revive events on any of the ttys. If we found an,
|
---|
| 292 | * event return a single status message for it. The FS will make another
|
---|
| 293 | * call to see if there is more.
|
---|
| 294 | */
|
---|
| 295 | event_found = 0;
|
---|
| 296 | for (tp = FIRST_TTY; tp < END_TTY; tp++) {
|
---|
| 297 | if ((ops = select_try(tp, tp->tty_select_ops)) &&
|
---|
| 298 | tp->tty_select_proc == m_ptr->m_source) {
|
---|
| 299 |
|
---|
| 300 | /* I/O for a selected minor device is ready. */
|
---|
| 301 | m_ptr->m_type = DEV_IO_READY;
|
---|
| 302 | m_ptr->DEV_MINOR = tp->tty_index;
|
---|
| 303 | m_ptr->DEV_SEL_OPS = ops;
|
---|
| 304 |
|
---|
| 305 | tp->tty_select_ops &= ~ops; /* unmark select event */
|
---|
| 306 | event_found = 1;
|
---|
| 307 | break;
|
---|
| 308 | }
|
---|
| 309 | else if (tp->tty_inrevived && tp->tty_incaller == m_ptr->m_source) {
|
---|
| 310 |
|
---|
| 311 | /* Suspended request finished. Send a REVIVE. */
|
---|
| 312 | m_ptr->m_type = DEV_REVIVE;
|
---|
| 313 | m_ptr->REP_PROC_NR = tp->tty_inproc;
|
---|
| 314 | m_ptr->REP_STATUS = tp->tty_incum;
|
---|
| 315 |
|
---|
| 316 | tp->tty_inleft = tp->tty_incum = 0;
|
---|
| 317 | tp->tty_inrevived = 0; /* unmark revive event */
|
---|
| 318 | event_found = 1;
|
---|
| 319 | break;
|
---|
| 320 | }
|
---|
| 321 | else if (tp->tty_outrevived && tp->tty_outcaller == m_ptr->m_source) {
|
---|
| 322 |
|
---|
| 323 | /* Suspended request finished. Send a REVIVE. */
|
---|
| 324 | m_ptr->m_type = DEV_REVIVE;
|
---|
| 325 | m_ptr->REP_PROC_NR = tp->tty_outproc;
|
---|
| 326 | m_ptr->REP_STATUS = tp->tty_outcum;
|
---|
| 327 |
|
---|
| 328 | tp->tty_outcum = 0;
|
---|
| 329 | tp->tty_outrevived = 0; /* unmark revive event */
|
---|
| 330 | event_found = 1;
|
---|
| 331 | break;
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | #if NR_PTYS > 0
|
---|
| 336 | if (!event_found)
|
---|
| 337 | event_found = pty_status(m_ptr);
|
---|
| 338 | #endif
|
---|
| 339 |
|
---|
| 340 | if (! event_found) {
|
---|
| 341 | /* No events of interest were found. Return an empty message. */
|
---|
| 342 | m_ptr->m_type = DEV_NO_STATUS;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | /* Almost done. Send back the reply message to the caller. */
|
---|
| 346 | if ((status = send(m_ptr->m_source, m_ptr)) != OK) {
|
---|
| 347 | panic("TTY","send in do_status failed, status\n", status);
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /*===========================================================================*
|
---|
| 352 | * do_read *
|
---|
| 353 | *===========================================================================*/
|
---|
| 354 | PRIVATE void do_read(tp, m_ptr)
|
---|
| 355 | register tty_t *tp; /* pointer to tty struct */
|
---|
| 356 | register message *m_ptr; /* pointer to message sent to the task */
|
---|
| 357 | {
|
---|
| 358 | /* A process wants to read from a terminal. */
|
---|
| 359 | int r, status;
|
---|
| 360 | phys_bytes phys_addr;
|
---|
| 361 |
|
---|
| 362 | /* Check if there is already a process hanging in a read, check if the
|
---|
| 363 | * parameters are correct, do I/O.
|
---|
| 364 | */
|
---|
| 365 | if (tp->tty_inleft > 0) {
|
---|
| 366 | r = EIO;
|
---|
| 367 | } else
|
---|
| 368 | if (m_ptr->COUNT <= 0) {
|
---|
| 369 | r = EINVAL;
|
---|
| 370 | } else
|
---|
| 371 | if (sys_umap(m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS, m_ptr->COUNT,
|
---|
| 372 | &phys_addr) != OK) {
|
---|
| 373 | r = EFAULT;
|
---|
| 374 | } else {
|
---|
| 375 | /* Copy information from the message to the tty struct. */
|
---|
| 376 | tp->tty_inrepcode = TASK_REPLY;
|
---|
| 377 | tp->tty_incaller = m_ptr->m_source;
|
---|
| 378 | tp->tty_inproc = m_ptr->PROC_NR;
|
---|
| 379 | tp->tty_in_vir = (vir_bytes) m_ptr->ADDRESS;
|
---|
| 380 | tp->tty_inleft = m_ptr->COUNT;
|
---|
| 381 |
|
---|
| 382 | if (!(tp->tty_termios.c_lflag & ICANON)
|
---|
| 383 | && tp->tty_termios.c_cc[VTIME] > 0) {
|
---|
| 384 | if (tp->tty_termios.c_cc[VMIN] == 0) {
|
---|
| 385 | /* MIN & TIME specify a read timer that finishes the
|
---|
| 386 | * read in TIME/10 seconds if no bytes are available.
|
---|
| 387 | */
|
---|
| 388 | settimer(tp, TRUE);
|
---|
| 389 | tp->tty_min = 1;
|
---|
| 390 | } else {
|
---|
| 391 | /* MIN & TIME specify an inter-byte timer that may
|
---|
| 392 | * have to be cancelled if there are no bytes yet.
|
---|
| 393 | */
|
---|
| 394 | if (tp->tty_eotct == 0) {
|
---|
| 395 | settimer(tp, FALSE);
|
---|
| 396 | tp->tty_min = tp->tty_termios.c_cc[VMIN];
|
---|
| 397 | }
|
---|
| 398 | }
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | /* Anything waiting in the input buffer? Clear it out... */
|
---|
| 402 | in_transfer(tp);
|
---|
| 403 | /* ...then go back for more. */
|
---|
| 404 | handle_events(tp);
|
---|
| 405 | if (tp->tty_inleft == 0) {
|
---|
| 406 | if (tp->tty_select_ops)
|
---|
| 407 | select_retry(tp);
|
---|
| 408 | return; /* already done */
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | /* There were no bytes in the input queue available, so either suspend
|
---|
| 412 | * the caller or break off the read if nonblocking.
|
---|
| 413 | */
|
---|
| 414 | if (m_ptr->TTY_FLAGS & O_NONBLOCK) {
|
---|
| 415 | r = EAGAIN; /* cancel the read */
|
---|
| 416 | tp->tty_inleft = tp->tty_incum = 0;
|
---|
| 417 | } else {
|
---|
| 418 | r = SUSPEND; /* suspend the caller */
|
---|
| 419 | tp->tty_inrepcode = REVIVE;
|
---|
| 420 | }
|
---|
| 421 | }
|
---|
| 422 | tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, r);
|
---|
| 423 | if (tp->tty_select_ops)
|
---|
| 424 | select_retry(tp);
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | /*===========================================================================*
|
---|
| 428 | * do_write *
|
---|
| 429 | *===========================================================================*/
|
---|
| 430 | PRIVATE void do_write(tp, m_ptr)
|
---|
| 431 | register tty_t *tp;
|
---|
| 432 | register message *m_ptr; /* pointer to message sent to the task */
|
---|
| 433 | {
|
---|
| 434 | /* A process wants to write on a terminal. */
|
---|
| 435 | int r;
|
---|
| 436 | phys_bytes phys_addr;
|
---|
| 437 |
|
---|
| 438 | /* Check if there is already a process hanging in a write, check if the
|
---|
| 439 | * parameters are correct, do I/O.
|
---|
| 440 | */
|
---|
| 441 | if (tp->tty_outleft > 0) {
|
---|
| 442 | r = EIO;
|
---|
| 443 | } else
|
---|
| 444 | if (m_ptr->COUNT <= 0) {
|
---|
| 445 | r = EINVAL;
|
---|
| 446 | } else
|
---|
| 447 | if (sys_umap(m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS, m_ptr->COUNT,
|
---|
| 448 | &phys_addr) != OK) {
|
---|
| 449 | r = EFAULT;
|
---|
| 450 | } else {
|
---|
| 451 | /* Copy message parameters to the tty structure. */
|
---|
| 452 | tp->tty_outrepcode = TASK_REPLY;
|
---|
| 453 | tp->tty_outcaller = m_ptr->m_source;
|
---|
| 454 | tp->tty_outproc = m_ptr->PROC_NR;
|
---|
| 455 | tp->tty_out_vir = (vir_bytes) m_ptr->ADDRESS;
|
---|
| 456 | tp->tty_outleft = m_ptr->COUNT;
|
---|
| 457 |
|
---|
| 458 | /* Try to write. */
|
---|
| 459 | handle_events(tp);
|
---|
| 460 | if (tp->tty_outleft == 0)
|
---|
| 461 | return; /* already done */
|
---|
| 462 |
|
---|
| 463 | /* None or not all the bytes could be written, so either suspend the
|
---|
| 464 | * caller or break off the write if nonblocking.
|
---|
| 465 | */
|
---|
| 466 | if (m_ptr->TTY_FLAGS & O_NONBLOCK) { /* cancel the write */
|
---|
| 467 | r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
|
---|
| 468 | tp->tty_outleft = tp->tty_outcum = 0;
|
---|
| 469 | } else {
|
---|
| 470 | r = SUSPEND; /* suspend the caller */
|
---|
| 471 | tp->tty_outrepcode = REVIVE;
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
| 474 | tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, r);
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | /*===========================================================================*
|
---|
| 478 | * do_ioctl *
|
---|
| 479 | *===========================================================================*/
|
---|
| 480 | PRIVATE void do_ioctl(tp, m_ptr)
|
---|
| 481 | register tty_t *tp;
|
---|
| 482 | message *m_ptr; /* pointer to message sent to task */
|
---|
| 483 | {
|
---|
| 484 | /* Perform an IOCTL on this terminal. Posix termios calls are handled
|
---|
| 485 | * by the IOCTL system call
|
---|
| 486 | */
|
---|
| 487 |
|
---|
| 488 | int r;
|
---|
| 489 | union {
|
---|
| 490 | int i;
|
---|
| 491 | } param;
|
---|
| 492 | size_t size;
|
---|
| 493 |
|
---|
| 494 | /* Size of the ioctl parameter. */
|
---|
| 495 | switch (m_ptr->TTY_REQUEST) {
|
---|
| 496 | case TCGETS: /* Posix tcgetattr function */
|
---|
| 497 | case TCSETS: /* Posix tcsetattr function, TCSANOW option */
|
---|
| 498 | case TCSETSW: /* Posix tcsetattr function, TCSADRAIN option */
|
---|
| 499 | case TCSETSF: /* Posix tcsetattr function, TCSAFLUSH option */
|
---|
| 500 | size = sizeof(struct termios);
|
---|
| 501 | break;
|
---|
| 502 |
|
---|
| 503 | case TCSBRK: /* Posix tcsendbreak function */
|
---|
| 504 | case TCFLOW: /* Posix tcflow function */
|
---|
| 505 | case TCFLSH: /* Posix tcflush function */
|
---|
| 506 | case TIOCGPGRP: /* Posix tcgetpgrp function */
|
---|
| 507 | case TIOCSPGRP: /* Posix tcsetpgrp function */
|
---|
| 508 | size = sizeof(int);
|
---|
| 509 | break;
|
---|
| 510 |
|
---|
| 511 | case TIOCGWINSZ: /* get window size (not Posix) */
|
---|
| 512 | case TIOCSWINSZ: /* set window size (not Posix) */
|
---|
| 513 | size = sizeof(struct winsize);
|
---|
| 514 | break;
|
---|
| 515 |
|
---|
| 516 | case KIOCSMAP: /* load keymap (Minix extension) */
|
---|
| 517 | size = sizeof(keymap_t);
|
---|
| 518 | break;
|
---|
| 519 |
|
---|
| 520 | case TIOCSFON: /* load font (Minix extension) */
|
---|
| 521 | size = sizeof(u8_t [8192]);
|
---|
| 522 | break;
|
---|
| 523 |
|
---|
| 524 | case TCDRAIN: /* Posix tcdrain function -- no parameter */
|
---|
| 525 | default: size = 0;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | r = OK;
|
---|
| 529 | switch (m_ptr->TTY_REQUEST) {
|
---|
| 530 | case TCGETS:
|
---|
| 531 | /* Get the termios attributes. */
|
---|
| 532 | r = sys_vircopy(SELF, D, (vir_bytes) &tp->tty_termios,
|
---|
| 533 | m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
|
---|
| 534 | (vir_bytes) size);
|
---|
| 535 | break;
|
---|
| 536 |
|
---|
| 537 | case TCSETSW:
|
---|
| 538 | case TCSETSF:
|
---|
| 539 | case TCDRAIN:
|
---|
| 540 | if (tp->tty_outleft > 0) {
|
---|
| 541 | /* Wait for all ongoing output processing to finish. */
|
---|
| 542 | tp->tty_iocaller = m_ptr->m_source;
|
---|
| 543 | tp->tty_ioproc = m_ptr->PROC_NR;
|
---|
| 544 | tp->tty_ioreq = m_ptr->REQUEST;
|
---|
| 545 | tp->tty_iovir = (vir_bytes) m_ptr->ADDRESS;
|
---|
| 546 | r = SUSPEND;
|
---|
| 547 | break;
|
---|
| 548 | }
|
---|
| 549 | if (m_ptr->TTY_REQUEST == TCDRAIN) break;
|
---|
| 550 | if (m_ptr->TTY_REQUEST == TCSETSF) tty_icancel(tp);
|
---|
| 551 | /*FALL THROUGH*/
|
---|
| 552 | case TCSETS:
|
---|
| 553 | /* Set the termios attributes. */
|
---|
| 554 | r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
|
---|
| 555 | SELF, D, (vir_bytes) &tp->tty_termios, (vir_bytes) size);
|
---|
| 556 | if (r != OK) break;
|
---|
| 557 | setattr(tp);
|
---|
| 558 | break;
|
---|
| 559 |
|
---|
| 560 | case TCFLSH:
|
---|
| 561 | r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
|
---|
| 562 | SELF, D, (vir_bytes) ¶m.i, (vir_bytes) size);
|
---|
| 563 | if (r != OK) break;
|
---|
| 564 | switch (param.i) {
|
---|
| 565 | case TCIFLUSH: tty_icancel(tp); break;
|
---|
| 566 | case TCOFLUSH: (*tp->tty_ocancel)(tp, 0); break;
|
---|
| 567 | case TCIOFLUSH: tty_icancel(tp); (*tp->tty_ocancel)(tp, 0); break;
|
---|
| 568 | default: r = EINVAL;
|
---|
| 569 | }
|
---|
| 570 | break;
|
---|
| 571 |
|
---|
| 572 | case TCFLOW:
|
---|
| 573 | r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
|
---|
| 574 | SELF, D, (vir_bytes) ¶m.i, (vir_bytes) size);
|
---|
| 575 | if (r != OK) break;
|
---|
| 576 | switch (param.i) {
|
---|
| 577 | case TCOOFF:
|
---|
| 578 | case TCOON:
|
---|
| 579 | tp->tty_inhibited = (param.i == TCOOFF);
|
---|
| 580 | tp->tty_events = 1;
|
---|
| 581 | break;
|
---|
| 582 | case TCIOFF:
|
---|
| 583 | (*tp->tty_echo)(tp, tp->tty_termios.c_cc[VSTOP]);
|
---|
| 584 | break;
|
---|
| 585 | case TCION:
|
---|
| 586 | (*tp->tty_echo)(tp, tp->tty_termios.c_cc[VSTART]);
|
---|
| 587 | break;
|
---|
| 588 | default:
|
---|
| 589 | r = EINVAL;
|
---|
| 590 | }
|
---|
| 591 | break;
|
---|
| 592 |
|
---|
| 593 | case TCSBRK:
|
---|
| 594 | if (tp->tty_break != NULL) (*tp->tty_break)(tp,0);
|
---|
| 595 | break;
|
---|
| 596 |
|
---|
| 597 | case TIOCGWINSZ:
|
---|
| 598 | r = sys_vircopy(SELF, D, (vir_bytes) &tp->tty_winsize,
|
---|
| 599 | m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
|
---|
| 600 | (vir_bytes) size);
|
---|
| 601 | break;
|
---|
| 602 |
|
---|
| 603 | case TIOCSWINSZ:
|
---|
| 604 | r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
|
---|
| 605 | SELF, D, (vir_bytes) &tp->tty_winsize, (vir_bytes) size);
|
---|
| 606 | /* SIGWINCH... */
|
---|
| 607 | break;
|
---|
| 608 |
|
---|
| 609 | case KIOCSMAP:
|
---|
| 610 | /* Load a new keymap (only /dev/console). */
|
---|
| 611 | if (isconsole(tp)) r = kbd_loadmap(m_ptr);
|
---|
| 612 | break;
|
---|
| 613 |
|
---|
| 614 | case TIOCSFON:
|
---|
| 615 | /* Load a font into an EGA or VGA card (hs@hck.hr) */
|
---|
| 616 | if (isconsole(tp)) r = con_loadfont(m_ptr);
|
---|
| 617 | break;
|
---|
| 618 |
|
---|
| 619 | /* These Posix functions are allowed to fail if _POSIX_JOB_CONTROL is
|
---|
| 620 | * not defined.
|
---|
| 621 | */
|
---|
| 622 | case TIOCGPGRP:
|
---|
| 623 | case TIOCSPGRP:
|
---|
| 624 | default:
|
---|
| 625 | r = ENOTTY;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | /* Send the reply. */
|
---|
| 629 | tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, r);
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | /*===========================================================================*
|
---|
| 633 | * do_open *
|
---|
| 634 | *===========================================================================*/
|
---|
| 635 | PRIVATE void do_open(tp, m_ptr)
|
---|
| 636 | register tty_t *tp;
|
---|
| 637 | message *m_ptr; /* pointer to message sent to task */
|
---|
| 638 | {
|
---|
| 639 | /* A tty line has been opened. Make it the callers controlling tty if
|
---|
| 640 | * O_NOCTTY is *not* set and it is not the log device. 1 is returned if
|
---|
| 641 | * the tty is made the controlling tty, otherwise OK or an error code.
|
---|
| 642 | */
|
---|
| 643 | int r = OK;
|
---|
| 644 |
|
---|
| 645 | if (m_ptr->TTY_LINE == LOG_MINOR) {
|
---|
| 646 | /* The log device is a write-only diagnostics device. */
|
---|
| 647 | if (m_ptr->COUNT & R_BIT) r = EACCES;
|
---|
| 648 | } else {
|
---|
| 649 | if (!(m_ptr->COUNT & O_NOCTTY)) {
|
---|
| 650 | tp->tty_pgrp = m_ptr->PROC_NR;
|
---|
| 651 | r = 1;
|
---|
| 652 | }
|
---|
| 653 | tp->tty_openct++;
|
---|
| 654 | }
|
---|
| 655 | tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, r);
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | /*===========================================================================*
|
---|
| 659 | * do_close *
|
---|
| 660 | *===========================================================================*/
|
---|
| 661 | PRIVATE void do_close(tp, m_ptr)
|
---|
| 662 | register tty_t *tp;
|
---|
| 663 | message *m_ptr; /* pointer to message sent to task */
|
---|
| 664 | {
|
---|
| 665 | /* A tty line has been closed. Clean up the line if it is the last close. */
|
---|
| 666 |
|
---|
| 667 | if (m_ptr->TTY_LINE != LOG_MINOR && --tp->tty_openct == 0) {
|
---|
| 668 | tp->tty_pgrp = 0;
|
---|
| 669 | tty_icancel(tp);
|
---|
| 670 | (*tp->tty_ocancel)(tp, 0);
|
---|
| 671 | (*tp->tty_close)(tp, 0);
|
---|
| 672 | tp->tty_termios = termios_defaults;
|
---|
| 673 | tp->tty_winsize = winsize_defaults;
|
---|
| 674 | setattr(tp);
|
---|
| 675 | }
|
---|
| 676 | tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, OK);
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | /*===========================================================================*
|
---|
| 680 | * do_cancel *
|
---|
| 681 | *===========================================================================*/
|
---|
| 682 | PRIVATE void do_cancel(tp, m_ptr)
|
---|
| 683 | register tty_t *tp;
|
---|
| 684 | message *m_ptr; /* pointer to message sent to task */
|
---|
| 685 | {
|
---|
| 686 | /* A signal has been sent to a process that is hanging trying to read or write.
|
---|
| 687 | * The pending read or write must be finished off immediately.
|
---|
| 688 | */
|
---|
| 689 |
|
---|
| 690 | int proc_nr;
|
---|
| 691 | int mode;
|
---|
| 692 |
|
---|
| 693 | /* Check the parameters carefully, to avoid cancelling twice. */
|
---|
| 694 | proc_nr = m_ptr->PROC_NR;
|
---|
| 695 | mode = m_ptr->COUNT;
|
---|
| 696 | if ((mode & R_BIT) && tp->tty_inleft != 0 && proc_nr == tp->tty_inproc) {
|
---|
| 697 | /* Process was reading when killed. Clean up input. */
|
---|
| 698 | tty_icancel(tp);
|
---|
| 699 | tp->tty_inleft = tp->tty_incum = 0;
|
---|
| 700 | }
|
---|
| 701 | if ((mode & W_BIT) && tp->tty_outleft != 0 && proc_nr == tp->tty_outproc) {
|
---|
| 702 | /* Process was writing when killed. Clean up output. */
|
---|
| 703 | (*tp->tty_ocancel)(tp, 0);
|
---|
| 704 | tp->tty_outleft = tp->tty_outcum = 0;
|
---|
| 705 | }
|
---|
| 706 | if (tp->tty_ioreq != 0 && proc_nr == tp->tty_ioproc) {
|
---|
| 707 | /* Process was waiting for output to drain. */
|
---|
| 708 | tp->tty_ioreq = 0;
|
---|
| 709 | }
|
---|
| 710 | tp->tty_events = 1;
|
---|
| 711 | tty_reply(TASK_REPLY, m_ptr->m_source, proc_nr, EINTR);
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | PUBLIC int select_try(struct tty *tp, int ops)
|
---|
| 715 | {
|
---|
| 716 | int ready_ops = 0;
|
---|
| 717 |
|
---|
| 718 | /* Special case. If line is hung up, no operations will block.
|
---|
| 719 | * (and it can be seen as an exceptional condition.)
|
---|
| 720 | */
|
---|
| 721 | if (tp->tty_termios.c_ospeed == B0) {
|
---|
| 722 | ready_ops |= ops;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | if (ops & SEL_RD) {
|
---|
| 726 | /* will i/o not block on read? */
|
---|
| 727 | if (tp->tty_inleft > 0) {
|
---|
| 728 | ready_ops |= SEL_RD; /* EIO - no blocking */
|
---|
| 729 | } else if (tp->tty_incount > 0) {
|
---|
| 730 | /* Is a regular read possible? tty_incount
|
---|
| 731 | * says there is data. But a read will only succeed
|
---|
| 732 | * in canonical mode if a newline has been seen.
|
---|
| 733 | */
|
---|
| 734 | if (!(tp->tty_termios.c_lflag & ICANON) ||
|
---|
| 735 | tp->tty_eotct > 0) {
|
---|
| 736 | ready_ops |= SEL_RD;
|
---|
| 737 | }
|
---|
| 738 | }
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 | if (ops & SEL_WR) {
|
---|
| 742 | if (tp->tty_outleft > 0) ready_ops |= SEL_WR;
|
---|
| 743 | else if ((*tp->tty_devwrite)(tp, 1)) ready_ops |= SEL_WR;
|
---|
| 744 | }
|
---|
| 745 |
|
---|
| 746 | return ready_ops;
|
---|
| 747 | }
|
---|
| 748 |
|
---|
| 749 | PUBLIC int select_retry(struct tty *tp)
|
---|
| 750 | {
|
---|
| 751 | if (select_try(tp, tp->tty_select_ops))
|
---|
| 752 | notify(tp->tty_select_proc);
|
---|
| 753 | return OK;
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | /*===========================================================================*
|
---|
| 757 | * handle_events *
|
---|
| 758 | *===========================================================================*/
|
---|
| 759 | PUBLIC void handle_events(tp)
|
---|
| 760 | tty_t *tp; /* TTY to check for events. */
|
---|
| 761 | {
|
---|
| 762 | /* Handle any events pending on a TTY. These events are usually device
|
---|
| 763 | * interrupts.
|
---|
| 764 | *
|
---|
| 765 | * Two kinds of events are prominent:
|
---|
| 766 | * - a character has been received from the console or an RS232 line.
|
---|
| 767 | * - an RS232 line has completed a write request (on behalf of a user).
|
---|
| 768 | * The interrupt handler may delay the interrupt message at its discretion
|
---|
| 769 | * to avoid swamping the TTY task. Messages may be overwritten when the
|
---|
| 770 | * lines are fast or when there are races between different lines, input
|
---|
| 771 | * and output, because MINIX only provides single buffering for interrupt
|
---|
| 772 | * messages (in proc.c). This is handled by explicitly checking each line
|
---|
| 773 | * for fresh input and completed output on each interrupt.
|
---|
| 774 | */
|
---|
| 775 | char *buf;
|
---|
| 776 | unsigned count;
|
---|
| 777 | int status;
|
---|
| 778 |
|
---|
| 779 | do {
|
---|
| 780 | tp->tty_events = 0;
|
---|
| 781 |
|
---|
| 782 | /* Read input and perform input processing. */
|
---|
| 783 | (*tp->tty_devread)(tp, 0);
|
---|
| 784 |
|
---|
| 785 | /* Perform output processing and write output. */
|
---|
| 786 | (*tp->tty_devwrite)(tp, 0);
|
---|
| 787 |
|
---|
| 788 | /* Ioctl waiting for some event? */
|
---|
| 789 | if (tp->tty_ioreq != 0) dev_ioctl(tp);
|
---|
| 790 | } while (tp->tty_events);
|
---|
| 791 |
|
---|
| 792 | /* Transfer characters from the input queue to a waiting process. */
|
---|
| 793 | in_transfer(tp);
|
---|
| 794 |
|
---|
| 795 | /* Reply if enough bytes are available. */
|
---|
| 796 | if (tp->tty_incum >= tp->tty_min && tp->tty_inleft > 0) {
|
---|
| 797 | if (tp->tty_inrepcode == REVIVE) {
|
---|
| 798 | notify(tp->tty_incaller);
|
---|
| 799 | tp->tty_inrevived = 1;
|
---|
| 800 | } else {
|
---|
| 801 | tty_reply(tp->tty_inrepcode, tp->tty_incaller,
|
---|
| 802 | tp->tty_inproc, tp->tty_incum);
|
---|
| 803 | tp->tty_inleft = tp->tty_incum = 0;
|
---|
| 804 | }
|
---|
| 805 | }
|
---|
| 806 | if (tp->tty_select_ops)
|
---|
| 807 | select_retry(tp);
|
---|
| 808 | #if NR_PTYS > 0
|
---|
| 809 | if (ispty(tp))
|
---|
| 810 | select_retry_pty(tp);
|
---|
| 811 | #endif
|
---|
| 812 | }
|
---|
| 813 |
|
---|
| 814 | /*===========================================================================*
|
---|
| 815 | * in_transfer *
|
---|
| 816 | *===========================================================================*/
|
---|
| 817 | PRIVATE void in_transfer(tp)
|
---|
| 818 | register tty_t *tp; /* pointer to terminal to read from */
|
---|
| 819 | {
|
---|
| 820 | /* Transfer bytes from the input queue to a process reading from a terminal. */
|
---|
| 821 |
|
---|
| 822 | int ch;
|
---|
| 823 | int count;
|
---|
| 824 | char buf[64], *bp;
|
---|
| 825 |
|
---|
| 826 | /* Force read to succeed if the line is hung up, looks like EOF to reader. */
|
---|
| 827 | if (tp->tty_termios.c_ospeed == B0) tp->tty_min = 0;
|
---|
| 828 |
|
---|
| 829 | /* Anything to do? */
|
---|
| 830 | if (tp->tty_inleft == 0 || tp->tty_eotct < tp->tty_min) return;
|
---|
| 831 |
|
---|
| 832 | bp = buf;
|
---|
| 833 | while (tp->tty_inleft > 0 && tp->tty_eotct > 0) {
|
---|
| 834 | ch = *tp->tty_intail;
|
---|
| 835 |
|
---|
| 836 | if (!(ch & IN_EOF)) {
|
---|
| 837 | /* One character to be delivered to the user. */
|
---|
| 838 | *bp = ch & IN_CHAR;
|
---|
| 839 | tp->tty_inleft--;
|
---|
| 840 | if (++bp == bufend(buf)) {
|
---|
| 841 | /* Temp buffer full, copy to user space. */
|
---|
| 842 | sys_vircopy(SELF, D, (vir_bytes) buf,
|
---|
| 843 | tp->tty_inproc, D, tp->tty_in_vir,
|
---|
| 844 | (vir_bytes) buflen(buf));
|
---|
| 845 | tp->tty_in_vir += buflen(buf);
|
---|
| 846 | tp->tty_incum += buflen(buf);
|
---|
| 847 | bp = buf;
|
---|
| 848 | }
|
---|
| 849 | }
|
---|
| 850 |
|
---|
| 851 | /* Remove the character from the input queue. */
|
---|
| 852 | if (++tp->tty_intail == bufend(tp->tty_inbuf))
|
---|
| 853 | tp->tty_intail = tp->tty_inbuf;
|
---|
| 854 | tp->tty_incount--;
|
---|
| 855 | if (ch & IN_EOT) {
|
---|
| 856 | tp->tty_eotct--;
|
---|
| 857 | /* Don't read past a line break in canonical mode. */
|
---|
| 858 | if (tp->tty_termios.c_lflag & ICANON) tp->tty_inleft = 0;
|
---|
| 859 | }
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 | if (bp > buf) {
|
---|
| 863 | /* Leftover characters in the buffer. */
|
---|
| 864 | count = bp - buf;
|
---|
| 865 | sys_vircopy(SELF, D, (vir_bytes) buf,
|
---|
| 866 | tp->tty_inproc, D, tp->tty_in_vir, (vir_bytes) count);
|
---|
| 867 | tp->tty_in_vir += count;
|
---|
| 868 | tp->tty_incum += count;
|
---|
| 869 | }
|
---|
| 870 |
|
---|
| 871 | /* Usually reply to the reader, possibly even if incum == 0 (EOF). */
|
---|
| 872 | if (tp->tty_inleft == 0) {
|
---|
| 873 | if (tp->tty_inrepcode == REVIVE) {
|
---|
| 874 | notify(tp->tty_incaller);
|
---|
| 875 | tp->tty_inrevived = 1;
|
---|
| 876 | } else {
|
---|
| 877 | tty_reply(tp->tty_inrepcode, tp->tty_incaller,
|
---|
| 878 | tp->tty_inproc, tp->tty_incum);
|
---|
| 879 | tp->tty_inleft = tp->tty_incum = 0;
|
---|
| 880 | }
|
---|
| 881 | }
|
---|
| 882 | }
|
---|
| 883 |
|
---|
| 884 | /*===========================================================================*
|
---|
| 885 | * in_process *
|
---|
| 886 | *===========================================================================*/
|
---|
| 887 | PUBLIC int in_process(tp, buf, count)
|
---|
| 888 | register tty_t *tp; /* terminal on which character has arrived */
|
---|
| 889 | char *buf; /* buffer with input characters */
|
---|
| 890 | int count; /* number of input characters */
|
---|
| 891 | {
|
---|
| 892 | /* Characters have just been typed in. Process, save, and echo them. Return
|
---|
| 893 | * the number of characters processed.
|
---|
| 894 | */
|
---|
| 895 |
|
---|
| 896 | int ch, sig, ct;
|
---|
| 897 | int timeset = FALSE;
|
---|
| 898 | static unsigned char csize_mask[] = { 0x1F, 0x3F, 0x7F, 0xFF };
|
---|
| 899 |
|
---|
| 900 | for (ct = 0; ct < count; ct++) {
|
---|
| 901 | /* Take one character. */
|
---|
| 902 | ch = *buf++ & BYTE;
|
---|
| 903 |
|
---|
| 904 | /* Strip to seven bits? */
|
---|
| 905 | if (tp->tty_termios.c_iflag & ISTRIP) ch &= 0x7F;
|
---|
| 906 |
|
---|
| 907 | /* Input extensions? */
|
---|
| 908 | if (tp->tty_termios.c_lflag & IEXTEN) {
|
---|
| 909 |
|
---|
| 910 | /* Previous character was a character escape? */
|
---|
| 911 | if (tp->tty_escaped) {
|
---|
| 912 | tp->tty_escaped = NOT_ESCAPED;
|
---|
| 913 | ch |= IN_ESC; /* protect character */
|
---|
| 914 | }
|
---|
| 915 |
|
---|
| 916 | /* LNEXT (^V) to escape the next character? */
|
---|
| 917 | if (ch == tp->tty_termios.c_cc[VLNEXT]) {
|
---|
| 918 | tp->tty_escaped = ESCAPED;
|
---|
| 919 | rawecho(tp, '^');
|
---|
| 920 | rawecho(tp, '\b');
|
---|
| 921 | continue; /* do not store the escape */
|
---|
| 922 | }
|
---|
| 923 |
|
---|
| 924 | /* REPRINT (^R) to reprint echoed characters? */
|
---|
| 925 | if (ch == tp->tty_termios.c_cc[VREPRINT]) {
|
---|
| 926 | reprint(tp);
|
---|
| 927 | continue;
|
---|
| 928 | }
|
---|
| 929 | }
|
---|
| 930 |
|
---|
| 931 | /* _POSIX_VDISABLE is a normal character value, so better escape it. */
|
---|
| 932 | if (ch == _POSIX_VDISABLE) ch |= IN_ESC;
|
---|
| 933 |
|
---|
| 934 | /* Map CR to LF, ignore CR, or map LF to CR. */
|
---|
| 935 | if (ch == '\r') {
|
---|
| 936 | if (tp->tty_termios.c_iflag & IGNCR) continue;
|
---|
| 937 | if (tp->tty_termios.c_iflag & ICRNL) ch = '\n';
|
---|
| 938 | } else
|
---|
| 939 | if (ch == '\n') {
|
---|
| 940 | if (tp->tty_termios.c_iflag & INLCR) ch = '\r';
|
---|
| 941 | }
|
---|
| 942 |
|
---|
| 943 | /* Canonical mode? */
|
---|
| 944 | if (tp->tty_termios.c_lflag & ICANON) {
|
---|
| 945 |
|
---|
| 946 | /* Erase processing (rub out of last character). */
|
---|
| 947 | if (ch == tp->tty_termios.c_cc[VERASE]) {
|
---|
| 948 | (void) back_over(tp);
|
---|
| 949 | if (!(tp->tty_termios.c_lflag & ECHOE)) {
|
---|
| 950 | (void) tty_echo(tp, ch);
|
---|
| 951 | }
|
---|
| 952 | continue;
|
---|
| 953 | }
|
---|
| 954 |
|
---|
| 955 | /* Kill processing (remove current line). */
|
---|
| 956 | if (ch == tp->tty_termios.c_cc[VKILL]) {
|
---|
| 957 | while (back_over(tp)) {}
|
---|
| 958 | if (!(tp->tty_termios.c_lflag & ECHOE)) {
|
---|
| 959 | (void) tty_echo(tp, ch);
|
---|
| 960 | if (tp->tty_termios.c_lflag & ECHOK)
|
---|
| 961 | rawecho(tp, '\n');
|
---|
| 962 | }
|
---|
| 963 | continue;
|
---|
| 964 | }
|
---|
| 965 |
|
---|
| 966 | /* EOF (^D) means end-of-file, an invisible "line break". */
|
---|
| 967 | if (ch == tp->tty_termios.c_cc[VEOF]) ch |= IN_EOT | IN_EOF;
|
---|
| 968 |
|
---|
| 969 | /* The line may be returned to the user after an LF. */
|
---|
| 970 | if (ch == '\n') ch |= IN_EOT;
|
---|
| 971 |
|
---|
| 972 | /* Same thing with EOL, whatever it may be. */
|
---|
| 973 | if (ch == tp->tty_termios.c_cc[VEOL]) ch |= IN_EOT;
|
---|
| 974 | }
|
---|
| 975 |
|
---|
| 976 | /* Start/stop input control? */
|
---|
| 977 | if (tp->tty_termios.c_iflag & IXON) {
|
---|
| 978 |
|
---|
| 979 | /* Output stops on STOP (^S). */
|
---|
| 980 | if (ch == tp->tty_termios.c_cc[VSTOP]) {
|
---|
| 981 | tp->tty_inhibited = STOPPED;
|
---|
| 982 | tp->tty_events = 1;
|
---|
| 983 | continue;
|
---|
| 984 | }
|
---|
| 985 |
|
---|
| 986 | /* Output restarts on START (^Q) or any character if IXANY. */
|
---|
| 987 | if (tp->tty_inhibited) {
|
---|
| 988 | if (ch == tp->tty_termios.c_cc[VSTART]
|
---|
| 989 | || (tp->tty_termios.c_iflag & IXANY)) {
|
---|
| 990 | tp->tty_inhibited = RUNNING;
|
---|
| 991 | tp->tty_events = 1;
|
---|
| 992 | if (ch == tp->tty_termios.c_cc[VSTART])
|
---|
| 993 | continue;
|
---|
| 994 | }
|
---|
| 995 | }
|
---|
| 996 | }
|
---|
| 997 |
|
---|
| 998 | if (tp->tty_termios.c_lflag & ISIG) {
|
---|
| 999 | /* Check for INTR (^?) and QUIT (^\) characters. */
|
---|
| 1000 | if (ch == tp->tty_termios.c_cc[VINTR]
|
---|
| 1001 | || ch == tp->tty_termios.c_cc[VQUIT]) {
|
---|
| 1002 | sig = SIGINT;
|
---|
| 1003 | if (ch == tp->tty_termios.c_cc[VQUIT]) sig = SIGQUIT;
|
---|
| 1004 | sigchar(tp, sig);
|
---|
| 1005 | (void) tty_echo(tp, ch);
|
---|
| 1006 | continue;
|
---|
| 1007 | }
|
---|
| 1008 | }
|
---|
| 1009 |
|
---|
| 1010 | /* Is there space in the input buffer? */
|
---|
| 1011 | if (tp->tty_incount == buflen(tp->tty_inbuf)) {
|
---|
| 1012 | /* No space; discard in canonical mode, keep in raw mode. */
|
---|
| 1013 | if (tp->tty_termios.c_lflag & ICANON) continue;
|
---|
| 1014 | break;
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
| 1017 | if (!(tp->tty_termios.c_lflag & ICANON)) {
|
---|
| 1018 | /* In raw mode all characters are "line breaks". */
|
---|
| 1019 | ch |= IN_EOT;
|
---|
| 1020 |
|
---|
| 1021 | /* Start an inter-byte timer? */
|
---|
| 1022 | if (!timeset && tp->tty_termios.c_cc[VMIN] > 0
|
---|
| 1023 | && tp->tty_termios.c_cc[VTIME] > 0) {
|
---|
| 1024 | settimer(tp, TRUE);
|
---|
| 1025 | timeset = TRUE;
|
---|
| 1026 | }
|
---|
| 1027 | }
|
---|
| 1028 |
|
---|
| 1029 | /* Perform the intricate function of echoing. */
|
---|
| 1030 | if (tp->tty_termios.c_lflag & (ECHO|ECHONL)) ch = tty_echo(tp, ch);
|
---|
| 1031 |
|
---|
| 1032 | /* Save the character in the input queue. */
|
---|
| 1033 | *tp->tty_inhead++ = ch;
|
---|
| 1034 | if (tp->tty_inhead == bufend(tp->tty_inbuf))
|
---|
| 1035 | tp->tty_inhead = tp->tty_inbuf;
|
---|
| 1036 | tp->tty_incount++;
|
---|
| 1037 | if (ch & IN_EOT) tp->tty_eotct++;
|
---|
| 1038 |
|
---|
| 1039 | /* Try to finish input if the queue threatens to overflow. */
|
---|
| 1040 | if (tp->tty_incount == buflen(tp->tty_inbuf)) in_transfer(tp);
|
---|
| 1041 | }
|
---|
| 1042 | return ct;
|
---|
| 1043 | }
|
---|
| 1044 |
|
---|
| 1045 | /*===========================================================================*
|
---|
| 1046 | * echo *
|
---|
| 1047 | *===========================================================================*/
|
---|
| 1048 | PRIVATE int tty_echo(tp, ch)
|
---|
| 1049 | register tty_t *tp; /* terminal on which to echo */
|
---|
| 1050 | register int ch; /* pointer to character to echo */
|
---|
| 1051 | {
|
---|
| 1052 | /* Echo the character if echoing is on. Some control characters are echoed
|
---|
| 1053 | * with their normal effect, other control characters are echoed as "^X",
|
---|
| 1054 | * normal characters are echoed normally. EOF (^D) is echoed, but immediately
|
---|
| 1055 | * backspaced over. Return the character with the echoed length added to its
|
---|
| 1056 | * attributes.
|
---|
| 1057 | */
|
---|
| 1058 | int len, rp;
|
---|
| 1059 |
|
---|
| 1060 | ch &= ~IN_LEN;
|
---|
| 1061 | if (!(tp->tty_termios.c_lflag & ECHO)) {
|
---|
| 1062 | if (ch == ('\n' | IN_EOT) && (tp->tty_termios.c_lflag
|
---|
| 1063 | & (ICANON|ECHONL)) == (ICANON|ECHONL))
|
---|
| 1064 | (*tp->tty_echo)(tp, '\n');
|
---|
| 1065 | return(ch);
|
---|
| 1066 | }
|
---|
| 1067 |
|
---|
| 1068 | /* "Reprint" tells if the echo output has been messed up by other output. */
|
---|
| 1069 | rp = tp->tty_incount == 0 ? FALSE : tp->tty_reprint;
|
---|
| 1070 |
|
---|
| 1071 | if ((ch & IN_CHAR) < ' ') {
|
---|
| 1072 | switch (ch & (IN_ESC|IN_EOF|IN_EOT|IN_CHAR)) {
|
---|
| 1073 | case '\t':
|
---|
| 1074 | len = 0;
|
---|
| 1075 | do {
|
---|
| 1076 | (*tp->tty_echo)(tp, ' ');
|
---|
| 1077 | len++;
|
---|
| 1078 | } while (len < TAB_SIZE && (tp->tty_position & TAB_MASK) != 0);
|
---|
| 1079 | break;
|
---|
| 1080 | case '\r' | IN_EOT:
|
---|
| 1081 | case '\n' | IN_EOT:
|
---|
| 1082 | (*tp->tty_echo)(tp, ch & IN_CHAR);
|
---|
| 1083 | len = 0;
|
---|
| 1084 | break;
|
---|
| 1085 | default:
|
---|
| 1086 | (*tp->tty_echo)(tp, '^');
|
---|
| 1087 | (*tp->tty_echo)(tp, '@' + (ch & IN_CHAR));
|
---|
| 1088 | len = 2;
|
---|
| 1089 | }
|
---|
| 1090 | } else
|
---|
| 1091 | if ((ch & IN_CHAR) == '\177') {
|
---|
| 1092 | /* A DEL prints as "^?". */
|
---|
| 1093 | (*tp->tty_echo)(tp, '^');
|
---|
| 1094 | (*tp->tty_echo)(tp, '?');
|
---|
| 1095 | len = 2;
|
---|
| 1096 | } else {
|
---|
| 1097 | (*tp->tty_echo)(tp, ch & IN_CHAR);
|
---|
| 1098 | len = 1;
|
---|
| 1099 | }
|
---|
| 1100 | if (ch & IN_EOF) while (len > 0) { (*tp->tty_echo)(tp, '\b'); len--; }
|
---|
| 1101 |
|
---|
| 1102 | tp->tty_reprint = rp;
|
---|
| 1103 | return(ch | (len << IN_LSHIFT));
|
---|
| 1104 | }
|
---|
| 1105 |
|
---|
| 1106 | /*===========================================================================*
|
---|
| 1107 | * rawecho *
|
---|
| 1108 | *===========================================================================*/
|
---|
| 1109 | PRIVATE void rawecho(tp, ch)
|
---|
| 1110 | register tty_t *tp;
|
---|
| 1111 | int ch;
|
---|
| 1112 | {
|
---|
| 1113 | /* Echo without interpretation if ECHO is set. */
|
---|
| 1114 | int rp = tp->tty_reprint;
|
---|
| 1115 | if (tp->tty_termios.c_lflag & ECHO) (*tp->tty_echo)(tp, ch);
|
---|
| 1116 | tp->tty_reprint = rp;
|
---|
| 1117 | }
|
---|
| 1118 |
|
---|
| 1119 | /*===========================================================================*
|
---|
| 1120 | * back_over *
|
---|
| 1121 | *===========================================================================*/
|
---|
| 1122 | PRIVATE int back_over(tp)
|
---|
| 1123 | register tty_t *tp;
|
---|
| 1124 | {
|
---|
| 1125 | /* Backspace to previous character on screen and erase it. */
|
---|
| 1126 | u16_t *head;
|
---|
| 1127 | int len;
|
---|
| 1128 |
|
---|
| 1129 | if (tp->tty_incount == 0) return(0); /* queue empty */
|
---|
| 1130 | head = tp->tty_inhead;
|
---|
| 1131 | if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
|
---|
| 1132 | if (*--head & IN_EOT) return(0); /* can't erase "line breaks" */
|
---|
| 1133 | if (tp->tty_reprint) reprint(tp); /* reprint if messed up */
|
---|
| 1134 | tp->tty_inhead = head;
|
---|
| 1135 | tp->tty_incount--;
|
---|
| 1136 | if (tp->tty_termios.c_lflag & ECHOE) {
|
---|
| 1137 | len = (*head & IN_LEN) >> IN_LSHIFT;
|
---|
| 1138 | while (len > 0) {
|
---|
| 1139 | rawecho(tp, '\b');
|
---|
| 1140 | rawecho(tp, ' ');
|
---|
| 1141 | rawecho(tp, '\b');
|
---|
| 1142 | len--;
|
---|
| 1143 | }
|
---|
| 1144 | }
|
---|
| 1145 | return(1); /* one character erased */
|
---|
| 1146 | }
|
---|
| 1147 |
|
---|
| 1148 | /*===========================================================================*
|
---|
| 1149 | * reprint *
|
---|
| 1150 | *===========================================================================*/
|
---|
| 1151 | PRIVATE void reprint(tp)
|
---|
| 1152 | register tty_t *tp; /* pointer to tty struct */
|
---|
| 1153 | {
|
---|
| 1154 | /* Restore what has been echoed to screen before if the user input has been
|
---|
| 1155 | * messed up by output, or if REPRINT (^R) is typed.
|
---|
| 1156 | */
|
---|
| 1157 | int count;
|
---|
| 1158 | u16_t *head;
|
---|
| 1159 |
|
---|
| 1160 | tp->tty_reprint = FALSE;
|
---|
| 1161 |
|
---|
| 1162 | /* Find the last line break in the input. */
|
---|
| 1163 | head = tp->tty_inhead;
|
---|
| 1164 | count = tp->tty_incount;
|
---|
| 1165 | while (count > 0) {
|
---|
| 1166 | if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
|
---|
| 1167 | if (head[-1] & IN_EOT) break;
|
---|
| 1168 | head--;
|
---|
| 1169 | count--;
|
---|
| 1170 | }
|
---|
| 1171 | if (count == tp->tty_incount) return; /* no reason to reprint */
|
---|
| 1172 |
|
---|
| 1173 | /* Show REPRINT (^R) and move to a new line. */
|
---|
| 1174 | (void) tty_echo(tp, tp->tty_termios.c_cc[VREPRINT] | IN_ESC);
|
---|
| 1175 | rawecho(tp, '\r');
|
---|
| 1176 | rawecho(tp, '\n');
|
---|
| 1177 |
|
---|
| 1178 | /* Reprint from the last break onwards. */
|
---|
| 1179 | do {
|
---|
| 1180 | if (head == bufend(tp->tty_inbuf)) head = tp->tty_inbuf;
|
---|
| 1181 | *head = tty_echo(tp, *head);
|
---|
| 1182 | head++;
|
---|
| 1183 | count++;
|
---|
| 1184 | } while (count < tp->tty_incount);
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
| 1187 | /*===========================================================================*
|
---|
| 1188 | * out_process *
|
---|
| 1189 | *===========================================================================*/
|
---|
| 1190 | PUBLIC void out_process(tp, bstart, bpos, bend, icount, ocount)
|
---|
| 1191 | tty_t *tp;
|
---|
| 1192 | char *bstart, *bpos, *bend; /* start/pos/end of circular buffer */
|
---|
| 1193 | int *icount; /* # input chars / input chars used */
|
---|
| 1194 | int *ocount; /* max output chars / output chars used */
|
---|
| 1195 | {
|
---|
| 1196 | /* Perform output processing on a circular buffer. *icount is the number of
|
---|
| 1197 | * bytes to process, and the number of bytes actually processed on return.
|
---|
| 1198 | * *ocount is the space available on input and the space used on output.
|
---|
| 1199 | * (Naturally *icount < *ocount.) The column position is updated modulo
|
---|
| 1200 | * the TAB size, because we really only need it for tabs.
|
---|
| 1201 | */
|
---|
| 1202 |
|
---|
| 1203 | int tablen;
|
---|
| 1204 | int ict = *icount;
|
---|
| 1205 | int oct = *ocount;
|
---|
| 1206 | int pos = tp->tty_position;
|
---|
| 1207 |
|
---|
| 1208 | while (ict > 0) {
|
---|
| 1209 | switch (*bpos) {
|
---|
| 1210 | case '\7':
|
---|
| 1211 | break;
|
---|
| 1212 | case '\b':
|
---|
| 1213 | pos--;
|
---|
| 1214 | break;
|
---|
| 1215 | case '\r':
|
---|
| 1216 | pos = 0;
|
---|
| 1217 | break;
|
---|
| 1218 | case '\n':
|
---|
| 1219 | if ((tp->tty_termios.c_oflag & (OPOST|ONLCR))
|
---|
| 1220 | == (OPOST|ONLCR)) {
|
---|
| 1221 | /* Map LF to CR+LF if there is space. Note that the
|
---|
| 1222 | * next character in the buffer is overwritten, so
|
---|
| 1223 | * we stop at this point.
|
---|
| 1224 | */
|
---|
| 1225 | if (oct >= 2) {
|
---|
| 1226 | *bpos = '\r';
|
---|
| 1227 | if (++bpos == bend) bpos = bstart;
|
---|
| 1228 | *bpos = '\n';
|
---|
| 1229 | pos = 0;
|
---|
| 1230 | ict--;
|
---|
| 1231 | oct -= 2;
|
---|
| 1232 | }
|
---|
| 1233 | goto out_done; /* no space or buffer got changed */
|
---|
| 1234 | }
|
---|
| 1235 | break;
|
---|
| 1236 | case '\t':
|
---|
| 1237 | /* Best guess for the tab length. */
|
---|
| 1238 | tablen = TAB_SIZE - (pos & TAB_MASK);
|
---|
| 1239 |
|
---|
| 1240 | if ((tp->tty_termios.c_oflag & (OPOST|XTABS))
|
---|
| 1241 | == (OPOST|XTABS)) {
|
---|
| 1242 | /* Tabs must be expanded. */
|
---|
| 1243 | if (oct >= tablen) {
|
---|
| 1244 | pos += tablen;
|
---|
| 1245 | ict--;
|
---|
| 1246 | oct -= tablen;
|
---|
| 1247 | do {
|
---|
| 1248 | *bpos = ' ';
|
---|
| 1249 | if (++bpos == bend) bpos = bstart;
|
---|
| 1250 | } while (--tablen != 0);
|
---|
| 1251 | }
|
---|
| 1252 | goto out_done;
|
---|
| 1253 | }
|
---|
| 1254 | /* Tabs are output directly. */
|
---|
| 1255 | pos += tablen;
|
---|
| 1256 | break;
|
---|
| 1257 | default:
|
---|
| 1258 | /* Assume any other character prints as one character. */
|
---|
| 1259 | pos++;
|
---|
| 1260 | }
|
---|
| 1261 | if (++bpos == bend) bpos = bstart;
|
---|
| 1262 | ict--;
|
---|
| 1263 | oct--;
|
---|
| 1264 | }
|
---|
| 1265 | out_done:
|
---|
| 1266 | tp->tty_position = pos & TAB_MASK;
|
---|
| 1267 |
|
---|
| 1268 | *icount -= ict; /* [io]ct are the number of chars not used */
|
---|
| 1269 | *ocount -= oct; /* *[io]count are the number of chars that are used */
|
---|
| 1270 | }
|
---|
| 1271 |
|
---|
| 1272 | /*===========================================================================*
|
---|
| 1273 | * dev_ioctl *
|
---|
| 1274 | *===========================================================================*/
|
---|
| 1275 | PRIVATE void dev_ioctl(tp)
|
---|
| 1276 | tty_t *tp;
|
---|
| 1277 | {
|
---|
| 1278 | /* The ioctl's TCSETSW, TCSETSF and TCDRAIN wait for output to finish to make
|
---|
| 1279 | * sure that an attribute change doesn't affect the processing of current
|
---|
| 1280 | * output. Once output finishes the ioctl is executed as in do_ioctl().
|
---|
| 1281 | */
|
---|
| 1282 | int result;
|
---|
| 1283 |
|
---|
| 1284 | if (tp->tty_outleft > 0) return; /* output not finished */
|
---|
| 1285 |
|
---|
| 1286 | if (tp->tty_ioreq != TCDRAIN) {
|
---|
| 1287 | if (tp->tty_ioreq == TCSETSF) tty_icancel(tp);
|
---|
| 1288 | result = sys_vircopy(tp->tty_ioproc, D, tp->tty_iovir,
|
---|
| 1289 | SELF, D, (vir_bytes) &tp->tty_termios,
|
---|
| 1290 | (vir_bytes) sizeof(tp->tty_termios));
|
---|
| 1291 | setattr(tp);
|
---|
| 1292 | }
|
---|
| 1293 | tp->tty_ioreq = 0;
|
---|
| 1294 | tty_reply(REVIVE, tp->tty_iocaller, tp->tty_ioproc, result);
|
---|
| 1295 | }
|
---|
| 1296 |
|
---|
| 1297 | /*===========================================================================*
|
---|
| 1298 | * setattr *
|
---|
| 1299 | *===========================================================================*/
|
---|
| 1300 | PRIVATE void setattr(tp)
|
---|
| 1301 | tty_t *tp;
|
---|
| 1302 | {
|
---|
| 1303 | /* Apply the new line attributes (raw/canonical, line speed, etc.) */
|
---|
| 1304 | u16_t *inp;
|
---|
| 1305 | int count;
|
---|
| 1306 |
|
---|
| 1307 | if (!(tp->tty_termios.c_lflag & ICANON)) {
|
---|
| 1308 | /* Raw mode; put a "line break" on all characters in the input queue.
|
---|
| 1309 | * It is undefined what happens to the input queue when ICANON is
|
---|
| 1310 | * switched off, a process should use TCSAFLUSH to flush the queue.
|
---|
| 1311 | * Keeping the queue to preserve typeahead is the Right Thing, however
|
---|
| 1312 | * when a process does use TCSANOW to switch to raw mode.
|
---|
| 1313 | */
|
---|
| 1314 | count = tp->tty_eotct = tp->tty_incount;
|
---|
| 1315 | inp = tp->tty_intail;
|
---|
| 1316 | while (count > 0) {
|
---|
| 1317 | *inp |= IN_EOT;
|
---|
| 1318 | if (++inp == bufend(tp->tty_inbuf)) inp = tp->tty_inbuf;
|
---|
| 1319 | --count;
|
---|
| 1320 | }
|
---|
| 1321 | }
|
---|
| 1322 |
|
---|
| 1323 | /* Inspect MIN and TIME. */
|
---|
| 1324 | settimer(tp, FALSE);
|
---|
| 1325 | if (tp->tty_termios.c_lflag & ICANON) {
|
---|
| 1326 | /* No MIN & TIME in canonical mode. */
|
---|
| 1327 | tp->tty_min = 1;
|
---|
| 1328 | } else {
|
---|
| 1329 | /* In raw mode MIN is the number of chars wanted, and TIME how long
|
---|
| 1330 | * to wait for them. With interesting exceptions if either is zero.
|
---|
| 1331 | */
|
---|
| 1332 | tp->tty_min = tp->tty_termios.c_cc[VMIN];
|
---|
| 1333 | if (tp->tty_min == 0 && tp->tty_termios.c_cc[VTIME] > 0)
|
---|
| 1334 | tp->tty_min = 1;
|
---|
| 1335 | }
|
---|
| 1336 |
|
---|
| 1337 | if (!(tp->tty_termios.c_iflag & IXON)) {
|
---|
| 1338 | /* No start/stop output control, so don't leave output inhibited. */
|
---|
| 1339 | tp->tty_inhibited = RUNNING;
|
---|
| 1340 | tp->tty_events = 1;
|
---|
| 1341 | }
|
---|
| 1342 |
|
---|
| 1343 | /* Setting the output speed to zero hangs up the phone. */
|
---|
| 1344 | if (tp->tty_termios.c_ospeed == B0) sigchar(tp, SIGHUP);
|
---|
| 1345 |
|
---|
| 1346 | /* Set new line speed, character size, etc at the device level. */
|
---|
| 1347 | (*tp->tty_ioctl)(tp, 0);
|
---|
| 1348 | }
|
---|
| 1349 |
|
---|
| 1350 | /*===========================================================================*
|
---|
| 1351 | * tty_reply *
|
---|
| 1352 | *===========================================================================*/
|
---|
| 1353 | PUBLIC void tty_reply(code, replyee, proc_nr, status)
|
---|
| 1354 | int code; /* TASK_REPLY or REVIVE */
|
---|
| 1355 | int replyee; /* destination address for the reply */
|
---|
| 1356 | int proc_nr; /* to whom should the reply go? */
|
---|
| 1357 | int status; /* reply code */
|
---|
| 1358 | {
|
---|
| 1359 | /* Send a reply to a process that wanted to read or write data. */
|
---|
| 1360 | message tty_mess;
|
---|
| 1361 |
|
---|
| 1362 | tty_mess.m_type = code;
|
---|
| 1363 | tty_mess.REP_PROC_NR = proc_nr;
|
---|
| 1364 | tty_mess.REP_STATUS = status;
|
---|
| 1365 |
|
---|
| 1366 | if ((status = send(replyee, &tty_mess)) != OK) {
|
---|
| 1367 | panic("TTY","tty_reply failed, status\n", status);
|
---|
| 1368 | }
|
---|
| 1369 | }
|
---|
| 1370 |
|
---|
| 1371 | /*===========================================================================*
|
---|
| 1372 | * sigchar *
|
---|
| 1373 | *===========================================================================*/
|
---|
| 1374 | PUBLIC void sigchar(tp, sig)
|
---|
| 1375 | register tty_t *tp;
|
---|
| 1376 | int sig; /* SIGINT, SIGQUIT, SIGKILL or SIGHUP */
|
---|
| 1377 | {
|
---|
| 1378 | /* Process a SIGINT, SIGQUIT or SIGKILL char from the keyboard or SIGHUP from
|
---|
| 1379 | * a tty close, "stty 0", or a real RS-232 hangup. MM will send the signal to
|
---|
| 1380 | * the process group (INT, QUIT), all processes (KILL), or the session leader
|
---|
| 1381 | * (HUP).
|
---|
| 1382 | */
|
---|
| 1383 | int status;
|
---|
| 1384 |
|
---|
| 1385 | if (tp->tty_pgrp != 0)
|
---|
| 1386 | if (OK != (status = sys_kill(tp->tty_pgrp, sig)))
|
---|
| 1387 | panic("TTY","Error, call to sys_kill failed", status);
|
---|
| 1388 |
|
---|
| 1389 | if (!(tp->tty_termios.c_lflag & NOFLSH)) {
|
---|
| 1390 | tp->tty_incount = tp->tty_eotct = 0; /* kill earlier input */
|
---|
| 1391 | tp->tty_intail = tp->tty_inhead;
|
---|
| 1392 | (*tp->tty_ocancel)(tp, 0); /* kill all output */
|
---|
| 1393 | tp->tty_inhibited = RUNNING;
|
---|
| 1394 | tp->tty_events = 1;
|
---|
| 1395 | }
|
---|
| 1396 | }
|
---|
| 1397 |
|
---|
| 1398 | /*===========================================================================*
|
---|
| 1399 | * tty_icancel *
|
---|
| 1400 | *===========================================================================*/
|
---|
| 1401 | PRIVATE void tty_icancel(tp)
|
---|
| 1402 | register tty_t *tp;
|
---|
| 1403 | {
|
---|
| 1404 | /* Discard all pending input, tty buffer or device. */
|
---|
| 1405 |
|
---|
| 1406 | tp->tty_incount = tp->tty_eotct = 0;
|
---|
| 1407 | tp->tty_intail = tp->tty_inhead;
|
---|
| 1408 | (*tp->tty_icancel)(tp, 0);
|
---|
| 1409 | }
|
---|
| 1410 |
|
---|
| 1411 | /*===========================================================================*
|
---|
| 1412 | * tty_init *
|
---|
| 1413 | *===========================================================================*/
|
---|
| 1414 | PRIVATE void tty_init()
|
---|
| 1415 | {
|
---|
| 1416 | /* Initialize tty structure and call device initialization routines. */
|
---|
| 1417 |
|
---|
| 1418 | register tty_t *tp;
|
---|
| 1419 | int s;
|
---|
| 1420 | struct sigaction sigact;
|
---|
| 1421 |
|
---|
| 1422 | /* Initialize the terminal lines. */
|
---|
| 1423 | for (tp = FIRST_TTY,s=0; tp < END_TTY; tp++,s++) {
|
---|
| 1424 |
|
---|
| 1425 | tp->tty_index = s;
|
---|
| 1426 |
|
---|
| 1427 | tmr_inittimer(&tp->tty_tmr);
|
---|
| 1428 |
|
---|
| 1429 | tp->tty_intail = tp->tty_inhead = tp->tty_inbuf;
|
---|
| 1430 | tp->tty_min = 1;
|
---|
| 1431 | tp->tty_termios = termios_defaults;
|
---|
| 1432 | tp->tty_icancel = tp->tty_ocancel = tp->tty_ioctl = tp->tty_close =
|
---|
| 1433 | tty_devnop;
|
---|
| 1434 | if (tp < tty_addr(NR_CONS)) {
|
---|
| 1435 | scr_init(tp);
|
---|
| 1436 | tp->tty_minor = CONS_MINOR + s;
|
---|
| 1437 | } else
|
---|
| 1438 | if (tp < tty_addr(NR_CONS+NR_RS_LINES)) {
|
---|
| 1439 | rs_init(tp);
|
---|
| 1440 | tp->tty_minor = RS232_MINOR + s-NR_CONS;
|
---|
| 1441 | } else {
|
---|
| 1442 | pty_init(tp);
|
---|
| 1443 | tp->tty_minor = s - (NR_CONS+NR_RS_LINES) + TTYPX_MINOR;
|
---|
| 1444 | }
|
---|
| 1445 | }
|
---|
| 1446 | }
|
---|
| 1447 |
|
---|
| 1448 | /*===========================================================================*
|
---|
| 1449 | * tty_timed_out *
|
---|
| 1450 | *===========================================================================*/
|
---|
| 1451 | PRIVATE void tty_timed_out(timer_t *tp)
|
---|
| 1452 | {
|
---|
| 1453 | /* This timer has expired. Set the events flag, to force processing. */
|
---|
| 1454 | tty_t *tty_ptr;
|
---|
| 1455 | tty_ptr = &tty_table[tmr_arg(tp)->ta_int];
|
---|
| 1456 | tty_ptr->tty_min = 0; /* force read to succeed */
|
---|
| 1457 | tty_ptr->tty_events = 1;
|
---|
| 1458 | }
|
---|
| 1459 |
|
---|
| 1460 | /*===========================================================================*
|
---|
| 1461 | * expire_timers *
|
---|
| 1462 | *===========================================================================*/
|
---|
| 1463 | PRIVATE void expire_timers(void)
|
---|
| 1464 | {
|
---|
| 1465 | /* A synchronous alarm message was received. Check if there are any expired
|
---|
| 1466 | * timers. Possibly set the event flag and reschedule another alarm.
|
---|
| 1467 | */
|
---|
| 1468 | clock_t now; /* current time */
|
---|
| 1469 | int s;
|
---|
| 1470 |
|
---|
| 1471 | /* Get the current time to compare the timers against. */
|
---|
| 1472 | if ((s=getuptime(&now)) != OK)
|
---|
| 1473 | panic("TTY","Couldn't get uptime from clock.", s);
|
---|
| 1474 |
|
---|
| 1475 | /* Scan the queue of timers for expired timers. This dispatch the watchdog
|
---|
| 1476 | * functions of expired timers. Possibly a new alarm call must be scheduled.
|
---|
| 1477 | */
|
---|
| 1478 | tmrs_exptimers(&tty_timers, now, NULL);
|
---|
| 1479 | if (tty_timers == NULL) tty_next_timeout = TMR_NEVER;
|
---|
| 1480 | else { /* set new sync alarm */
|
---|
| 1481 | tty_next_timeout = tty_timers->tmr_exp_time;
|
---|
| 1482 | if ((s=sys_setalarm(tty_next_timeout, 1)) != OK)
|
---|
| 1483 | panic("TTY","Couldn't set synchronous alarm.", s);
|
---|
| 1484 | }
|
---|
| 1485 | }
|
---|
| 1486 |
|
---|
| 1487 | /*===========================================================================*
|
---|
| 1488 | * settimer *
|
---|
| 1489 | *===========================================================================*/
|
---|
| 1490 | PRIVATE void settimer(tty_ptr, enable)
|
---|
| 1491 | tty_t *tty_ptr; /* line to set or unset a timer on */
|
---|
| 1492 | int enable; /* set timer if true, otherwise unset */
|
---|
| 1493 | {
|
---|
| 1494 | clock_t now; /* current time */
|
---|
| 1495 | clock_t exp_time;
|
---|
| 1496 | int s;
|
---|
| 1497 |
|
---|
| 1498 | /* Get the current time to calculate the timeout time. */
|
---|
| 1499 | if ((s=getuptime(&now)) != OK)
|
---|
| 1500 | panic("TTY","Couldn't get uptime from clock.", s);
|
---|
| 1501 | if (enable) {
|
---|
| 1502 | exp_time = now + tty_ptr->tty_termios.c_cc[VTIME] * (HZ/10);
|
---|
| 1503 | /* Set a new timer for enabling the TTY events flags. */
|
---|
| 1504 | tmrs_settimer(&tty_timers, &tty_ptr->tty_tmr,
|
---|
| 1505 | exp_time, tty_timed_out, NULL);
|
---|
| 1506 | } else {
|
---|
| 1507 | /* Remove the timer from the active and expired lists. */
|
---|
| 1508 | tmrs_clrtimer(&tty_timers, &tty_ptr->tty_tmr, NULL);
|
---|
| 1509 | }
|
---|
| 1510 |
|
---|
| 1511 | /* Now check if a new alarm must be scheduled. This happens when the front
|
---|
| 1512 | * of the timers queue was disabled or reinserted at another position, or
|
---|
| 1513 | * when a new timer was added to the front.
|
---|
| 1514 | */
|
---|
| 1515 | if (tty_timers == NULL) tty_next_timeout = TMR_NEVER;
|
---|
| 1516 | else if (tty_timers->tmr_exp_time != tty_next_timeout) {
|
---|
| 1517 | tty_next_timeout = tty_timers->tmr_exp_time;
|
---|
| 1518 | if ((s=sys_setalarm(tty_next_timeout, 1)) != OK)
|
---|
| 1519 | panic("TTY","Couldn't set synchronous alarm.", s);
|
---|
| 1520 | }
|
---|
| 1521 | }
|
---|
| 1522 |
|
---|
| 1523 | /*===========================================================================*
|
---|
| 1524 | * tty_devnop *
|
---|
| 1525 | *===========================================================================*/
|
---|
| 1526 | PUBLIC int tty_devnop(tp, try)
|
---|
| 1527 | tty_t *tp;
|
---|
| 1528 | int try;
|
---|
| 1529 | {
|
---|
| 1530 | /* Some functions need not be implemented at the device level. */
|
---|
| 1531 | }
|
---|
| 1532 |
|
---|
| 1533 | /*===========================================================================*
|
---|
| 1534 | * do_select *
|
---|
| 1535 | *===========================================================================*/
|
---|
| 1536 | PRIVATE void do_select(tp, m_ptr)
|
---|
| 1537 | register tty_t *tp; /* pointer to tty struct */
|
---|
| 1538 | register message *m_ptr; /* pointer to message sent to the task */
|
---|
| 1539 | {
|
---|
| 1540 | int ops, ready_ops = 0, watch;
|
---|
| 1541 |
|
---|
| 1542 | ops = m_ptr->PROC_NR & (SEL_RD|SEL_WR|SEL_ERR);
|
---|
| 1543 | watch = (m_ptr->PROC_NR & SEL_NOTIFY) ? 1 : 0;
|
---|
| 1544 |
|
---|
| 1545 | ready_ops = select_try(tp, ops);
|
---|
| 1546 |
|
---|
| 1547 | if (!ready_ops && ops && watch) {
|
---|
| 1548 | tp->tty_select_ops |= ops;
|
---|
| 1549 | tp->tty_select_proc = m_ptr->m_source;
|
---|
| 1550 | }
|
---|
| 1551 |
|
---|
| 1552 | tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, ready_ops);
|
---|
| 1553 |
|
---|
| 1554 | return;
|
---|
| 1555 | }
|
---|