home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / 93src.lha / src / runtime / mac / os_mac_console.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-09  |  4.7 KB  |  224 lines

  1. /* os_mac_console.c
  2.  * 15Aug92  e
  3.  *
  4.  * The following functions are needed for console support...
  5.  *  __console_options (boot.c)
  6.  *  __cecho2file (ANSI Tricia ╣)
  7.  *  __cecho2printer (ANSI Tricia ╣)
  8.  *  __open_std (ANSI Tricia ╣)
  9.  *  __cshow (ANSI Tricia ╣)
  10.  * 
  11.  * and were shamelessly stolen from Symantec's console.c
  12.  * so that the eEdit code could be interfaced with the ANSI library
  13.  * so...
  14.  * portions Copyright ⌐ e 1992. All rights reserved.
  15.  * portions Copyright (c) 1991 Symantec Corporation.  All rights reserved.
  16.  */
  17.  
  18. #include <MacHeaders>
  19. #include <console.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <signal.h>
  23. #include <errno.h>
  24. #include <ansi_private.h>
  25.  
  26. #include "os_mac.h"
  27.  
  28. extern short interrupted;    /* from os_mac.c */
  29.  
  30. struct __copt console_options = { 50, 10, "\pconsole", 8, 4, 9, 0, 25, 80, 1 };
  31.  
  32. /* closeecho - close echo file (if any) */
  33. /* unimplemented; was...
  34. static void closeecho(void)
  35. {
  36.     if (c.echo2fp)
  37.     {    if (c.spool)
  38.             print_console();
  39.         fclose(c.echo2fp);
  40.     }
  41. }
  42. */
  43.  
  44. /* cecho2file - echo console display to file */
  45.  
  46. void cecho2file(char *s, int append, FILE *fp)
  47. {
  48.     /* unimplemented; was...
  49.     struct save save;
  50.     setup(cflush(fp), &save);
  51.     closeecho();
  52.     c.echo2fp = fopen(s, append ? "a" : "w");
  53.     c.spool = 0;
  54.     restore(&save);
  55.     */
  56. }
  57.  
  58. /* cecho2printer - echo console display to printer */
  59.  
  60. void cecho2printer(FILE *fp)
  61. {
  62.     /* unimplemented; was...
  63.     struct save save;
  64.     setup(cflush(fp), &save);
  65.     closeecho();
  66.     c.echo2fp = tmpfile();
  67.     c.spool = 1;
  68.     restore(&save);
  69.     */
  70. }
  71.  
  72. /* cflush - flush all pending output to a console */
  73.  
  74. static WindowPeek cflush(FILE *fp)
  75. {
  76.     WindowPeek wp = __checkfile(fp)->window;
  77.     int n;
  78.     for (fp = &__file[0], n = FOPEN_MAX; n--; fp++) {
  79.         if (fp->dirty && fp->window == wp)
  80.             fflush(fp);
  81.     }
  82.     return(wp);
  83. }
  84.  
  85. /* cshow - show a console window
  86.  *
  87.  *  All pending output to the window is forced to appear. */
  88.  
  89. void cshow(FILE *fp)
  90. {
  91.     WindowPeek wp = cflush(fp);
  92.     if (wp != (WindowPeek) FrontWindow())
  93.         SelectWindow(wp);
  94.     ShowWindow(wp);
  95. }
  96.  
  97. /*  __open_std - open the std streams
  98.  *
  99.  *  This is called automatically (by "__checkfile") whenever an
  100.  *  unopened std stream is referenced. */
  101.  
  102. void
  103. __open_std(void)
  104. {
  105.     FILE *fp = NULL;
  106.     char buf[40];
  107.     
  108.     if (stdin->std)
  109.         fp = freopenc(fp, stdin);
  110.     if (stdout->std)
  111.         fp = freopenc(fp, stdout);
  112.     if (stderr->std)
  113.         fp = freopenc(fp, stderr);
  114.     /* unimplemented; was...
  115.     if (__log_stdout)
  116.     {    sprintf(buf, "%#s.log", CurApName);
  117.         cecho2file(buf, 1, stdout);
  118.         console_options.pause_atexit = 0;
  119.     }
  120.     */
  121. }
  122.  
  123. /* consoleio - I/O handler proc for console windows */
  124.  
  125. static int consoleio(FILE *fp, int i)
  126. {
  127.     int result = 0;
  128.     long  old_cnt = fp->cnt; /* 15Sep92  e */
  129.     void *old_ptr = fp->ptr; /* 15Sep92  e */
  130.     if (_abnormal_exit)
  131.         return(0);
  132.     switch (i)
  133.     {    case 0:                /*  read  */
  134. consoleio_read_again:
  135.             cshow(fp);
  136.             fp->eof = 0;
  137.             if ((fp->cnt = os_console_read(fp->window, fp->ptr, fp->cnt)) == 0)
  138.             {    fp->eof = 1;
  139.                 result = EOF; /* why EOF !?? */
  140.             }
  141.             break;
  142.         case 1:                /*  write  */
  143.             cshow(fp);
  144.             os_console_write(fp->window, fp->ptr, fp->cnt);
  145.             break;
  146.         case 2:                /*  close  */
  147.             os_console_close(fp->window);
  148.             break;
  149.     }
  150.     if (interrupted)
  151.     {    interrupted = 0;
  152.         FlushEvents(keyDownMask, 0);
  153.         fp->cnt = 0;
  154.         raise(SIGINT);
  155.         /* 15Sep92  e */
  156.         if( i == 0 )
  157.         { fp->cnt = old_cnt;
  158.           fp->ptr = old_ptr;
  159.           result = 0;
  160.           goto consoleio_read_again;
  161.         }
  162.         else
  163.         { errno = EINTR;
  164.           result = EOF; /* why EOF !?? */
  165.         }
  166.         /* */
  167.     }
  168.     return(result);
  169. }
  170.  
  171. /* console_exit - console shutdown routine */
  172.  
  173. static void console_exit(void)
  174. {    register FILE *fp;
  175.     int n;
  176.     /*  complete pending output  */
  177.     for (fp = &__file[0], n = FOPEN_MAX; n--; fp++)
  178.     {    if (fp->dirty && fp->window)
  179.             fflush(fp);
  180.     }
  181.     /*  pause for user acknowledgement  */
  182.     /* not implemented...
  183.     if (console_environment && console_options.pause_atexit)
  184.     {    for (fp = &__file[0], n = FOPEN_MAX; n--; fp++)
  185.         {    if (fp->window)
  186.             {    SetWTitle(fp->window, "\ppress ╟return╚ to exit");
  187.                 c.raw = c.cbreak = c.edit = 0;
  188.                 setbuf(fp, NULL);
  189.                 fgetc(fp);
  190.                 break;
  191.             }
  192.         }
  193.     }
  194.     */
  195.     /*  close consoles  */
  196.     for (fp = &__file[0], n = FOPEN_MAX; n--; fp++)
  197.     {    if (fp->window)
  198.             fclose(fp);
  199.     }
  200. }
  201.  
  202. /*  freopenc - reopen a stream on a new or existing console
  203.  *
  204.  *  "fp" is closed, if necessary, then opened as a console.  If "fp2"
  205.  *  is NULL, a new console is created; otherwise "fp2" must refer to
  206.  *  a console, and "fp" is made to refer to the same console. */
  207.  
  208. FILE *freopenc(FILE *fp2, FILE *fp)
  209. {
  210.     if (fp == NULL)
  211.         return(NULL);
  212.     /* if (WWExist)  ???
  213.         InitConsole(); */
  214.     fclose(fp);
  215.     fp->refnum = -1;
  216.     fp->window = fp2 ? fp2->window : os_console_new( console_options.title );
  217.     setvbuf(fp, NULL, _IOLBF, BUFSIZ);
  218.     fp->proc = consoleio;
  219.     __atexit_console(console_exit);
  220.     return(fp);
  221. }
  222.  
  223. /* end of os_mac_console.c */
  224.