home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Sound / LAME / WarpOS / src / frontend / console.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-15  |  4.5 KB  |  181 lines

  1. #ifdef HAVE_CONFIG_H
  2. # include <config.h>
  3. #endif
  4.  
  5. #ifdef STDC_HEADERS
  6. # include <stdlib.h>
  7. # include <string.h>
  8. #else
  9. # ifndef HAVE_STRCHR
  10. #  define strchr index
  11. #  define strrchr rindex
  12. # endif
  13. char *strchr (), *strrchr ();
  14. # ifndef HAVE_MEMCPY
  15. #  define memcpy(d, s, n) bcopy ((s), (d), (n))
  16. #  define memmove(d, s, n) bcopy ((s), (d), (n))
  17. # endif
  18. #endif
  19.  
  20. #include "console.h"
  21.  
  22. #if defined(TERMCAP_AVAILABLE)
  23. # include <termcap.h>
  24. #endif
  25.  
  26. #ifdef WITH_DMALLOC
  27. #include <dmalloc.h>
  28. #endif
  29.  
  30. #define CLASS_ID           0x434F4E53
  31. #define REPORT_BUFF_SIZE   1024
  32.  
  33. /* 
  34.  * Taken from Termcap_Manual.html:
  35.  *
  36.  * With the Unix version of termcap, you must allocate space for the description yourself and pass
  37.  * the address of the space as the argument buffer. There is no way you can tell how much space is
  38.  * needed, so the convention is to allocate a buffer 2048 characters long and assume that is
  39.  * enough.  (Formerly the convention was to allocate 1024 characters and assume that was enough.
  40.  * But one day, for one kind of terminal, that was not enough.)
  41.  */
  42.  
  43. Console_IO_t*  open_console ( int debug )
  44. {
  45.     Console_IO_t* const  mfp = calloc ( 1, sizeof (*mfp) );
  46. #ifdef TERMCAP_AVAILABLE
  47.     const char*          term_name;
  48.     char                 term_buff [2048];
  49.     char*                tp;
  50.     char                 tc [10];
  51.     int                  val;
  52. #endif
  53.  
  54.     /* setup basics of brhist I/O channels */
  55.     mfp -> disp_width   = 80;
  56.     mfp -> disp_height  = 25;
  57.     mfp -> Console_fp   = stderr;
  58.     mfp -> Error_fp     = stderr;
  59.     mfp -> Report_fp    = debug  ?  fopen ( "/tmp/lame_reports", "a" )  :  NULL;
  60.  
  61.     mfp -> Console_buff = calloc ( 1, REPORT_BUFF_SIZE );
  62.     setvbuf ( mfp -> Console_fp, mfp -> Console_buff, _IOFBF, REPORT_BUFF_SIZE );
  63. //  setvbuf ( mfp -> Error_fp  , NULL                   , _IONBF, 0                                );
  64.  
  65. #if defined(_WIN32)  &&  !defined(__CYGWIN__) 
  66.     mfp -> Console_Handle = GetStdHandle (STD_ERROR_HANDLE);
  67. #endif
  68.  
  69.     strcpy ( mfp -> str_up, "\033[A" );
  70.     
  71. #ifdef TERMCAP_AVAILABLE
  72.     /* try to catch additional information about special console sequences */
  73.     
  74.     if ((term_name = getenv("TERM")) == NULL) {
  75.     fprintf ( mfp -> Error_fp, "LAME: Can't get \"TERM\" environment string.\n" );
  76.     return -1;
  77.     }
  78.     if ( tgetent (term_buff, term_name) != 1 ) {
  79.     fprintf ( mfp -> Error_fp, "LAME: Can't find termcap entry for terminal \"%s\"\n", term_name );
  80.     return -1;
  81.     }
  82.     
  83.     val = tgetnum ("co");
  84.     if ( val >= 40  &&  val <= 512 )
  85.         mfp -> disp_width   = val;
  86.     val = tgetnum ("li");
  87.     if ( val >= 16  &&  val <= 256 )
  88.         mfp -> disp_height  = val;
  89.         
  90.     *(tp = tc) = '\0';
  91.     tp = tgetstr ("up", &tp);
  92.     if (tp != NULL)
  93.         strcpy ( mfp -> str_up, tp );
  94.  
  95.     *(tp = tc) = '\0';
  96.     tp = tgetstr ("ce", &tp);
  97.     if (tp != NULL)
  98.         strcpy ( mfp -> str_clreoln, tp );
  99.  
  100.     *(tp = tc) = '\0';
  101.     tp = tgetstr ("md", &tp);
  102.     if (tp != NULL)
  103.         strcpy ( mfp -> str_emph, tp );
  104.  
  105.     *(tp = tc) = '\0';
  106.     tp = tgetstr ("me", &tp);
  107.     if (tp != NULL)
  108.         strcpy ( mfp -> str_norm, tp );
  109.         
  110. #endif /* TERMCAP_AVAILABLE */
  111.  
  112.     return mfp;
  113. }
  114.  
  115. /* printf for console */
  116.  
  117. int  Console_printf ( Console_IO_t* const mfp, const char* const format, ... )
  118. {
  119.     va_list  args;
  120.     int      ret;
  121.  
  122.     va_start ( args, format );
  123.     ret = vfprintf ( mfp -> Console_fp, s, args );
  124.     va_end ( args );
  125.     
  126.     return ret;
  127. }
  128.  
  129. /* printf for errors */
  130.  
  131. int  Error_printf ( Console_IO_t* const mfp, const char* const format, ... )
  132. {
  133.     va_list  args;
  134.     int      ret;
  135.  
  136.     va_start ( args, format );
  137.     ret = vfprintf ( mfp -> Error_fp, s, args );
  138.     va_end ( args );
  139.     
  140.     flush ( mfp -> Error_fp );
  141.     return ret;
  142. }
  143.  
  144. /* printf for additional reporting information */
  145.  
  146. int  Report_printf ( Console_IO_t* const mfp, const char* const format, ... )
  147. {
  148.     va_list  args;
  149.     int      ret;
  150.  
  151.     if ( mfp -> Report_fp != NULL ) {
  152.         va_start ( args, format );
  153.         ret = vfprintf ( mfp -> Report_fp, s, args );
  154.         va_end ( args );
  155.         
  156.         return ret;
  157.     }
  158.     
  159.     return 0;
  160. }
  161.  
  162.  
  163. int  close_console ( Console_IO_t* const mfp )
  164. {
  165.     if ( mfp == NULL  ||  mfp -> ClassID != CLASS_ID  ||  mfp -> Console_buff == NULL )
  166.         return -1;
  167.     
  168.     fflush  ( mfp -> Console_fp );
  169.     setvbuf ( mfp -> Console_fp, NULL, _IONBF, (size_t)0 );
  170.     
  171.     memset ( mfp -> Console_buff, 0x55, REPORT_BUFF_SIZE );
  172.     free   ( mfp -> Console_buff );
  173.     
  174.     memset ( mfp, 0x55, sizeof (*mfp) );
  175.     free   ( mfp );
  176.     
  177.     return 0;
  178. }
  179.  
  180. /* end of console.c */
  181.