From 0b4f0bc0bdc90f7c0d63c94d18059612679f3fc4 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 30 Oct 2002 18:12:46 +0000 Subject: [PATCH] Further deglobalisation: settings.c now has a more sensible interface. git-svn-id: svn://svn.tartarus.org/sgt/putty@2162 cda61777-01e9-0310-a592-d414129be87e --- putty.h | 26 ++++++++++++++++++-------- settings.c | 37 ++++++++++++++++++------------------- windlg.c | 36 +++++++++++++++++++----------------- window.c | 18 +++++++++++++----- winstuff.h | 2 +- 5 files changed, 69 insertions(+), 50 deletions(-) diff --git a/putty.h b/putty.h index 3d971aee..85e058ae 100644 --- a/putty.h +++ b/putty.h @@ -92,15 +92,14 @@ typedef struct terminal_tag Terminal; #define ATTR_CUR_AND (~(ATTR_BOLD|ATTR_REVERSE|ATTR_BLINK|ATTR_COLOURS)) #define ATTR_CUR_XOR 0x00BA0000UL -GLOBAL int session_closed; - -GLOBAL int nsessions; -GLOBAL char **sessions; +struct sesslist { + int nsessions; + char **sessions; + char *buffer; /* so memory can be freed later */ +}; -GLOBAL int utf; GLOBAL int dbcs_screenfont; GLOBAL int font_codepage; -GLOBAL int kbd_codepage; GLOBAL int line_codepage; GLOBAL wchar_t unitab_scoacs[256]; GLOBAL wchar_t unitab_line[256]; @@ -375,16 +374,27 @@ struct config_tag { * being run, _either_ because no remote command has been provided * _or_ because the application is GUI and can't run non- * interactively. + * + * These flags describe the type of _application_ - they wouldn't + * vary between individual sessions - and so it's OK to have this + * variable be GLOBAL. */ #define FLAG_VERBOSE 0x0001 #define FLAG_STDERR 0x0002 #define FLAG_INTERACTIVE 0x0004 GLOBAL int flags; -GLOBAL Config cfg; +/* + * Likewise, these two variables are set up when the application + * initialises, and inform all default-settings accesses after + * that. + */ GLOBAL int default_protocol; GLOBAL int default_port; +/* This variable, OTOH, needs to be made non-global ASAP. FIXME. */ +GLOBAL Config cfg; + struct RSAKey; /* be a little careful of scope */ /* @@ -445,7 +455,7 @@ void random_destroy_seed(void); */ void save_settings(char *section, int do_host, Config * cfg); void load_settings(char *section, int do_host, Config * cfg); -void get_sesslist(int allocate); +void get_sesslist(struct sesslist *, int allocate); void do_defaults(char *, Config *); void registry_cleanup(void); diff --git a/settings.c b/settings.c index 736d6775..bce2a62e 100644 --- a/settings.c +++ b/settings.c @@ -659,10 +659,9 @@ static int sessioncmp(const void *av, const void *bv) return strcmp(a, b); /* otherwise, compare normally */ } -void get_sesslist(int allocate) +void get_sesslist(struct sesslist *list, int allocate) { - static char otherbuf[2048]; - static char *buffer; + char otherbuf[2048]; int buflen, bufsize, i; char *p, *ret; void *handle; @@ -670,7 +669,7 @@ void get_sesslist(int allocate) if (allocate) { buflen = bufsize = 0; - buffer = NULL; + list->buffer = NULL; if ((handle = enum_settings_start())) { do { ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf)); @@ -678,16 +677,16 @@ void get_sesslist(int allocate) int len = strlen(otherbuf) + 1; if (bufsize < buflen + len) { bufsize = buflen + len + 2048; - buffer = srealloc(buffer, bufsize); + list->buffer = srealloc(list->buffer, bufsize); } - strcpy(buffer + buflen, otherbuf); - buflen += strlen(buffer + buflen) + 1; + strcpy(list->buffer + buflen, otherbuf); + buflen += strlen(list->buffer + buflen) + 1; } } while (ret); enum_settings_finish(handle); } - buffer = srealloc(buffer, buflen + 1); - buffer[buflen] = '\0'; + list->buffer = srealloc(list->buffer, buflen + 1); + list->buffer[buflen] = '\0'; /* * Now set up the list of sessions. Note that "Default @@ -695,31 +694,31 @@ void get_sesslist(int allocate) * doesn't really. */ - p = buffer; - nsessions = 1; /* "Default Settings" counts as one */ + p = list->buffer; + list->nsessions = 1; /* "Default Settings" counts as one */ while (*p) { if (strcmp(p, "Default Settings")) - nsessions++; + list->nsessions++; while (*p) p++; p++; } - sessions = smalloc((nsessions + 1) * sizeof(char *)); - sessions[0] = "Default Settings"; - p = buffer; + list->sessions = smalloc((list->nsessions + 1) * sizeof(char *)); + list->sessions[0] = "Default Settings"; + p = list->buffer; i = 1; while (*p) { if (strcmp(p, "Default Settings")) - sessions[i++] = p; + list->sessions[i++] = p; while (*p) p++; p++; } - qsort(sessions, i, sizeof(char *), sessioncmp); + qsort(list->sessions, i, sizeof(char *), sessioncmp); } else { - sfree(buffer); - sfree(sessions); + sfree(list->buffer); + sfree(list->sessions); } } diff --git a/windlg.c b/windlg.c index 5040a241..81194648 100644 --- a/windlg.c +++ b/windlg.c @@ -27,6 +27,8 @@ static int requested_help; static struct prefslist cipherlist; +struct sesslist sesslist; /* exported to window.c */ + #define PRINTER_DISABLED_STRING "None (printing disabled)" void force_normal(HWND hwnd) @@ -1069,9 +1071,9 @@ static void init_dlg_ctrls(HWND hwnd, int keepsess) n = SendDlgItemMessage(hwnd, IDC_SESSLIST, LB_GETCOUNT, 0, 0); for (i = n; i-- > 0;) SendDlgItemMessage(hwnd, IDC_SESSLIST, LB_DELETESTRING, i, 0); - for (i = 0; i < nsessions; i++) + for (i = 0; i < sesslist.nsessions; i++) SendDlgItemMessage(hwnd, IDC_SESSLIST, LB_ADDSTRING, - 0, (LPARAM) (sessions[i])); + 0, (LPARAM) (sesslist.sessions[i])); } SetDlgItemInt(hwnd, IDC_PORT, cfg.port, FALSE); CheckRadioButton(hwnd, IDC_PROTRAW, IDC_PROTSSH, @@ -2059,11 +2061,11 @@ static int load_selected_session(HWND hwnd) MessageBeep(0); return 0; } - isdef = !strcmp(sessions[n], "Default Settings"); - load_settings(sessions[n], !isdef, &cfg); + isdef = !strcmp(sesslist.sessions[n], "Default Settings"); + load_settings(sesslist.sessions[n], !isdef, &cfg); init_dlg_ctrls(hwnd, TRUE); if (!isdef) - SetDlgItemText(hwnd, IDC_SESSEDIT, sessions[n]); + SetDlgItemText(hwnd, IDC_SESSEDIT, sesslist.sessions[n]); else SetDlgItemText(hwnd, IDC_SESSEDIT, ""); /* Restore the selection, which will have been clobbered by @@ -2413,20 +2415,20 @@ static int GenericMainDlgProc(HWND hwnd, UINT msg, MessageBeep(0); break; } - strcpy(str, sessions[n]); + strcpy(str, sesslist.sessions[n]); } save_settings(str, !!strcmp(str, "Default Settings"), &cfg); - get_sesslist(FALSE); - get_sesslist(TRUE); + get_sesslist(&sesslist, FALSE); + get_sesslist(&sesslist, TRUE); SendDlgItemMessage(hwnd, IDC_SESSLIST, WM_SETREDRAW, FALSE, 0); SendDlgItemMessage(hwnd, IDC_SESSLIST, LB_RESETCONTENT, 0, 0); - for (i = 0; i < nsessions; i++) + for (i = 0; i < sesslist.nsessions; i++) SendDlgItemMessage(hwnd, IDC_SESSLIST, LB_ADDSTRING, 0, - (LPARAM) (sessions[i])); + (LPARAM) (sesslist.sessions[i])); SendDlgItemMessage(hwnd, IDC_SESSLIST, LB_SETCURSEL, (WPARAM) - 1, 0); SendDlgItemMessage(hwnd, IDC_SESSLIST, WM_SETREDRAW, @@ -2472,17 +2474,17 @@ static int GenericMainDlgProc(HWND hwnd, UINT msg, MessageBeep(0); break; } - del_settings(sessions[n]); - get_sesslist(FALSE); - get_sesslist(TRUE); + del_settings(sesslist.sessions[n]); + get_sesslist(&sesslist, FALSE); + get_sesslist(&sesslist, TRUE); SendDlgItemMessage(hwnd, IDC_SESSLIST, WM_SETREDRAW, FALSE, 0); SendDlgItemMessage(hwnd, IDC_SESSLIST, LB_RESETCONTENT, 0, 0); - for (i = 0; i < nsessions; i++) + for (i = 0; i < sesslist.nsessions; i++) SendDlgItemMessage(hwnd, IDC_SESSLIST, LB_ADDSTRING, 0, - (LPARAM) (sessions[i])); + (LPARAM) (sesslist.sessions[i])); SendDlgItemMessage(hwnd, IDC_SESSLIST, LB_SETCURSEL, (WPARAM) - 1, 0); SendDlgItemMessage(hwnd, IDC_SESSLIST, WM_SETREDRAW, @@ -3726,11 +3728,11 @@ int do_config(void) { int ret; - get_sesslist(TRUE); + get_sesslist(&sesslist, TRUE); savedsession[0] = '\0'; ret = DialogBox(hinst, MAKEINTRESOURCE(IDD_MAINBOX), NULL, MainDlgProc); - get_sesslist(FALSE); + get_sesslist(&sesslist, FALSE); return ret; } diff --git a/window.c b/window.c index e97fc942..78cb1e09 100644 --- a/window.c +++ b/window.c @@ -115,10 +115,16 @@ static time_t last_movement = 0; static int caret_x = -1, caret_y = -1; +static int kbd_codepage; + static void *ldisc; static Backend *back; static void *backhandle; +static int session_closed; + +extern struct sesslist sesslist; /* imported from windlg.c */ + #define FONT_NORMAL 0 #define FONT_BOLD 1 #define FONT_UNDERLINE 2 @@ -683,10 +689,12 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) AppendMenu(m, MF_ENABLED, IDM_NEWSESS, "Ne&w Session..."); AppendMenu(m, MF_ENABLED, IDM_DUPSESS, "&Duplicate Session"); s = CreateMenu(); - get_sesslist(TRUE); - for (i = 1; i < ((nsessions < 256) ? nsessions : 256); i++) + get_sesslist(&sesslist, TRUE); + for (i = 1; + i < ((sesslist.nsessions < 256) ? sesslist.nsessions : 256); + i++) AppendMenu(s, MF_ENABLED, IDM_SAVED_MIN + (16 * i), - sessions[i]); + sesslist.sessions[i]); AppendMenu(m, MF_POPUP | MF_ENABLED, (UINT) s, "Sa&ved Sessions"); AppendMenu(m, MF_ENABLED, IDM_RECONF, "Chan&ge Settings..."); AppendMenu(m, MF_SEPARATOR, 0, 0); @@ -1692,9 +1700,9 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, sprintf(c, "putty &%p", filemap); cl = c; } else if (wParam == IDM_SAVEDSESS) { - if ((lParam - IDM_SAVED_MIN) / 16 < nsessions) { + if ((lParam - IDM_SAVED_MIN) / 16 < sesslist.nsessions) { char *session = - sessions[(lParam - IDM_SAVED_MIN) / 16]; + sesslist.sessions[(lParam - IDM_SAVED_MIN) / 16]; cl = smalloc(16 + strlen(session)); /* 8, but play safe */ if (!cl) diff --git a/winstuff.h b/winstuff.h index 90c03481..9e626645 100644 --- a/winstuff.h +++ b/winstuff.h @@ -59,7 +59,7 @@ GLOBAL int help_has_contents; /* * The terminal and logging context are notionally local to the * Windows front end, but they must be shared between window.c and - * windlg.c. + * windlg.c. Likewise the saved-sessions list. */ GLOBAL Terminal *term; GLOBAL void *logctx; -- 2.11.0