2007-05-02 01:07:00 +00:00
|
|
|
/*
|
2012-08-16 03:47:55 +00:00
|
|
|
* $Id$
|
2007-05-02 01:07:00 +00:00
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2009-06-04 15:50:32 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
2009-06-04 15:50:32 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-05-02 01:07:00 +00:00
|
|
|
*/
|
|
|
|
|
2009-06-04 15:50:32 +00:00
|
|
|
#include <qse/cmn/main.h>
|
2012-02-12 13:31:27 +00:00
|
|
|
#include <qse/cmn/mbwc.h>
|
2013-02-18 13:45:50 +00:00
|
|
|
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "mem-prv.h"
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2018-09-13 03:16:23 +00:00
|
|
|
int qse_run_main (
|
|
|
|
int argc, qse_achar_t* argv[], qse_run_main_handler_t handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-03-16 09:23:37 +00:00
|
|
|
#if (defined(QSE_ACHAR_IS_MCHAR) && defined(QSE_CHAR_IS_MCHAR)) || \
|
2011-03-15 21:40:39 +00:00
|
|
|
(defined(QSE_ACHAR_IS_WCHAR) && defined(QSE_CHAR_IS_WCHAR))
|
2012-02-19 14:38:22 +00:00
|
|
|
|
|
|
|
return handler (argc, (qse_char_t**)argv);
|
|
|
|
|
2011-03-15 21:40:39 +00:00
|
|
|
#else
|
2012-10-15 09:36:39 +00:00
|
|
|
int i, ret;
|
|
|
|
qse_char_t** v;
|
|
|
|
qse_mmgr_t* mmgr = QSE_MMGR_GETDFL ();
|
2010-04-06 06:50:01 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
v = (qse_char_t**) QSE_MMGR_ALLOC (
|
|
|
|
mmgr, (argc + 1) * QSE_SIZEOF(qse_char_t*));
|
|
|
|
if (v == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
for (i = 0; i < argc + 1; i++) v[i] = QSE_NULL;
|
2011-03-13 07:40:15 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
2012-10-18 06:52:03 +00:00
|
|
|
v[i]= qse_mbstowcsalldup (argv[i], QSE_NULL, mmgr);
|
2012-10-15 09:36:39 +00:00
|
|
|
if (v[i] == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2012-10-15 09:36:39 +00:00
|
|
|
ret = -1;
|
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2012-10-15 09:36:39 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
ret = handler (argc, v);
|
2010-04-06 06:50:01 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
oops:
|
|
|
|
for (i = 0; i < argc + 1; i++)
|
|
|
|
{
|
|
|
|
if (v[i] != QSE_NULL) QSE_MMGR_FREE (mmgr, v[i]);
|
2011-03-16 09:23:37 +00:00
|
|
|
}
|
2012-10-15 09:36:39 +00:00
|
|
|
QSE_MMGR_FREE (mmgr, v);
|
|
|
|
|
|
|
|
return ret;
|
2011-03-15 21:40:39 +00:00
|
|
|
#endif
|
2011-03-13 07:40:15 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2018-09-13 03:16:23 +00:00
|
|
|
int qse_run_main_with_env (
|
2011-03-13 07:40:15 +00:00
|
|
|
int argc, qse_achar_t* argv[],
|
2018-09-13 03:16:23 +00:00
|
|
|
qse_achar_t* envp[], qse_run_main_with_env_handler_t handler)
|
2011-03-13 07:40:15 +00:00
|
|
|
{
|
2011-03-16 09:23:37 +00:00
|
|
|
#if (defined(QSE_ACHAR_IS_MCHAR) && defined(QSE_CHAR_IS_MCHAR)) || \
|
2011-03-15 21:40:39 +00:00
|
|
|
(defined(QSE_ACHAR_IS_WCHAR) && defined(QSE_CHAR_IS_WCHAR))
|
2012-10-15 09:36:39 +00:00
|
|
|
return handler (argc, (qse_char_t**)argv, (qse_char_t**)envp);
|
2011-03-15 21:40:39 +00:00
|
|
|
#else
|
2012-10-15 09:36:39 +00:00
|
|
|
int i, ret, envc;
|
|
|
|
qse_char_t** v;
|
|
|
|
qse_mmgr_t* mmgr = QSE_MMGR_GETDFL ();
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
if (envp) for (envc = 0; envp[envc]; envc++) ; /* count the number of env items */
|
|
|
|
else envc = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
v = (qse_char_t**) QSE_MMGR_ALLOC (
|
|
|
|
mmgr, (argc + 1 + envc + 1) * QSE_SIZEOF(qse_char_t*));
|
|
|
|
if (v == QSE_NULL) return -1;
|
2007-05-23 01:01:00 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
for (i = 0; i < argc + 1 + envc + 1; i++) v[i] = QSE_NULL;
|
2011-03-13 07:40:15 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
for (i = 0; i < argc + 1 + envc; i++)
|
|
|
|
{
|
|
|
|
qse_achar_t* x;
|
2011-03-13 07:40:15 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
if (i < argc) x = argv[i];
|
|
|
|
else if (i == argc) continue;
|
|
|
|
else x = envp[i - argc - 1];
|
2007-05-23 01:01:00 +00:00
|
|
|
|
2012-10-18 06:52:03 +00:00
|
|
|
v[i]= qse_mbstowcsalldup (x, QSE_NULL, mmgr);
|
2012-10-15 09:36:39 +00:00
|
|
|
if (v[i] == QSE_NULL)
|
2011-03-13 07:40:15 +00:00
|
|
|
{
|
2012-10-15 09:36:39 +00:00
|
|
|
ret = -1;
|
|
|
|
goto oops;
|
2011-03-13 07:40:15 +00:00
|
|
|
}
|
2012-10-15 09:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = handler (argc, v, &v[argc + 1]);
|
2011-03-13 07:40:15 +00:00
|
|
|
|
2012-10-15 09:36:39 +00:00
|
|
|
oops:
|
|
|
|
for (i = 0; i < argc + 1 + envc + 1; i++)
|
|
|
|
{
|
|
|
|
if (v[i] != QSE_NULL) QSE_MMGR_FREE (mmgr, v[i]);
|
2011-03-16 09:23:37 +00:00
|
|
|
}
|
2012-10-15 09:36:39 +00:00
|
|
|
QSE_MMGR_FREE (mmgr, v);
|
|
|
|
|
|
|
|
return ret;
|
2011-03-15 21:40:39 +00:00
|
|
|
#endif
|
2011-03-13 07:40:15 +00:00
|
|
|
}
|
2011-03-16 09:23:37 +00:00
|
|
|
|