Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

termio.c

Go to the documentation of this file.
00001 
00002 /* From old X interface for McDraw by Steve McMillan*/
00003 
00004 #include "win.h"
00005 #include <string.h>
00006 
00007 #include <termio.h>             /* Going to use getchar with timeout */
00008 
00009 /*
00010  * (Use termio rather than termios here, because the HP can't
00011  *  get/set termios structures.)
00012  *
00013  * Note that some systems use sys/termio.h...
00014  */
00015 
00016 #include <fcntl.h>
00017 
00018 #define MYCMASK 0377
00019 #define BACKSPACE 8
00020 #define TAB 9
00021 #define LINE_FEED 10
00022 #define CARRIAGE_RETURN 13
00023 #define REFRESH 18
00024 #define KILL_LINE 21
00025 #define KILL_WORD 23
00026 #define DEL 127
00027 
00028 #define IDLESTRNGCNT_MAX 255
00029 
00030 /* struct termio ios0, ios1; */
00031 
00032                                 /* Hmmm... */
00033 
00034 struct termios ios0, ios1;      /* NEED termios here on DEC UNIX... */
00035                                 /* NEED termio  here on HP UNIX... */
00036 
00037 char idlestrng[256];
00038 int  fd, idlestrngcnt;
00039 
00040 get_timeout()
00041 
00042 /*
00043    Read a character from a terminal stream, with timeout.
00044    Accumulate string in global variable idlestrng
00045  */
00046 
00047 {
00048     char c;
00049 
00050     if ( (c = getchar_ub()) > 0) return(process_char(c));
00051 
00052     return(0);
00053 
00054     /* c > 0 ==> a character was read before timeout */
00055 }
00056 
00057 getchar_ub()    /* unbuffered input */
00058 {
00059     char c;
00060 
00061     return ((read(0, &c, 1) > 0) ? c & MYCMASK : EOF);
00062 }
00063 
00064 process_char(c)  /* Add char to storage string, deal with display. */
00065 char c;
00066 {
00067     int i,idletog = 0;
00068 
00069     if (c == '\b' || c == DEL) {
00070 
00071         /* Backspace/delete ==> erase character */
00072 
00073         if (idlestrngcnt > 0)
00074             erase_char();
00075         else {
00076 /*          putchar('\a');
00077             fflush(stdout);     */      /* Sun CC doesn't understand \a!! */
00078         }
00079     } 
00080     else if (c == KILL_WORD) {
00081 
00082         /* Delete word. */
00083 
00084         if (idlestrngcnt > 0)
00085             erase_word();
00086         else {
00087 /*          putchar('\a');
00088             fflush(stdout);     */
00089         }
00090     }
00091     else if (c == REFRESH) {
00092 
00093         /* Erase and rewrite the line */
00094 
00095         putchar('^');putchar('R');putchar('\n');
00096         for (i = 0; i < idlestrngcnt; i++) putchar(idlestrng[i]);
00097         fflush(stdout);
00098     } 
00099     else if (c == KILL_LINE) {
00100 
00101         /* Erase the line */
00102 
00103         if (idlestrngcnt == 0) {
00104 /*          putchar('\a');
00105             fflush(stdout);     */
00106         }
00107         while(idlestrngcnt) erase_char();
00108         idlestrng[0] = '\0';
00109     } 
00110     else if (c == '\n' || (c >= ' '&& c <= '~')) {
00111 
00112         idlestrng[idlestrngcnt] = c;
00113         idlestrng[++idlestrngcnt] = '\0';
00114         
00115         if (c == '\n') idletog++;
00116         
00117         if (idlestrngcnt >= IDLESTRNGCNT_MAX) {
00118             idlestrng[idlestrngcnt] = '\n';
00119             idletog++;
00120         }
00121         putchar(c);
00122         fflush(stdout);
00123     }
00124     return(idletog);
00125 }
00126 
00127 erase_char()  /* Erase a character from the display. */
00128 {
00129     idlestrngcnt--;
00130     putchar('\b');
00131     putchar(' ');
00132     putchar('\b');
00133     fflush(stdout);
00134     idlestrng[idlestrngcnt] = '\0';
00135 }
00136 
00137 erase_word()  /* Erase a work from the display. */
00138 {
00139     int i,is = 0,len,lens;
00140 
00141     len = idlestrngcnt;
00142 
00143     i = idlestrngcnt-1;
00144     for(i=idlestrngcnt-1;i>=0;i--) if (idlestrng[i] != ' ') break;
00145     lens = i;
00146 
00147     for(i=0;i<lens;i++) 
00148       if (idlestrng[i] == ' ' || idlestrng[i] == ';') is = i;
00149 
00150     if (is!=0) is = is+1;
00151     for(i=is;i<len;i++) erase_char();
00152 }
00153 
00154 
00155 set_timeout()
00156 {
00157 
00158     /* Identify the terminal stream and get its current configuration. */
00159 
00160     if ( (fd = open("/dev/tty", O_RDONLY)) < 0 ) exit(1);
00161     ioctl(fd, TCGETA, &ios0);
00162     ioctl(fd, TCGETA, &ios1);
00163 
00164     /* Turn off ICANON mode and inhibit ECHO (set bits 1 and 3 = 0). */
00165 
00166     ios1.c_lflag &= 0xfff5;
00167 
00168     /* Set MIN = 0, TIME = 1 * (1/10 secs). */
00169 
00170     ios1.c_cc[VMIN] = 0;
00171     ios1.c_cc[VTIME] = 1;
00172 
00173     ioctl(fd, TCSETA, &ios1);
00174 
00175     idlestrngcnt = 0;
00176     idlestrng[0] = '\0';
00177 }
00178 
00179 reset_term(strng)
00180 char *strng;
00181 {
00182 
00183     /* Restore original terminal settings. */
00184 
00185     ioctl(fd, TCSETA, &ios0);
00186 
00187     close(fd);
00188 
00189     strcat(strng,idlestrng);
00190 }
00191 
00192 clear_buffer()
00193 {
00194     idlestrng[0] = '\0';
00195     idlestrngcnt = 0;
00196 }

Generated at Sun Feb 24 09:57:18 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001