update credits
[LibreOffice.git] / desktop / source / app / cmdlineargs.cxx
blobf9baa07ee5b5324ebf42786fa53312c4d6d11009
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
33 #include <cstdio>
35 using namespace com::sun::star::lang;
36 using namespace com::sun::star::uri;
37 using namespace com::sun::star::uno;
40 namespace desktop
43 namespace {
45 OUString translateExternalUris(OUString const & input) {
46 OUString t(
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));
62 return t;
65 class ExtCommandLineSupplier: public CommandLineArgs::Supplier {
66 public:
67 explicit ExtCommandLineSupplier():
68 m_count(rtl_getAppCommandArgCount()),
69 m_index(0)
71 OUString url;
72 if (tools::getProcessWorkingDir(url)) {
73 m_cwdUrl.reset(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);
85 return true;
86 } else {
87 return false;
91 private:
92 boost::optional< OUString > m_cwdUrl;
93 sal_uInt32 m_count;
94 sal_uInt32 m_index;
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 &)
107 { return *this; }
109 CommandLineArgs::Supplier::~Supplier() {}
111 // intialize class with command line parameters from process environment
112 CommandLineArgs::CommandLineArgs()
114 InitParamValues();
115 ExtCommandLineSupplier s;
116 ParseCommandLine_Impl( s );
119 CommandLineArgs::CommandLineArgs( Supplier& supplier )
121 InitParamValues();
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);
148 for (;;)
150 OUString aArg;
151 if ( !supplier.next( &aArg ) )
153 break;
156 if ( !aArg.isEmpty() )
158 m_bEmpty = false;
159 OUString oArg;
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;
169 bOpenEvent = false;
170 bForceOpenEvent = false;
171 bPrintToEvent = false;
172 bPrintEvent = false;
173 bViewEvent = false;
174 bStartEvent = false;
175 bDisplaySpec = false;
177 else if ( aArg.equalsIgnoreAsciiCase("-o"))
179 // force open documents regardless if they are templates or not
180 bForceOpenEvent = true;
181 bOpenEvent = false;
182 bForceNewEvent = false;
183 bPrintToEvent = false;
184 bPrintEvent = false;
185 bViewEvent = false;
186 bStartEvent = false;
187 bDisplaySpec = false;
189 else if ( oArg.equalsIgnoreAsciiCase("pt"))
191 // Print to special printer
192 bPrintToEvent = true;
193 bPrinterName = true;
194 bPrintEvent = false;
195 bOpenEvent = false;
196 bForceNewEvent = false;
197 bViewEvent = false;
198 bStartEvent = false;
199 bDisplaySpec = false;
200 bForceOpenEvent = false;
202 else if ( aArg.equalsIgnoreAsciiCase("-p"))
204 // Print to default printer
205 bPrintEvent = true;
206 bPrintToEvent = false;
207 bOpenEvent = false;
208 bForceNewEvent = false;
209 bForceOpenEvent = false;
210 bViewEvent = false;
211 bStartEvent = false;
212 bDisplaySpec = false;
214 else if ( oArg.equalsIgnoreAsciiCase("view"))
216 // open in viewmode
217 bOpenEvent = false;
218 bPrintEvent = false;
219 bPrintToEvent = false;
220 bForceNewEvent = false;
221 bForceOpenEvent = false;
222 bViewEvent = true;
223 bStartEvent = false;
224 bDisplaySpec = false;
226 else if ( oArg.equalsIgnoreAsciiCase("show"))
228 // open in viewmode
229 bOpenEvent = false;
230 bViewEvent = false;
231 bStartEvent = true;
232 bPrintEvent = false;
233 bPrintToEvent = false;
234 bForceNewEvent = false;
235 bForceOpenEvent = false;
236 bDisplaySpec = false;
238 else if ( oArg.equalsIgnoreAsciiCase("display"))
240 // set display
241 bOpenEvent = false;
242 bPrintEvent = false;
243 bForceOpenEvent = false;
244 bPrintToEvent = false;
245 bForceNewEvent = false;
246 bViewEvent = false;
247 bStartEvent = false;
248 bDisplaySpec = true;
250 else if ( oArg.equalsIgnoreAsciiCase("language"))
252 bOpenEvent = false;
253 bPrintEvent = false;
254 bForceOpenEvent = false;
255 bPrintToEvent = false;
256 bForceNewEvent = false;
257 bViewEvent = false;
258 bStartEvent = false;
259 bDisplaySpec = false;
261 else if ( oArg.equalsIgnoreAsciiCase("convert-to"))
263 bOpenEvent = false;
264 bConversionEvent = true;
265 bConversionParamsEvent = true;
267 else if ( oArg.equalsIgnoreAsciiCase("print-to-file"))
269 bOpenEvent = false;
270 bBatchPrintEvent = true;
272 else if ( oArg.equalsIgnoreAsciiCase("printer-name") &&
273 bBatchPrintEvent )
275 bBatchPrinterNameEvent = true;
277 else if ( oArg.equalsIgnoreAsciiCase("outdir") &&
278 (bConversionEvent || bBatchPrintEvent) )
280 bConversionOutEvent = true;
282 else
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
286 if (
287 #if defined UNX
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=") &&
294 #endif
295 //ignore additional legacy options that don't do anything anymore
296 !oArg.equalsIgnoreAsciiCase("nocrashreport") &&
297 m_unknown.isEmpty())
299 m_unknown = aArg;
302 else
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;
327 else
329 // handle this argument as a filename
330 if ( bOpenEvent )
332 m_openlist.push_back(aArg);
333 bOpenDoc = true;
335 else if ( bViewEvent )
337 m_viewlist.push_back(aArg);
338 bOpenDoc = true;
340 else if ( bStartEvent )
342 m_startlist.push_back(aArg);
343 bOpenDoc = true;
345 else if ( bPrintEvent )
347 m_printlist.push_back(aArg);
348 bOpenDoc = true;
350 else if ( bPrintToEvent )
352 m_printtolist.push_back(aArg);
353 bOpenDoc = true;
355 else if ( bForceNewEvent )
357 m_forcenewlist.push_back(aArg);
358 bOpenDoc = true;
360 else if ( bForceOpenEvent )
362 m_forceopenlist.push_back(aArg);
363 bOpenDoc = true;
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);
378 if ( bOpenDoc )
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
392 bDeprecated = true;
393 oArg = OUString(aArg.getStr()+1, aArg.getLength()-1);
395 else
397 return false;
400 if ( oArg == "minimized" )
402 m_minimized = true;
404 else if ( oArg == "invisible" )
406 m_invisible = true;
408 else if ( oArg == "norestore" )
410 m_norestore = true;
412 else if ( oArg == "nodefault" )
414 m_nodefault = true;
416 else if ( oArg == "headless" )
418 // Headless means also invisibile, so set this parameter to true!
419 m_headless = true;
420 m_invisible = true;
422 else if ( oArg == "quickstart" )
424 #if defined(ENABLE_QUICKSTART_APPLET)
425 m_quickstart = true;
426 #endif
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" )
444 m_nologo = true;
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 );
453 #endif
454 else if ( oArg == "help" || aArg == "-h" || aArg == "-?" )
456 m_help = true;
458 else if ( oArg == "helpwriter" )
460 m_helpwriter = true;
462 else if ( oArg == "helpcalc" )
464 m_helpcalc = true;
466 else if ( oArg == "helpdraw" )
468 m_helpdraw = true;
470 else if ( oArg == "helpimpress" )
472 m_helpimpress = true;
474 else if ( oArg == "helpbase" )
476 m_helpbase = true;
478 else if ( oArg == "helpbasic" )
480 m_helpbasic = true;
482 else if ( oArg == "helpmath" )
484 m_helpmath = true;
486 else if ( oArg == "version" )
488 m_version = true;
490 else if ( oArg.matchIgnoreAsciiCase("splash-pipe=") )
492 m_splashpipe = true;
494 #ifdef MACOSX
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") )
502 m_psn = true;
503 return true;
505 #endif
506 #ifdef WIN32
507 /* fdo#57203 ignore -Embedding on Windows
508 when LibreOffice is launched by COM+
510 else if ( oArg == "Embedding" )
512 return true;
514 #endif
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" )
537 m_writer = true;
538 m_bDocumentArgs = true;
540 else if ( oArg == "calc" )
542 m_calc = true;
543 m_bDocumentArgs = true;
545 else if ( oArg == "draw" )
547 m_draw = true;
548 m_bDocumentArgs = true;
550 else if ( oArg == "impress" )
552 m_impress = true;
553 m_bDocumentArgs = true;
555 else if ( oArg == "base" )
557 m_base = true;
558 m_bDocumentArgs = true;
560 else if ( oArg == "global" )
562 m_global = true;
563 m_bDocumentArgs = true;
565 else if ( oArg == "math" )
567 m_math = true;
568 m_bDocumentArgs = true;
570 else if ( oArg == "web" )
572 m_web = true;
573 m_bDocumentArgs = true;
575 else
576 return false;
578 if (bDeprecated)
580 OString sArg(OUStringToOString(aArg, osl_getThreadTextEncoding()));
581 fprintf(stderr, "Warning: %s is deprecated. Use -%s instead.\n", sArg.getStr(), sArg.getStr());
583 return true;
586 void CommandLineArgs::InitParamValues()
588 m_minimized = false;
589 m_norestore = false;
590 #ifdef LIBO_HEADLESS
591 m_invisible = true;
592 m_headless = true;
593 #else
594 m_invisible = false;
595 m_headless = false;
596 #endif
597 m_quickstart = false;
598 m_noquickstart = false;
599 m_terminateafterinit = false;
600 m_nofirststartwizard = false;
601 m_nologo = false;
602 m_nolockcheck = false;
603 m_nodefault = false;
604 m_help = false;
605 m_writer = false;
606 m_calc = false;
607 m_draw = false;
608 m_impress = false;
609 m_global = false;
610 m_math = false;
611 m_web = false;
612 m_base = false;
613 m_helpwriter = false;
614 m_helpcalc = false;
615 m_helpdraw = false;
616 m_helpbasic = false;
617 m_helpmath = false;
618 m_helpimpress = false;
619 m_helpbase = false;
620 m_psn = false;
621 m_version = false;
622 m_splashpipe = false;
623 m_bEmpty = true;
624 m_bDocumentArgs = false;
627 bool CommandLineArgs::IsMinimized() const
629 return m_minimized;
632 bool CommandLineArgs::IsInvisible() const
634 return m_invisible;
637 bool CommandLineArgs::IsNoRestore() const
639 return m_norestore;
642 bool CommandLineArgs::IsNoDefault() const
644 return m_nodefault;
647 bool CommandLineArgs::IsHeadless() const
649 return m_headless;
652 bool CommandLineArgs::IsQuickstart() const
654 return m_quickstart;
657 bool CommandLineArgs::IsNoQuickstart() const
659 return m_noquickstart;
662 bool CommandLineArgs::IsTerminateAfterInit() const
664 return m_terminateafterinit;
667 bool CommandLineArgs::IsNoLogo() const
669 return m_nologo;
672 bool CommandLineArgs::IsNoLockcheck() const
674 return m_nolockcheck;
677 bool CommandLineArgs::IsHelp() const
679 return m_help;
681 bool CommandLineArgs::IsHelpWriter() const
683 return m_helpwriter;
686 bool CommandLineArgs::IsHelpCalc() const
688 return m_helpcalc;
691 bool CommandLineArgs::IsHelpDraw() const
693 return m_helpdraw;
696 bool CommandLineArgs::IsHelpImpress() const
698 return m_helpimpress;
701 bool CommandLineArgs::IsHelpBase() const
703 return m_helpbase;
705 bool CommandLineArgs::IsHelpMath() const
707 return m_helpmath;
709 bool CommandLineArgs::IsHelpBasic() const
711 return m_helpbasic;
714 bool CommandLineArgs::IsWriter() const
716 return m_writer;
719 bool CommandLineArgs::IsCalc() const
721 return m_calc;
724 bool CommandLineArgs::IsDraw() const
726 return m_draw;
729 bool CommandLineArgs::IsImpress() const
731 return m_impress;
734 bool CommandLineArgs::IsBase() const
736 return m_base;
739 bool CommandLineArgs::IsGlobal() const
741 return m_global;
744 bool CommandLineArgs::IsMath() const
746 return m_math;
749 bool CommandLineArgs::IsWeb() const
751 return m_web;
754 bool CommandLineArgs::IsVersion() const
756 return m_version;
759 OUString CommandLineArgs::GetUnknown() const
761 return m_unknown;
764 bool CommandLineArgs::HasModuleParam() const
766 return m_writer || m_calc || m_draw || m_impress || m_global || m_math
767 || m_web || m_base;
770 bool CommandLineArgs::HasSplashPipe() const
772 return m_splashpipe;
775 std::vector< OUString > const & CommandLineArgs::GetAccept() const
777 return m_accept;
780 std::vector< OUString > const & CommandLineArgs::GetUnaccept() const
782 return m_unaccept;
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
827 return m_language;
830 std::vector< OUString > const & CommandLineArgs::GetInFilter() const
832 return m_infilter;
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
851 return m_bEmpty;
854 bool CommandLineArgs::WantsToLoadDocument() const
856 return m_bDocumentArgs;
859 OUString CommandLineArgs::GetPidfileName() const
861 return m_pidfile;
864 } // namespace desktop
866 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */