1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 09:58:01 +00:00
putty-source/unix/uxucs.c
Simon Tatham 3214563d8e Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.

PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.

I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!

To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.

In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
 - the 'multisel' field in dialog.h's list box structure, for which
   the GTK front end in particular recognises a difference between 1
   and 2 but nearly everything else treats as boolean
 - the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
   something about the specific location of the urgent pointer, but
   most clients only care about 0 vs 'something nonzero'
 - the return value of wc_match, where -1 indicates a syntax error in
   the wildcard.
 - the return values from SSH-1 RSA-key loading functions, which use
   -1 for 'wrong passphrase' and 0 for all other failures (so any
   caller which already knows it's not loading an _encrypted private_
   key can treat them as boolean)
 - term->esc_query, and the 'query' parameter in toggle_mode in
   terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
   but can also hold -1 for some other intervening character that we
   don't support.

In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
 - the return value of plug_accepting uses the POSIXish convention of
   0=success and nonzero=error; I think if I made it bool then I'd
   also want to reverse its sense, and that's a job for a separate
   piece of work.
 - the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
   represent the default and alternate screens. There's no obvious
   reason why one of those should be considered 'true' or 'positive'
   or 'success' - they're just indices - so I've left it as int.

ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.

In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.

Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-03 13:45:00 +00:00

269 lines
6.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <locale.h>
#include <limits.h>
#include <wchar.h>
#include <time.h>
#include "putty.h"
#include "charset.h"
#include "terminal.h"
#include "misc.h"
/*
* Unix Unicode-handling routines.
*/
bool is_dbcs_leadbyte(int codepage, char byte)
{
return false; /* we don't do DBCS */
}
int mb_to_wc(int codepage, int flags, const char *mbstr, int mblen,
wchar_t *wcstr, int wclen)
{
if (codepage == DEFAULT_CODEPAGE) {
int n = 0;
mbstate_t state;
memset(&state, 0, sizeof state);
while (mblen > 0) {
size_t i = mbrtowc(wcstr+n, mbstr, (size_t)mblen, &state);
if (i == (size_t)-1 || i == (size_t)-2)
break;
n++;
mbstr += i;
mblen -= i;
}
return n;
} else if (codepage == CS_NONE) {
int n = 0;
while (mblen > 0) {
wcstr[n] = 0xD800 | (mbstr[0] & 0xFF);
n++;
mbstr++;
mblen--;
}
return n;
} else
return charset_to_unicode(&mbstr, &mblen, wcstr, wclen, codepage,
NULL, NULL, 0);
}
int wc_to_mb(int codepage, int flags, const wchar_t *wcstr, int wclen,
char *mbstr, int mblen, const char *defchr,
struct unicode_data *ucsdata)
{
if (codepage == DEFAULT_CODEPAGE) {
char output[MB_LEN_MAX];
mbstate_t state;
int n = 0;
memset(&state, 0, sizeof state);
while (wclen > 0) {
int i = wcrtomb(output, wcstr[0], &state);
if (i == (size_t)-1 || i > n - mblen)
break;
memcpy(mbstr+n, output, i);
n += i;
wcstr++;
wclen--;
}
return n;
} else if (codepage == CS_NONE) {
int n = 0;
while (wclen > 0 && n < mblen) {
if (*wcstr >= 0xD800 && *wcstr < 0xD900)
mbstr[n++] = (*wcstr & 0xFF);
else if (defchr)
mbstr[n++] = *defchr;
wcstr++;
wclen--;
}
return n;
} else {
return charset_from_unicode(&wcstr, &wclen, mbstr, mblen, codepage,
NULL, defchr?defchr:NULL, defchr?1:0);
}
}
/*
* Return value is true if pterm is to run in direct-to-font mode.
*/
bool init_ucs(struct unicode_data *ucsdata, char *linecharset,
bool utf8_override, int font_charset, int vtmode)
{
int i;
bool ret = false;
/*
* In the platform-independent parts of the code, font_codepage
* is used only for system DBCS support - which we don't
* support at all. So we set this to something which will never
* be used.
*/
ucsdata->font_codepage = -1;
/*
* If utf8_override is set and the POSIX locale settings
* dictate a UTF-8 character set, then just go straight for
* UTF-8.
*/
ucsdata->line_codepage = CS_NONE;
if (utf8_override) {
const char *s;
if (((s = getenv("LC_ALL")) && *s) ||
((s = getenv("LC_CTYPE")) && *s) ||
((s = getenv("LANG")) && *s)) {
if (strstr(s, "UTF-8"))
ucsdata->line_codepage = CS_UTF8;
}
}
/*
* Failing that, line_codepage should be decoded from the
* specification in conf.
*/
if (ucsdata->line_codepage == CS_NONE)
ucsdata->line_codepage = decode_codepage(linecharset);
/*
* If line_codepage is _still_ CS_NONE, we assume we're using
* the font's own encoding. This has been passed in to us, so
* we use that. If it's still CS_NONE after _that_ - i.e. the
* font we were given had an incomprehensible charset - then we
* fall back to using the D800 page.
*/
if (ucsdata->line_codepage == CS_NONE)
ucsdata->line_codepage = font_charset;
if (ucsdata->line_codepage == CS_NONE)
ret = true;
/*
* Set up unitab_line, by translating each individual character
* in the line codepage into Unicode.
*/
for (i = 0; i < 256; i++) {
char c[1];
const char *p;
wchar_t wc[1];
int len;
c[0] = i;
p = c;
len = 1;
if (ucsdata->line_codepage == CS_NONE)
ucsdata->unitab_line[i] = 0xD800 | i;
else if (1 == charset_to_unicode(&p, &len, wc, 1,
ucsdata->line_codepage,
NULL, L"", 0))
ucsdata->unitab_line[i] = wc[0];
else
ucsdata->unitab_line[i] = 0xFFFD;
}
/*
* Set up unitab_xterm. This is the same as unitab_line except
* in the line-drawing regions, where it follows the Unicode
* encoding.
*
* (Note that the strange X encoding of line-drawing characters
* in the bottom 32 glyphs of ISO8859-1 fonts is taken care of
* by the font encoding, which will spot such a font and act as
* if it were in a variant encoding of ISO8859-1.)
*/
for (i = 0; i < 256; i++) {
static const wchar_t unitab_xterm_std[32] = {
0x2666, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1,
0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba,
0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c,
0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, 0x0020
};
static const wchar_t unitab_xterm_poorman[32] =
L"*#****o~**+++++-----++++|****L. ";
const wchar_t *ptr;
if (vtmode == VT_POORMAN)
ptr = unitab_xterm_poorman;
else
ptr = unitab_xterm_std;
if (i >= 0x5F && i < 0x7F)
ucsdata->unitab_xterm[i] = ptr[i & 0x1F];
else
ucsdata->unitab_xterm[i] = ucsdata->unitab_line[i];
}
/*
* Set up unitab_scoacs. The SCO Alternate Character Set is
* simply CP437.
*/
for (i = 0; i < 256; i++) {
char c[1];
const char *p;
wchar_t wc[1];
int len;
c[0] = i;
p = c;
len = 1;
if (1 == charset_to_unicode(&p, &len, wc, 1, CS_CP437, NULL, L"", 0))
ucsdata->unitab_scoacs[i] = wc[0];
else
ucsdata->unitab_scoacs[i] = 0xFFFD;
}
/*
* Find the control characters in the line codepage. For
* direct-to-font mode using the D800 hack, we assume 00-1F and
* 7F are controls, but allow 80-9F through. (It's as good a
* guess as anything; and my bet is that half the weird fonts
* used in this way will be IBM or MS code pages anyway.)
*/
for (i = 0; i < 256; i++) {
int lineval = ucsdata->unitab_line[i];
if (lineval < ' ' || (lineval >= 0x7F && lineval < 0xA0) ||
(lineval >= 0xD800 && lineval < 0xD820) || (lineval == 0xD87F))
ucsdata->unitab_ctrl[i] = i;
else
ucsdata->unitab_ctrl[i] = 0xFF;
}
return ret;
}
const char *cp_name(int codepage)
{
if (codepage == CS_NONE)
return "Use font encoding";
return charset_to_localenc(codepage);
}
const char *cp_enumerate(int index)
{
int charset;
charset = charset_localenc_nth(index);
if (charset == CS_NONE) {
/* "Use font encoding" comes after all the named charsets */
if (charset_localenc_nth(index-1) != CS_NONE)
return "Use font encoding";
return NULL;
}
return charset_to_localenc(charset);
}
int decode_codepage(char *cp_name)
{
if (!cp_name || !*cp_name)
return CS_UTF8;
return charset_from_localenc(cp_name);
}