From 86cca22cce37a30b4e36775a4dd7b46bad62260f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Sun, 22 Sep 2024 19:03:13 -0400
Subject: [PATCH] Fix up handling of -b option

First, we need to parse it before load_settings() so we can deal
with it there.

Next, we need to keep the one from the config file available so it
is edited from the program settings, and not the command line version
---
 src/syncterm/bbslist.c  |  8 +++--
 src/syncterm/syncterm.c | 65 ++++++++++++++++++++++++++++++++++-------
 src/syncterm/syncterm.h |  2 ++
 3 files changed, 62 insertions(+), 13 deletions(-)

diff --git a/src/syncterm/bbslist.c b/src/syncterm/bbslist.c
index cc1995c71c..c0abc58744 100644
--- a/src/syncterm/bbslist.c
+++ b/src/syncterm/bbslist.c
@@ -1962,7 +1962,7 @@ change_settings(int connected)
 		SAFEPRINTF(opts[6], "Modem/Comm Rate         %s", str);
 		SAFEPRINTF(opts[7], "Modem Init String       %s", settings.mdm.init_string);
 		SAFEPRINTF(opts[8], "Modem Dial String       %s", settings.mdm.dial_string);
-		SAFEPRINTF(opts[9], "List Path               %s", settings.list_path);
+		SAFEPRINTF(opts[9], "List Path               %s", settings.stored_list_path);
 		SAFEPRINTF(opts[10], "TERM For Shell          %s", settings.TERM);
 		sprintf(opts[11],   "Scaling                 %s", scaling_names[settings_to_scale()]);
 		if (connected)
@@ -2211,13 +2211,15 @@ change_settings(int connected)
 			case 9:
 				uifc.helpbuf = "`List Path`\n\n"
 				    "The complete path to the BBS list goes here.\n";
