1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-03-22 14:39:24 -05:00

Pageant's command line handling now uses my new split_into_argv()

function, because it's silly to have two (and because the old one
was not the same as the new one, violating the Principle of Least
Surprise).

[originally from svn r1811]
This commit is contained in:
Simon Tatham 2002-08-06 17:57:37 +00:00
parent 5e49e3fe1c
commit 437d740fb3
5 changed files with 47 additions and 52 deletions

View File

@ -15,6 +15,7 @@
#include "ssh.h" #include "ssh.h"
#include "misc.h" #include "misc.h"
#include "tree234.h" #include "tree234.h"
#include "winstuff.h"
#define IDI_MAINICON 200 #define IDI_MAINICON 200
#define IDI_TRAYICON 201 #define IDI_TRAYICON 201
@ -1786,6 +1787,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
HMODULE advapi; HMODULE advapi;
char *command = NULL; char *command = NULL;
int added_keys = 0; int added_keys = 0;
int argc, i;
char **argv, **argstart;
/* /*
* Determine whether we're an NT system (should have security * Determine whether we're an NT system (should have security
@ -1936,48 +1939,25 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
/* /*
* Process the command line and add keys as listed on it. * Process the command line and add keys as listed on it.
* If we already determined that we need to spawn a program from above we
* need to ignore the first two arguments. [DBW]
*/ */
{ split_into_argv(cmdline, &argc, &argv, &argstart);
char *p; for (i = 0; i < argc; i++) {
int inquotes = 0; if (!strcmp(argv[i], "-c")) {
p = cmdline;
while (*p) {
while (*p && isspace(*p))
p++;
if (*p && !isspace(*p)) {
char *q = p, *pp = p;
while (*p && (inquotes || !isspace(*p))) {
if (*p == '"') {
inquotes = !inquotes;
p++;
continue;
}
*pp++ = *p++;
}
if (*pp) {
if (*p)
p++;
*pp++ = '\0';
}
if (!strcmp(q, "-c")) {
/* /*
* If we see `-c', then the rest of the * If we see `-c', then the rest of the
* command line should be treated as a * command line should be treated as a
* command to be spawned. * command to be spawned.
*/ */
while (*p && isspace(*p)) if (i < argc-1)
p++; command = argstart[i+1];
command = p; else
command = "";
break; break;
} else { } else {
add_keyfile(q); add_keyfile(argv[i]);
added_keys = TRUE; added_keys = TRUE;
} }
} }
}
}
/* /*
* Forget any passphrase that we retained while going over * Forget any passphrase that we retained while going over

View File

@ -1384,7 +1384,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
int argc; int argc;
char **argv; char **argv;
split_into_argv(cmdline, &argc, &argv); split_into_argv(cmdline, &argc, &argv, NULL);
if (argc > 0) { if (argc > 0) {
/* /*

View File

@ -312,7 +312,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
int argc, i; int argc, i;
char **argv; char **argv;
split_into_argv(cmdline, &argc, &argv); split_into_argv(cmdline, &argc, &argv, NULL);
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
char *p = argv[i]; char *p = argv[i];

View File

@ -32,7 +32,7 @@ struct ctlpos {
/* /*
* Exports from winutils.c. * Exports from winutils.c.
*/ */
void split_into_argv(char *, int *, char ***); void split_into_argv(char *, int *, char ***, char ***);
/* /*
* Private structure for prefslist state. Only in the header file * Private structure for prefslist state. Only in the header file

View File

@ -4,8 +4,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#define lenof(x) ( sizeof((x)) / sizeof(*(x)) ) #include "misc.h"
#ifdef TESTMODE #ifdef TESTMODE
/* Definitions to allow this module to be compiled standalone for testing. */ /* Definitions to allow this module to be compiled standalone for testing. */
@ -20,13 +21,22 @@
* utilities which get a whole command line and must break it * utilities which get a whole command line and must break it
* themselves). * themselves).
* *
* Does not modify the input command line (just in case). * Does not modify the input command line.
*
* The final parameter (argstart) is used to return a second array
* of char * pointers, the same length as argv, each one pointing
* at the start of the corresponding element of argv in the
* original command line. So if you get half way through processing
* your command line in argc/argv form and then decide you want to
* treat the rest as a raw string, you can. If you don't want to,
* `argstart' can be safely left NULL.
*/ */
void split_into_argv(const char *cmdline, int *argc, char ***argv) void split_into_argv(char *cmdline, int *argc, char ***argv,
char ***argstart)
{ {
const char *p; char *p;
char *outputline, *q; char *outputline, *q;
char **outputargv; char **outputargv, **outputargstart;
int outputargc; int outputargc;
/* /*
@ -124,8 +134,9 @@ void split_into_argv(const char *cmdline, int *argc, char ***argv)
* This will guaranteeably be big enough; we can realloc it * This will guaranteeably be big enough; we can realloc it
* down later. * down later.
*/ */
outputline = malloc(1+strlen(cmdline)); outputline = smalloc(1+strlen(cmdline));
outputargv = malloc(sizeof(char *) * (strlen(cmdline)+1 / 2)); outputargv = smalloc(sizeof(char *) * (strlen(cmdline)+1 / 2));
outputargstart = smalloc(sizeof(char *) * (strlen(cmdline)+1 / 2));
p = cmdline; q = outputline; outputargc = 0; p = cmdline; q = outputline; outputargc = 0;
@ -137,7 +148,9 @@ void split_into_argv(const char *cmdline, int *argc, char ***argv)
if (!*p) break; if (!*p) break;
/* We have an argument; start it. */ /* We have an argument; start it. */
outputargv[outputargc++] = q; outputargv[outputargc] = q;
outputargstart[outputargc] = p;
outputargc++;
quote = 0; quote = 0;
/* Copy data into the argument until it's finished. */ /* Copy data into the argument until it's finished. */
@ -190,10 +203,12 @@ void split_into_argv(const char *cmdline, int *argc, char ***argv)
*q++ = '\0'; *q++ = '\0';
} }
outputargv = realloc(outputargv, sizeof(char *) * outputargc); outputargv = srealloc(outputargv, sizeof(char *) * outputargc);
outputargstart = srealloc(outputargstart, sizeof(char *) * outputargc);
if (argc) *argc = outputargc; if (argc) *argc = outputargc;
if (argv) *argv = outputargv; if (argv) *argv = outputargv; else sfree(outputargv);
if (argstart) *argstart = outputargstart; else sfree(outputargstart);
} }
#ifdef TESTMODE #ifdef TESTMODE