Skip to content

Commit

Permalink
ini: fix default config not loaded from -ini option.
Browse files Browse the repository at this point in the history
  • Loading branch information
ranvis committed Jan 28, 2025
1 parent c4561a9 commit cc1018e
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 35 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ installed_program(psftp)
add_executable(psocks
${platform}/psocks.c
psocks.c
stubs/no-ini.c
stubs/no-rand.c
proxy/nocproxy.c
proxy/nosshproxy.c
Expand Down
6 changes: 3 additions & 3 deletions pscp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2282,12 +2282,12 @@ int psftp_main(CmdlineArgList *arglist)

/* Load Default Settings before doing anything else. */
conf = conf_new();
#ifdef _WINDOWS
process_ini_option(cmdline_error);
#endif
do_defaults(NULL, conf);

size_t arglistpos = 0;
#ifdef _WINDOWS
cmdline_arg_process_ini_option(arglist, &arglistpos, cmdline_error);
#endif
while (arglist->args[arglistpos]) {
CmdlineArg *arg = arglist->args[arglistpos++];
CmdlineArg *nextarg = arglist->args[arglistpos];
Expand Down
6 changes: 3 additions & 3 deletions psftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2813,12 +2813,12 @@ int psftp_main(CmdlineArgList *arglist)

/* Load Default Settings before doing anything else. */
conf = conf_new();
#ifdef _WINDOWS
process_ini_option(cmdline_error);
#endif
do_defaults(NULL, conf);

size_t arglistpos = 0;
#ifdef _WINDOWS
cmdline_arg_process_ini_option(arglist, &arglistpos, cmdline_error);
#endif
while (arglist->args[arglistpos]) {
CmdlineArg *arg = arglist->args[arglistpos++];
CmdlineArg *nextarg = arglist->args[arglistpos];
Expand Down
6 changes: 6 additions & 0 deletions stubs/no-ini.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "putty.h"

size_t skip_ini_option(size_t index, size_t argc, wchar_t **argv)
{
return index;
}
44 changes: 28 additions & 16 deletions windows/ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static bool change_ini_path_core(const char *new_path)
return true;
}

char *change_ini_path(const char *new_path)
static char *change_ini_path(const char *new_path)
{
if (!new_path || !*new_path) {
return l10n_dupstr("-ini must be followed by a filename");
Expand All @@ -75,24 +75,36 @@ char *change_ini_path(const char *new_path)
return NULL;
}

void cmdline_arg_process_ini_option(CmdlineArgList *arglist, size_t *arglistpos, sprintf_void_fp error_cb)
void process_ini_option(sprintf_void_fp error_cb)
{
CmdlineArg *arg = arglist->args[*arglistpos];
const char *p = cmdline_arg_to_str(arg);
if (!p || strcmp(p, "-ini")) {
return;
}
(*arglistpos)++;
CmdlineArg *nextarg = arglist->args[*arglistpos];
if (nextarg) {
(*arglistpos)++;
const wchar_t *cmdline = GetCommandLineW();
const wchar_t *ini_arg = wcsstr(cmdline, L"-ini");
if (!ini_arg || (cmdline != ini_arg && (ini_arg[-1] != ' ' && ini_arg[-1] != '\t' && ini_arg[-1] != '"'))) return;
int argc;
wchar_t **argv;
split_into_argv_w(cmdline, true, &argc, &argv, NULL);
if (argc >= 2 && !wcscmp(argv[1], L"-ini")) {
char *new_path = argc > 2 ? dup_wc_to_mb(CP_ACP, argv[2], "") : NULL;
char *error_msg = change_ini_path(new_path);
sfree(new_path);
if (error_msg) {
error_cb(error_msg);
exit(1);
}
}
const char *value = cmdline_arg_to_str(nextarg);
char *error_msg = change_ini_path(value);
if (error_msg) {
error_cb(error_msg);
exit(1);
sfree(argv[0]);
sfree(argv);
}

size_t skip_ini_option(size_t index, size_t argc, wchar_t **argv)
{
if (argc > index) {
if (!wcscmp(argv[index], L"-ini")) {
index++;
if (argc > index) index++;
}
}
return index;
}

static void put_ini_section(const char *in, strbuf *out, const char *prefix)
Expand Down
5 changes: 2 additions & 3 deletions windows/ini.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ extern char inifile[];
typedef void (*sprintf_void_fp)(const char *fmt, ...);

bool get_use_inifile(void);
char *change_ini_path(const char *new_path);
void cmdline_arg_process_ini_option(CmdlineArgList *arglist, size_t *arglistpos, sprintf_void_fp error_cb);
void aux_match_process_ini_option(AuxMatchOpt *amo);
void process_ini_option(sprintf_void_fp error_cb);
size_t skip_ini_option(size_t index, size_t argc, wchar_t **argv);

char *create_ini_section(const char *name, const char *prefix, const char *ini_file);
char *open_ini_section(const char *name, const char *prefix, const char *ini_file);
Expand Down
2 changes: 1 addition & 1 deletion windows/pageant.c
Original file line number Diff line number Diff line change
Expand Up @@ -2089,10 +2089,10 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
* stored in the 'clkeys' array.
*/

process_ini_option(opt_error);
confirm_any_request = (bool)app_conf_get_int("ConfirmAnyRequest", 0, 0, 1);
bool add_keys_encrypted = false;
AuxMatchOpt amo = aux_match_opt_init(opt_error);
aux_match_process_ini_option(&amo);
while (!aux_match_done(&amo)) {
CmdlineArg *valarg;
#define match_opt(...) aux_match_opt( \
Expand Down
2 changes: 1 addition & 1 deletion windows/plink.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ int main(int argc, char **argv)
* Process the command line.
*/
conf = conf_new();
process_ini_option(cmdline_error);
do_defaults(NULL, conf);
settings_set_default_protocol(conf_get_int(conf, CONF_protocol));
settings_set_default_port(conf_get_int(conf, CONF_port));
Expand All @@ -330,7 +331,6 @@ int main(int argc, char **argv)
}
CmdlineArgList *arglist = cmdline_arg_list_from_GetCommandLineW();
size_t arglistpos = 0;
cmdline_arg_process_ini_option(arglist, &arglistpos, cmdline_error);
while (arglist->args[arglistpos]) {
CmdlineArg *arg = arglist->args[arglistpos++];
CmdlineArg *nextarg = arglist->args[arglistpos];
Expand Down
2 changes: 1 addition & 1 deletion windows/pterm.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const unsigned cmdline_tooltype =

void gui_term_process_cmdline(Conf *conf, char *cmdline)
{
process_ini_option(cmdline_error);
do_defaults(NULL, conf);
conf_set_str(conf, CONF_remote_cmd, "");

Expand All @@ -18,7 +19,6 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline)

CmdlineArgList *arglist = cmdline_arg_list_from_GetCommandLineW();
size_t arglistpos = 0;
cmdline_arg_process_ini_option(arglist, &arglistpos, cmdline_error);
while (arglist->args[arglistpos]) {
CmdlineArg *arg = arglist->args[arglistpos++];
CmdlineArg *nextarg = arglist->args[arglistpos];
Expand Down
2 changes: 1 addition & 1 deletion windows/putty.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline)
}
conf_set_int(conf, CONF_logtype, LGTYP_NONE);

process_ini_option(cmdline_error);
do_defaults(NULL, conf);

p = handle_restrict_acl_cmdline_prefix(cmdline);
Expand All @@ -58,7 +59,6 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline)
*/
CmdlineArgList *arglist = cmdline_arg_list_from_GetCommandLineW();
size_t arglistpos = 0;
cmdline_arg_process_ini_option(arglist, &arglistpos, cmdline_error);
while (arglist->args[arglistpos]) {
CmdlineArg *arg = arglist->args[arglistpos++];
CmdlineArg *nextarg = arglist->args[arglistpos];
Expand Down
5 changes: 0 additions & 5 deletions windows/utils/aux_match_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ AuxMatchOpt aux_match_opt_init(aux_opt_error_fn_t opt_error)
return amo[0];
}

void aux_match_process_ini_option(AuxMatchOpt *amo)
{
cmdline_arg_process_ini_option(amo->arglist, &amo->index, amo->error);
}

/*
* Call this with a NULL-terminated list of synonymous option names.
* Point 'val' at a char * to receive the option argument, if one is
Expand Down
4 changes: 3 additions & 1 deletion windows/utils/cmdline_arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <wchar.h>
#include "putty.h"
#include "ini.h"

typedef struct CmdlineArgWin CmdlineArgWin;
struct CmdlineArgWin {
Expand Down Expand Up @@ -90,7 +91,8 @@ CmdlineArgList *cmdline_arg_list_from_GetCommandLineW(void)

listp->args = NULL;
listp->nargs = listp->argssize = 0;
for (size_t i = 1; i < list->argc; i++) {
size_t i = skip_ini_option(1, list->argc, list->argv);
for (; i < list->argc; i++) {
CmdlineArg *argp = cmdline_arg_from_wide_argv_word(
listp, list->argv[i]);
CmdlineArgWin *arg = container_of(argp, CmdlineArgWin, argp);
Expand Down

0 comments on commit cc1018e

Please sign in to comment.