-				if (uifc.input(WIN_MID | WIN_SAV, 0, 0, "List Path", settings.list_path, MAX_PATH,
+				if (uifc.input(WIN_MID | WIN_SAV, 0, 0, "List Path", settings.stored_list_path, MAX_PATH,
 				    K_EDIT) >= 0) {
 					iniSetString(&inicontents,
 					    "SyncTERM",
 					    "ListPath",
-					    settings.list_path,
+					    settings.stored_list_path,
 					    &ini_style);
+					if (list_override == NULL)
+						SAFECOPY(settings.list_path, settings.stored_list_path);
 				}
 				else {
 					check_exit(false);
diff --git a/src/syncterm/syncterm.c b/src/syncterm/syncterm.c
index 0a1553b1c7..439c5f75d8 100644
--- a/src/syncterm/syncterm.c
+++ b/src/syncterm/syncterm.c
@@ -1257,8 +1257,6 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared)
 		sprintf(fn, "%.*s", fnlen - 1, config_override);
 		return fn;
 	}
-if ((type == SYNCTERM_PATH_LIST) && !shared)
-fprintf(stderr, "List override = %p, type = %d (%d), shared: %d\n", list_override, type, SYNCTERM_PATH_LIST, shared);
 	if ((list_override != NULL) && (type == SYNCTERM_PATH_LIST) && !shared) {
 		sprintf(fn, "%.*s", fnlen - 1, list_override);
 		return fn;
@@ -1338,8 +1336,14 @@ load_settings(struct syncterm_settings *set)
 	set->custom_fontheight = iniReadInteger(inifile, "SyncTERM", "CustomFontHeight", 16);
 	set->custom_aw = iniReadInteger(inifile, "SyncTERM", "CustomAspectWidth", 4);
 	set->custom_ah = iniReadInteger(inifile, "SyncTERM", "CustomAspectHeight", 3);
-	get_syncterm_filename(set->list_path, sizeof(set->list_path), SYNCTERM_PATH_LIST, false);
-	iniReadSString(inifile, "SyncTERM", "ListPath", set->list_path, set->list_path, sizeof(set->list_path));
+	get_syncterm_filename(set->stored_list_path, sizeof(set->stored_list_path), SYNCTERM_PATH_LIST, false);
+	iniReadSString(inifile, "SyncTERM", "ListPath", set->stored_list_path, set->stored_list_path, sizeof(set->stored_list_path));
+	if (list_override != NULL) {
+		SAFECOPY(set->list_path, list_override);
+	}
+	else {
+		SAFECOPY(set->list_path, set->stored_list_path);
+	}
 	set->scaling_factor = iniReadFloat(inifile, "SyncTERM", "ScalingFactor", 0);
 	set->blocky = iniReadBool(inifile, "SyncTERM", "BlockyScaling", true);
 	set->extern_scale = iniReadBool(inifile, "SyncTERM", "ExternalScaling", false);
@@ -1564,6 +1568,41 @@ main(int argc, char **argv)
 	uifc.esc_delay = 25;
 	url[0] = 0;
 
+	/*
+	 * We need to parse -n and -b before we call load_settings()
+	 * so that get_syncterm_filename works properly
+	 */
+	for (i = 1; i < argc; i++) {
+		if (argv[i][0] == '-'
+#ifndef __unix__
+		    || argv[i][0] == '/'
+#endif
+		    ) {
+			switch (toupper(argv[i][1])) {
+				case 'B':
+					if (argv[i][2] == 0) {
+						if ((i + 1) < argc)
+							list_override = argv[++i];
+						// Error handling below
+					}
+					else {
+						list_override = &argv[i][2];
+					}
+					break;
+				case 'N':
+					if (argv[i][2] == 0) {
+						if ((i + 1) < argc)
+							config_override = argv[++i];
+						// Error handling below
+					}
+					else {
+						config_override = &argv[i][2];
+					}
+					break;
+			}
+		}
+	}
+
 	load_settings(&settings);
 	cvmode = find_vmode(CIOLIB_MODE_CUSTOM);
 	vparams[cvmode].cols = settings.custom_cols;
@@ -1598,24 +1637,30 @@ main(int argc, char **argv)
 					break;
 				case 'B':
 					if (argv[i][2] == 0) {
-						if ((i + 1) < argc)
-							list_override = argv[++i];
+						if ((i + 1) < argc) {
+							// This has already been parsed above...
+							//list_override = argv[++i];
+						}
 						else
 							goto USAGE;
 					}
 					else {
-						list_override = &argv[i][2];
+						// This has already been parsed above...
+						//list_override = &argv[i][2];
 					}
 					break;
 				case 'N':
 					if (argv[i][2] == 0) {
-						if ((i + 1) < argc)
-							config_override = argv[++i];
+						if ((i + 1) < argc) {
+							// This has already been parsed above...
+							//config_override = argv[++i];
+						}
 						else
 							goto USAGE;
 					}
 					else {
-						config_override = &argv[i][2];
+						// This has already been parsed above...
+						//config_override = &argv[i][2];
 					}
 					break;
 				case 'C':
diff --git a/src/syncterm/syncterm.h b/src/syncterm/syncterm.h
index 30f401fcef..5ca4a75652 100644
--- a/src/syncterm/syncterm.h
+++ b/src/syncterm/syncterm.h
@@ -64,6 +64,7 @@ struct syncterm_settings {
 	struct modem_settings mdm;
 	char                  TERM[INI_MAX_VALUE_LEN + 1];
 	char                  list_path[MAX_PATH + 1];
+	char                  stored_list_path[MAX_PATH + 1];
 	double                scaling_factor;
 	int                   xfer_failure_keypress_timeout; /* wait for user acknowledgement via keypress, in seconds
                                                               */
@@ -82,6 +83,7 @@ struct syncterm_settings {
 };
 
 extern char                    *inpath;
+extern char                    *list_override;
 extern const char              *syncterm_version;
 extern struct vmem_cell        *scrollback_buf;
 extern uint32_t                *scrollback_fbuf;
-- 
GitLab