]> 4ch.mooo.com Git - 16.git/blob - src/lib/dl/ext/lame/console.c
meh did some cleanings and i will work on mapread to mm thingy sometime soon! oops...
[16.git] / src / lib / dl / ext / lame / console.c
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 #if defined(HAVE_NCURSES_TERMCAP_H)
21 # include <ncurses/termcap.h>
22 #elif defined(HAVE_TERMCAP_H)
23 # include <termcap.h>
24 #elif defined(HAVE_TERMCAP)
25 # include <curses.h>
26 # if !defined(__bsdi__)
27 #  include <term.h>
28 # endif
29 #endif
30
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include "console.h"
34 #include "main.h"
35
36 #ifdef WITH_DMALLOC
37 #include <dmalloc.h>
38 #endif
39
40 #define CLASS_ID           0x434F4E53
41 #define REPORT_BUFF_SIZE   1024
42
43 #if defined(_WIN32)  &&  !defined(__CYGWIN__)
44 # include <windows.h>
45 #endif
46
47
48
49 static int
50 my_console_printing(FILE * fp, const char *format, va_list ap)
51 {
52     if (fp != NULL)
53         return vfprintf(fp, format, ap);
54     return 0;
55 }
56
57 static int
58 my_error_printing(FILE * fp, const char *format, va_list ap)
59 {
60     if (fp != NULL)
61         return vfprintf(fp, format, ap);
62     return 0;
63 }
64
65 static int
66 my_report_printing(FILE * fp, const char *format, va_list ap)
67 {
68     if (fp != NULL)
69         return vfprintf(fp, format, ap);
70     return 0;
71 }
72
73
74 /*
75  * Taken from Termcap_Manual.html:
76  *
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.)
82  */
83
84 #ifdef HAVE_TERMCAP
85
86 static void
87 get_termcap_string(char const* id, char* dest, size_t n)
88 {
89     char    tc[16];
90     char   *tp = tc;
91     tp[0] = '\0';
92     tp = tgetstr(id, &tp);
93     if (tp != NULL && dest != NULL && n > 0) {
94         strncpy(dest, tp, n);
95         dest[n-1] = '\0';
96     }
97 }
98
99 static void 
100 get_termcap_number(char const* id, int* dest, int low, int high)
101 {
102     int const val = tgetnum(id);
103     if (low <= val && val <= high) {
104         *dest = val;
105     }
106 }
107
108 static void
109 apply_termcap_settings(Console_IO_t * const mfp)
110 {
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);
116         if (1 == ret) {
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));
123         }
124     }
125 }
126 #endif /* TERMCAP_AVAILABLE */
127
128 static int
129 init_console(Console_IO_t * const mfp)
130 {
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;
138
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                                ); */
142
143 #if defined(_WIN32)  &&  !defined(__CYGWIN__)
144     mfp->Console_Handle = GetStdHandle(STD_ERROR_HANDLE);
145 #endif
146
147     strcpy(mfp->str_up, "\033[A");
148
149 #ifdef HAVE_TERMCAP
150     apply_termcap_settings(mfp);
151 #endif /* TERMCAP_AVAILABLE */
152
153     mfp->ClassID = CLASS_ID;
154
155 #if defined(_WIN32)  &&  !defined(__CYGWIN__)
156 #else
157 #endif
158     return 0;
159 }
160
161 static void
162 deinit_console(Console_IO_t * const mfp)
163 {
164     if (mfp->Report_fp != NULL) {
165         fclose(mfp->Report_fp);
166         mfp->Report_fp = NULL;
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 }
173
174
175 /*  LAME console
176  */
177 Console_IO_t Console_IO;
178
179 int
180 frontend_open_console(void)
181 {
182     return init_console(&Console_IO);
183 }
184
185 void
186 frontend_close_console(void)
187 {
188     deinit_console(&Console_IO);
189 }
190
191 void
192 frontend_debugf(const char *format, va_list ap)
193 {
194     (void) my_report_printing(Console_IO.Report_fp, format, ap);
195 }
196
197 void
198 frontend_msgf(const char *format, va_list ap)
199 {
200     (void) my_console_printing(Console_IO.Console_fp, format, ap);
201 }
202
203 void
204 frontend_errorf(const char *format, va_list ap)
205 {
206     (void) my_error_printing(Console_IO.Error_fp, format, ap);
207 }
208
209 void
210 frontend_print_null(const char *format, va_list ap)
211 {
212     (void) format;
213     (void) ap;
214 }
215
216 int
217 console_printf(const char *format, ...)
218 {
219     va_list args;
220     int     ret;
221
222     va_start(args, format);
223     ret = my_console_printing(Console_IO.Console_fp, format, args);
224     va_end(args);
225
226     return ret;
227 }
228
229 int
230 error_printf(const char *format, ...)
231 {
232     va_list args;
233     int     ret;
234
235     va_start(args, format);
236     ret = my_console_printing(Console_IO.Error_fp, format, args);
237     va_end(args);
238
239     return ret;
240 }
241
242 int
243 report_printf(const char *format, ...)
244 {
245     va_list args;
246     int     ret;
247
248     va_start(args, format);
249     ret = my_console_printing(Console_IO.Report_fp, format, args);
250     va_end(args);
251
252     return ret;
253 }
254
255 void
256 console_flush()
257 {
258     fflush(Console_IO.Console_fp);
259 }
260
261 void
262 error_flush()
263 {
264     fflush(Console_IO.Error_fp);
265 }
266
267 void
268 report_flush()
269 {
270     fflush(Console_IO.Report_fp);
271 }
272
273 void
274 console_up(int n_lines)
275 {
276     while (n_lines-- > 0)
277         fputs(Console_IO.str_up, Console_IO.Console_fp);
278     console_flush();
279 }
280
281
282 void
283 set_debug_file(const char *fn)
284 {
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);
289         }
290         else {
291             error_printf("Error: can't open for debug info: %s\n", fn);
292         }
293     }
294 }
295
296 /* end of console.c */