1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <cmdlineargs.hxx>
23 #include <vcl/svapp.hxx>
24 #include <rtl/uri.hxx>
25 #include <rtl/ustring.hxx>
26 #include "rtl/process.h"
27 #include <comphelper/processfactory.hxx>
28 #include <com/sun/star/uri/ExternalUriReferenceTranslator.hpp>
29 #include "tools/getprocessworkingdir.hxx"
31 #include <svl/documentlockfile.hxx>
35 using namespace com::sun::star::lang
;
36 using namespace com::sun::star::uri
;
37 using namespace com::sun::star::uno
;
45 OUString
translateExternalUris(OUString
const & input
) {
47 com::sun::star::uri::ExternalUriReferenceTranslator::create(
48 comphelper::getProcessComponentContext())->
49 translateToInternal(input
));
50 return t
.isEmpty() ? input
: t
;
53 std::vector
< OUString
> translateExternalUris(
54 std::vector
< OUString
> const & input
)
56 std::vector
< OUString
> t
;
57 for (std::vector
< OUString
>::const_iterator
i(input
.begin());
58 i
!= input
.end(); ++i
)
60 t
.push_back(translateExternalUris(*i
));
65 class ExtCommandLineSupplier
: public CommandLineArgs::Supplier
{
67 explicit ExtCommandLineSupplier():
68 m_count(rtl_getAppCommandArgCount()),
72 if (tools::getProcessWorkingDir(url
)) {
77 virtual ~ExtCommandLineSupplier() {}
79 virtual boost::optional
< OUString
> getCwdUrl() { return m_cwdUrl
; }
81 virtual bool next(OUString
* argument
) {
82 OSL_ASSERT(argument
!= NULL
);
83 if (m_index
< m_count
) {
84 rtl_getAppCommandArg(m_index
++, &argument
->pData
);
92 boost::optional
< OUString
> m_cwdUrl
;
99 CommandLineArgs::Supplier::Exception::Exception() {}
101 CommandLineArgs::Supplier::Exception::Exception(Exception
const &) {}
103 CommandLineArgs::Supplier::Exception::~Exception() {}
105 CommandLineArgs::Supplier::Exception
&
106 CommandLineArgs::Supplier::Exception::operator =(Exception
const &)
109 CommandLineArgs::Supplier::~Supplier() {}
111 // intialize class with command line parameters from process environment
112 CommandLineArgs::CommandLineArgs()
115 ExtCommandLineSupplier s
;
116 ParseCommandLine_Impl( s
);
119 CommandLineArgs::CommandLineArgs( Supplier
& supplier
)
122 ParseCommandLine_Impl( supplier
);
125 // ----------------------------------------------------------------------------
127 void CommandLineArgs::ParseCommandLine_Impl( Supplier
& supplier
)
129 m_cwdUrl
= supplier
.getCwdUrl();
131 // parse command line arguments
132 bool bOpenEvent(true);
133 bool bPrintEvent(false);
134 bool bViewEvent(false);
135 bool bStartEvent(false);
136 bool bPrintToEvent(false);
137 bool bPrinterName(false);
138 bool bForceOpenEvent(false);
139 bool bForceNewEvent(false);
140 bool bDisplaySpec(false);
141 bool bOpenDoc(false);
142 bool bConversionEvent(false);
143 bool bConversionParamsEvent(false);
144 bool bBatchPrintEvent(false);
145 bool bBatchPrinterNameEvent(false);
146 bool bConversionOutEvent(false);
151 if ( !supplier
.next( &aArg
) )
156 if ( !aArg
.isEmpty() )
160 if ( !InterpretCommandLineParameter( aArg
, oArg
))
162 if ( aArg
.toChar() == '-' )
164 // handle this argument as an option
165 if ( aArg
.equalsIgnoreAsciiCase("-n"))
167 // force new documents based on the following documents
168 bForceNewEvent
= true;
170 bForceOpenEvent
= false;
171 bPrintToEvent
= false;
175 bDisplaySpec
= false;
177 else if ( aArg
.equalsIgnoreAsciiCase("-o"))
179 // force open documents regardless if they are templates or not
180 bForceOpenEvent
= true;
182 bForceNewEvent
= false;
183 bPrintToEvent
= false;
187 bDisplaySpec
= false;
189 else if ( oArg
.equalsIgnoreAsciiCase("pt"))
191 // Print to special printer
192 bPrintToEvent
= true;
196 bForceNewEvent
= false;
199 bDisplaySpec
= false;
200 bForceOpenEvent
= false;
202 else if ( aArg
.equalsIgnoreAsciiCase("-p"))
204 // Print to default printer
206 bPrintToEvent
= false;
208 bForceNewEvent
= false;
209 bForceOpenEvent
= false;
212 bDisplaySpec
= false;
214 else if ( oArg
.equalsIgnoreAsciiCase("view"))
219 bPrintToEvent
= false;
220 bForceNewEvent
= false;
221 bForceOpenEvent
= false;
224 bDisplaySpec
= false;
226 else if ( oArg
.equalsIgnoreAsciiCase("show"))
233 bPrintToEvent
= false;
234 bForceNewEvent
= false;
235 bForceOpenEvent
= false;
236 bDisplaySpec
= false;
238 else if ( oArg
.equalsIgnoreAsciiCase("display"))
243 bForceOpenEvent
= false;
244 bPrintToEvent
= false;
245 bForceNewEvent
= false;
250 else if ( oArg
.equalsIgnoreAsciiCase("language"))
254 bForceOpenEvent
= false;
255 bPrintToEvent
= false;
256 bForceNewEvent
= false;
259 bDisplaySpec
= false;
261 else if ( oArg
.equalsIgnoreAsciiCase("convert-to"))
264 bConversionEvent
= true;
265 bConversionParamsEvent
= true;
267 else if ( oArg
.equalsIgnoreAsciiCase("print-to-file"))
270 bBatchPrintEvent
= true;
272 else if ( oArg
.equalsIgnoreAsciiCase("printer-name") &&
275 bBatchPrinterNameEvent
= true;
277 else if ( oArg
.equalsIgnoreAsciiCase("outdir") &&
278 (bConversionEvent
|| bBatchPrintEvent
) )
280 bConversionOutEvent
= true;
283 // because it's impossible to filter these options that
284 // are handled in the soffice shell script with the
285 // primitive tools that /bin/sh offers, ignore them here
288 !oArg
.equalsIgnoreAsciiCase("backtrace") &&
289 !oArg
.equalsIgnoreAsciiCase("strace") &&
290 !oArg
.equalsIgnoreAsciiCase("valgrind") &&
291 // for X Session Management, handled in
292 // vcl/unx/generic/app/sm.cxx:
293 !oArg
.match("session=") &&
295 //ignore additional legacy options that don't do anything anymore
296 !oArg
.equalsIgnoreAsciiCase("nocrashreport") &&
304 if ( bPrinterName
&& bPrintToEvent
)
306 // first argument after "-pt" this must be the printer name
307 m_printername
= aArg
;
308 bPrinterName
= false;
310 else if ( bConversionParamsEvent
&& bConversionEvent
)
312 // first argument must be the params
313 m_conversionparams
= aArg
;
314 bConversionParamsEvent
= false;
316 else if ( bBatchPrinterNameEvent
&& bBatchPrintEvent
)
318 // first argument is the printer name
319 m_printername
= aArg
;
320 bBatchPrinterNameEvent
= false;
322 else if ( (bConversionEvent
|| bBatchPrintEvent
) && bConversionOutEvent
)
324 m_conversionout
= aArg
;
325 bConversionOutEvent
= false;
329 // handle this argument as a filename
332 m_openlist
.push_back(aArg
);
335 else if ( bViewEvent
)
337 m_viewlist
.push_back(aArg
);
340 else if ( bStartEvent
)
342 m_startlist
.push_back(aArg
);
345 else if ( bPrintEvent
)
347 m_printlist
.push_back(aArg
);
350 else if ( bPrintToEvent
)
352 m_printtolist
.push_back(aArg
);
355 else if ( bForceNewEvent
)
357 m_forcenewlist
.push_back(aArg
);
360 else if ( bForceOpenEvent
)
362 m_forceopenlist
.push_back(aArg
);
365 else if ( bDisplaySpec
)
367 bDisplaySpec
= false; // only one display, not a lsit
368 bOpenEvent
= true; // set back to standard
370 else if ( bConversionEvent
|| bBatchPrintEvent
)
371 m_conversionlist
.push_back(aArg
);
379 m_bDocumentArgs
= true;
382 bool CommandLineArgs::InterpretCommandLineParameter( const OUString
& aArg
, OUString
& oArg
)
384 bool bDeprecated
= false;
385 if (aArg
.matchIgnoreAsciiCase("--"))
387 oArg
= OUString(aArg
.getStr()+2, aArg
.getLength()-2);
389 else if (aArg
.toChar() == '-')
391 if ( aArg
.getLength() > 2 ) // -h, -o, -n, -? are still valid
393 oArg
= OUString(aArg
.getStr()+1, aArg
.getLength()-1);
400 if ( oArg
== "minimized" )
404 else if ( oArg
== "invisible" )
408 else if ( oArg
== "norestore" )
412 else if ( oArg
== "nodefault" )
416 else if ( oArg
== "headless" )
418 // Headless means also invisibile, so set this parameter to true!
422 else if ( oArg
== "quickstart" )
424 #if defined(ENABLE_QUICKSTART_APPLET)
427 m_noquickstart
= false;
429 else if ( oArg
== "quickstart=no" )
431 m_noquickstart
= true;
432 m_quickstart
= false;
434 else if ( oArg
== "terminate_after_init" )
436 m_terminateafterinit
= true;
438 else if ( oArg
== "nofirststartwizard" )
440 m_nofirststartwizard
= true;
442 else if ( oArg
== "nologo" )
446 #if HAVE_FEATURE_MULTIUSER_ENVIRONMENT
447 else if ( oArg
== "nolockcheck" )
449 m_nolockcheck
= true;
450 // Workaround for automated testing
451 ::svt::DocumentLockFile::AllowInteraction( false );
454 else if ( oArg
== "help" || aArg
== "-h" || aArg
== "-?" )
458 else if ( oArg
== "helpwriter" )
462 else if ( oArg
== "helpcalc" )
466 else if ( oArg
== "helpdraw" )
470 else if ( oArg
== "helpimpress" )
472 m_helpimpress
= true;
474 else if ( oArg
== "helpbase" )
478 else if ( oArg
== "helpbasic" )
482 else if ( oArg
== "helpmath" )
486 else if ( oArg
== "version" )
490 else if ( oArg
.matchIgnoreAsciiCase("splash-pipe=") )
495 /* #i84053# ignore -psn on Mac
496 Platform dependent #ifdef here is ugly, however this is currently
497 the only platform dependent parameter. Should more appear
498 we should find a better solution
500 else if ( aArg
.match("-psn") )
507 /* fdo#57203 ignore -Embedding on Windows
508 when LibreOffice is launched by COM+
510 else if ( oArg
== "Embedding" )
515 else if ( oArg
.matchIgnoreAsciiCase("infilter="))
517 m_infilter
.push_back(oArg
.copy(RTL_CONSTASCII_LENGTH("infilter=")));
519 else if ( oArg
.matchIgnoreAsciiCase("accept="))
521 m_accept
.push_back(oArg
.copy(RTL_CONSTASCII_LENGTH("accept=")));
523 else if ( oArg
.matchIgnoreAsciiCase("unaccept="))
525 m_unaccept
.push_back(oArg
.copy(RTL_CONSTASCII_LENGTH("unaccept=")));
527 else if ( oArg
.matchIgnoreAsciiCase("language="))
529 m_language
= oArg
.copy(RTL_CONSTASCII_LENGTH("language="));
531 else if ( oArg
.matchIgnoreAsciiCase("pidfile="))
533 m_pidfile
= oArg
.copy(RTL_CONSTASCII_LENGTH("pidfile="));
535 else if ( oArg
== "writer" )
538 m_bDocumentArgs
= true;
540 else if ( oArg
== "calc" )
543 m_bDocumentArgs
= true;
545 else if ( oArg
== "draw" )
548 m_bDocumentArgs
= true;
550 else if ( oArg
== "impress" )
553 m_bDocumentArgs
= true;
555 else if ( oArg
== "base" )
558 m_bDocumentArgs
= true;
560 else if ( oArg
== "global" )
563 m_bDocumentArgs
= true;
565 else if ( oArg
== "math" )
568 m_bDocumentArgs
= true;
570 else if ( oArg
== "web" )
573 m_bDocumentArgs
= true;
580 OString
sArg(OUStringToOString(aArg
, osl_getThreadTextEncoding()));
581 fprintf(stderr
, "Warning: %s is deprecated. Use -%s instead.\n", sArg
.getStr(), sArg
.getStr());
586 void CommandLineArgs::InitParamValues()
597 m_quickstart
= false;
598 m_noquickstart
= false;
599 m_terminateafterinit
= false;
600 m_nofirststartwizard
= false;
602 m_nolockcheck
= false;
613 m_helpwriter
= false;
618 m_helpimpress
= false;
622 m_splashpipe
= false;
624 m_bDocumentArgs
= false;
627 bool CommandLineArgs::IsMinimized() const
632 bool CommandLineArgs::IsInvisible() const
637 bool CommandLineArgs::IsNoRestore() const
642 bool CommandLineArgs::IsNoDefault() const
647 bool CommandLineArgs::IsHeadless() const
652 bool CommandLineArgs::IsQuickstart() const
657 bool CommandLineArgs::IsNoQuickstart() const
659 return m_noquickstart
;
662 bool CommandLineArgs::IsTerminateAfterInit() const
664 return m_terminateafterinit
;
667 bool CommandLineArgs::IsNoLogo() const
672 bool CommandLineArgs::IsNoLockcheck() const
674 return m_nolockcheck
;
677 bool CommandLineArgs::IsHelp() const
681 bool CommandLineArgs::IsHelpWriter() const
686 bool CommandLineArgs::IsHelpCalc() const
691 bool CommandLineArgs::IsHelpDraw() const
696 bool CommandLineArgs::IsHelpImpress() const
698 return m_helpimpress
;
701 bool CommandLineArgs::IsHelpBase() const
705 bool CommandLineArgs::IsHelpMath() const
709 bool CommandLineArgs::IsHelpBasic() const
714 bool CommandLineArgs::IsWriter() const
719 bool CommandLineArgs::IsCalc() const
724 bool CommandLineArgs::IsDraw() const
729 bool CommandLineArgs::IsImpress() const
734 bool CommandLineArgs::IsBase() const
739 bool CommandLineArgs::IsGlobal() const
744 bool CommandLineArgs::IsMath() const
749 bool CommandLineArgs::IsWeb() const
754 bool CommandLineArgs::IsVersion() const
759 OUString
CommandLineArgs::GetUnknown() const
764 bool CommandLineArgs::HasModuleParam() const
766 return m_writer
|| m_calc
|| m_draw
|| m_impress
|| m_global
|| m_math
770 bool CommandLineArgs::HasSplashPipe() const
775 std::vector
< OUString
> const & CommandLineArgs::GetAccept() const
780 std::vector
< OUString
> const & CommandLineArgs::GetUnaccept() const
785 std::vector
< OUString
> CommandLineArgs::GetOpenList() const
787 return translateExternalUris(m_openlist
);
790 std::vector
< OUString
> CommandLineArgs::GetViewList() const
792 return translateExternalUris(m_viewlist
);
795 std::vector
< OUString
> CommandLineArgs::GetStartList() const
797 return translateExternalUris(m_startlist
);
800 std::vector
< OUString
> CommandLineArgs::GetForceOpenList() const
802 return translateExternalUris(m_forceopenlist
);
805 std::vector
< OUString
> CommandLineArgs::GetForceNewList() const
807 return translateExternalUris(m_forcenewlist
);
810 std::vector
< OUString
> CommandLineArgs::GetPrintList() const
812 return translateExternalUris(m_printlist
);
815 std::vector
< OUString
> CommandLineArgs::GetPrintToList() const
817 return translateExternalUris(m_printtolist
);
820 OUString
CommandLineArgs::GetPrinterName() const
822 return m_printername
;
825 OUString
CommandLineArgs::GetLanguage() const
830 std::vector
< OUString
> const & CommandLineArgs::GetInFilter() const
835 std::vector
< OUString
> CommandLineArgs::GetConversionList() const
837 return translateExternalUris(m_conversionlist
);
840 OUString
CommandLineArgs::GetConversionParams() const
842 return m_conversionparams
;
844 OUString
CommandLineArgs::GetConversionOut() const
846 return translateExternalUris(m_conversionout
);
849 bool CommandLineArgs::IsEmpty() const
854 bool CommandLineArgs::WantsToLoadDocument() const
856 return m_bDocumentArgs
;
859 OUString
CommandLineArgs::GetPidfileName() const
864 } // namespace desktop
866 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */