1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-02 03:52:49 -05:00

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'!
This commit is contained in:
Simon Tatham
2018-11-02 19:23:19 +00:00
parent 1378bb049a
commit 3214563d8e
164 changed files with 2914 additions and 2805 deletions

View File

@ -44,12 +44,12 @@ struct Filename {
struct FontSpec {
char *name;
int isbold;
bool isbold;
int height;
int charset;
};
struct FontSpec *fontspec_new(const char *name,
int bold, int height, int charset);
struct FontSpec *fontspec_new(
const char *name, bool bold, int height, int charset);
#ifndef CLEARTYPE_QUALITY
#define CLEARTYPE_QUALITY 5
@ -228,7 +228,7 @@ GLOBAL HINSTANCE hinst;
*/
void init_help(void);
void shutdown_help(void);
int has_help(void);
bool has_help(void);
void launch_help(HWND hwnd, const char *topic);
void quit_help(HWND hwnd);
@ -259,7 +259,7 @@ int win_seat_confirm_weak_cached_hostkey(
* which takes the data string in the system code page instead of
* Unicode.
*/
void write_aclip(int clipboard, char *, int, int);
void write_aclip(int clipboard, char *, int, bool);
#define WM_NETEVENT (WM_APP + 5)
@ -331,7 +331,7 @@ DECL_WINDOWS_FUNCTION(GLOBAL, int, select,
fd_set FAR *, const struct timeval FAR *));
#endif
extern int socket_writable(SOCKET skt);
extern bool socket_writable(SOCKET skt);
extern void socket_reselect_all(void);
@ -354,7 +354,7 @@ void init_common_controls(void); /* also does some DLL-loading */
* Exports from winutils.c.
*/
typedef struct filereq_tag filereq; /* cwd for file requester */
BOOL request_file(filereq *state, OPENFILENAME *of, int preserve, int save);
bool request_file(filereq *state, OPENFILENAME *of, bool preserve, bool save);
filereq *filereq_new(void);
void filereq_free(filereq *state);
int message_box(LPCTSTR text, LPCTSTR caption, DWORD style, DWORD helpctxid);
@ -369,7 +369,7 @@ struct prefslist {
int listid, upbid, dnbid;
int srcitem;
int dummyitem;
int dragging;
bool dragging;
};
/*
@ -384,13 +384,17 @@ struct dlgparam {
char *errtitle; /* title of error sub-messageboxes */
void *data; /* data to pass in refresh events */
union control *focused, *lastfocused; /* which ctrl has focus now/before */
char shortcuts[128]; /* track which shortcuts in use */
int coloursel_wanted; /* has an event handler asked for
bool shortcuts[128]; /* track which shortcuts in use */
bool coloursel_wanted; /* has an event handler asked for
* a colour selector? */
struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */
struct {
unsigned char r, g, b; /* 0-255 */
bool ok;
} coloursel_result;
tree234 *privdata; /* stores per-control private data */
int ended, endresult; /* has the dialog been ended? */
int fixed_pitch_fonts; /* are we constrained to fixed fonts? */
bool ended; /* has the dialog been ended? */
int endresult; /* and if so, what was the result? */
bool fixed_pitch_fonts; /* are we constrained to fixed fonts? */
};
/*
@ -403,7 +407,7 @@ HWND doctl(struct ctlpos *cp, RECT r,
void bartitle(struct ctlpos *cp, char *name, int id);
void beginbox(struct ctlpos *cp, char *name, int idbox);
void endbox(struct ctlpos *cp);
void editboxfw(struct ctlpos *cp, int password, char *text,
void editboxfw(struct ctlpos *cp, bool password, char *text,
int staticid, int editid);
void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...);
void bareradioline(struct ctlpos *cp, int nacross, ...);
@ -440,7 +444,7 @@ void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
char *stext, int sid, int listid, int upbid, int dnbid);
int handle_prefslist(struct prefslist *hdl,
int *array, int maxmemb,
int is_dlmsg, HWND hwnd,
bool is_dlmsg, HWND hwnd,
WPARAM wParam, LPARAM lParam);
void progressbar(struct ctlpos *cp, int id);
void fwdsetter(struct ctlpos *cp, int listid, char *stext, int sid,
@ -450,8 +454,8 @@ void fwdsetter(struct ctlpos *cp, int listid, char *stext, int sid,
char *r1text, int r1id, char *r2text, int r2id);
void dlg_auto_set_fixed_pitch_flag(dlgparam *dlg);
int dlg_get_fixed_pitch_flag(dlgparam *dlg);
void dlg_set_fixed_pitch_flag(dlgparam *dlg, int flag);
bool dlg_get_fixed_pitch_flag(dlgparam *dlg);
void dlg_set_fixed_pitch_flag(dlgparam *dlg, bool flag);
#define MAX_SHORTCUTS_PER_CTRL 16
@ -503,10 +507,10 @@ struct winctrl *winctrl_findbyid(struct winctrls *, int);
struct winctrl *winctrl_findbyindex(struct winctrls *, int);
void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
struct ctlpos *cp, struct controlset *s, int *id);
int winctrl_handle_command(struct dlgparam *dp, UINT msg,
WPARAM wParam, LPARAM lParam);
bool winctrl_handle_command(struct dlgparam *dp, UINT msg,
WPARAM wParam, LPARAM lParam);
void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c);
int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id);
bool winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id);
void dp_init(struct dlgparam *dp);
void dp_add_tree(struct dlgparam *dp, struct winctrls *tree);
@ -515,15 +519,15 @@ void dp_cleanup(struct dlgparam *dp);
/*
* Exports from wincfg.c.
*/
void win_setup_config_box(struct controlbox *b, HWND *hwndp, int has_help,
int midsession, int protocol);
void win_setup_config_box(struct controlbox *b, HWND *hwndp, bool has_help,
bool midsession, int protocol);
/*
* Exports from windlg.c.
*/
void defuse_showwindow(void);
int do_config(void);
int do_reconfig(HWND, int);
bool do_config(void);
bool do_reconfig(HWND, int);
void showeventlog(HWND);
void showabout(HWND);
void force_normal(HWND hwnd);
@ -539,7 +543,7 @@ void dll_hijacking_protection(void);
HMODULE load_system32_dll(const char *libname);
const char *win_strerror(int error);
void restrict_process_acl(void);
GLOBAL int restricted_acl;
GLOBAL bool restricted_acl;
/* A few pieces of up-to-date Windows API definition needed for older
* compilers. */
@ -561,7 +565,7 @@ DECLSPEC_IMPORT DLL_DIRECTORY_COOKIE WINAPI AddDllDirectory (PCWSTR NewDirectory
* Exports from sizetip.c.
*/
void UpdateSizeTip(HWND src, int cx, int cy);
void EnableSizeTip(int bEnable);
void EnableSizeTip(bool bEnable);
/*
* Exports from unicode.c.
@ -617,12 +621,12 @@ extern const struct BackendVtable serial_backend;
void add_session_to_jumplist(const char * const sessionname);
void remove_session_from_jumplist(const char * const sessionname);
void clear_jumplist(void);
BOOL set_explicit_app_user_model_id();
bool set_explicit_app_user_model_id();
/*
* Exports from winnoise.c.
*/
int win_read_random(void *buf, unsigned wanted); /* returns true on success */
bool win_read_random(void *buf, unsigned wanted); /* returns true on success */
/*
* Extra functions in winstore.c over and above the interface in