1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 01:48:00 +00:00

Invent structure tags for the storage.h abstractions.

Most of these were 'void *' because they weren't even reliably a
structure type underneath - the per-OS storage systems would directly
cast read/write/enum settings handles to and from random things like
FILE *, Unix DIR *, or Windows HKEY. So I've wrapped them in tiny
structs for the sake of having a sensible structure tag visible
elsewhere in the code.
This commit is contained in:
Simon Tatham 2018-09-14 08:45:42 +01:00
parent 7efa4a5305
commit 733fcca2cd
7 changed files with 131 additions and 97 deletions

4
defs.h
View File

@ -61,6 +61,10 @@ typedef struct share_channel share_channel;
typedef struct dlgparam dlgparam; typedef struct dlgparam dlgparam;
typedef struct settings_w settings_w;
typedef struct settings_r settings_r;
typedef struct settings_e settings_e;
/* Note indirection: for historical reasons (it used to be closer to /* Note indirection: for historical reasons (it used to be closer to
* the OS socket type), the type that most code uses for a socket is * the OS socket type), the type that most code uses for a socket is
* 'Socket', not 'Socket *'. So an implementation of Socket or Plug * 'Socket', not 'Socket *'. So an implementation of Socket or Plug

View File

@ -1074,9 +1074,9 @@ const struct Backend_vtable *backend_vt_from_name(const char *name);
const struct Backend_vtable *backend_vt_from_proto(int proto); const struct Backend_vtable *backend_vt_from_proto(int proto);
char *get_remote_username(Conf *conf); /* dynamically allocated */ char *get_remote_username(Conf *conf); /* dynamically allocated */
char *save_settings(const char *section, Conf *conf); char *save_settings(const char *section, Conf *conf);
void save_open_settings(void *sesskey, Conf *conf); void save_open_settings(settings_w *sesskey, Conf *conf);
void load_settings(const char *section, Conf *conf); void load_settings(const char *section, Conf *conf);
void load_open_settings(void *sesskey, Conf *conf); void load_open_settings(settings_r *sesskey, Conf *conf);
void get_sesslist(struct sesslist *, int allocate); void get_sesslist(struct sesslist *, int allocate);
void do_defaults(const char *, Conf *); void do_defaults(const char *, Conf *);
void registry_cleanup(void); void registry_cleanup(void);

View File

