Unified · Split
| @@ -298,6 +298,31 @@ int mp_fstat(int fd, struct mp_stat *buf) · 297 unmodified lines | ||
| 298 | 298 | return hstat(h, buf); |
| 299 | 299 | } |
| 300 | 300 | |
| 301 | +static inline HANDLE get_handle(FILE *stream) | |
| 302 | +{ | |
| 303 | + HANDLE wstream = INVALID_HANDLE_VALUE; | |
| 304 | + | |
| 305 | + if (stream == stdout || stream == stderr) { | |
| 306 | + wstream = GetStdHandle(stream == stdout ? | |
| 307 | + STD_OUTPUT_HANDLE : STD_ERROR_HANDLE); | |
| 308 | + } | |
| 309 | + return wstream; | |
| 310 | +} | |
| 311 | + | |
| 312 | +int mp_fputs(const char *str, FILE *stream) | |
| 313 | +{ | |
| 314 | + HANDLE wstream = get_handle(stream); | |
| 315 | + if (mp_check_console(wstream)) | |
| 316 | + return mp_console_fputs(wstream, bstr0(str)); | |
| 317 | + | |
| 318 | + return fputs(str, stream); | |
| 319 | +} | |
| 320 | + | |
| 321 | +int mp_puts(const char *str) | |
| 322 | +{ | |
| 323 | + return mp_fputs(str, stdout); | |
| 324 | +} | |
| 325 | + | |
| 301 | 326 | #if HAVE_UWP |
| 302 | 327 | static int mp_vfprintf(FILE *stream, const char *format, va_list args) |
| 303 | 328 | { |
| @@ -307,15 +332,9 @@ static int mp_vfprintf(FILE *stream, const char *format, va_list args) · 3 unmodified lines | ||
| 307 | 332 | |
| 308 | 333 | static int mp_vfprintf(FILE *stream, const char *format, va_list args) |
| 309 | 334 | { |
| 310 | − HANDLE wstream = INVALID_HANDLE_VALUE; | |
| 311 | − | |
| 312 | − if (stream == stdout || stream == stderr) { | |
| 313 | − wstream = GetStdHandle(stream == stdout ? | |
| 314 | − STD_OUTPUT_HANDLE : STD_ERROR_HANDLE); | |
| 315 | − } | |
| 316 | − | |
| 335 | + HANDLE wstream = get_handle(stream); | |
| 317 | 336 | if (mp_check_console(wstream)) |
| 318 | − return mp_write_console_ansi(wstream, format, args); | |
| 337 | + return mp_console_vfprintf(wstream, format, args); | |
| 319 | 338 | |
| 320 | 339 | return vfprintf(stream, format, args); |
| 321 | 340 | } |
| @@ -106,6 +106,8 @@ char *mp_to_utf8(void *talloc_ctx, const wchar_t *s); · 105 unmodified lines | ||
| 106 | 106 | #include <sys/stat.h> |
| 107 | 107 | #include <fcntl.h> |
| 108 | 108 | |
| 109 | +int mp_puts(const char *str); | |
| 110 | +int mp_fputs(const char *str, FILE *stream); | |
| 109 | 111 | int mp_printf(const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
| 110 | 112 | int mp_fprintf(FILE *stream, const char *format, ...) PRINTF_ATTRIBUTE(2, 3); |
| 111 | 113 | int mp_open(const char *filename, int oflag, ...); |
| @@ -174,6 +176,8 @@ int mp_glob(const char *restrict pattern, int flags, · 62 unmodified lines | ||
| 174 | 176 | int (*errfunc)(const char*, int), mp_glob_t *restrict pglob); |
| 175 | 177 | void mp_globfree(mp_glob_t *pglob); |
| 176 | 178 | |
| 179 | +#define puts(...) mp_puts(__VA_ARGS__) | |
| 180 | +#define fputs(...) mp_fputs(__VA_ARGS__) | |
| 177 | 181 | #define printf(...) mp_printf(__VA_ARGS__) |
| 178 | 182 | #define fprintf(...) mp_fprintf(__VA_ARGS__) |
| 179 | 183 | #define open(...) mp_open(__VA_ARGS__) |
| @@ -27,7 +27,12 @@ void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height) · 26 unmodified lines | ||
| 27 | 27 | { |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | −int mp_write_console_ansi(void *wstream, const char *format, va_list args) | |
| 30 | +int mp_console_vfprintf(void *wstream, const char *format, va_list args) | |
| 31 | +{ | |
| 32 | + return 0; | |
| 33 | +} | |
| 34 | + | |
| 35 | +int mp_console_fputs(void *wstream, bstr str) | |
| 31 | 36 | { |
| 32 | 37 | return 0; |
| 33 | 38 | } |
| @@ -226,15 +226,36 @@ bool terminal_in_background(void) · 225 unmodified lines | ||
| 226 | 226 | return false; |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | −int mp_write_console_ansi(HANDLE wstream, const char *format, va_list args) | |
| 229 | +int mp_console_vfprintf(HANDLE wstream, const char *format, va_list args) | |
| 230 | 230 | { |
| 231 | 231 | struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key); |
| 232 | − if (!buffers) | |
| 232 | + bool free_buf = false; | |
| 233 | + if (!buffers) { | |
| 233 | 234 | buffers = talloc_zero(NULL, struct tmp_buffers); |
| 235 | + free_buf = !FlsSetValue(tmp_buffers_key, buffers); | |
| 236 | + } | |
| 234 | 237 | |
| 235 | 238 | buffers->write_console_buf.len = 0; |
| 236 | 239 | bstr_xappend_vasprintf(buffers, &buffers->write_console_buf, format, args); |
| 237 | − int wlen = bstr_to_wchar(buffers, buffers->write_console_buf, &buffers->write_console_wbuf); | |
| 240 | + | |
| 241 | + int ret = mp_console_fputs(wstream, buffers->write_console_buf); | |
| 242 | + | |
| 243 | + if (free_buf) | |
| 244 | + talloc_free(buffers); | |
| 245 | + | |
| 246 | + return ret; | |
| 247 | +} | |
| 248 | + | |
| 249 | +int mp_console_fputs(HANDLE wstream, bstr str) | |
| 250 | +{ | |
| 251 | + struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key); | |
| 252 | + bool free_buf = false; | |
| 253 | + if (!buffers) { | |
| 254 | + buffers = talloc_zero(NULL, struct tmp_buffers); | |
| 255 | + free_buf = !FlsSetValue(tmp_buffers_key, buffers); | |
| 256 | + } | |
| 257 | + | |
| 258 | + int wlen = bstr_to_wchar(buffers, str, &buffers->write_console_wbuf); | |
| 238 | 259 | wchar_t *pos = buffers->write_console_wbuf; |
| 239 | 260 | |
| 240 | 261 | while (*pos) { |
| @@ -380,7 +401,7 @@ int mp_write_console_ansi(HANDLE wstream, const char *format, va_list args) · 139 unmodified lines | ||
| 380 | 401 | |
| 381 | 402 | int ret = buffers->write_console_buf.len; |
| 382 | 403 | |
| 383 | − if (!FlsSetValue(tmp_buffers_key, buffers)) | |
| 404 | + if (free_buf) | |
| 384 | 405 | talloc_free(buffers); |
| 385 | 406 | |
| 386 | 407 | return ret; |
| @@ -54,7 +54,8 @@ void terminal_get_size(int *w, int *h); · 53 unmodified lines | ||
| 54 | 54 | void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height); |
| 55 | 55 | |
| 56 | 56 | // Windows only. |
| 57 | −int mp_write_console_ansi(void *wstream, const char *format, va_list args); | |
| 57 | +int mp_console_vfprintf(void *wstream, const char *format, va_list args); | |
| 58 | +int mp_console_fputs(void *wstream, bstr str); | |
| 58 | 59 | bool mp_check_console(void *handle); |
| 59 | 60 | |
| 60 | 61 | /* Windows-only function to attach to the parent process's console */ |
| @@ -107,7 +107,8 @@ void mp_msg(struct mp_log *log, int lev, const char *format, ...) {}; · 106 unmodified lines | ||
| 107 | 107 | int mp_msg_find_level(const char *s) {return 0;}; |
| 108 | 108 | int mp_msg_level(struct mp_log *log) {return 0;}; |
| 109 | 109 | void mp_msg_set_max_level(struct mp_log *log, int lev) {}; |
| 110 | −int mp_write_console_ansi(void *wstream, const char *format, va_list args) {return 0;}; | |
| 110 | +int mp_console_vfprintf(void *wstream, const char *format, va_list args) {return 0;}; | |
| 111 | +int mp_console_fputs(void *wstream, bstr str) {return 0;}; | |
| 111 | 112 | bool mp_check_console(void *handle) { return false; }; |
| 112 | 113 | void mp_set_avdict(AVDictionary **dict, char **kv) {}; |
| 113 | 114 | struct mp_log *mp_log_new(void *talloc_ctx, struct mp_log *parent, |