1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: tty.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2006-2007 University of Washington
8 * Copyright 2013-2017 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
18 * Program: tty routines
24 #include "../estruct.h"
29 #include "../keydefs.h"
49 * ttopen - this function is called once to set up the terminal device
50 * streams. if called as pine composer, don't mess with
51 * tty modes, but set signal handlers.
62 xonxoff_proc(preserve_start_stop
);
72 * ttclose - this function gets called just before we go back home to
73 * the command interpreter. If called as pine composer, don't
74 * worry about modes, but set signals to default, pine will
75 * rewire things as needed.
81 signal(SIGHUP
, SIG_DFL
);
83 signal(SIGCONT
, SIG_DFL
);
85 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
86 signal(SIGWINCH
, SIG_DFL
);
101 * ttgetc - Read a character from the terminal, performing no editing
102 * and doing no echo at all.
104 * Args: return_on_intr -- Function to get a single character from stdin,
105 * recorder -- If non-NULL, function used to record keystroke.
106 * bail_handler -- Function used to bail out on read error.
108 * Returns: The character read from stdin.
109 * Return_on_intr is returned if read is interrupted.
110 * If read error, BAIL_OUT is returned unless bail_handler is
111 * non-NULL, in which case it is called (and usually it exits).
113 * If recorder is non-null, it is used to record the keystroke.
116 ttgetc(int return_on_intr
, int (*recorder
)(int), void (*bail_handler
)(void))
120 switch(c
= read_one_char()){
122 return(return_on_intr
);
131 return(recorder
? (*recorder
)(c
) : c
);
137 * Simple version of ttgetc with simple error handling
139 * Args: recorder -- If non-NULL, function used to record keystroke.
140 * bail_handler -- Function used to bail out on read error.
142 * Returns: The character read from stdin.
143 * If read error, BAIL_OUT is returned unless bail_handler is
144 * non-NULL, in which case it is called (and usually it exits).
146 * If recorder is non-null, it is used to record the keystroke.
147 * Retries if interrupted.
150 simple_ttgetc(int (*recorder
)(int), void (*bail_handler
)(void))
155 while((res
= read(STDIN_FD
, &c
, 1)) <= 0)
156 if(!(res
< 0 && errno
== EINTR
))
159 return(recorder
? (*recorder
)((int)c
) : (int)c
);
164 * ttputc - Write a character to the display.
169 unsigned char obuf
[MAX(MB_LEN_MAX
,32)];
170 int r
, i
, width
= 0, outchars
= 0;
174 return(putchar((unsigned char) ucs
));
176 width
= wcellwidth(ucs
);
180 obuf
[outchars
++] = '?';
184 * Convert the ucs into the multibyte
185 * character that corresponds to the
186 * ucs in the users locale.
188 outchars
= wtomb((char *) obuf
, ucs
);
196 for(i
= 0; i
< outchars
; i
++){
197 r
= putchar(obuf
[i
]);
198 ret
= (ret
== EOF
) ? EOF
: r
;
206 * ttflush - flush terminal buffer. Does real work where the terminal
207 * output is buffered up. A no-operation on systems where byte
208 * at a time terminal I/O is done.
213 return(fflush(stdout
));
218 * ttresize - recompute the screen dimensions if necessary, and then
219 * adjust pico's internal buffers accordingly.
224 int row
= -1, col
= -1;
226 ttgetwinsz(&row
, &col
);
227 resize_pico(row
, col
);
233 * ttgetwinsz - set global row and column values (if we can get them)
237 ttgetwinsz(int *row
, int *col
)
239 extern int _tlines
, _tcolumns
;
242 *row
= (_tlines
> 0) ? _tlines
- 1 : NROW
- 1;
244 *col
= (_tcolumns
> 0) ? _tcolumns
: NCOL
;
245 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
249 if (ioctl(0, TIOCGWINSZ
, &win
) == 0) { /* set to anything useful.. */
250 if(win
.ws_row
) /* ... the tty drivers says */
251 *row
= win
.ws_row
- 1;
257 signal(SIGWINCH
, winch_handler
); /* window size changes */
267 #define MARGIN 8 /* size of minimim margin and */
268 #define SCRSIZ 64 /* scroll size for extended lines */
269 #define MROW 2 /* rows in menu */
271 /* internal prototypes */
272 int mswin_resize (int, int);
275 * Standard terminal interface dispatch table. Fields point to functions
276 * that operate the terminal. All these functions live in mswin.c, but
277 * this structure is defined here because it is specific to pico.
287 NULL
, /* was mswin_getc, but not used? */
298 * This function is called once to set up the terminal device streams.
306 mswin_getscreensize (&rows
, &columns
);
307 term
.t_nrow
= rows
- 1;
308 term
.t_ncol
= columns
;
309 /* term.t_scrsiz = (columns * 2) / 3; */
312 * Do we implement optimized character insertion and deletion?
313 * o_insert() and o_delete()
315 /* inschar = delchar = FALSE; */
316 /* revexist = TRUE; dead code? */
318 mswin_setresizecallback (mswin_resize
);
326 * This function gets called just before we go back home to the command
332 mswin_clearresizecallback (mswin_resize
);
337 * Flush terminal buffer. Does real work where the terminal output is buffered
338 * up. A no-operation on systems where byte at a time terminal I/O is done.
347 * ttresize - recompute the screen dimensions if necessary, and then
348 * adjust pico's internal buffers accordingly.
355 mswin_getscreensize(&row
, &col
);
356 resize_pico (row
-1, col
);
361 * mswin_resize - windows specific callback to set pico's internal tables
362 * to new screen dimensions.
365 mswin_resize(int row
, int col
)
368 resize_pico (row
-1, col
);
372 #endif /* _WINDOWS */