home *** CD-ROM | disk | FTP | other *** search
- /* SMiLE -- Simple MIni Line Editor
- Copyright (C) 1989 By Alejandro Liu
- You are given permission to Freely Copy and distribute this program
- in its unmodifed form provided that this copyright notice is included.
- */
-
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include "bsd_tty.h"
- #define DEL_00 127
- #define DEL_01 20
- #define DEL_02 8
- #define LEFT_C 2
- #define RIGHT_C 6
- #define DONE0 13
- #define DONE1 10
-
- #define STDIN 0
- #define STDOUT 1
- #define STDERR 2
- #define ERROR -1
- #define TSTR 300
-
- struct sgttyb old;
-
- init_tty()
- {
- struct sgttyb new;
- if (ioctl(STDIN, TIOCGETP, &old) == ERROR) {
- perror("Can not get Terminal parameters -- ioctl");
- exit(1);
- }
- new.sg_ospeed = old.sg_ospeed;
- new.sg_ispeed = old.sg_ispeed;
- new.sg_flags = ((old.sg_flags | CBREAK) & ~ECHO) & ~TANDEM;
- if (ioctl(STDIN, TIOCSETN, &new) == ERROR) {
- perror("Can not set Terminal Characteristics -- ioctl");
- exit(1);
- }
- }
-
- restore_tty()
- {
- if (ioctl(STDIN, TIOCSETN, &old) == ERROR) {
- perror("Can not restore Terminal Characteristics -- ioctl");
- exit(1);
- }
- }
-
- char *getstr()
- {
- char *tmp1, *tmp2;
- char key;
-
- tmp1 = tmp2 = (char *)malloc(TSTR);
- *(tmp1) = 0;
- for (;;) {
- key = 127 & getkey();
- switch (key) {
- case DEL_00:
- case DEL_01:
- case DEL_02:
- if (tmp1 == tmp2)
- beep();
- else {
- delete(1);
- *(--tmp1) = 0;
- }
- break;
- case LEFT_C:
- if (tmp1 == tmp2)
- beep();
- else {
- backspace(1);
- --tmp1;
- }
- break;
- case RIGHT_C:
- if (*tmp1)
- prtchr(*(tmp1++));
- else
- beep();
- break;
- case DONE0:
- case DONE1:
- key = strlen(tmp2) + 1;
- tmp1 = (char *)malloc(key);
- strcpy(tmp1,tmp2);
- free(tmp2);
- return(tmp1);
- default:
- if ((31<key)&&(key<127)) {
- if (!(*tmp1))
- *(tmp1+1) = 0;
- *(tmp1++) = key;
- prtchr(key);
- }
- else
- beep();
- }
- }
- }
-