• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
tdecmdlineargs.cpp
1 /*
2  Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include <config.h>
20 
21 #include <sys/param.h>
22 
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #ifdef HAVE_LIMITS_H
30 #include <limits.h>
31 #endif
32 
33 #include <tqdir.h>
34 #include <tqfile.h>
35 #include <tqasciidict.h>
36 #include <tqstrlist.h>
37 
38 #include "tdecmdlineargs.h"
39 #include <tdeaboutdata.h>
40 #include <tdelocale.h>
41 #include <tdeapplication.h>
42 #include <tdeglobal.h>
43 #include <kstringhandler.h>
44 #include <kstaticdeleter.h>
45 
46 #ifdef TQ_WS_X11
47 #define DISPLAY "DISPLAY"
48 #elif defined(TQ_WS_QWS)
49 #define DISPLAY "QWS_DISPLAY"
50 #endif
51 
52 #ifdef TQ_WS_WIN
53 #include <win32_utils.h>
54 #endif
55 
56 template class TQAsciiDict<TQCString>;
57 template class TQPtrList<TDECmdLineArgs>;
58 
59 class TDECmdLineParsedOptions : public TQAsciiDict<TQCString>
60 {
61 public:
62  TDECmdLineParsedOptions()
63  : TQAsciiDict<TQCString>( 7 ) { }
64 
65  // WABA: Huh?
66  // The compiler doesn't find TDECmdLineParsedOptions::write(s) by itself ???
67  // WABA: No, because there is another write function that hides the
68  // write function in the base class even though this function has a
69  // different signature. (obscure C++ feature)
70  TQDataStream& save( TQDataStream &s) const
71  { return TQGDict::write(s); }
72 
73  TQDataStream& load( TQDataStream &s)
74  { return TQGDict::read(s); }
75 
76 protected:
77  virtual TQDataStream& write( TQDataStream &s, TQPtrCollection::Item data) const
78  {
79  TQCString *str = (TQCString *) data;
80  s << (*str);
81  return s;
82  }
83 
84  virtual TQDataStream& read( TQDataStream &s, TQPtrCollection::Item &item)
85  {
86  TQCString *str = new TQCString;
87  s >> (*str);
88  item = (void *)str;
89  return s;
90  }
91 
92 };
93 
94 class TDECmdLineParsedArgs : public TQStrList
95 {
96 public:
97  TDECmdLineParsedArgs()
98  : TQStrList( true ) { }
99  TQDataStream& save( TQDataStream &s) const
100  { return TQGList::write(s); }
101 
102  TQDataStream& load( TQDataStream &s)
103  { return TQGList::read(s); }
104 };
105 
106 
107 class TDECmdLineArgsList: public TQPtrList<TDECmdLineArgs>
108 {
109 public:
110  TDECmdLineArgsList() { }
111 };
112 
113 TDECmdLineArgsList *TDECmdLineArgs::argsList = 0;
114 int TDECmdLineArgs::argc = 0;
115 char **TDECmdLineArgs::argv = 0;
116 char *TDECmdLineArgs::mCwd = 0;
117 static KStaticDeleter <char> mCwdd;
118 const TDEAboutData *TDECmdLineArgs::about = 0;
119 bool TDECmdLineArgs::parsed = false;
120 bool TDECmdLineArgs::ignoreUnknown = false;
121 
122 //
123 // Static functions
124 //
125 
126 void
127 TDECmdLineArgs::init(int _argc, char **_argv, const char *_appname, const char* programName,
128  const char *_description, const char *_version, bool noKApp)
129 {
130  init(_argc, _argv,
131  new TDEAboutData(_appname, programName, _version, _description),
132  noKApp);
133 }
134 
135 void
136 TDECmdLineArgs::init(int _argc, char **_argv, const char *_appname,
137  const char *_description, const char *_version, bool noKApp)
138 {
139  init(_argc, _argv,
140  new TDEAboutData(_appname, _appname, _version, _description),
141  noKApp);
142 }
143 
144 void
145 TDECmdLineArgs::initIgnore(int _argc, char **_argv, const char *_appname )
146 {
147  init(_argc, _argv,
148  new TDEAboutData(_appname, _appname, "unknown", "TDE Application", false));
149  ignoreUnknown = true;
150 }
151 
152 void
153 TDECmdLineArgs::init(const TDEAboutData* ab)
154 {
155  char **_argv = (char **) malloc(sizeof(char *));
156  _argv[0] = (char *) ab->appName();
157  init(1,_argv,ab, true);
158 }
159 
160 
161 void
162 TDECmdLineArgs::init(int _argc, char **_argv, const TDEAboutData *_about, bool noKApp)
163 {
164  argc = _argc;
165  argv = _argv;
166 
167  if (!argv)
168  {
169  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs):\n");
170  fprintf(stderr, "Passing null-pointer to 'argv' is not allowed.\n\n");
171 
172  assert( 0 );
173  exit(255);
174  }
175 
176  // Strip path from argv[0]
177  if (argc) {
178  char *p = strrchr( argv[0], '/');
179  if (p)
180  argv[0] = p+1;
181  }
182 
183  about = _about;
184  parsed = false;
185  mCwd = mCwdd.setObject(mCwd, new char [PATH_MAX+1], true);
186  (void) getcwd(mCwd, PATH_MAX);
187 #ifdef TQ_WS_WIN
188  win32_slashify(mCwd, PATH_MAX);
189 #endif
190  if (!noKApp)
191  TDEApplication::addCmdLineOptions();
192 }
193 
194 TQString TDECmdLineArgs::cwd()
195 {
196  return TQFile::decodeName(TQCString(mCwd));
197 }
198 
199 const char * TDECmdLineArgs::appName()
200 {
201  if (!argc) return 0;
202  return argv[0];
203 }
204 
205 void
206 TDECmdLineArgs::addCmdLineOptions( const TDECmdLineOptions *options, const char *name,
207  const char *id, const char *afterId)
208 {
209  if (!argsList)
210  argsList = new TDECmdLineArgsList();
211 
212  int pos = argsList->count();
213 
214  if (pos && id && argsList->last() && !argsList->last()->name)
215  pos--;
216 
217  TDECmdLineArgs *args;
218  int i = 0;
219  for(args = argsList->first(); args; args = argsList->next(), i++)
220  {
221  if (!id && !args->id)
222  return; // Options already present.
223 
224  if (id && args->id && (::qstrcmp(id, args->id) == 0))
225  return; // Options already present.
226 
227  if (afterId && args->id && (::qstrcmp(afterId, args->id) == 0))
228  pos = i+1;
229  }
230 
231  assert( parsed == false ); // You must add _ALL_ cmd line options
232  // before accessing the arguments!
233  args = new TDECmdLineArgs(options, name, id);
234  argsList->insert(pos, args);
235 }
236 
237 void
238 TDECmdLineArgs::saveAppArgs( TQDataStream &ds)
239 {
240  if (!parsed)
241  parseAllArgs();
242 
243  // Remove Qt and TDE options.
244  removeArgs("qt");
245  removeArgs("tde");
246 
247  TQCString qCwd = mCwd;
248  ds << qCwd;
249 
250  uint count = argsList ? argsList->count() : 0;
251  ds << count;
252 
253  if (!count) return;
254 
255  TDECmdLineArgs *args;
256  for(args = argsList->first(); args; args = argsList->next())
257  {
258  ds << TQCString(args->id);
259  args->save(ds);
260  }
261 }
262 
263 void
264 TDECmdLineArgs::loadAppArgs( TQDataStream &ds)
265 {
266  parsed = true; // don't reparse argc/argv!
267 
268  // Remove Qt and TDE options.
269  removeArgs("qt");
270  removeArgs("tde");
271 
272  TDECmdLineArgs *args;
273  if ( argsList ) {
274  for(args = argsList->first(); args; args = argsList->next())
275  {
276  args->clear();
277  }
278  }
279 
280  if (ds.atEnd())
281  return;
282 
283  TQCString qCwd;
284  ds >> qCwd;
285  delete [] mCwd;
286 
287  mCwd = mCwdd.setObject(mCwd, new char[qCwd.length()+1], true);
288  strncpy(mCwd, qCwd.data(), qCwd.length()+1);
289 
290  uint count;
291  ds >> count;
292 
293  while(count--)
294  {
295  TQCString id;
296  ds >> id;
297  assert( argsList );
298  for(args = argsList->first(); args; args = argsList->next())
299  {
300  if (args->id == id)
301  {
302  args->load(ds);
303  break;
304  }
305  }
306  }
307  parsed = true;
308 }
309 
310 TDECmdLineArgs *TDECmdLineArgs::parsedArgs(const char *id)
311 {
312  TDECmdLineArgs *args = argsList ? argsList->first() : 0;
313  while(args)
314  {
315  if ((id && ::qstrcmp(args->id, id) == 0) || (!id && !args->id))
316  {
317  if (!parsed)
318  parseAllArgs();
319  return args;
320  }
321  args = argsList->next();
322  }
323 
324  return args;
325 }
326 
327 void TDECmdLineArgs::removeArgs(const char *id)
328 {
329  TDECmdLineArgs *args = argsList ? argsList->first() : 0;
330  while(args)
331  {
332  if (args->id && id && ::qstrcmp(args->id, id) == 0)
333  {
334  if (!parsed)
335  parseAllArgs();
336  break;
337  }
338  args = argsList->next();
339  }
340 
341  if (args)
342  delete args;
343 }
344 
345 /*
346  * @return:
347  * 0 - option not found.
348  * 1 - option found // -fork
349  * 2 - inverse option found ('no') // -nofork
350  * 3 - option + arg found // -fork now
351  *
352  * +4 - no more options follow // !fork
353  */
354 static int
355 findOption(const TDECmdLineOptions *options, TQCString &opt,
356  const char *&opt_name, const char *&def, bool &enabled)
357 {
358  int result;
359  bool inverse;
360  int len = opt.length();
361  while(options && options->name)
362  {
363  result = 0;
364  inverse = false;
365  opt_name = options->name;
366  if ((opt_name[0] == ':') || (opt_name[0] == 0))
367  {
368  options++;
369  continue;
370  }
371 
372  if (opt_name[0] == '!')
373  {
374  opt_name++;
375  result = 4;
376  }
377  if ((opt_name[0] == 'n') && (opt_name[1] == 'o'))
378  {
379  opt_name += 2;
380  inverse = true;
381  }
382  if (strncmp(opt.data(), opt_name, len) == 0)
383  {
384  opt_name += len;
385  if (!opt_name[0])
386  {
387  if (inverse)
388  return result+2;
389 
390  if (!options->description)
391  {
392  options++;
393  if (!options->name)
394  return result+0;
395  TQCString nextOption = options->name;
396  int p = nextOption.find(' ');
397  if (p > 0)
398  nextOption = nextOption.left(p);
399  if (nextOption[0] == '!')
400  nextOption = nextOption.mid(1);
401  if (strncmp(nextOption.data(), "no", 2) == 0)
402  {
403  nextOption = nextOption.mid(2);
404  enabled = !enabled;
405  }
406  result = findOption(options, nextOption, opt_name, def, enabled);
407  assert(result);
408  opt = nextOption;
409  return result;
410  }
411 
412  return 1;
413  }
414  if (opt_name[0] == ' ')
415  {
416  opt_name++;
417  def = options->def;
418  return result+3;
419  }
420  }
421 
422  options++;
423  }
424  return 0;
425 }
426 
427 
428 void
429 TDECmdLineArgs::findOption(const char *_opt, TQCString opt, int &i, bool _enabled, bool &moreOptions)
430 {
431  TDECmdLineArgs *args = argsList->first();
432  const char *opt_name;
433  const char *def;
434  TQCString argument;
435  int j = opt.find('=');
436  if (j != -1)
437  {
438  argument = opt.mid(j+1);
439  opt = opt.left(j);
440  }
441 
442  bool enabled = true;
443  int result = 0;
444  while (args)
445  {
446  enabled = _enabled;
447  result = ::findOption(args->options, opt, opt_name, def, enabled);
448  if (result) break;
449  args = argsList->next();
450  }
451  if (!args && (_opt[0] == '-') && _opt[1] && (_opt[1] != '-'))
452  {
453  // Option not found check if it is a valid option
454  // in the style of -Pprinter1 or ps -aux
455  int p = 1;
456  while (true)
457  {
458  TQCString singleCharOption = " ";
459  singleCharOption[0] = _opt[p];
460  args = argsList->first();
461  while (args)
462  {
463  enabled = _enabled;
464  result = ::findOption(args->options, singleCharOption, opt_name, def, enabled);
465  if (result) break;
466  args = argsList->next();
467  }
468  if (!args)
469  break; // Unknown argument
470 
471  p++;
472  if (result == 1) // Single option
473  {
474  args->setOption(singleCharOption, enabled);
475  if (_opt[p])
476  continue; // Next option
477  else
478  return; // Finished
479  }
480  else if (result == 3) // This option takes an argument
481  {
482  if (argument.isEmpty())
483  {
484  argument = _opt+p;
485  }
486  args->setOption(singleCharOption, (const char*)argument);
487  return;
488  }
489  break; // Unknown argument
490  }
491  args = 0;
492  result = 0;
493  }
494 
495  if (!args || !result)
496  {
497  if (ignoreUnknown)
498  return;
499  enable_i18n();
500  usage( i18n("Unknown option '%1'.").arg(TQString::fromLocal8Bit(_opt)));
501  }
502 
503  if ((result & 4) != 0)
504  {
505  result &= ~4;
506  moreOptions = false;
507  }
508 
509  if (result == 3) // This option takes an argument
510  {
511  if (!enabled)
512  {
513  if (ignoreUnknown)
514  return;
515  enable_i18n();
516  usage( i18n("Unknown option '%1'.").arg(TQString::fromLocal8Bit(_opt)));
517  }
518  if (argument.isEmpty())
519  {
520  i++;
521  if (i >= argc)
522  {
523  enable_i18n();
524  usage( i18n("'%1' missing.").arg( opt_name));
525  }
526  argument = argv[i];
527  }
528  args->setOption(opt, (const char*)argument);
529  }
530  else
531  {
532  args->setOption(opt, enabled);
533  }
534 }
535 
536 void
537 TDECmdLineArgs::printQ(const TQString &msg)
538 {
539  TQCString localMsg = msg.local8Bit();
540  fprintf(stdout, "%s", localMsg.data());
541 }
542 
543 void
544 TDECmdLineArgs::parseAllArgs()
545 {
546  bool allowArgs = false;
547  bool inOptions = true;
548  bool everythingAfterArgIsArgs = false;
549  TDECmdLineArgs *appOptions = argsList->last();
550  if (!appOptions->id)
551  {
552  const TDECmdLineOptions *option = appOptions->options;
553  while(option && option->name)
554  {
555  if (option->name[0] == '+')
556  allowArgs = true;
557  if ( option->name[0] == '!' && option->name[1] == '+' )
558  {
559  allowArgs = true;
560  everythingAfterArgIsArgs = true;
561  }
562  option++;
563  }
564  }
565  for(int i = 1; i < argc; i++)
566  {
567  if (!argv[i])
568  continue;
569 
570  if ((argv[i][0] == '-') && argv[i][1] && inOptions)
571  {
572  bool enabled = true;
573  const char *option = &argv[i][1];
574  const char *orig = argv[i];
575  if (option[0] == '-')
576  {
577  option++;
578  argv[i]++;
579  if (!option[0])
580  {
581  inOptions = false;
582  continue;
583  }
584  }
585  if (::qstrcmp(option, "help") == 0)
586  {
587  usage(0);
588  }
589  else if (strncmp(option, "help-",5) == 0)
590  {
591  usage(option+5);
592  }
593  else if ( (::qstrcmp(option, "version") == 0) ||
594  (::qstrcmp(option, "v") == 0))
595  {
596  printQ( TQString("Qt: %1\n").arg(tqVersion()));
597  printQ( TQString("TDE: %1\n").arg(TDE_VERSION_STRING));
598  printQ( TQString("%1: %2\n").
599  arg(about->programName()).arg(about->version()));
600  exit(0);
601  } else if ( (::qstrcmp(option, "license") == 0) )
602  {
603  enable_i18n();
604  printQ( about->license() );
605  printQ( "\n" );
606  exit(0);
607  } else if ( ::qstrcmp( option, "author") == 0 ) {
608  enable_i18n();
609  if ( about ) {
610  const TQValueList<TDEAboutPerson> authors = about->authors();
611  if ( !authors.isEmpty() ) {
612  TQString authorlist;
613  for (TQValueList<TDEAboutPerson>::ConstIterator it = authors.begin(); it != authors.end(); ++it ) {
614  TQString email;
615  if ( !(*it).emailAddress().isEmpty() )
616  email = " <" + (*it).emailAddress() + ">";
617  authorlist += TQString(" ") + (*it).name() + email + "\n";
618  }
619  printQ( i18n("the 2nd argument is a list of name+address, one on each line","%1 was written by\n%2").arg ( TQString(about->programName()) ).arg( authorlist ) );
620  }
621  } else {
622  printQ( i18n("This application was written by somebody who wants to remain anonymous.") );
623  }
624  if (about)
625  {
626  if (!about->customAuthorTextEnabled ())
627  {
628  if (about->bugAddress().isEmpty()
629  || about->bugAddress() == "submit@bugs.trinitydesktop.org"
630  || about->bugAddress() == "http://bugs.trinitydesktop.org" )
631  printQ( i18n( "Please use http://bugs.trinitydesktop.org to report bugs.\n" ) );
632  else {
633  if( about->authors().count() == 1 && about->authors().first().emailAddress() == about->bugAddress() )
634  printQ( i18n( "Please report bugs to %1.\n" ).arg( about->authors().first().emailAddress() ) );
635  else
636  printQ( i18n( "Please report bugs to %1.\n" ).arg(about->bugAddress()) );
637  }
638  }
639  else
640  {
641  printQ(about->customAuthorPlainText());
642  }
643  }
644  exit(0);
645  } else {
646  if ((option[0] == 'n') && (option[1] == 'o'))
647  {
648  option += 2;
649  enabled = false;
650  }
651  findOption(orig, option, i, enabled, inOptions);
652  }
653  }
654  else
655  {
656  // Check whether appOptions allows these arguments
657  if (!allowArgs)
658  {
659  if (ignoreUnknown)
660  continue;
661  enable_i18n();
662  usage( i18n("Unexpected argument '%1'.").arg(TQString::fromLocal8Bit(argv[i])));
663  }
664  else
665  {
666  appOptions->addArgument(argv[i]);
667  if (everythingAfterArgIsArgs)
668  inOptions = false;
669  }
670  }
671  }
672  parsed = true;
673 }
674 
680 int *
681 TDECmdLineArgs::tqt_argc()
682 {
683  if (!argsList)
684  TDEApplication::addCmdLineOptions(); // Lazy bastards!
685 
686  static int tqt_argc = -1;
687  if( tqt_argc != -1 )
688  return &tqt_argc;
689 
690  TDECmdLineArgs *args = parsedArgs("qt");
691  assert(args); // No qt options have been added!
692  if (!argv)
693  {
694  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs):\n");
695  fprintf(stderr, "Application has not called TDECmdLineArgs::init(...).\n\n");
696 
697  assert( 0 );
698  exit(255);
699  }
700 
701  assert(argc >= (args->count()+1));
702  tqt_argc = args->count() +1;
703  return &tqt_argc;
704 }
705 
711 char ***
712 TDECmdLineArgs::tqt_argv()
713 {
714  if (!argsList)
715  TDEApplication::addCmdLineOptions(); // Lazy bastards!
716 
717  static char** tqt_argv;
718  if( tqt_argv != NULL )
719  return &tqt_argv;
720 
721  TDECmdLineArgs *args = parsedArgs("qt");
722  assert(args); // No qt options have been added!
723  if (!argv)
724  {
725  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs):\n");
726  fprintf(stderr, "Application has not called TDECmdLineArgs::init(...).\n\n");
727 
728  assert( 0 );
729  exit(255);
730  }
731 
732  tqt_argv = new char*[ args->count() + 2 ];
733  tqt_argv[ 0 ] = tqstrdup( appName());
734  int i = 0;
735  for(; i < args->count(); i++)
736  {
737  tqt_argv[i+1] = tqstrdup((char *) args->arg(i));
738  }
739  tqt_argv[i+1] = 0;
740 
741  return &tqt_argv;
742 }
743 
744 void
745 TDECmdLineArgs::enable_i18n()
746 {
747  // called twice or too late
748  if (TDEGlobal::_locale)
749  return;
750 
751  if (!TDEGlobal::_instance) {
752  TDEInstance *instance = new TDEInstance(about);
753  (void) instance->config();
754  // Don't delete instance!
755  }
756 }
757 
758 void
759 TDECmdLineArgs::usage(const TQString &error)
760 {
761  assert(TDEGlobal::_locale);
762  TQCString localError = error.local8Bit();
763  if (localError[error.length()-1] == '\n')
764  localError = localError.left(error.length()-1);
765  fprintf(stderr, "%s: %s\n", argv[0], localError.data());
766 
767  TQString tmp = i18n("Use --help to get a list of available command line options.");
768  localError = tmp.local8Bit();
769  fprintf(stderr, "%s: %s\n", argv[0], localError.data());
770  exit(254);
771 }
772 
773 void
774 TDECmdLineArgs::usage(const char *id)
775 {
776  enable_i18n();
777  assert(argsList != 0); // It's an error to call usage(...) without
778  // having done addCmdLineOptions first!
779 
780  TQString optionFormatString = " %1 %2\n";
781  TQString optionFormatStringDef = " %1 %2 [%3]\n";
782  TQString optionHeaderString = i18n("\n%1:\n");
783  TQString tmp;
784  TQString usage;
785 
786  TDECmdLineArgs *args = argsList->last();
787 
788  if (!(args->id) && (args->options) &&
789  (args->options->name) && (args->options->name[0] != '+'))
790  {
791  usage = i18n("[options] ")+usage;
792  }
793 
794  while(args)
795  {
796  if (args->name)
797  {
798  usage = i18n("[%1-options]").arg(args->name)+" "+usage;
799  }
800  args = argsList->prev();
801  }
802 
803  TDECmdLineArgs *appOptions = argsList->last();
804  if (!appOptions->id)
805  {
806  const TDECmdLineOptions *option = appOptions->options;
807  while(option && option->name)
808  {
809  if (option->name[0] == '+')
810  usage = usage + (option->name+1) + " ";
811  else if ( option->name[0] == '!' && option->name[1] == '+' )
812  usage = usage + (option->name+2) + " ";
813 
814  option++;
815  }
816  }
817 
818  printQ(i18n("Usage: %1 %2\n").arg(argv[0]).arg(usage));
819  printQ("\n"+about->shortDescription()+"\n");
820 
821  printQ(optionHeaderString.arg(i18n("Generic options")));
822  printQ(optionFormatString.arg("--help", -25).arg(i18n("Show help about options")));
823 
824  args = argsList->first();
825  while(args)
826  {
827  if (args->name && args->id)
828  {
829  TQString option = TQString("--help-%1").arg(args->id);
830  TQString desc = i18n("Show %1 specific options").arg(args->name);
831 
832  printQ(optionFormatString.arg(option, -25).arg(desc));
833  }
834  args = argsList->next();
835  }
836 
837  printQ(optionFormatString.arg("--help-all",-25).arg(i18n("Show all options")));
838  printQ(optionFormatString.arg("--author",-25).arg(i18n("Show author information")));
839  printQ(optionFormatString.arg("-v, --version",-25).arg(i18n("Show version information")));
840  printQ(optionFormatString.arg("--license",-25).arg(i18n("Show license information")));
841  printQ(optionFormatString.arg("--", -25).arg(i18n("End of options")));
842 
843  args = argsList->first(); // Sets current to 1st.
844 
845  bool showAll = id && (::qstrcmp(id, "all") == 0);
846 
847  if (!showAll)
848  {
849  while(args)
850  {
851  if (!id && !args->id) break;
852  if (id && (::qstrcmp(args->id, id) == 0)) break;
853  args = argsList->next();
854  }
855  }
856 
857  while(args)
858  {
859  bool hasArgs = false;
860  bool hasOptions = false;
861  TQString optionsHeader;
862  if (args->name)
863  optionsHeader = optionHeaderString.arg(i18n("%1 options").arg(TQString::fromLatin1(args->name)));
864  else
865  optionsHeader = i18n("\nOptions:\n");
866 
867  while (args)
868  {
869  const TDECmdLineOptions *option = args->options;
870  TQCString opt = "";
871 //
872  while(option && option->name)
873  {
874  TQString description;
875  TQString descriptionRest;
876  TQStringList dl;
877 
878  // Option header
879  if (option->name[0] == ':')
880  {
881  if (option->description)
882  {
883  optionsHeader = "\n"+i18n(option->description);
884  if (!optionsHeader.endsWith("\n"))
885  optionsHeader.append("\n");
886  hasOptions = false;
887  }
888  option++;
889  continue;
890  }
891 
892  // Free-form comment
893  if (option->name[0] == 0)
894  {
895  if (option->description)
896  {
897  TQString tmp = "\n"+i18n(option->description);
898  if (!tmp.endsWith("\n"))
899  tmp.append("\n");
900  printQ(tmp);
901  }
902  option++;
903  continue;
904  }
905 
906  // Options
907  if (option->description)
908  {
909  description = i18n(option->description);
910  dl = TQStringList::split("\n", description, true);
911  description = dl.first();
912  dl.remove( dl.begin() );
913  }
914  TQCString name = option->name;
915  if (name[0] == '!')
916  name = name.mid(1);
917 
918  if (name[0] == '+')
919  {
920  if (!hasArgs)
921  {
922  printQ(i18n("\nArguments:\n"));
923  hasArgs = true;
924  }
925 
926  name = name.mid(1);
927  if ((name[0] == '[') && (name[name.length()-1] == ']'))
928  name = name.mid(1, name.length()-2);
929  printQ(optionFormatString.arg(TQString(name), -25)
930  .arg(description));
931  }
932  else
933  {
934  if (!hasOptions)
935  {
936  printQ(optionsHeader);
937  hasOptions = true;
938  }
939 
940  if ((name.length() == 1) || (name[1] == ' '))
941  name = "-"+name;
942  else
943  name = "--"+name;
944  if (!option->description)
945  {
946  opt = name + ", ";
947  }
948  else
949  {
950  opt = opt + name;
951  if (!option->def)
952  {
953  printQ(optionFormatString.arg(TQString(opt), -25)
954  .arg(description));
955  }
956  else
957  {
958  printQ(optionFormatStringDef.arg(TQString(opt), -25)
959  .arg(description).arg(option->def));
960  }
961  opt = "";
962  }
963  }
964  for(TQStringList::Iterator it = dl.begin();
965  it != dl.end();
966  ++it)
967  {
968  printQ(optionFormatString.arg("", -25).arg(*it));
969  }
970 
971  option++;
972  }
973  args = argsList->next();
974  if (!args || args->name || !args->id) break;
975  }
976  if (!showAll) break;
977  }
978 
979  exit(254);
980 }
981 
982 //
983 // Member functions
984 //
985 
991 TDECmdLineArgs::TDECmdLineArgs( const TDECmdLineOptions *_options,
992  const char *_name, const char *_id)
993  : options(_options), name(_name), id(_id)
994 {
995  parsedOptionList = 0;
996  parsedArgList = 0;
997  isQt = (::qstrcmp(id, "qt") == 0);
998 }
999 
1003 TDECmdLineArgs::~TDECmdLineArgs()
1004 {
1005  delete parsedOptionList;
1006  delete parsedArgList;
1007  if (argsList)
1008  argsList->removeRef(this);
1009 }
1010 
1011 void
1012 TDECmdLineArgs::clear()
1013 {
1014  delete parsedArgList;
1015  parsedArgList = 0;
1016  delete parsedOptionList;
1017  parsedOptionList = 0;
1018 }
1019 
1020 void
1021 TDECmdLineArgs::reset()
1022 {
1023  if ( argsList ) {
1024  argsList->setAutoDelete( true );
1025  argsList->clear();
1026  delete argsList;
1027  argsList = 0;
1028  }
1029  parsed = false;
1030 }
1031 
1032 void
1033 TDECmdLineArgs::save( TQDataStream &ds) const
1034 {
1035  uint count = 0;
1036  if (parsedOptionList)
1037  parsedOptionList->save( ds );
1038  else
1039  ds << count;
1040 
1041  if (parsedArgList)
1042  parsedArgList->save( ds );
1043  else
1044  ds << count;
1045 }
1046 
1047 void
1048 TDECmdLineArgs::load( TQDataStream &ds)
1049 {
1050  if (!parsedOptionList) parsedOptionList = new TDECmdLineParsedOptions;
1051  if (!parsedArgList) parsedArgList = new TDECmdLineParsedArgs;
1052 
1053  parsedOptionList->load( ds );
1054  parsedArgList->load( ds );
1055 
1056  if (parsedOptionList->count() == 0)
1057  {
1058  delete parsedOptionList;
1059  parsedOptionList = 0;
1060  }
1061  if (parsedArgList->count() == 0)
1062  {
1063  delete parsedArgList;
1064  parsedArgList = 0;
1065  }
1066 }
1067 
1068 void
1069 TDECmdLineArgs::setOption(const TQCString &opt, bool enabled)
1070 {
1071  if (isQt)
1072  {
1073  // Qt does it own parsing.
1074  TQCString arg = "-";
1075  if( !enabled )
1076  arg += "no";
1077  arg += opt;
1078  addArgument(arg);
1079  }
1080  if (!parsedOptionList) {
1081  parsedOptionList = new TDECmdLineParsedOptions;
1082  parsedOptionList->setAutoDelete(true);
1083  }
1084 
1085  if (enabled)
1086  parsedOptionList->replace( opt, new TQCString("t") );
1087  else
1088  parsedOptionList->replace( opt, new TQCString("f") );
1089 }
1090 
1091 void
1092 TDECmdLineArgs::setOption(const TQCString &opt, const char *value)
1093 {
1094  if (isQt)
1095  {
1096  // Qt does it's own parsing.
1097  TQCString arg = "-";
1098  arg += opt;
1099  addArgument(arg);
1100  addArgument(value);
1101 
1102 #ifdef TQ_WS_X11
1103  // Hack coming up!
1104  if (arg == "-display")
1105  {
1106  setenv(DISPLAY, value, true);
1107  }
1108 #endif
1109  }
1110  if (!parsedOptionList) {
1111  parsedOptionList = new TDECmdLineParsedOptions;
1112  parsedOptionList->setAutoDelete(true);
1113  }
1114 
1115  parsedOptionList->insert( opt, new TQCString(value) );
1116 }
1117 
1118 TQCString
1119 TDECmdLineArgs::getOption(const char *_opt) const
1120 {
1121  TQCString *value = 0;
1122  if (parsedOptionList)
1123  {
1124  value = parsedOptionList->find(_opt);
1125  }
1126 
1127  if (value)
1128  return (*value);
1129 
1130  // Look up the default.
1131  const char *opt_name;
1132  const char *def;
1133  bool dummy = true;
1134  TQCString opt = _opt;
1135  int result = ::findOption( options, opt, opt_name, def, dummy) & ~4;
1136 
1137  if (result != 3)
1138  {
1139  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs):\n");
1140  fprintf(stderr, "Application requests for getOption(\"%s\") but the \"%s\" option\n",
1141  _opt, _opt);
1142  fprintf(stderr, "has never been specified via addCmdLineOptions( ... )\n\n");
1143 
1144  assert( 0 );
1145  exit(255);
1146  }
1147  return TQCString(def);
1148 }
1149 
1150 QCStringList
1151 TDECmdLineArgs::getOptionList(const char *_opt) const
1152 {
1153  QCStringList result;
1154  if (!parsedOptionList)
1155  return result;
1156 
1157  while(true)
1158  {
1159  TQCString *value = parsedOptionList->take(_opt);
1160  if (!value)
1161  break;
1162  result.prepend(*value);
1163  delete value;
1164  }
1165 
1166  // Reinsert items in dictionary
1167  // WABA: This is rather silly, but I don't want to add restrictions
1168  // to the API like "you can only call this function once".
1169  // I can't access all items without taking them out of the list.
1170  // So taking them out and then putting them back is the only way.
1171  for(QCStringList::ConstIterator it=result.begin();
1172  it != result.end();
1173  ++it)
1174  {
1175  parsedOptionList->insert(_opt, new TQCString(*it));
1176  }
1177  return result;
1178 }
1179 
1180 bool
1181 TDECmdLineArgs::isSet(const char *_opt) const
1182 {
1183  // Look up the default.
1184  const char *opt_name;
1185  const char *def;
1186  bool dummy = true;
1187  TQCString opt = _opt;
1188  int result = ::findOption( options, opt, opt_name, def, dummy) & ~4;
1189 
1190  if (result == 0)
1191  {
1192  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs):\n");
1193  fprintf(stderr, "Application requests for isSet(\"%s\") but the \"%s\" option\n",
1194  _opt, _opt);
1195  fprintf(stderr, "has never been specified via addCmdLineOptions( ... )\n\n");
1196 
1197  assert( 0 );
1198  exit(255);
1199  }
1200 
1201  TQCString *value = 0;
1202  if (parsedOptionList)
1203  {
1204  value = parsedOptionList->find(opt);
1205  }
1206 
1207  if (value)
1208  {
1209  if (result == 3)
1210  return true;
1211  else
1212  return ((*value)[0] == 't');
1213  }
1214 
1215  if (result == 3)
1216  return false; // String option has 'false' as default.
1217 
1218  // We return 'true' as default if the option was listed as '-nofork'
1219  // We return 'false' as default if the option was listed as '-fork'
1220  return (result == 2);
1221 }
1222 
1223 int
1224 TDECmdLineArgs::count() const
1225 {
1226  if (!parsedArgList)
1227  return 0;
1228  return parsedArgList->count();
1229 }
1230 
1231 const char *
1232 TDECmdLineArgs::arg(int n) const
1233 {
1234  if (!parsedArgList || (n >= (int) parsedArgList->count()))
1235  {
1236  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs): Argument out of bounds\n");
1237  fprintf(stderr, "Application requests for arg(%d) without checking count() first.\n",
1238  n);
1239 
1240  assert( 0 );
1241  exit(255);
1242  }
1243 
1244  return parsedArgList->at(n);
1245 }
1246 
1247 KURL
1248 TDECmdLineArgs::url(int n) const
1249 {
1250  return makeURL( arg(n) );
1251 }
1252 
1253 KURL TDECmdLineArgs::makeURL(const char *_urlArg)
1254 {
1255  const TQString urlArg = TQFile::decodeName(_urlArg);
1256  TQFileInfo fileInfo(urlArg);
1257  if (!fileInfo.isRelative()) { // i.e. starts with '/', on unix
1258  KURL result;
1259  result.setPath(urlArg);
1260  return result; // Absolute path.
1261  }
1262 
1263  if ( KURL::isRelativeURL(urlArg) || fileInfo.exists() ) {
1264  KURL result;
1265  result.setPath( cwd()+'/'+urlArg );
1266  result.cleanPath();
1267  return result; // Relative path
1268  }
1269 
1270  return KURL(urlArg); // Argument is a URL
1271 }
1272 
1273 void
1274 TDECmdLineArgs::addArgument(const char *argument)
1275 {
1276  if (!parsedArgList)
1277  parsedArgList = new TDECmdLineParsedArgs;
1278 
1279  parsedArgList->append(argument);
1280 }
1281 
1282 static const TDECmdLineOptions kde_tempfile_option[] =
1283 {
1284  { "tempfile", I18N_NOOP("The files/URLs opened by the application will be deleted after use"), 0},
1285  TDECmdLineLastOption
1286 };
1287 
1288 void
1289 TDECmdLineArgs::addTempFileOption()
1290 {
1291  TDECmdLineArgs::addCmdLineOptions( kde_tempfile_option, "TDE-tempfile", "tde-tempfile" );
1292 }
1293 
1294 bool TDECmdLineArgs::isTempFileSet()
1295 {
1296  TDECmdLineArgs* args = TDECmdLineArgs::parsedArgs( "tde-tempfile" );
1297  if ( args )
1298  return args->isSet( "tempfile" );
1299  return false;
1300 }
KStaticDeleter
Little helper class to clean up static objects that are held as pointer.
Definition: kstaticdeleter.h:74
KURL
Represents and parses a URL.
Definition: kurl.h:128
KURL::isRelativeURL
static bool isRelativeURL(const TQString &_url)
Tests if a given URL is a relative as opposed to an absolute URL.
Definition: kurl.cpp:409
KURL::setPath
void setPath(const TQString &path)
Sets the decoded path of the URL.
Definition: kurl.cpp:2025
KURL::cleanPath
void cleanPath(bool cleanDirSeparator=true)
Resolves "." and ".." components in path.
Definition: kurl.cpp:1288
TDEAboutData
This class is used to store information about a program.
Definition: tdeaboutdata.h:183
TDEAboutData::authors
const TQValueList< TDEAboutPerson > authors() const
Returns a list of authors.
Definition: tdeaboutdata.cpp:324
TDEAboutData::shortDescription
TQString shortDescription() const
Returns a short, translated description.
Definition: tdeaboutdata.cpp:303
TDEAboutData::license
TQString license() const
Returns the license.
Definition: tdeaboutdata.cpp:408
TDEAboutData::customAuthorPlainText
TQString customAuthorPlainText() const
Returns the plain text displayed around the list of authors instead of the default message telling us...
Definition: tdeaboutdata.cpp:492
TDEAboutData::bugAddress
TQString bugAddress() const
Returns the email address for bugs.
Definition: tdeaboutdata.cpp:318
TDEAboutData::version
TQString version() const
Returns the program's version.
Definition: tdeaboutdata.cpp:297
TDEAboutData::appName
const char * appName() const
Returns the application's internal name.
Definition: tdeaboutdata.cpp:237
TDEAboutData::programName
TQString programName() const
Returns the translated program name.
Definition: tdeaboutdata.cpp:252
TDEAboutData::customAuthorTextEnabled
bool customAuthorTextEnabled() const
Returns whether custom text should be displayed around the list of authors.
Definition: tdeaboutdata.cpp:504
TDEApplication::addCmdLineOptions
static void addCmdLineOptions()
Add Qt and KDE command line options to TDECmdLineArgs.
Definition: tdeapplication.cpp:1690
TDECmdLineArgs
A class for command-line argument handling.
Definition: tdecmdlineargs.h:223
TDECmdLineArgs::isSet
bool isSet(const char *option) const
Read out a boolean option or check for the presence of string option.
Definition: tdecmdlineargs.cpp:1181
TDECmdLineArgs::enable_i18n
static void enable_i18n()
Enable i18n to be able to print a translated error message.
Definition: tdecmdlineargs.cpp:745
TDECmdLineArgs::appName
static const char * appName()
Get the appname according to argv[0].
Definition: tdecmdlineargs.cpp:199
TDECmdLineArgs::getOptionList
QCStringList getOptionList(const char *option) const
Read out all occurrences of a string option.
Definition: tdecmdlineargs.cpp:1151
TDECmdLineArgs::isTempFileSet
static bool isTempFileSet()
Definition: tdecmdlineargs.cpp:1294
TDECmdLineArgs::parsedArgs
static TDECmdLineArgs * parsedArgs(const char *id=0)
Access parsed arguments.
Definition: tdecmdlineargs.cpp:310
TDECmdLineArgs::clear
void clear()
Clear all options and arguments.
Definition: tdecmdlineargs.cpp:1012
TDECmdLineArgs::makeURL
static KURL makeURL(const char *urlArg)
Used by url().
Definition: tdecmdlineargs.cpp:1253
TDECmdLineArgs::addCmdLineOptions
static void addCmdLineOptions(const TDECmdLineOptions *options, const char *name=0, const char *id=0, const char *afterId=0)
Add options to your application.
Definition: tdecmdlineargs.cpp:206
TDECmdLineArgs::url
KURL url(int n) const
Read out an argument representing a URL.
Definition: tdecmdlineargs.cpp:1248
TDECmdLineArgs::loadAppArgs
static void loadAppArgs(TQDataStream &)
Load arguments from a stream.
Definition: tdecmdlineargs.cpp:264
TDECmdLineArgs::getOption
TQCString getOption(const char *option) const
Read out a string option.
Definition: tdecmdlineargs.cpp:1119
TDECmdLineArgs::addTempFileOption
static void addTempFileOption()
Add standard option –tempfile.
Definition: tdecmdlineargs.cpp:1289
TDECmdLineArgs::reset
static void reset()
Reset all option definitions, i.e.
Definition: tdecmdlineargs.cpp:1021
TDECmdLineArgs::TDECmdLineArgs
TDECmdLineArgs(const TDECmdLineOptions *_options, const char *_name, const char *_id)
Constructor.
Definition: tdecmdlineargs.cpp:991
TDECmdLineArgs::arg
const char * arg(int n) const
Read out an argument.
Definition: tdecmdlineargs.cpp:1232
TDECmdLineArgs::count
int count() const
Read the number of arguments that aren't options (but, for example, filenames).
Definition: tdecmdlineargs.cpp:1224
TDECmdLineArgs::cwd
static TQString cwd()
Get the CWD (Current Working Directory) associated with the current command line arguments.
Definition: tdecmdlineargs.cpp:194
TDECmdLineArgs::init
static void init(int _argc, char **_argv, const char *_appname, const char *programName, const char *_description, const char *_version, bool noKApp=false)
Initialize class.
Definition: tdecmdlineargs.cpp:127
TDECmdLineArgs::usage
static void usage(const char *id=0)
Print the usage help to stdout and exit.
Definition: tdecmdlineargs.cpp:774
TDECmdLineArgs::~TDECmdLineArgs
~TDECmdLineArgs()
Destructor.
Definition: tdecmdlineargs.cpp:1003
TDEInstance
Access to KDE global objects for use in shared libraries.
Definition: kinstance.h:48
TDEInstance::config
TDEConfig * config() const
Returns the general config object ("appnamerc").
Definition: kinstance.cpp:212
TDELocale::I18N_NOOP
#define I18N_NOOP(x)
I18N_NOOP marks a string to be translated without translating it.
Definition: tdelocale.h:51
KStdAction::save
TDEAction * save(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDECmdLineOptions
Structure that holds command line options.
Definition: tdecmdlineargs.h:41
TDECmdLineOptions::name
const char * name
The name of the argument as it should be called on the command line and appear in myapp –help.
Definition: tdecmdlineargs.h:55
TDECmdLineOptions::def
const char * def
The default value for the option, if it is not specified on the command line.
Definition: tdecmdlineargs.h:65
TDECmdLineOptions::description
const char * description
The text description of the option as should appear in myapp –help.
Definition: tdecmdlineargs.h:60
tdelocale.h

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.