console.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #ifdef HAVE_CONFIG_H
  2. # include <config.h>
  3. #endif
  4. #ifdef STDC_HEADERS
  5. # include <stdlib.h>
  6. # include <string.h>
  7. #else
  8. # ifndef HAVE_STRCHR
  9. # define strchr index
  10. # define strrchr rindex
  11. # endif
  12. char *strchr(), *strrchr();
  13. # ifndef HAVE_MEMCPY
  14. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  15. # define memmove(d, s, n) bcopy ((s), (d), (n))
  16. # endif
  17. #endif
  18. #if defined(HAVE_NCURSES_TERMCAP_H)
  19. # include <ncurses/termcap.h>
  20. #elif defined(HAVE_TERMCAP_H)
  21. # include <termcap.h>
  22. #elif defined(HAVE_TERMCAP)
  23. # include <curses.h>
  24. # if !defined(__bsdi__)
  25. # include <term.h>
  26. # endif
  27. #endif
  28. #include <stdio.h>
  29. #include <stdarg.h>
  30. #include "console.h"
  31. #include "main.h"
  32. #ifdef WITH_DMALLOC
  33. #include <dmalloc.h>
  34. #endif
  35. #define CLASS_ID 0x434F4E53
  36. #define REPORT_BUFF_SIZE 1024
  37. #if defined(_WIN32) && !defined(__CYGWIN__)
  38. # include <windows.h>
  39. #endif
  40. static int
  41. my_console_printing(FILE * fp, const char *format, va_list ap)
  42. {
  43. if (fp != NULL)
  44. return vfprintf(fp, format, ap);
  45. return 0;
  46. }
  47. static int
  48. my_error_printing(FILE * fp, const char *format, va_list ap)
  49. {
  50. if (fp != NULL)
  51. return vfprintf(fp, format, ap);
  52. return 0;
  53. }
  54. static int
  55. my_report_printing(FILE * fp, const char *format, va_list ap)
  56. {
  57. if (fp != NULL)
  58. return vfprintf(fp, format, ap);
  59. return 0;
  60. }
  61. /*
  62. * Taken from Termcap_Manual.html:
  63. *
  64. * With the Unix version of termcap, you must allocate space for the description yourself and pass
  65. * the address of the space as the argument buffer. There is no way you can tell how much space is
  66. * needed, so the convention is to allocate a buffer 2048 characters long and assume that is
  67. * enough. (Formerly the convention was to allocate 1024 characters and assume that was enough.
  68. * But one day, for one kind of terminal, that was not enough.)
  69. */
  70. #ifdef HAVE_TERMCAP
  71. static void
  72. get_termcap_string(char const* id, char* dest, size_t n)
  73. {
  74. char tc[16];
  75. char *tp = tc;
  76. tp[0] = '\0';
  77. tp = tgetstr(id, &tp);
  78. if (tp != NULL && dest != NULL && n > 0) {
  79. strncpy(dest, tp, n);
  80. dest[n-1] = '\0';
  81. }
  82. }
  83. static void
  84. get_termcap_number(char const* id, int* dest, int low, int high)
  85. {
  86. int const val = tgetnum(id);
  87. if (low <= val && val <= high) {
  88. *dest = val;
  89. }
  90. }
  91. static void
  92. apply_termcap_settings(Console_IO_t * const mfp)
  93. {
  94. /* try to catch additional information about special console sequences */
  95. char const* term_name = getenv("TERM");
  96. if (NULL != term_name) {
  97. char term_buff[4096];
  98. int const ret = tgetent(term_buff, term_name);
  99. if (1 == ret) {
  100. get_termcap_number("co", &mfp->disp_width, 40, 512);
  101. get_termcap_number("li", &mfp->disp_height, 16, 256);
  102. get_termcap_string("up", mfp->str_up, sizeof(mfp->str_up));
  103. get_termcap_string("md", mfp->str_emph, sizeof(mfp->str_emph));
  104. get_termcap_string("me", mfp->str_norm, sizeof(mfp->str_norm));
  105. get_termcap_string("ce", mfp->str_clreoln, sizeof(mfp->str_clreoln));
  106. }
  107. }
  108. }
  109. #endif /* TERMCAP_AVAILABLE */
  110. static int
  111. init_console(Console_IO_t * const mfp)
  112. {
  113. /* setup basics of brhist I/O channels */
  114. mfp->disp_width = 80;
  115. mfp->disp_height = 25;
  116. mfp->Console_fp = stderr;
  117. mfp->Error_fp = stderr;
  118. mfp->Report_fp = NULL;
  119. /*mfp -> Console_buff = calloc ( 1, REPORT_BUFF_SIZE ); */
  120. setvbuf(mfp->Console_fp, mfp->Console_buff, _IOFBF, sizeof(mfp->Console_buff));
  121. /* setvbuf ( mfp -> Error_fp , NULL , _IONBF, 0 ); */
  122. #if defined(_WIN32) && !defined(__CYGWIN__)
  123. mfp->Console_Handle = GetStdHandle(STD_ERROR_HANDLE);
  124. #endif
  125. strcpy(mfp->str_up, "\033[A");
  126. #ifdef HAVE_TERMCAP
  127. apply_termcap_settings(mfp);
  128. #endif /* TERMCAP_AVAILABLE */
  129. mfp->ClassID = CLASS_ID;
  130. #if defined(_WIN32) && !defined(__CYGWIN__)
  131. mfp->Console_file_type = GetFileType(Console_IO.Console_Handle);
  132. #else
  133. mfp->Console_file_type = 0;
  134. #endif
  135. return 0;
  136. }
  137. static void
  138. deinit_console(Console_IO_t * const mfp)
  139. {
  140. if (mfp->Report_fp != NULL) {
  141. fclose(mfp->Report_fp);
  142. mfp->Report_fp = NULL;
  143. }
  144. fflush(mfp->Console_fp);
  145. setvbuf(mfp->Console_fp, NULL, _IONBF, (size_t) 0);
  146. memset(mfp->Console_buff, 0x55, REPORT_BUFF_SIZE);
  147. }
  148. /* LAME console
  149. */
  150. Console_IO_t Console_IO;
  151. int
  152. frontend_open_console(void)
  153. {
  154. return init_console(&Console_IO);
  155. }
  156. void
  157. frontend_close_console(void)
  158. {
  159. deinit_console(&Console_IO);
  160. }
  161. void
  162. frontend_debugf(const char *format, va_list ap)
  163. {
  164. (void) my_report_printing(Console_IO.Report_fp, format, ap);
  165. }
  166. void
  167. frontend_msgf(const char *format, va_list ap)
  168. {
  169. (void) my_console_printing(Console_IO.Console_fp, format, ap);
  170. }
  171. void
  172. frontend_errorf(const char *format, va_list ap)
  173. {
  174. (void) my_error_printing(Console_IO.Error_fp, format, ap);
  175. }
  176. void
  177. frontend_print_null(const char *format, va_list ap)
  178. {
  179. (void) format;
  180. (void) ap;
  181. }
  182. int
  183. console_printf(const char *format, ...)
  184. {
  185. va_list args;
  186. int ret;
  187. va_start(args, format);
  188. ret = my_console_printing(Console_IO.Console_fp, format, args);
  189. va_end(args);
  190. return ret;
  191. }
  192. int
  193. error_printf(const char *format, ...)
  194. {
  195. va_list args;
  196. int ret;
  197. va_start(args, format);
  198. ret = my_console_printing(Console_IO.Error_fp, format, args);
  199. va_end(args);
  200. return ret;
  201. }
  202. int
  203. report_printf(const char *format, ...)
  204. {
  205. va_list args;
  206. int ret;
  207. va_start(args, format);
  208. ret = my_console_printing(Console_IO.Report_fp, format, args);
  209. va_end(args);
  210. return ret;
  211. }
  212. void
  213. console_flush()
  214. {
  215. fflush(Console_IO.Console_fp);
  216. }
  217. void
  218. error_flush()
  219. {
  220. fflush(Console_IO.Error_fp);
  221. }
  222. void
  223. report_flush()
  224. {
  225. fflush(Console_IO.Report_fp);
  226. }
  227. void
  228. console_up(int n_lines)
  229. {
  230. #if defined(_WIN32) && !defined(__CYGWIN__)
  231. if (Console_IO.Console_file_type != FILE_TYPE_PIPE) {
  232. COORD Pos;
  233. CONSOLE_SCREEN_BUFFER_INFO CSBI;
  234. console_flush();
  235. GetConsoleScreenBufferInfo(Console_IO.Console_Handle, &CSBI);
  236. Pos.Y = (SHORT)(CSBI.dwCursorPosition.Y - n_lines);
  237. Pos.X = 0;
  238. SetConsoleCursorPosition(Console_IO.Console_Handle, Pos);
  239. }
  240. #else
  241. while (n_lines-- > 0)
  242. fputs(Console_IO.str_up, Console_IO.Console_fp);
  243. console_flush();
  244. #endif
  245. }
  246. void
  247. set_debug_file(const char *fn)
  248. {
  249. if (Console_IO.Report_fp == NULL) {
  250. Console_IO.Report_fp = lame_fopen(fn, "a");
  251. if (Console_IO.Report_fp != NULL) {
  252. error_printf("writing debug info into: %s\n", fn);
  253. }
  254. else {
  255. error_printf("Error: can't open for debug info: %s\n", fn);
  256. }
  257. }
  258. }
  259. /* end of console.c */