diff --git a/defs.h b/defs.h index 1bc8f9b8..ba61708b 100644 --- a/defs.h +++ b/defs.h @@ -61,6 +61,10 @@ typedef struct share_channel share_channel; 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 * the OS socket type), the type that most code uses for a socket is * 'Socket', not 'Socket *'. So an implementation of Socket or Plug diff --git a/putty.h b/putty.h index 7b1f90e4..dd43e8c4 100644 --- a/putty.h +++ b/putty.h @@ -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); char *get_remote_username(Conf *conf); /* dynamically allocated */ 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_open_settings(void *sesskey, Conf *conf); +void load_open_settings(settings_r *sesskey, Conf *conf); void get_sesslist(struct sesslist *, int allocate); void do_defaults(const char *, Conf *); void registry_cleanup(void); diff --git a/settings.c b/settings.c index ecdb3a4b..ab735488 100644 --- a/settings.c +++ b/settings.c @@ -502,7 +502,7 @@ static void read_clip_setting(void *handle, const char *savekey, char *save_settings(const char *section, Conf *conf) { - void *sesskey; + struct settings_w *sesskey; char *errmsg; sesskey = open_settings_w(section, &errmsg); @@ -513,7 +513,7 @@ char *save_settings(const char *section, Conf *conf) return NULL; } -void save_open_settings(void *sesskey, Conf *conf) +void save_open_settings(settings_w *sesskey, Conf *conf) { int i; const char *p; @@ -760,7 +760,7 @@ void save_open_settings(void *sesskey, Conf *conf) void load_settings(const char *section, Conf *conf) { - void *sesskey; + settings_r *sesskey; sesskey = open_settings_r(section); load_open_settings(sesskey, conf); @@ -770,7 +770,7 @@ void load_settings(const char *section, Conf *conf) add_session_to_jumplist(section); } -void load_open_settings(void *sesskey, Conf *conf) +void load_open_settings(settings_r *sesskey, Conf *conf) { int i; char *prot; @@ -1242,7 +1242,7 @@ void get_sesslist(struct sesslist *list, int allocate) char otherbuf[2048]; int buflen, bufsize, i; char *p, *ret; - void *handle; + settings_e *handle; if (allocate) { diff --git a/storage.h b/storage.h index 8e07ef0c..6186e91d 100644 --- a/storage.h +++ b/storage.h @@ -28,12 +28,14 @@ * * Any returned error message must be freed after use. */ -void *open_settings_w(const char *sessionname, char **errmsg); -void write_setting_s(void *handle, const char *key, const char *value); -void write_setting_i(void *handle, const char *key, int value); -void write_setting_filename(void *handle, const char *key, Filename *value); -void write_setting_fontspec(void *handle, const char *key, FontSpec *font); -void close_settings_w(void *handle); +settings_w *open_settings_w(const char *sessionname, char **errmsg); +void write_setting_s(settings_w *handle, const char *key, const char *value); +void write_setting_i(settings_w *handle, const char *key, int value); +void write_setting_filename(settings_w *handle, + const char *key, Filename *value); +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 @@ -51,12 +53,12 @@ void close_settings_w(void *handle); * should invent a sensible default. If an integer setting is not * present, read_setting_i() returns its provided default. */ -void *open_settings_r(const char *sessionname); -char *read_setting_s(void *handle, const char *key); -int read_setting_i(void *handle, const char *key, int defvalue); -Filename *read_setting_filename(void *handle, const char *key); -FontSpec *read_setting_fontspec(void *handle, const char *key); -void close_settings_r(void *handle); +settings_r *open_settings_r(const char *sessionname); +char *read_setting_s(settings_r *handle, const char *key); +int read_setting_i(settings_r *handle, const char *key, int defvalue); +Filename *read_setting_filename(settings_r *handle, const char *key); +FontSpec *read_setting_fontspec(settings_r *handle, const char *key); +void close_settings_r(settings_r *handle); /* * Delete a whole saved session. @@ -66,9 +68,9 @@ void del_settings(const char *sessionname); /* * Enumerate all saved sessions. */ -void *enum_settings_start(void); -char *enum_settings_next(void *handle, char *buffer, int buflen); -void enum_settings_finish(void *handle); +settings_e *enum_settings_start(void); +char *enum_settings_next(settings_e *handle, char *buffer, int buflen); +void enum_settings_finish(settings_e *handle); /* ---------------------------------------------------------------------- * Functions to access PuTTY's host key database. diff --git a/unix/uxstore.c b/unix/uxstore.c index 15801785..54a20b8a 100644 --- a/unix/uxstore.c +++ b/unix/uxstore.c @@ -218,7 +218,11 @@ static char *make_filename(int index, const char *subname) 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; FILE *fp; @@ -256,25 +260,26 @@ void *open_settings_w(const char *sessionname, char **errmsg) return NULL; /* can't open */ } 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(fp, "%s=%s\n", key, value); + fprintf(handle->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(fp, "%s=%d\n", key, value); + fprintf(handle->fp, "%s=%d\n", key, value); } -void close_settings_w(void *handle) +void close_settings_w(settings_w *handle) { - FILE *fp = (FILE *)handle; - fclose(fp); + fclose(handle->fp); + sfree(handle); } /* @@ -347,12 +352,16 @@ const char *get_setting(const char *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; FILE *fp; char *line; - tree234 *ret; + settings_r *toret; filename = make_filename(INDEX_SESSION, sessionname); fp = fopen(filename, "r"); @@ -360,7 +369,8 @@ void *open_settings_r(const char *sessionname) if (!fp) return NULL; /* can't open */ - ret = newtree234(keycmp); + toret = snew(settings_r); + toret->t = newtree234(keycmp); while ( (line = fgetline(fp)) ) { char *value = strchr(line, '='); @@ -376,25 +386,24 @@ void *open_settings_r(const char *sessionname) kv = snew(struct skeyval); kv->key = dupstr(line); kv->value = dupstr(value); - add234(ret, kv); + add234(toret->t, kv); sfree(line); } 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; struct skeyval tmp, *kv; tmp.key = key; - if (tree != NULL && - (kv = find234(tree, &tmp, NULL)) != NULL) { + if (handle != NULL && + (kv = find234(handle->t, &tmp, NULL)) != NULL) { val = kv->value; assert(val != NULL); } else @@ -406,15 +415,14 @@ char *read_setting_s(void *handle, const char *key) 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; struct skeyval tmp, *kv; tmp.key = key; - if (tree != NULL && - (kv = find234(tree, &tmp, NULL)) != NULL) { + if (handle != NULL && + (kv = find234(handle->t, &tmp, NULL)) != NULL) { val = kv->value; assert(val != NULL); } else @@ -426,7 +434,7 @@ int read_setting_i(void *handle, const char *key, int defvalue) 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 @@ -464,7 +472,7 @@ FontSpec *read_setting_fontspec(void *handle, const char *name) 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); if (tmp) { @@ -475,7 +483,7 @@ Filename *read_setting_filename(void *handle, const char *name) 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 @@ -486,27 +494,28 @@ void write_setting_fontspec(void *handle, const char *name, FontSpec *fs) write_setting_s(handle, suffname, fs->name); 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); } -void close_settings_r(void *handle) +void close_settings_r(settings_r *handle) { - tree234 *tree = (tree234 *)handle; struct skeyval *kv; - if (!tree) + if (!handle) return; - while ( (kv = index234(tree, 0)) != NULL) { - del234(tree, kv); + while ( (kv = index234(handle->t, 0)) != NULL) { + del234(handle->t, kv); sfree((char *)kv->key); sfree((char *)kv->value); sfree(kv); } - freetree234(tree); + freetree234(handle->t); + sfree(handle); } void del_settings(const char *sessionname) @@ -517,7 +526,11 @@ void del_settings(const char *sessionname) sfree(filename); } -void *enum_settings_start(void) +struct settings_e { + DIR *dp; +}; + +settings_e *enum_settings_start(void) { DIR *dp; char *filename; @@ -526,12 +539,13 @@ void *enum_settings_start(void) dp = opendir(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 stat st; char *fullpath; @@ -541,7 +555,7 @@ char *enum_settings_next(void *handle, char *buffer, int buflen) fullpath = make_filename(INDEX_SESSIONDIR, NULL); maxlen = len = strlen(fullpath); - while ( (de = readdir(dp)) != NULL ) { + while ( (de = readdir(handle->dp)) != NULL ) { thislen = len + 1 + strlen(de->d_name); if (maxlen < thislen) { maxlen = thislen; @@ -566,10 +580,10 @@ char *enum_settings_next(void *handle, char *buffer, int buflen) return NULL; } -void enum_settings_finish(void *handle) +void enum_settings_finish(settings_e *handle) { - DIR *dp = (DIR *)handle; - closedir(dp); + closedir(handle->dp); + sfree(handle); } /* diff --git a/windows/winjump.c b/windows/winjump.c index 7e7b34e8..e42ba275 100644 --- a/windows/winjump.c +++ b/windows/winjump.c @@ -383,7 +383,6 @@ static IShellLink *make_shell_link(const char *appname, { IShellLink *ret; char *app_path, *param_string, *desc_string; - void *psettings_tmp; IPropertyStore *pPS; 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. */ if (sessionname) { - psettings_tmp = open_settings_r(sessionname); + settings_r *psettings_tmp = open_settings_r(sessionname); if (!psettings_tmp) { sfree(app_path); return NULL; diff --git a/windows/winstore.c b/windows/winstore.c index 26cbf634..7c4be074 100644 --- a/windows/winstore.c +++ b/windows/winstore.c @@ -74,7 +74,11 @@ static void unmungestr(const char *in, char *out, int outlen) 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; int ret; @@ -104,29 +108,37 @@ void *open_settings_w(const char *sessionname, char **errmsg) return NULL; } 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) - RegSetValueEx((HKEY) handle, key, 0, REG_SZ, (CONST BYTE *)value, + RegSetValueEx(handle->sesskey, key, 0, REG_SZ, (CONST BYTE *)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) - RegSetValueEx((HKEY) handle, key, 0, REG_DWORD, + RegSetValueEx(handle->sesskey, key, 0, REG_DWORD, (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; char *p; @@ -148,10 +160,12 @@ void *open_settings_r(const char *sessionname) 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; char *ret; @@ -160,14 +174,14 @@ char *read_setting_s(void *handle, const char *key) return NULL; /* 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 != REG_SZ) return NULL; allocsize = size+1; /* allow for an extra NUL if needed */ ret = snewn(allocsize, char); - if (RegQueryValueEx((HKEY) handle, key, 0, + if (RegQueryValueEx(handle->sesskey, key, 0, &type, (BYTE *)ret, &size) != ERROR_SUCCESS || type != REG_SZ) { sfree(ret); @@ -180,13 +194,13 @@ char *read_setting_s(void *handle, const char *key) 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; size = sizeof(val); if (!handle || - RegQueryValueEx((HKEY) handle, key, 0, &type, + RegQueryValueEx(handle->sesskey, key, 0, &type, (BYTE *) &val, &size) != ERROR_SUCCESS || size != sizeof(val) || type != REG_DWORD) return defvalue; @@ -194,7 +208,7 @@ int read_setting_i(void *handle, const char *key, int defvalue) return val; } -FontSpec *read_setting_fontspec(void *handle, const char *name) +FontSpec *read_setting_fontspec(settings_r *handle, const char *name) { char *settingname; char *fontname; @@ -234,7 +248,8 @@ FontSpec *read_setting_fontspec(void *handle, const char *name) 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; @@ -250,7 +265,7 @@ void write_setting_fontspec(void *handle, const char *name, FontSpec *font) 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); if (tmp) { @@ -261,14 +276,16 @@ Filename *read_setting_filename(void *handle, const char *name) 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); } -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) @@ -289,20 +306,20 @@ void del_settings(const char *sessionname) remove_session_from_jumplist(sessionname); } -struct enumsettings { +struct settings_e { HKEY key; int i; }; -void *enum_settings_start(void) +settings_e *enum_settings_start(void) { - struct enumsettings *ret; + settings_e *ret; HKEY key; if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS) return NULL; - ret = snew(struct enumsettings); + ret = snew(settings_e); if (ret) { ret->key = key; ret->i = 0; @@ -311,9 +328,8 @@ void *enum_settings_start(void) 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; otherbuf = snewn(3 * buflen, char); 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); sfree(e); } @@ -656,7 +671,7 @@ static int transform_jumplist_registry (const char *add, const char *rem, char **out) { int ret; - HKEY pjumplist_key, psettings_tmp; + HKEY pjumplist_key; DWORD type; DWORD value_length; char *old_value, *new_value; @@ -741,7 +756,7 @@ static int transform_jumplist_registry while (*piterator_old != '\0') { if (!rem || strcmp(piterator_old, rem) != 0) { /* 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) { close_settings_r(psettings_tmp); strcpy(piterator_new, piterator_old);