home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / smile / suntty.c < prev   
Encoding:
C/C++ Source or Header  |  1989-10-01  |  1.8 KB  |  103 lines

  1. /* SMiLE -- Simple MIni Line Editor
  2.    Copyright (C) 1989 By Alejandro Liu
  3.    You are given permission to Freely Copy and distribute this program
  4.    in its unmodifed form provided that this copyright notice is included.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/ioctl.h>
  9. #include "bsd_tty.h"
  10. #define DEL_00        127
  11. #define DEL_01         20
  12. #define DEL_02          8
  13. #define LEFT_C          2
  14. #define RIGHT_C          6
  15. #define DONE0         13
  16. #define DONE1         10
  17.  
  18. #define STDIN   0
  19. #define STDOUT  1
  20. #define STDERR  2
  21. #define ERROR    -1
  22. #define TSTR    300
  23.  
  24. struct sgttyb old;
  25.  
  26. init_tty()
  27. {
  28.   struct sgttyb new;
  29.   if (ioctl(STDIN, TIOCGETP, &old) == ERROR) {
  30.     perror("Can not get Terminal parameters -- ioctl");
  31.     exit(1);
  32.   }
  33.   new.sg_ospeed = old.sg_ospeed;
  34.   new.sg_ispeed = old.sg_ispeed;
  35.   new.sg_flags  = ((old.sg_flags | CBREAK) & ~ECHO) & ~TANDEM;
  36.   if (ioctl(STDIN, TIOCSETN, &new) == ERROR) {
  37.     perror("Can not set Terminal Characteristics -- ioctl");
  38.     exit(1);
  39.   }
  40. }
  41.  
  42. restore_tty()
  43. {
  44.   if (ioctl(STDIN, TIOCSETN, &old) == ERROR) {
  45.     perror("Can not restore Terminal Characteristics -- ioctl");
  46.     exit(1);
  47.   }
  48. }
  49.  
  50. char *getstr()
  51. {
  52.   char *tmp1, *tmp2;
  53.   char key;
  54.  
  55.   tmp1 = tmp2 = (char *)malloc(TSTR);
  56.   *(tmp1) = 0;
  57.   for (;;) {
  58.     key = 127 & getkey();
  59.     switch (key) {
  60.       case DEL_00:
  61.       case DEL_01:
  62.       case DEL_02:
  63.         if (tmp1 == tmp2)
  64.       beep();
  65.     else {
  66.       delete(1);
  67.       *(--tmp1) = 0;
  68.     }
  69.     break;
  70.       case LEFT_C:
  71.     if (tmp1 == tmp2)
  72.       beep();
  73.     else {
  74.       backspace(1);
  75.       --tmp1;
  76.     }
  77.     break;
  78.       case RIGHT_C:
  79.     if (*tmp1)
  80.       prtchr(*(tmp1++));
  81.     else
  82.       beep();
  83.     break;
  84.       case DONE0:
  85.       case DONE1:
  86.     key = strlen(tmp2) + 1;
  87.     tmp1 = (char *)malloc(key);
  88.     strcpy(tmp1,tmp2);
  89.     free(tmp2);
  90.     return(tmp1);
  91.       default:
  92.     if ((31<key)&&(key<127)) {
  93.       if (!(*tmp1))
  94.         *(tmp1+1) = 0;
  95.       *(tmp1++) = key;
  96.       prtchr(key);
  97.     }
  98.     else
  99.       beep();
  100.     }
  101.   }
  102. }
  103.