@ -502,7 +502,7 @@ static void read_clip_setting(void *handle, const char *savekey,
char *save_settings(const char *section, Conf *conf) char *save_settings(const char *section, Conf *conf)
{ {
void *sesskey; struct settings_w *sesskey;
char *errmsg; char *errmsg;
sesskey = open_settings_w(section, &errmsg); sesskey = open_settings_w(section, &errmsg);
@ -513,7 +513,7 @@ char *save_settings(const char *section, Conf *conf)
return NULL; return NULL;
} }
void save_open_settings(void *sesskey, Conf *conf) void save_open_settings(settings_w *sesskey, Conf *conf)
{ {
int i; int i;
const char *p; const char *p;
@ -760,7 +760,7 @@ void save_open_settings(void *sesskey, Conf *conf)
void load_settings(const char *section, Conf *conf) void load_settings(const char *section, Conf *conf)
{ {
void *sesskey; settings_r *sesskey;
sesskey = open_settings_r(section); sesskey = open_settings_r(section);
load_open_settings(sesskey, conf); load_open_settings(sesskey, conf);
@ -770,7 +770,7 @@ void load_settings(const char *section, Conf *conf)
add_session_to_jumplist(section); add_session_to_jumplist(section);
} }
void load_open_settings(void *sesskey, Conf *conf) void load_open_settings(settings_r *sesskey, Conf *conf)
{ {
int i; int i;
char *prot; char *prot;
@ -1242,7 +1242,7 @@ void get_sesslist(struct sesslist *list, int allocate)
char otherbuf[2048]; char otherbuf[2048];
int buflen, bufsize, i; int buflen, bufsize, i;
char *p, *ret; char *p, *ret;
void *handle; settings_e *handle;
if (allocate) { if (allocate) {

View File

@ -28,12 +28,14 @@
* *
* Any returned error message must be freed after use. * Any returned error message must be freed after use.
*/ */
void *open_settings_w(const char *sessionname, char **errmsg); settings_w *open_settings_w(const char *sessionname, char **errmsg);
void write_setting_s(void *handle, const char *key, const char *value); void write_setting_s(settings_w *handle, const char *key, const char *value);
void write_setting_i(void *handle, const char *key, int value); void write_setting_i(settings_w *handle, const char *key, int value);
void write_setting_filename(void *handle, const char *key, Filename *value); void write_setting_filename(settings_w *handle,
void write_setting_fontspec(void *handle, const char *key, FontSpec *font); const char *key, Filename *value);
void close_settings_w(void *handle); void write_setting_fontspec(settings_w *handle,
const char *key, FontSpec *font);
void close_settings_w(settings_w *handle);
/* /*
* Read a saved session. The caller is expected to call * Read a saved session. The caller is expected to call
@ -51,12 +53,12 @@ void close_settings_w(void *handle);
* should invent a sensible default. If an integer setting is not * should invent a sensible default. If an integer setting is not
* present, read_setting_i() returns its provided default. * present, read_setting_i() returns its provided default.
*/ */
void *open_settings_r(const char *sessionname); settings_r *open_settings_r(const char *sessionname);
char *read_setting_s(void *handle, const char *key); char *read_setting_s(settings_r *handle, const char *key);
int read_setting_i(void *handle, const char *key, int defvalue); int read_setting_i(settings_r *handle, const char *key, int defvalue);
Filename *read_setting_filename(void *handle, const char *key); Filename *read_setting_filename(settings_r *handle, const char *key);
FontSpec *read_setting_fontspec(void *handle, const char *key); FontSpec *read_setting_fontspec(settings_r *handle, const char *key);
void close_settings_r(void *handle); void close_settings_r(settings_r *handle);
/* /*
* Delete a whole saved session. * Delete a whole saved session.
@ -66,9 +68,9 @@ void del_settings(const char *sessionname);
/* /*
* Enumerate all saved sessions. * Enumerate all saved sessions.
*/ */
void *enum_settings_start(void); settings_e *enum_settings_start(void);
char *enum_settings_next(void *handle, char *buffer, int buflen); char *enum_settings_next(settings_e *handle, char *buffer, int buflen);
void enum_settings_finish(void *handle); void enum_settings_finish(settings_e *handle);
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
* Functions to access PuTTY's host key database. * Functions to access PuTTY's host key database.

View File

@ -218,7 +218,11 @@ static char *make_filename(int index, const char *subname)
return ret; return ret;
} }
void *open_settings_w(const char *sessionname, char **errmsg) struct settings_w {
FILE *fp;
};
settings_w *open_settings_w(const char *sessionname, char **errmsg)
{ {
char *filename, *err; char *filename, *err;
FILE *fp; FILE *fp;
@ -256,25 +260,26 @@ void *open_settings_w(const char *sessionname, char **errmsg)
return NULL; /* can't open */ return NULL; /* can't open */
} }
sfree(filename); sfree(filename);
return fp;
settings_w *toret = snew(settings_w);
toret->fp = fp;
return toret;
} }
void write_setting_s(void *handle, const char *key, const char *value) void write_setting_s(settings_w *handle, const char *key, const char *value)
{ {
FILE *fp = (FILE *)handle; fprintf(handle->fp, "%s=%s\n", key, value);
fprintf(fp, "%s=%s\n", key, value);
} }
void write_setting_i(void *handle, const char *key, int value) void write_setting_i(settings_w *handle, const char *key, int value)
{ {
FILE *fp = (FILE *)handle; fprintf(handle->fp, "%s=%d\n", key, value);
fprintf(fp, "%s=%d\n", key, value);
} }
void close_settings_w(void *handle) void close_settings_w(settings_w *handle)
{ {
FILE *fp = (FILE *)handle; fclose(handle->fp);
fclose(fp); sfree(handle);
} }
/* /*
@ -347,12 +352,16 @@ const char *get_setting(const char *key)
return x_get_default(key); return x_get_default(key);
} }
void *open_settings_r(const char *sessionname) struct settings_r {
tree234 *t;
};
settings_r *open_settings_r(const char *sessionname)
{ {
char *filename; char *filename;
FILE *fp; FILE *fp;
char *line; char *line;
tree234 *ret; settings_r *toret;
filename = make_filename(INDEX_SESSION, sessionname); filename = make_filename(INDEX_SESSION, sessionname);
fp = fopen(filename, "r"); fp = fopen(filename, "r");
@ -360,7 +369,8 @@ void *open_settings_r(const char *sessionname)
if (!fp) if (!fp)
return NULL; /* can't open */ return NULL; /* can't open */
ret = newtree234(keycmp); toret = snew(settings_r);
toret->t = newtree234(keycmp);
while ( (line = fgetline(fp)) ) { while ( (line = fgetline(fp)) ) {
char *value = strchr(line, '='); char *value = strchr(line, '=');
@ -376,25 +386,24 @@ void *open_settings_r(const char *sessionname)
kv = snew(struct skeyval); kv = snew(struct skeyval);
kv->key = dupstr(line); kv->key = dupstr(line);
kv->value = dupstr(value); kv->value = dupstr(value);
add234(ret, kv); add234(toret->t, kv);
sfree(line); sfree(line);
} }
fclose(fp); fclose(fp);
return ret; return toret;
} }
char *read_setting_s(void *handle, const char *key) char *read_setting_s(settings_r *handle, const char *key)
{ {
tree234 *tree = (tree234 *)handle;
const char *val; const char *val;
struct skeyval tmp, *kv; struct skeyval tmp, *kv;
tmp.key = key; tmp.key = key;
if (tree != NULL && if (handle != NULL &&
(kv = find234(tree, &tmp, NULL)) != NULL) { (kv = find234(handle->t, &tmp, NULL)) != NULL) {
val = kv->value; val = kv->value;
assert(val != NULL); assert(val != NULL);
} else } else
@ -406,15 +415,14 @@ char *read_setting_s(void *handle, const char *key)
return dupstr(val); return dupstr(val);
} }
int read_setting_i(void *handle, const char *key, int defvalue) int read_setting_i(settings_r *handle, const char *key, int defvalue)
{ {
tree234 *tree = (tree234 *)handle;
const char *val; const char *val;
struct skeyval tmp, *kv; struct skeyval tmp, *kv;
tmp.key = key; tmp.key = key;
if (tree != NULL && if (handle != NULL &&
(kv = find234(tree, &tmp, NULL)) != NULL) { (kv = find234(handle->t, &tmp, NULL)) != NULL) {
val = kv->value; val = kv->value;
assert(val != NULL); assert(val != NULL);
} else } else
@ -426,7 +434,7 @@ int read_setting_i(void *handle, const char *key, int defvalue)
return atoi(val); return atoi(val);
} }
FontSpec *read_setting_fontspec(void *handle, const char *name) FontSpec *read_setting_fontspec(settings_r *handle, const char *name)
{ {
/* /*
* In GTK1-only PuTTY, we used to store font names simply as a * In GTK1-only PuTTY, we used to store font names simply as a
@ -464,7 +472,7 @@ FontSpec *read_setting_fontspec(void *handle, const char *name)
return NULL; return NULL;
} }
} }
Filename *read_setting_filename(void *handle, const char *name) Filename *read_setting_filename(settings_r *handle, const char *name)
{ {
char *tmp = read_setting_s(handle, name); char *tmp = read_setting_s(handle, name);
if (tmp) { if (tmp) {
@ -475,7 +483,7 @@ Filename *read_setting_filename(void *handle, const char *name)
return NULL; return NULL;
} }
void write_setting_fontspec(void *handle, const char *name, FontSpec *fs) void write_setting_fontspec(settings_w *handle, const char *name, FontSpec *fs)
{ {
/* /*
* read_setting_fontspec had to handle two cases, but when * read_setting_fontspec had to handle two cases, but when
@ -486,27 +494,28 @@ void write_setting_fontspec(void *handle, const char *name, FontSpec *fs)
write_setting_s(handle, suffname, fs->name); write_setting_s(handle, suffname, fs->name);
sfree(suffname); sfree(suffname);
} }
void write_setting_filename(void *handle, const char *name, Filename *result) void write_setting_filename(settings_w *handle,
const char *name, Filename *result)
{ {
write_setting_s(handle, name, result->path); write_setting_s(handle, name, result->path);
} }
void close_settings_r(void *handle) void close_settings_r(settings_r *handle)
{ {
tree234 *tree = (tree234 *)handle;
struct skeyval *kv; struct skeyval *kv;
if (!tree) if (!handle)
return; return;
while ( (kv = index234(tree, 0)) != NULL) { while ( (kv = index234(handle->t, 0)) != NULL) {
del234(tree, kv); del234(handle->t, kv);
sfree((char *)kv->key); sfree((char *)kv->key);
sfree((char *)kv->value); sfree((char *)kv->value);
sfree(kv); sfree(kv);
} }
freetree234(tree); freetree234(handle->t);
sfree(handle);
} }
void del_settings(const char *sessionname) void del_settings(const char *sessionname)
@ -517,7 +526,11 @@ void del_settings(const char *sessionname)
sfree(filename); sfree(filename);
} }
void *enum_settings_start(void) struct settings_e {
DIR *dp;
};
settings_e *enum_settings_start(void)
{ {
DIR *dp; DIR *dp;
char *filename; char *filename;
@ -526,12 +539,13 @@ void *enum_settings_start(void)
dp = opendir(filename); dp = opendir(filename);
sfree(filename); sfree(filename);
return dp; settings_e *toret = snew(settings_e);
toret->dp = dp;
return toret;
} }
char *enum_settings_next(void *handle, char *buffer, int buflen) char *enum_settings_next(settings_e *handle, char *buffer, int buflen)
{ {
DIR *dp = (DIR *)handle;
struct dirent *de; struct dirent *de;
struct stat st; struct stat st;
char *fullpath; char *fullpath;
@ -541,7 +555,7 @@ char *enum_settings_next(void *handle, char *buffer, int buflen)
fullpath = make_filename(INDEX_SESSIONDIR, NULL); fullpath = make_filename(INDEX_SESSIONDIR, NULL);
maxlen = len = strlen(fullpath); maxlen = len = strlen(fullpath);
while ( (de = readdir(dp)) != NULL ) { while ( (de = readdir(handle->dp)) != NULL ) {
thislen = len + 1 + strlen(de->d_name); thislen = len + 1 + strlen(de->d_name);
if (maxlen < thislen) { if (maxlen < thislen) {
maxlen = thislen; maxlen = thislen;
@ -566,10 +580,10 @@ char *enum_settings_next(void *handle, char *buffer, int buflen)
return NULL; return NULL;
} }
void enum_settings_finish(void *handle) void enum_settings_finish(settings_e *handle)
{ {
DIR *dp = (DIR *)handle; closedir(handle->dp);
closedir(dp); sfree(handle);
} }
/* /*

View File

@ -383,7 +383,6 @@ static IShellLink *make_shell_link(const char *appname,
{ {
IShellLink *ret; IShellLink *ret;
char *app_path, *param_string, *desc_string; char *app_path, *param_string, *desc_string;
void *psettings_tmp;
IPropertyStore *pPS; IPropertyStore *pPS;
PROPVARIANT pv; PROPVARIANT pv;
@ -409,7 +408,7 @@ static IShellLink *make_shell_link(const char *appname,
/* Check if this is a valid session, otherwise don't add. */ /* Check if this is a valid session, otherwise don't add. */
if (sessionname) { if (sessionname) {
psettings_tmp = open_settings_r(sessionname); settings_r *psettings_tmp = open_settings_r(sessionname);
if (!psettings_tmp) { if (!psettings_tmp) {
sfree(app_path); sfree(app_path);
return NULL; return NULL;

View File

@ -74,7 +74,11 @@ static void unmungestr(const char *in, char *out, int outlen)
return; return;
} }
void *open_settings_w(const char *sessionname, char **errmsg) struct settings_w {
HKEY sesskey;
};
settings_w *open_settings_w(const char *sessionname, char **errmsg)
{ {
HKEY subkey1, sesskey; HKEY subkey1, sesskey;
int ret; int ret;
@ -104,29 +108,37 @@ void *open_settings_w(const char *sessionname, char **errmsg)
return NULL; return NULL;
} }
sfree(p); sfree(p);
return (void *) sesskey;
settings_w *toret = snew(settings_w);
toret->sesskey = sesskey;
return toret;
} }
void write_setting_s(void *handle, const char *key, const char *value) void write_setting_s(settings_w *handle, const char *key, const char *value)
{ {
if (handle) if (handle)
RegSetValueEx((HKEY) handle, key, 0, REG_SZ, (CONST BYTE *)value, RegSetValueEx(handle->sesskey, key, 0, REG_SZ, (CONST BYTE *)value,
1 + strlen(value)); 1 + strlen(value));
} }
void write_setting_i(void *handle, const char *key, int value) void write_setting_i(settings_w *handle, const char *key, int value)
{ {
if (handle) if (handle)
RegSetValueEx((HKEY) handle, key, 0, REG_DWORD, RegSetValueEx(handle->sesskey, key, 0, REG_DWORD,
(CONST BYTE *) &value, sizeof(value)); (CONST BYTE *) &value, sizeof(value));
} }
void close_settings_w(void *handle) void close_settings_w(settings_w *handle)
{ {
RegCloseKey((HKEY) handle); RegCloseKey(handle->sesskey);
sfree(handle);
} }
void *open_settings_r(const char *sessionname) struct settings_r {
HKEY sesskey;
};
settings_r *open_settings_r(const char *sessionname)
{ {
HKEY subkey1, sesskey; HKEY subkey1, sesskey;
char *p; char *p;
@ -148,10 +160,12 @@ void *open_settings_r(const char *sessionname)
sfree(p); sfree(p);
return (void *) sesskey; settings_r *toret = snew(settings_r);
toret->sesskey = sesskey;
return toret;
} }
char *read_setting_s(void *handle, const char *key) char *read_setting_s(settings_r *handle, const char *key)
{ {
DWORD type, allocsize, size; DWORD type, allocsize, size;
char *ret; char *ret;
@ -160,14 +174,14 @@ char *read_setting_s(void *handle, const char *key)
return NULL; return NULL;
/* Find out the type and size of the data. */ /* Find out the type and size of the data. */
if (RegQueryValueEx((HKEY) handle, key, 0, if (RegQueryValueEx(handle->sesskey, key, 0,
&type, NULL, &size) != ERROR_SUCCESS || &type, NULL, &size) != ERROR_SUCCESS ||
type != REG_SZ) type != REG_SZ)
return NULL; return NULL;
allocsize = size+1; /* allow for an extra NUL if needed */ allocsize = size+1; /* allow for an extra NUL if needed */
ret = snewn(allocsize, char); ret = snewn(allocsize, char);
if (RegQueryValueEx((HKEY) handle, key, 0, if (RegQueryValueEx(handle->sesskey, key, 0,
&type, (BYTE *)ret, &size) != ERROR_SUCCESS || &type, (BYTE *)ret, &size) != ERROR_SUCCESS ||
type != REG_SZ) { type != REG_SZ) {
sfree(ret); sfree(ret);
@ -180,13 +194,13 @@ char *read_setting_s(void *handle, const char *key)
return ret; return ret;
} }
int read_setting_i(void *handle, const char *key, int defvalue) int read_setting_i(settings_r *handle, const char *key, int defvalue)
{ {
DWORD type, val, size; DWORD type, val, size;
size = sizeof(val); size = sizeof(val);
if (!handle || if (!handle ||
RegQueryValueEx((HKEY) handle, key, 0, &type, RegQueryValueEx(handle->sesskey, key, 0, &type,
(BYTE *) &val, &size) != ERROR_SUCCESS || (BYTE *) &val, &size) != ERROR_SUCCESS ||
size != sizeof(val) || type != REG_DWORD) size != sizeof(val) || type != REG_DWORD)
return defvalue; return defvalue;
@ -194,7 +208,7 @@ int read_setting_i(void *handle, const char *key, int defvalue)
return val; return val;
} }
FontSpec *read_setting_fontspec(void *handle, const char *name) FontSpec *read_setting_fontspec(settings_r *handle, const char *name)
{ {
char *settingname; char *settingname;
char *fontname; char *fontname;
@ -234,7 +248,8 @@ FontSpec *read_setting_fontspec(void *handle, const char *name)
return ret; return ret;
} }
void write_setting_fontspec(void *handle, const char *name, FontSpec *font) void write_setting_fontspec(settings_w *handle,
const char *name, FontSpec *font)
{ {
char *settingname; char *settingname;
@ -250,7 +265,7 @@ void write_setting_fontspec(void *handle, const char *name, FontSpec *font)
sfree(settingname); sfree(settingname);
} }
Filename *read_setting_filename(void *handle, const char *name) Filename *read_setting_filename(settings_r *handle, const char *name)
{ {
char *tmp = read_setting_s(handle, name); char *tmp = read_setting_s(handle, name);
if (tmp) { if (tmp) {
@ -261,14 +276,16 @@ Filename *read_setting_filename(void *handle, const char *name)
return NULL; return NULL;
} }
void write_setting_filename(void *handle, const char *name, Filename *result) void write_setting_filename(settings_w *handle,
const char *name, Filename *result)
{ {
write_setting_s(handle, name, result->path); write_setting_s(handle, name, result->path);
} }
void close_settings_r(void *handle) void close_settings_r(settings_r *handle)
{ {
RegCloseKey((HKEY) handle); RegCloseKey(handle->sesskey);
sfree(handle);
} }
void del_settings(const char *sessionname) void del_settings(const char *sessionname)
@ -289,20 +306,20 @@ void del_settings(const char *sessionname)
remove_session_from_jumplist(sessionname); remove_session_from_jumplist(sessionname);
} }
struct enumsettings { struct settings_e {
HKEY key; HKEY key;
int i; int i;
}; };
void *enum_settings_start(void) settings_e *enum_settings_start(void)
{ {
struct enumsettings *ret; settings_e *ret;
HKEY key; HKEY key;
if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS) if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
return NULL; return NULL;
ret = snew(struct enumsettings); ret = snew(settings_e);
if (ret) { if (ret) {
ret->key = key; ret->key = key;
ret->i = 0; ret->i = 0;
@ -311,9 +328,8 @@ void *enum_settings_start(void)
return ret; return ret;
} }
char *enum_settings_next(void *handle, char *buffer, int buflen) char *enum_settings_next(settings_e *e, char *buffer, int buflen)
{ {
struct enumsettings *e = (struct enumsettings *) handle;
char *otherbuf; char *otherbuf;
otherbuf = snewn(3 * buflen, char); otherbuf = snewn(3 * buflen, char);
if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) { if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) {
@ -326,9 +342,8 @@ char *enum_settings_next(void *handle, char *buffer, int buflen)
} }
} }
void enum_settings_finish(void *handle) void enum_settings_finish(settings_e *e)
{ {
struct enumsettings *e = (struct enumsettings *) handle;
RegCloseKey(e->key); RegCloseKey(e->key);
sfree(e); sfree(e);
} }
@ -656,7 +671,7 @@ static int transform_jumplist_registry
(const char *add, const char *rem, char **out) (const char *add, const char *rem, char **out)
{ {
int ret; int ret;
HKEY pjumplist_key, psettings_tmp; HKEY pjumplist_key;
DWORD type; DWORD type;
DWORD value_length; DWORD value_length;
char *old_value, *new_value; char *old_value, *new_value;
@ -741,7 +756,7 @@ static int transform_jumplist_registry
while (*piterator_old != '\0') { while (*piterator_old != '\0') {
if (!rem || strcmp(piterator_old, rem) != 0) { if (!rem || strcmp(piterator_old, rem) != 0) {
/* Check if this is a valid session, otherwise don't add. */ /* Check if this is a valid session, otherwise don't add. */
psettings_tmp = open_settings_r(piterator_old); settings_r *psettings_tmp = open_settings_r(piterator_old);
if (psettings_tmp != NULL) { if (psettings_tmp != NULL) {
close_settings_r(psettings_tmp); close_settings_r(psettings_tmp);
strcpy(piterator_new, piterator_old); strcpy(piterator_new, piterator_old);