11 # define strrchr rindex
13 char *strchr(), *strrchr();
15 # define memcpy(d, s, n) bcopy ((s), (d), (n))
16 # define memmove(d, s, n) bcopy ((s), (d), (n))
20 #if defined(HAVE_NCURSES_TERMCAP_H)
21 # include <ncurses/termcap.h>
22 #elif defined(HAVE_TERMCAP_H)
24 #elif defined(HAVE_TERMCAP)
26 # if !defined(__bsdi__)
40 #define CLASS_ID 0x434F4E53
41 #define REPORT_BUFF_SIZE 1024
43 #if defined(_WIN32) && !defined(__CYGWIN__)
50 my_console_printing(FILE * fp, const char *format, va_list ap)
53 return vfprintf(fp, format, ap);
58 my_error_printing(FILE * fp, const char *format, va_list ap)
61 return vfprintf(fp, format, ap);
66 my_report_printing(FILE * fp, const char *format, va_list ap)
69 return vfprintf(fp, format, ap);
75 * Taken from Termcap_Manual.html:
77 * With the Unix version of termcap, you must allocate space for the description yourself and pass
78 * the address of the space as the argument buffer. There is no way you can tell how much space is
79 * needed, so the convention is to allocate a buffer 2048 characters long and assume that is
80 * enough. (Formerly the convention was to allocate 1024 characters and assume that was enough.
81 * But one day, for one kind of terminal, that was not enough.)
87 get_termcap_string(char const* id, char* dest, size_t n)
92 tp = tgetstr(id, &tp);
93 if (tp != NULL && dest != NULL && n > 0) {
100 get_termcap_number(char const* id, int* dest, int low, int high)
102 int const val = tgetnum(id);
103 if (low <= val && val <= high) {
109 apply_termcap_settings(Console_IO_t * const mfp)
111 /* try to catch additional information about special console sequences */
112 char const* term_name = getenv("TERM");
113 if (NULL != term_name) {
114 char term_buff[4096];
115 int const ret = tgetent(term_buff, term_name);
117 get_termcap_number("co", &mfp->disp_width, 40, 512);
118 get_termcap_number("li", &mfp->disp_height, 16, 256);
119 get_termcap_string("up", mfp->str_up, sizeof(mfp->str_up));
120 get_termcap_string("md", mfp->str_emph, sizeof(mfp->str_emph));
121 get_termcap_string("me", mfp->str_norm, sizeof(mfp->str_norm));
122 get_termcap_string("ce", mfp->str_clreoln, sizeof(mfp->str_clreoln));
126 #endif /* TERMCAP_AVAILABLE */
129 init_console(Console_IO_t * const mfp)
131 /* setup basics of brhist I/O channels */
132 memset(mfp,0,sizeof(*mfp));
133 mfp->disp_width = 80;
134 mfp->disp_height = 25;
135 mfp->Console_fp = stderr;
136 mfp->Error_fp = stderr;
137 mfp->Report_fp = NULL;
139 /*mfp -> Console_buff = calloc ( 1, REPORT_BUFF_SIZE ); */
140 // setvbuf(mfp->Console_fp, mfp->Console_buff, _IOFBF, sizeof(mfp->Console_buff));
141 /* setvbuf ( mfp -> Error_fp , NULL , _IONBF, 0 ); */
143 #if defined(_WIN32) && !defined(__CYGWIN__)
144 mfp->Console_Handle = GetStdHandle(STD_ERROR_HANDLE);
147 strcpy(mfp->str_up, "\033[A");
150 apply_termcap_settings(mfp);
151 #endif /* TERMCAP_AVAILABLE */
153 mfp->ClassID = CLASS_ID;
155 #if defined(_WIN32) && !defined(__CYGWIN__)
162 deinit_console(Console_IO_t * const mfp)
164 if (mfp->Report_fp != NULL) {
165 fclose(mfp->Report_fp);
166 mfp->Report_fp = NULL;
168 fflush(mfp->Console_fp);
169 // setvbuf(mfp->Console_fp, NULL, _IONBF, (size_t) 0);
171 // memset(mfp->Console_buff, 0x55, REPORT_BUFF_SIZE);
177 Console_IO_t Console_IO;
180 frontend_open_console(void)
182 return init_console(&Console_IO);
186 frontend_close_console(void)
188 deinit_console(&Console_IO);
192 frontend_debugf(const char *format, va_list ap)
194 (void) my_report_printing(Console_IO.Report_fp, format, ap);
198 frontend_msgf(const char *format, va_list ap)
200 (void) my_console_printing(Console_IO.Console_fp, format, ap);
204 frontend_errorf(const char *format, va_list ap)
206 (void) my_error_printing(Console_IO.Error_fp, format, ap);
210 frontend_print_null(const char *format, va_list ap)
217 console_printf(const char *format, ...)
222 va_start(args, format);
223 ret = my_console_printing(Console_IO.Console_fp, format, args);
230 error_printf(const char *format, ...)
235 va_start(args, format);
236 ret = my_console_printing(Console_IO.Error_fp, format, args);
243 report_printf(const char *format, ...)
248 va_start(args, format);
249 ret = my_console_printing(Console_IO.Report_fp, format, args);
258 fflush(Console_IO.Console_fp);
264 fflush(Console_IO.Error_fp);
270 fflush(Console_IO.Report_fp);
274 console_up(int n_lines)
276 while (n_lines-- > 0)
277 fputs(Console_IO.str_up, Console_IO.Console_fp);
283 set_debug_file(const char *fn)
285 if (Console_IO.Report_fp == NULL) {
286 Console_IO.Report_fp = lame_fopen(fn, "a");
287 if (Console_IO.Report_fp != NULL) {
288 error_printf("writing debug info into: %s\n", fn);
291 error_printf("Error: can't open for debug info: %s\n", fn);
296 /* end of console.c */