From 12b38ad9e171a17c583488156f0c91255546afca Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 24 May 2018 08:59:01 +0100 Subject: [PATCH] New header file 'defs.h'. This centralises a few things that multiple header files were previously defining, and were protecting against each other's redefinition with ifdefs - small things like structs and typedefs. Now all those things are in a defs.h which is by definition safe to include _first_ (out of all the codebase-local headers) and only need to be defined once. --- defs.h | 37 +++++++++++++++++++++++++++++++++++++ misc.h | 10 ++-------- network.h | 11 +---------- putty.h | 10 +--------- ssh.h | 5 ----- unix/unix.h | 3 --- windows/winstuff.h | 14 ++------------ 7 files changed, 43 insertions(+), 47 deletions(-) create mode 100644 defs.h diff --git a/defs.h b/defs.h new file mode 100644 index 00000000..c406b3e9 --- /dev/null +++ b/defs.h @@ -0,0 +1,37 @@ +/* + * defs.h: initial definitions for PuTTY. + * + * The rule about this header file is that it can't depend on any + * other header file in this code base. This is where we define + * things, as much as we can, that other headers will want to refer + * to, such as opaque structure types and their associated typedefs, + * or macros that are used by other headers. + */ + +#ifndef PUTTY_DEFS_H +#define PUTTY_DEFS_H + +typedef struct conf_tag Conf; +typedef struct backend_tag Backend; +typedef struct terminal_tag Terminal; + +typedef struct Filename Filename; +typedef struct FontSpec FontSpec; + +typedef struct bufchain_tag bufchain; + +typedef struct strbuf strbuf; + +struct RSAKey; + +#include +typedef uint32_t uint32; + +/* Do a compile-time type-check of 'to_check' (without evaluating it), + * as a side effect of returning the value 'to_return'. Note that + * although this macro double-*expands* to_return, it always + * *evaluates* exactly one copy of it, so it's side-effect safe. */ +#define TYPECHECK(to_check, to_return) \ + (sizeof(to_check) ? (to_return) : (to_return)) + +#endif /* PUTTY_DEFS_H */ diff --git a/misc.h b/misc.h index d2efdca7..8075423b 100644 --- a/misc.h +++ b/misc.h @@ -5,6 +5,7 @@ #ifndef PUTTY_MISC_H #define PUTTY_MISC_H +#include "defs.h" #include "puttymem.h" #include /* for FILE * */ @@ -18,9 +19,6 @@ #define TRUE 1 #endif -typedef struct Filename Filename; -typedef struct FontSpec FontSpec; - unsigned long parse_blocksize(const char *bs); char ctrlparse(char *s, char **next); @@ -38,7 +36,7 @@ char *dupprintf(const char *fmt, ...) ; char *dupvprintf(const char *fmt, va_list ap); void burnstr(char *string); -typedef struct strbuf strbuf; + strbuf *strbuf_new(void); void strbuf_free(strbuf *buf); char *strbuf_str(strbuf *buf); /* does not free buf */ @@ -70,10 +68,6 @@ struct bufchain_tag { struct bufchain_granule *head, *tail; int buffersize; /* current amount of buffered data */ }; -#ifndef BUFCHAIN_TYPEDEF -typedef struct bufchain_tag bufchain; /* rest of declaration in misc.c */ -#define BUFCHAIN_TYPEDEF -#endif void bufchain_init(bufchain *ch); void bufchain_clear(bufchain *ch); diff --git a/network.h b/network.h index 0941f721..669424ad 100644 --- a/network.h +++ b/network.h @@ -13,12 +13,7 @@ #ifndef PUTTY_NETWORK_H #define PUTTY_NETWORK_H -#ifndef DONE_TYPEDEFS -#define DONE_TYPEDEFS -typedef struct conf_tag Conf; -typedef struct backend_tag Backend; -typedef struct terminal_tag Terminal; -#endif +#include "defs.h" typedef struct SockAddr_tag *SockAddr; /* pay attention to levels of indirection */ @@ -229,10 +224,6 @@ Socket new_error_socket(const char *errmsg, Plug plug); void backend_socket_log(void *frontend, int type, SockAddr addr, int port, const char *error_msg, int error_code, Conf *conf, int session_started); -#ifndef BUFCHAIN_TYPEDEF -typedef struct bufchain_tag bufchain; /* rest of declaration in misc.c */ -#define BUFCHAIN_TYPEDEF -#endif void log_proxy_stderr(Plug plug, bufchain *buf, const void *vdata, int len); #endif diff --git a/putty.h b/putty.h index 7555aede..139ed5d6 100644 --- a/putty.h +++ b/putty.h @@ -16,13 +16,7 @@ #endif #endif -#ifndef DONE_TYPEDEFS -#define DONE_TYPEDEFS -typedef struct conf_tag Conf; -typedef struct backend_tag Backend; -typedef struct terminal_tag Terminal; -#endif - +#include "defs.h" #include "puttyps.h" #include "network.h" #include "misc.h" @@ -538,8 +532,6 @@ GLOBAL int loaded_session; */ GLOBAL char *cmdline_session_name; -struct RSAKey; /* be a little careful of scope */ - /* * Mechanism for getting text strings such as usernames and passwords * from the front-end. diff --git a/ssh.h b/ssh.h index 579fd2cd..80677b7c 100644 --- a/ssh.h +++ b/ssh.h @@ -199,11 +199,6 @@ unsigned char *rsa_ssh1_public_blob(struct RSAKey *key, int *len, int rsa_public_blob_len(void *data, int maxlen); void freersakey(struct RSAKey *key); -#ifndef PUTTY_UINT32_DEFINED -/* This makes assumptions about the int type. */ -typedef unsigned int uint32; -#define PUTTY_UINT32_DEFINED -#endif typedef uint32 word32; unsigned long crc32_compute(const void *s, size_t len); diff --git a/unix/unix.h b/unix/unix.h index a6dcaca0..87ff6ea3 100644 --- a/unix/unix.h +++ b/unix/unix.h @@ -71,9 +71,6 @@ extern Backend pty_backend; #define BROKEN_PIPE_ERROR_CODE EPIPE /* used in sshshare.c */ -typedef uint32_t uint32; /* C99: uint32_t defined in stdint.h */ -#define PUTTY_UINT32_DEFINED - /* * Under GTK, we send MA_CLICK _and_ MA_2CLK, or MA_CLICK _and_ * MA_3CLK, when a button is pressed for the second or third time. diff --git a/windows/winstuff.h b/windows/winstuff.h index 84340b2f..8a65f38d 100644 --- a/windows/winstuff.h +++ b/windows/winstuff.h @@ -23,6 +23,8 @@ #include #endif +#include "defs.h" + #include "tree234.h" #include "winhelp.h" @@ -132,8 +134,6 @@ struct FontSpec *fontspec_new(const char *name, * * (DECL_WINDOWS_FUNCTION works with both these variants.) */ -#define TYPECHECK(to_check, to_return) \ - (sizeof(to_check) ? to_return : to_return) #define DECL_WINDOWS_FUNCTION(linkage, rettype, name, params) \ typedef rettype (WINAPI *t_##name) params; \ linkage t_##name p_##name @@ -164,13 +164,6 @@ struct FontSpec *fontspec_new(const char *name, #endif #endif -#ifndef DONE_TYPEDEFS -#define DONE_TYPEDEFS -typedef struct conf_tag Conf; -typedef struct backend_tag Backend; -typedef struct terminal_tag Terminal; -#endif - #define PUTTY_REG_POS "Software\\SimonTatham\\PuTTY" #define PUTTY_REG_PARENT "Software\\SimonTatham" #define PUTTY_REG_PARENT_CHILD "PuTTY" @@ -198,9 +191,6 @@ typedef struct terminal_tag Terminal; typedef HDC Context; -typedef unsigned int uint32; /* int is 32-bits on Win32 and Win64. */ -#define PUTTY_UINT32_DEFINED - #ifndef NO_GSSAPI /* * GSS-API stuff