wrowe 02/02/17 21:31:48
Modified: misc/win32 apr_app.c
Log:
Simplification - code moving to internal.c for sharing, and misc.c for
cross-platform apr_app_initialize. This stub will only handle the
wmain() entry point for Unicode arguments in a _native_ NT-only build.
Revision Changes Path
1.11 +6 -184 apr/misc/win32/apr_app.c
Index: apr_app.c
===================================================================
RCS file: /home/cvs/apr/misc/win32/apr_app.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- apr_app.c 12 Feb 2002 21:55:08 -0000 1.10
+++ apr_app.c 18 Feb 2002 05:31:47 -0000 1.11
@@ -78,86 +78,22 @@
#include "fileio.h"
#include "assert.h"
#include "apr_private.h"
-
-static int wastrtoastr(char ***retarr, wchar_t **arr, int args)
-{
- size_t elesize = 0;
- char **newarr;
- char *elements;
- char *ele;
- int arg;
-
- if (args < 0) {
- for (args = 0; arr[args]; ++args)
- ;
- }
-
- newarr = _malloc_dbg((args + 1) * sizeof(char *),
- _CRT_BLOCK, __FILE__, __LINE__);
-
- for (arg = 0; arg < args; ++arg) {
- newarr[arg] = (void*)(wcslen(arr[arg]) + 1);
- elesize += (size_t)newarr[arg];
- }
-
- /* This is a safe max allocation, we will realloc after
- * processing and return the excess to the free store.
- * 3 ucs bytes hold any single wchar_t value (16 bits)
- * 4 ucs bytes will hold a wchar_t pair value (20 bits)
- */
- elesize = elesize * 3 + 1;
- ele = elements = _malloc_dbg(elesize * sizeof(char),
- _CRT_BLOCK, __FILE__, __LINE__);
-
- for (arg = 0; arg < args; ++arg) {
- size_t len = (size_t)newarr[arg];
- size_t newlen = elesize;
-
- newarr[arg] = ele;
- (void)apr_conv_ucs2_to_utf8(arr[arg], &len,
- newarr[arg], &elesize);
-
- newlen -= elesize;
- ele += newlen;
- assert(elesize && (len == 0));
- }
-
- newarr[arg] = NULL;
- *(ele++) = '\0';
-
- /* Return to the free store if the heap realloc is the least bit
optimized
- */
- ele = _realloc_dbg(elements, ele - elements,
- _CRT_BLOCK, __FILE__, __LINE__);
-
- if (ele != elements) {
- size_t diff = ele - elements;
- for (arg = 0; arg < args; ++arg) {
- newarr[arg] += diff;
- }
- }
-
- *retarr = newarr;
- return args;
-}
-
-#ifdef APR_APP
+#include "misc.h"
/* This symbol is _private_, although it must be exported.
*/
-extern int APR_DECLARE_DATA apr_app_init_complete;
-extern int main(int argc, char **argv, char **env);
+extern int main(int argc, const char **argv, const char **env);
-int wmain(int argc, wchar_t **wargv, wchar_t **wenv)
+int wmain(int argc, const wchar_t **wargv, const wchar_t **wenv)
{
char **argv;
char **env;
int dupenv;
- (void)wastrtoastr(&argv, wargv, argc);
+ (void)apr_wastrtoastr(&argv, wargv, argc);
- dupenv = wastrtoastr(&env, wenv, -1);
+ dupenv = apr_wastrtoastr(&env, wenv, -1);
_environ = _malloc_dbg((dupenv + 1) * sizeof (char *),
_CRT_BLOCK, __FILE__, __LINE__ );
@@ -173,124 +109,10 @@
if (_wenviron) {
wenv = _wenviron;
_wenviron = NULL;
- free(wenv);
+ free((wchar_t **)wenv);
}
apr_app_init_complete = 1;
return main(argc, argv, env);
}
-
-#else
-
-/* This symbol is _private_, although it must be exported.
- */
-int APR_DECLARE_DATA apr_app_init_complete = 0;
-
-static int warrsztoastr(char ***retarr, wchar_t *arrsz, int args)
-{
- apr_wchar_t *wch;
- size_t totlen;
- size_t newlen;
- size_t wsize;
- char **newarr;
- int arg;
-
- if (args < 0) {
- for (args = 1, wch = arrsz; wch[0] || wch[1]; ++wch)
- if (!*wch)
- ++args;
- }
- wsize = 1 + wch - arrsz;
-
- newarr = _malloc_dbg((args + 1) * sizeof(char *),
- _CRT_BLOCK, __FILE__, __LINE__);
-
- /* This is a safe max allocation, we will realloc after
- * processing and return the excess to the free store.
- * 3 ucs bytes hold any single wchar_t value (16 bits)
- * 4 ucs bytes will hold a wchar_t pair value (20 bits)
- */
- newlen = totlen = wsize * 3 + 1;
- newarr[0] = _malloc_dbg(newlen * sizeof(char),
- _CRT_BLOCK, __FILE__, __LINE__);
-
- (void)apr_conv_ucs2_to_utf8(arrsz, &wsize,
- newarr[0], &newlen);
-
- assert(newlen && !wsize);
- /* Return to the free store if the heap realloc is the least bit
optimized
- */
- newarr[0] = _realloc_dbg(newarr[0], totlen - newlen,
- _CRT_BLOCK, __FILE__, __LINE__);
-
- for (arg = 1; arg < args; ++arg) {
- newarr[arg] = newarr[arg - 1] + 2;
- while (*(newarr[arg]++)) {
- ;
- }
- }
-
- newarr[arg] = NULL;
-
- *retarr = newarr;
- return args;
-}
-
-/* Reprocess the arguments to main() for a completely apr-ized application
- */
-
-APR_DECLARE(apr_status_t) apr_app_main(int *argc, char ***argv, char ***env)
-{
-#if APR_HAS_UNICODE_FS
- IF_WIN_OS_IS_UNICODE
- {
- apr_wchar_t **wstrs;
- apr_wchar_t *sysstr;
- int wstrc;
- int dupenv;
-
- if (apr_app_init_complete) {
- return APR_SUCCESS;
- }
-
- sysstr = GetCommandLineW();
- if (sysstr) {
- wstrs = CommandLineToArgvW(sysstr, &wstrc);
- if (wstrs) {
- *argc = wastrtoastr(argv, wstrs, wstrc);
- GlobalFree(wstrs);
- }
- }
-
- sysstr = GetEnvironmentStringsW();
- dupenv = warrsztoastr(&_environ, sysstr, -1);
-
- if (env) {
- env = _malloc_dbg((dupenv + 1) * sizeof (char *),
- _CRT_BLOCK, __FILE__, __LINE__ );
- memcpy(*env, _environ, (dupenv + 1) * sizeof (char *));
- }
- else {
- }
-
- /* MSVCRT will attempt to maintain the wide environment calls
- * on _putenv(), which is bogus if we've passed a non-ascii
- * string to _putenv(), since they use MultiByteToWideChar
- * and breaking the implicit utf-8 assumption we've built.
- *
- * Reset _wenviron for good measure.
- */
- if (_wenviron) {
- apr_wchar_t **wenv = _wenviron;
- _wenviron = NULL;
- free(wenv);
- }
-
- apr_app_init_complete = 1;
- }
-#endif
- return APR_SUCCESS;
-}
-
-#endif