#include "tsystem.h"
/* We don't have libiberty, so use malloc. */
#define xmalloc(S) malloc (S)
+#define xrealloc(V,S) realloc (V,S)
#else
#include "config.h"
#include "system.h"
extern int gnat_argc;
extern char **gnat_argv;
-#ifndef RTX
-/* Do not define for RTX since it is only used for creating child processes
- which is not supported in RTX. */
-extern void __gnat_plist_init (void);
-#endif
-
#ifdef GNAT_UNICODE_SUPPORT
#define EXPAND_ARGV_RATE 128
static void
-append_arg (int *index, LPWSTR value, char ***argv, int *last)
+append_arg (int *index, LPWSTR dir, LPWSTR value,
+ char ***argv, int *last, int quoted)
{
int size;
+ LPWSTR fullvalue;
+ int vallen = _tcslen (value);
+ int dirlen;
- if (*last < *index)
+ if (dir == NULL)
{
- char **old_argv = *argv;
- int old_last = *last;
- int k;
-
- *last += EXPAND_ARGV_RATE;
- *argv = (char **) xmalloc ((*last) * sizeof (char *));
-
- for (k=0; k<=old_last; k++)
- (*argv)[k] = old_argv[k];
+ /* no dir prefix */
+ dirlen = 0;
+ fullvalue = xmalloc ((vallen + 1) * sizeof(TCHAR));
+ }
+ else
+ {
+ /* Add dir first */
+ dirlen = _tcslen (dir);
- free (old_argv);
+ fullvalue = xmalloc ((dirlen + vallen + 1) * sizeof(TCHAR));
+ _tcscpy (fullvalue, dir);
}
- size = WS2SC (NULL, value, 0);
- (*argv)[*index] = (char *) xmalloc (size + 1);
- WS2SC ((*argv)[*index], value, size);
+ /* Append value */
- (*index)++;
-}
+ if (quoted)
+ {
+ _tcsncpy (fullvalue + dirlen, value + 1, vallen - 1);
+ fullvalue [dirlen + vallen - sizeof(TCHAR)] = _T('\0');
+ }
+ else
+ _tcscpy (fullvalue + dirlen, value);
-static void
-adjust_arg (int last, char ***argv)
-{
- char **old_argv = *argv;
- int k;
+ if (*last <= *index)
+ {
+ *last += EXPAND_ARGV_RATE;
+ *argv = (char **) xrealloc (*argv, (*last) * sizeof (char *));
+ }
- *argv = (char **) xmalloc (last * sizeof (char *));
+ size = WS2SC (NULL, fullvalue, 0);
+ (*argv)[*index] = (char *) xmalloc (size + sizeof(TCHAR));
+ WS2SC ((*argv)[*index], fullvalue, size);
- for (k=0; k<last; k++)
- (*argv)[k] = old_argv[k];
+ free (fullvalue);
- free (old_argv);
+ (*index)++;
}
#endif
void
-__gnat_initialize (void *eh)
+__gnat_initialize (void *eh ATTRIBUTE_UNUSED)
{
/* Initialize floating-point coprocessor. This call is needed because
the MS libraries default to 64-bit precision instead of 80-bit
CurrentCodePage = CP_UTF8;
if (codepage != NULL)
- if (strcmp (codepage, "CP_ACP") == 0)
- CurrentCodePage = CP_ACP;
- else if (strcmp (codepage, "CP_UTF8") == 0)
- CurrentCodePage = CP_UTF8;
+ {
+ if (strcmp (codepage, "CP_ACP") == 0)
+ CurrentCodePage = CP_ACP;
+ else if (strcmp (codepage, "CP_UTF8") == 0)
+ CurrentCodePage = CP_UTF8;
+ }
}
/* Adjust gnat_argv to support Unicode characters. */
int last;
int argc_expanded = 0;
TCHAR result [MAX_PATH];
+ int quoted;
wargv = CommandLineToArgvW (GetCommandLineW(), &wargc);
/* argv[0] is the executable full path-name. */
SearchPath (NULL, wargv[0], _T(".exe"), MAX_PATH, result, NULL);
- append_arg (&argc_expanded, result, &gnat_argv, &last);
+ append_arg (&argc_expanded, NULL, result, &gnat_argv, &last, 0);
for (k=1; k<wargc; k++)
{
- /* Check for wildcard expansion. */
- if (_tcsstr (wargv[k], _T("?")) != 0 ||
- _tcsstr (wargv[k], _T("*")) != 0)
+ quoted = (wargv[k][0] == _T('\''));
+
+ /* Check for wildcard expansion if the argument is not quoted. */
+ if (!quoted
+ && (_tcsstr (wargv[k], _T("?")) != 0 ||
+ _tcsstr (wargv[k], _T("*")) != 0))
{
/* Wilcards are present, append all corresponding matches. */
WIN32_FIND_DATA FileData;
HANDLE hDir = FindFirstFile (wargv[k], &FileData);
+ LPWSTR dir = NULL;
+ LPWSTR ldir = _tcsrchr (wargv[k], _T('\\'));
+
+ if (ldir == NULL)
+ ldir = _tcsrchr (wargv[k], _T('/'));
if (hDir == INVALID_HANDLE_VALUE)
{
/* No match, append arg as-is. */
- append_arg (&argc_expanded, wargv[k], &gnat_argv, &last);
+ append_arg (&argc_expanded, NULL, wargv[k],
+ &gnat_argv, &last, quoted);
}
else
{
+ if (ldir != NULL)
+ {
+ int n = ldir - wargv[k] + 1;
+ dir = xmalloc ((n + 1) * sizeof (TCHAR));
+ _tcsncpy (dir, wargv[k], n);
+ dir[n] = _T('\0');
+ }
+
/* Append first match and all remaining ones. */
do {
- append_arg (&argc_expanded,
- FileData.cFileName, &gnat_argv, &last);
+ /* Do not add . and .. special entries */
+
+ if (_tcscmp (FileData.cFileName, _T(".")) != 0
+ && _tcscmp (FileData.cFileName, _T("..")) != 0)
+ append_arg (&argc_expanded, dir, FileData.cFileName,
+ &gnat_argv, &last, 0);
} while (FindNextFile (hDir, &FileData));
FindClose (hDir);
+
+ if (dir != NULL)
+ free (dir);
}
}
else
{
- /* No wildcard. Store parameter as-is. */
- append_arg (&argc_expanded, wargv[k], &gnat_argv, &last);
+ /* No wildcard. Store parameter as-is. Remove quote if
+ needed. */
+ append_arg (&argc_expanded, NULL, wargv[k],
+ &gnat_argv, &last, quoted);
}
}
LocalFree (wargv);
gnat_argc = argc_expanded;
- adjust_arg (argc_expanded, &gnat_argv);
+ gnat_argv = (char **) xrealloc
+ (gnat_argv, argc_expanded * sizeof (char *));
}
}
#endif