diff options
author | Michael Meeks <michael.meeks@novell.com> | 2011-03-21 16:15:22 +0000 |
---|---|---|
committer | Michael Meeks <michael.meeks@novell.com> | 2011-03-21 21:18:41 +0000 |
commit | e08dcb189d2560fd761bbbd955652e930551e12d (patch) | |
tree | 072cc865b92e4ee56f1630dee942c14f5bfd846e /desktop | |
parent | 1df79912a5503f3f5f3c87bac3258e1c5eff93dc (diff) |
split out argument parsing to clean it up
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/unx/source/args.c | 144 | ||||
-rw-r--r-- | desktop/unx/source/args.h | 48 | ||||
-rwxr-xr-x | desktop/unx/source/makefile.mk | 3 | ||||
-rwxr-xr-x | desktop/unx/source/start.c | 155 |
4 files changed, 217 insertions, 133 deletions
diff --git a/desktop/unx/source/args.c b/desktop/unx/source/args.c new file mode 100644 index 0000000000..ff08cacf39 --- /dev/null +++ b/desktop/unx/source/args.c @@ -0,0 +1,144 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Version: MPL 1.1 / GPLv3+ / LGPLv3+ + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License or as specified alternatively below. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Initial Developer of the Original Code is + * Novell, Inc. + * Portions created by the Initial Developer are Copyright (C) 2010 the + * Initial Developer. All Rights Reserved. + * + * Major Contributor(s): + * Michael Meeks <michael.meeks@novell.com> + * Portions created by the Ted are Copyright (C) 2010 Ted. All Rights Reserved. + * + * For minor contributions see the git repository. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 3 or later (the "GPLv3+"), or + * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), + * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable + * instead of those above. + */ +#include <malloc.h> +#include <string.h> +#include <osl/process.h> + +#include "args.h" + +/* do we start -env: */ +static int +is_env_arg (rtl_uString *str) +{ + return !rtl_ustr_ascii_compare_WithLength (str->buffer, 5, "-env:"); +} + +static struct { + const char *name; + unsigned int bTwoArgs : 1; + unsigned int bInhibitSplash : 1; + unsigned int bInhibitPagein : 1; + unsigned int bInhibitJavaLdx : 1; + const char *pagein_type; +} pArgDescr[] = { + /* have a trailing argument */ + { "pt", 1, 0, 0, 0, NULL }, + { "display", 1, 0, 0, 0, NULL }, + + /* no splash */ + { "nologo", 0, 1, 0, 0, NULL }, + { "headless", 0, 1, 0, 0, NULL }, + { "invisible", 0, 1, 0, 0, NULL }, + { "minimized", 0, 1, 0, 0, NULL }, + + /* pagein bits */ + { "writer", 0, 0, 0, 0, "@pagein-writer" }, + { "calc", 0, 0, 0, 0, "@pagein-calc" }, + { "draw", 0, 0, 0, 0, "@pagein-draw" }, + { "impress", 0, 0, 0, 0, "@pagein-impress" }, + + /* nothing much */ + { "version", 0, 1, 1, 1, NULL }, + { "help", 0, 1, 1, 1, NULL }, + { "h", 0, 1, 1, 1, NULL }, + { "?", 0, 1, 1, 1, NULL }, +}; + +Args *parse_args (void) +{ + Args *args; + sal_uInt32 nArgs, i, j; + sal_Bool skipNextArg; + + nArgs = osl_getCommandArgCount(); + args = malloc (sizeof (Args) + sizeof (rtl_uString *) * nArgs); + args->nArgsTotal = nArgs; + + /* sort the -env: args to the front */ + for ( j = i = 0; i < nArgs; ++i ) + { + rtl_uString *pTmp = NULL; + osl_getCommandArg( i, &pTmp ); + if (is_env_arg (pTmp)) + args->ppArgs[j++] = pTmp; + else + rtl_uString_release (pTmp); + } + args->nArgsEnv = j; + + /* Then the other args */ + for ( j = i = 0; i < nArgs; ++i ) + { + rtl_uString *pTmp = NULL; + + osl_getCommandArg( i, &pTmp ); + if (!is_env_arg (pTmp)) + args->ppArgs[j++] = pTmp; + else + rtl_uString_release (pTmp); + } + + skipNextArg = sal_False; + for ( i = args->nArgsEnv; i < args->nArgsTotal; i++ ) + { + sal_uInt32 j; + const sal_Unicode *arg = args->ppArgs[i]->buffer; + sal_Int32 length = args->ppArgs[i]->length; + + /* grok only parameters */ + if (arg[0] != '-') + continue; + + while (length > 2 && arg[0] == '-') { + arg++; + length--; + } + + for ( j = 0; j < SAL_N_ELEMENTS (pArgDescr); ++j ) { + if (!rtl_ustr_indexOfAscii_WithLength + (arg, length, pArgDescr[j].name, strlen (pArgDescr[j].name))) { + + args->bInhibitSplash |= pArgDescr[j].bInhibitSplash; + args->bInhibitPagein |= pArgDescr[j].bInhibitPagein; + args->bInhibitJavaLdx |= pArgDescr[j].bInhibitJavaLdx; + if (pArgDescr[j].pagein_type) + args->pagein_type = pArgDescr[j].pagein_type; + + skipNextArg = pArgDescr[j].bTwoArgs; + } + } + } + + return args; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/desktop/unx/source/args.h b/desktop/unx/source/args.h new file mode 100644 index 0000000000..9dd0c3b10e --- /dev/null +++ b/desktop/unx/source/args.h @@ -0,0 +1,48 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Version: MPL 1.1 / GPLv3+ / LGPLv3+ + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License or as specified alternatively below. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Initial Developer of the Original Code is + * Novell, Inc. + * Portions created by the Initial Developer are Copyright (C) 2010 the + * Initial Developer. All Rights Reserved. + * + * Major Contributor(s): + * Michael Meeks <michael.meeks@novell.com> + * Portions created by the Ted are Copyright (C) 2010 Ted. All Rights Reserved. + * + * For minor contributions see the git repository. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 3 or later (the "GPLv3+"), or + * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), + * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable + * instead of those above. + */ +#include <sal/types.h> +#include <rtl/ustring.h> + +typedef struct { + const char *pagein_type; // @pagein-writer for - writer etc. else NULL + sal_Bool bInhibitSplash; // should we show a splash screen + sal_Bool bInhibitPagein; // should we run pagein ? + sal_Bool bInhibitJavaLdx; // should we run javaldx ? + + sal_uInt32 nArgsEnv; // number of -env: style args + sal_uInt32 nArgsTotal; // number of -env: as well as -writer style args + rtl_uString *ppArgs[1]; // sorted argument array +} Args; + +Args *parse_args (void); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/desktop/unx/source/makefile.mk b/desktop/unx/source/makefile.mk index 820520153a..e406ce488e 100755 --- a/desktop/unx/source/makefile.mk +++ b/desktop/unx/source/makefile.mk @@ -41,7 +41,8 @@ CFLAGS+=$(LIBPNG_CFLAGS) OBJFILES= \ $(OBJ)$/splashx.obj \ - $(OBJ)$/start.obj + $(OBJ)$/start.obj \ + $(OBJ)$/args.obj PAGEIN_OBJS= \ $(OBJ)$/pagein.obj \ diff --git a/desktop/unx/source/start.c b/desktop/unx/source/start.c index 7e550b3172..35281110f4 100755 --- a/desktop/unx/source/start.c +++ b/desktop/unx/source/start.c @@ -47,6 +47,7 @@ #include <rtl/ustrbuf.h> #include <sal/main.h> +#include "args.h" #include "splashx.h" #define IMG_SUFFIX ".png" @@ -655,42 +656,17 @@ int status_pipe[2]; rtl_uString *pAppPath = NULL; void -exec_pagein (void) +exec_pagein (Args *args) { - char buffer[64] = ""; char *argv[5]; - sal_uInt32 nArgs, i, j; - static const char *cmd_names[] = { "writer", "impress", "draw", "calc" }; argv[0] = "dummy-pagein"; argv[1] = "-L../basis-link/program"; argv[2] = "@pagein-common"; - argv[3] = buffer; + argv[3] = (char *)args->pagein_type; argv[4] = NULL; - nArgs = osl_getCommandArgCount(); - for ( i = 0; i < nArgs; ++i ) - { - rtl_uString *pArg = NULL; - osl_getCommandArg( i, &pArg ); - - sal_Int32 length = pArg->length; - const sal_Unicode *arg = pArg->buffer; - while (length > 2 && arg[0] == '-') { - arg++; length--; - } - - for ( j = 0; j < SAL_N_ELEMENTS (cmd_names); ++j ) { - if (!rtl_ustr_indexOfAscii_WithLength( - arg, length, cmd_names[j], strlen (cmd_names[j]))) { - strcpy (buffer, "@pagein-"); - strcat (buffer, cmd_names[j]); - goto found_app; - } - } - } - found_app: - pagein_execute (buffer[0] != '\0' ? 4 : 3, argv); + pagein_execute (args->pagein_type ? 4 : 3, argv); } static void extend_library_path (const char *new_element) @@ -720,34 +696,22 @@ static void extend_library_path (const char *new_element) } static void -exec_javaldx (void) +exec_javaldx (Args *args) { char *newpath; sal_Sequence *line; - sal_uInt32 i, j, nArgs; + sal_uInt32 nArgs; rtl_uString *pApp = NULL; rtl_uString **ppArgs; rtl_uString *pEnvironment[1] = { NULL }; - nArgs = osl_getCommandArgCount(); - ppArgs = (rtl_uString **)calloc( nArgs + 2, sizeof( rtl_uString* ) ); - -#warning FIXME - copy this and just sort the arguments globally first ... + ppArgs = (rtl_uString **)calloc( args->nArgsEnv + 2, sizeof( rtl_uString* ) ); - for ( j = i = 0; i < nArgs; ++i ) - { - rtl_uString *pTmp = NULL; - osl_getCommandArg( i, &pTmp ); - if (rtl_ustr_ascii_compare_WithLength (pTmp->buffer, 5, "-env:")) - { - rtl_uString_acquire (pTmp); - ppArgs[j++] = pTmp; - } - rtl_uString_release (pTmp); - } + for ( nArgs = 0; nArgs < args->nArgsEnv; ++nArgs ) + ppArgs[nArgs] = args->ppArgs[nArgs]; /* FIXME: do we need to check / turn program/redirectrc into an absolute path ? */ - rtl_uString_newFromAscii( &ppArgs[j++], "-env:INIFILENAME=vnd.sun.star.pathname:./redirectrc" ); + rtl_uString_newFromAscii( &ppArgs[nArgs++], "-env:INIFILENAME=vnd.sun.star.pathname:./redirectrc" ); oslProcess javaldx = NULL; oslFileHandle fileOut= 0; @@ -756,7 +720,7 @@ exec_javaldx (void) rtl_uString_newFromAscii( &pApp, "../ure/bin/javaldx" ); /* unset to avoid bogus output */ rtl_uString_newFromAscii( &pEnvironment[0], "G_SLICE" ); - err = osl_executeProcess_WithRedirectedIO( pApp, ppArgs, j, + err = osl_executeProcess_WithRedirectedIO( pApp, ppArgs, nArgs, osl_Process_HIDDEN, NULL, // security NULL, // work dir @@ -794,9 +758,8 @@ exec_javaldx (void) } static void SAL_CALL -fork_app_thread( void *dummy ) +fork_app_thread( Args *args ) { - (void)dummy; rtl_uString *pApp = NULL, *pTmp = NULL, *pArg = NULL; rtl_uString **ppArgs; sal_uInt32 nArgs, i; @@ -805,9 +768,11 @@ fork_app_thread( void *dummy ) oslProcess child; oslProcessError nError; - exec_pagein (); + if (!args->bInhibitJavaLdx) + exec_pagein (args); - exec_javaldx (); + if (!args->bInhibitJavaLdx) + exec_javaldx (args); /* application name */ rtl_uString_newFromAscii( &pApp, "file://" ); @@ -885,98 +850,24 @@ fork_app_thread( void *dummy ) close( status_pipe[1] ); } -/* Check if 'pArg' is -pCmpWith or --pCmpWith */ -static sal_Bool -arg_check( rtl_uString *pArg, const char *pCmpWith ) -{ - sal_Unicode *pUnicode = rtl_uString_getStr( pArg ); - - if ( pUnicode[0] == (sal_Unicode)'-' ) - pUnicode++; - else - return sal_False; - - /* tolerate -- prefixes etc. */ - if ( pUnicode[0] == (sal_Unicode)'-' ) - pUnicode++; - - return !rtl_ustr_ascii_compare( pUnicode, pCmpWith ); -} - -static const char *ppInhibit[] = { - "nologo", "headless", "invisible", "help", "h", "?", - "minimized", "version", NULL -}; -static const char *ppTwoArgs[] = { - "pt", "display", NULL -}; - -/* Read command line parameters and return whether we display the splash. */ -static sal_Bool -get_inhibit_splash() -{ - rtl_uString *pTmp = NULL; - sal_Bool bSkipNextArg = sal_False; - const char **ppIter; - - rtl_uString_new( &pTmp ); - - sal_uInt32 nArg; - sal_uInt32 nArgCount = osl_getCommandArgCount(); - for ( nArg = 0; nArg < nArgCount; ++nArg ) - { - if ( bSkipNextArg ) - { - bSkipNextArg = sal_False; - continue; - } - - osl_getCommandArg( nArg, &pTmp ); - - /* check for inhibit splash params */ - for ( ppIter = ppInhibit; *ppIter; ++ppIter ) - { - if ( arg_check( pTmp, *ppIter ) ) - { - rtl_uString_release( pTmp ); - return sal_True; - } - } - /* check for 2 arguments params */ - for ( ppIter = ppTwoArgs; *ppIter; ++ppIter ) - { - if ( arg_check( pTmp, *ppIter ) ) - { - bSkipNextArg = sal_True; - break; - } - } - } - - /* cleanup */ - rtl_uString_release( pTmp ); - - return sal_False; -} - SAL_IMPLEMENT_MAIN_WITH_ARGS( argc, argv ) { int fd = 0; - sal_Bool bInhibitSplash; sal_Bool bSentArgs = sal_False; rtl_uString *pPipePath = NULL; ProgressStatus eResult = ProgressExit; + Args *args; fprintf (stderr, "start !\n"); /* turn SIGPIPE into an error */ signal( SIGPIPE, SIG_IGN ); - bInhibitSplash = get_inhibit_splash(); + args = parse_args(); #ifndef ENABLE_QUICKSTART_LIBPNG /* we can't load and render it anyway */ - bInhibitSplash = sal_True; + args->bInhibitSplash = sal_True; #endif pAppPath = get_app_path( argv[0] ); @@ -1019,15 +910,15 @@ SAL_IMPLEMENT_MAIN_WITH_ARGS( argc, argv ) exit( 1 ); } int status_fd = status_pipe[0]; - osl_createThread( fork_app_thread, NULL ); + osl_createThread( fork_app_thread, args ); - if ( !bInhibitSplash ) + if ( !args->bInhibitSplash ) { load_splash_image( pAppPath ); - load_splash_defaults( pAppPath, &bInhibitSplash ); + load_splash_defaults( pAppPath, &args->bInhibitSplash ); } - if ( !bInhibitSplash && splash_create_window( argc, argv ) ) + if ( !args->bInhibitSplash && splash_create_window( argc, argv ) ) { splash_draw_progress( 0 ); eResult = show_splash( status_fd ); |