home *** CD-ROM | disk | FTP | other *** search
/ Emulator Universe / Emulator Universe CD (1998).iso / CPC / Utils / CpcFile System / UNIX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-12  |  4.9 KB  |  218 lines

  1.  
  2. /*                <<<<Last Modified: Mon Feb 12 17:31:21 1996>>>>
  3. ------------------------------------------------------------------------------
  4.  
  5.         =====
  6.         CPCfs  --  u n i x . c   ---   Unix specific routines
  7.         =====
  8.  
  9.     Version 0.85                    (c) February '96 by Derik van Zuetphen
  10. ------------------------------------------------------------------------------
  11. */
  12.  
  13.  
  14. #include <signal.h>
  15. #include <glob.h>
  16. #include <malloc.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <termcap.h>
  20. #include <termios.h>
  21. #include <unistd.h>
  22. #include "unix.h"
  23.  
  24.  
  25.  
  26. char    termcap_buffer[1024];
  27. char    *term_entries;
  28. char    *cl_string, *cm_string;    /* Clear Screen, Cursor Motion */
  29.  
  30. const char    perr_str[] = "terminal";
  31.  
  32. void break_handler();
  33.  
  34. void disable_break() {
  35. /*   ^^^^^^^^^^^^^ */
  36.     signal (SIGINT,break_handler);        /* Ctrl-C */
  37. /*        signal (SIGQUIT,break_handler);*/    /* Ctrl-\, hard interrupt */
  38. /*        signal (SIGTSTP,break_handler);*/    /* Ctrl-Z, suspend */
  39. }
  40.  
  41.  
  42. void break_handler () {
  43. /*   ^^^^^^^^^^^^^ */
  44.     Break_Wish = 1;
  45.     disable_break();
  46. }
  47.  
  48.  
  49.  
  50. glob_t    glob_buffer;
  51. int    current_glob;
  52.  
  53.  
  54. char *glob_file (char *pattern) {
  55. /*    ^^^^^^^^^ */
  56.     current_glob=0;
  57.     if (glob(pattern, 0, NULL, &glob_buffer)!=0) {
  58.         return NULL;
  59.     }
  60.     return glob_buffer.gl_pathv[current_glob++];
  61. }
  62.  
  63.  
  64. char *glob_next () {
  65. /*    ^^^^^^^^^ */
  66.  
  67.     if (current_glob<glob_buffer.gl_pathc) {
  68.         return glob_buffer.gl_pathv[current_glob++];
  69.     } else {
  70.         globfree(&glob_buffer);
  71.         return NULL;
  72.     }
  73. }
  74.  
  75.  
  76. /* doesn't work */
  77.  
  78. /*struct mstats core_statistics;*/
  79.  
  80. long coreleft () {
  81. /*   ^^^^^^^^ */
  82. /*    core_statistics = mstats();
  83.     return core_statistics.bytes_free;*/
  84.     return 0;
  85. }
  86.  
  87.  
  88. char*    tmp_nam(char* buf) {    return tmpnam(buf);    }
  89.  
  90.  
  91. int raw () {
  92. /*  ^^^ */
  93. struct termios tio;
  94.     if (tcgetattr(0,&tio)) perror(perr_str);    /* 0 is stdin */
  95.     tio.c_lflag &= ~(ICANON|ISIG);
  96.     if (tcsetattr(0,TCSANOW,&tio)) perror(perr_str);
  97.     return 0;
  98. }
  99.  
  100. char wait_for_key (int dsec, char/*bool*/ intr) {
  101. /*   ^^^^^^^^^^^^
  102. dsec = 0    wait and answer the next char
  103. dsec > 0    answer the next char or 0 after `dsec' 1/10 seconds
  104. dsec < 0    wait `-dsec' 1/10 seconds and answer then 0
  105. intr = TRUE    ^C interrupts
  106. constraints:    -MAXINT < d(ezi)sec < 256
  107. Characters > 127 may cause trouble! (resetting the terminal??)
  108. I am using the POSIX termios structure, SYSV needs termio, BSD needs sgtty 
  109. */
  110.  
  111. struct termios    tio, save;
  112. char    answer=0;    /* default answer */
  113. int    tty = 0;    /* stdin (must be console) */
  114.  
  115. /* open terminal and set timeout */
  116.     if (tcgetattr(tty,&tio)) perror(perr_str); /* also test stdin */
  117.     memcpy(&save,&tio,sizeof(tio));
  118.  
  119.     tio.c_lflag &= ~(ISIG | ICANON | ECHO);    /* raw mode */
  120.     if (dsec == 0) {
  121.         tio.c_cc[VMIN] = 1;        /* one character */
  122.         tio.c_cc[VTIME] = 0;        /* and don't wait */
  123.     } else if (dsec > 0) {
  124.         tio.c_cc[VMIN]=0;        /* no inter-character delay */
  125.         tio.c_cc[VTIME]=dsec;        /* wait for ... seconds */
  126.     }
  127.     if (intr) {
  128.         tio.c_lflag |= ISIG;
  129.         tio.c_cc[VQUIT]=3;        /* 3=^C invokes Quit signal */
  130.     }
  131.     if (tcsetattr(tty,TCSANOW,&tio)) perror(perr_str);
  132.  
  133. /* read a character */
  134.     if (dsec >= 0) {
  135.         fflush (stdout);
  136.         read (tty,&answer,1);
  137.     } else {
  138. /* or simply wait */
  139.         sleep((-dsec+5)/10);
  140.         tcflush(tty,TCIFLUSH);        /* flush pending input */
  141.     }
  142.         
  143. /* reset terminal */
  144.     if (tcsetattr(tty,TCSANOW,&save)) perror(perr_str);
  145.     return answer;
  146. } /* wait_for_key */
  147.  
  148.  
  149. /*****************
  150.   Terminal Access
  151.  *****************/
  152.  
  153. void clrscr() {
  154. /*   ^^^^^^ */
  155.     fflush(stdout);
  156.     printf("%s",cl_string);
  157. } /*clrscr*/
  158.  
  159.  
  160. void gotoxy(int col,int lin) {
  161. /*   ^^^^^^ */
  162.     if (*cm_string!=0)
  163.         printf ("%s",tgoto (cm_string,col-1,lin-1));
  164. } /*gotoxy*/
  165.  
  166.  
  167. /* usefull, but not necessary:
  168. #include <sys/filio.h>
  169. bool kbhit () {
  170. / *   ^^^^^ * /
  171. long    x;  / * holds # of pending chars * /
  172.     fflush(stdout);
  173.     return((ioctl(0,FIONREAD,&x) < 0) ? 0 : x);
  174. } / *kbhit* /
  175. */
  176.  
  177.  
  178. /****************
  179.   Initialization
  180.  ****************/
  181.  
  182. void os_init () {
  183. /*   ^^^^ */
  184. int     err;
  185. char    *term;
  186. struct termios tio;
  187.  
  188.  
  189. /* Init TERMCAP: */
  190.     if ((term = getenv("TERM")) == NULL) err = 2;
  191.     else err = tgetent(termcap_buffer,term);
  192.     switch (err) {
  193.     case -1:    fprintf(stderr,"Can't access TERMINFO Directory!\n");
  194.             break;
  195.     case 0:        fprintf(stderr,"No entry for terminal '%s'!\n",term);
  196.             break;
  197.     case 2:        fprintf(stderr,"No $TERM Variable!\n");
  198.             break;
  199.     } /*switch*/
  200.     if (err !=1) exit(1);
  201.     term_entries = (char*) malloc(100);
  202.     if ((cl_string=tgetstr("cl",&term_entries))==NULL) {
  203.         fprintf (stderr,"\n'%s' can't clear the screen!\n",term);
  204.         cl_string="";
  205.     }
  206.     if ((cm_string=tgetstr("cm",&term_entries))==NULL) {
  207.         fprintf(stderr,"\n'%s' can't move the cursor!\n",term);
  208.         cm_string="";
  209.     }
  210.  
  211. /* get attributes of stdin and stdout; if failed, output was redirected! */
  212.     if (tcgetattr(0,&tio) || tcgetattr(1,&tio)) {
  213.         fprintf(stderr,"Redirection of stdin/stdout is not allowed!");
  214.     }
  215.  
  216. } /*os_init*/
  217.  
  218.