| 1 |
#ifndef H_LOCAL |
| 2 |
#define H_LOCAL |
| 3 |
/* |
| 4 |
local: some stuff for localisation |
| 5 |
|
| 6 |
Currently, this is just about determining if we got UTF-8 locale and |
| 7 |
checking output terminal properties. |
| 8 |
|
| 9 |
copyright 2008-2020 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 10 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 11 |
initially written by Thomas Orgis, based on a patch by Thorsten Glaser. |
| 12 |
|
| 13 |
This got extended for the distinciton between a vague wish for UTF-8 in/output |
| 14 |
encoding from environment variables (utf8env) and a system actually being |
| 15 |
configured to correcty work with it (utf8loc). The configured locale is needed |
| 16 |
to enable proper (?) length computation and control character filtering. |
| 17 |
|
| 18 |
The fallback shall be either dumb UTF-8 with just filtering C0 and C1 control |
| 19 |
characters, or even full-on stripping to 7-bit ASCII. |
| 20 |
*/ |
| 21 |
|
| 22 |
/* Pulled in by mpg123app.h! */ |
| 23 |
|
| 24 |
/* Set this to enforce UTF-8 output (utf8env will be 1, but utf8loc may not) */ |
| 25 |
extern int utf8force; |
| 26 |
|
| 27 |
/* This is 1 if check_locale found some UTF-8 hint, 0 if not. */ |
| 28 |
extern int utf8env; |
| 29 |
|
| 30 |
/* This is 1 if check_locale configured a locale with UTF-8 charset */ |
| 31 |
extern int utf8loc; |
| 32 |
|
| 33 |
/* Check/set locale, set the utf8env variable. |
| 34 |
After calling this, a locale for LC_CTYPE should be configured, |
| 35 |
but one that is based on UTF-8 only if utf8loc is set. */ |
| 36 |
void check_locale(void); |
| 37 |
|
| 38 |
/* Return non-zero if full terminal fun is desired/possible. */ |
| 39 |
int term_have_fun(int fd, int want_visuals); |
| 40 |
|
| 41 |
/* Return width of terminal associated with given descriptor, |
| 42 |
-1 when there is none. */ |
| 43 |
int term_width(int fd); |
| 44 |
|
| 45 |
// Prepare a string that is safe and sensible to print to the console. |
| 46 |
// Input: UTF-8 or 7-bit ASCII string |
| 47 |
// Output: sanitized output string for UTF-8 or ASCII, control characters |
| 48 |
// dropped (including line breaks) |
| 49 |
// Modifer: Output is destined for a terminal and non-printing characters |
| 50 |
// shall be filtered out. Otherwise, things are just adjusted for non-utf8 |
| 51 |
// locale. |
| 52 |
// Return value: An estimate of the printed string width. In an UTF-8 |
| 53 |
// locale, this should often be right, but there never is a guarantee |
| 54 |
// with Unicode. If the whole operation failed, dest will be NULL. |
| 55 |
size_t utf8outstr(char **dest, const char *source, int to_terminal); |
| 56 |
// Take an unspecified (assumed ASCII-based) encoding and at least |
| 57 |
// construct valid UTF-8 with 7-bit ASCII and replacement characters. |
| 58 |
// This looses C1 control characters. |
| 59 |
// If count is >= 0, it is used instead of strlen(source), enabling |
| 60 |
// processing of data without closing zero byte. |
| 61 |
// Returns 0 if all went well, as do the others following. |
| 62 |
// On error, the dest memory is freed and the pointer nulled. |
| 63 |
int unknown2utf8(char **dest, const char *source, int count); |
| 64 |
// Wrapper around the above for printing the string to some stream. |
| 65 |
// Return value is directly from fprintf or also -1 if there was trouble |
| 66 |
// processing the string. |
| 67 |
// If the output is a terminal as indicated by is_terminal, this applies |
| 68 |
// all the filtering to avoid non-print characters, non-utf8 stuff is reduced |
| 69 |
// to safe ASCII. If not printing to a terminal, only utf8 is reduced for |
| 70 |
// non-utf8-locales. Without explicit is_utf8, the string is assumed to |
| 71 |
// match the current environment. So it's taken as UTF-8 for UTF-8 environment |
| 72 |
// and something ASCII-based otherwise (triggering ASCII C0 and C1 filtering). |
| 73 |
int outstr(char **dest, const char *str, int is_utf8, int is_terminal); |
| 74 |
int print_outstr(FILE *out, const char *str, int is_utf8, int is_terminal); |
| 75 |
|
| 76 |
#endif |