• 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 Q_WS_X11
47 #define DISPLAY "DISPLAY"
48 #elif defined(Q_WS_QWS)
49 #define DISPLAY "QWS_DISPLAY"
50 #endif
51 
52 #ifdef Q_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 Q_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() || about->bugAddress() == "bugs.pearsoncomputing.net" )
629  printQ( i18n( "Please use http://bugs.pearsoncomputing.net to report bugs.\n" ) );
630  else {
631  if( about->authors().count() == 1 && about->authors().first().emailAddress() == about->bugAddress() )
632  printQ( i18n( "Please report bugs to %1.\n" ).arg( about->authors().first().emailAddress() ) );
633  else
634  printQ( i18n( "Please report bugs to %1.\n" ).arg(about->bugAddress()) );
635  }
636  }
637  else
638  {
639  printQ(about->customAuthorPlainText());
640  }
641  }
642  exit(0);
643  } else {
644  if ((option[0] == 'n') && (option[1] == 'o'))
645  {
646  option += 2;
647  enabled = false;
648  }
649  findOption(orig, option, i, enabled, inOptions);
650  }
651  }
652  else
653  {
654  // Check whether appOptions allows these arguments
655  if (!allowArgs)
656  {
657  if (ignoreUnknown)
658  continue;
659  enable_i18n();
660  usage( i18n("Unexpected argument '%1'.").arg(TQString::fromLocal8Bit(argv[i])));
661  }
662  else
663  {
664  appOptions->addArgument(argv[i]);
665  if (everythingAfterArgIsArgs)
666  inOptions = false;
667  }
668  }
669  }
670  parsed = true;
671 }
672 
678 int *
679 TDECmdLineArgs::tqt_argc()
680 {
681  if (!argsList)
682  TDEApplication::addCmdLineOptions(); // Lazy bastards!
683 
684  static int tqt_argc = -1;
685  if( tqt_argc != -1 )
686  return &tqt_argc;
687 
688  TDECmdLineArgs *args = parsedArgs("qt");
689  assert(args); // No qt options have been added!
690  if (!argv)
691  {
692  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs):\n");
693  fprintf(stderr, "Application has not called TDECmdLineArgs::init(...).\n\n");
694 
695  assert( 0 );
696  exit(255);
697  }
698 
699  assert(argc >= (args->count()+1));
700  tqt_argc = args->count() +1;
701  return &tqt_argc;
702 }
703 
709 char ***
710 TDECmdLineArgs::tqt_argv()
711 {
712  if (!argsList)
713  TDEApplication::addCmdLineOptions(); // Lazy bastards!
714 
715  static char** tqt_argv;
716  if( tqt_argv != NULL )
717  return &tqt_argv;
718 
719  TDECmdLineArgs *args = parsedArgs("qt");
720  assert(args); // No qt options have been added!
721  if (!argv)
722  {
723  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs):\n");
724  fprintf(stderr, "Application has not called TDECmdLineArgs::init(...).\n\n");
725 
726  assert( 0 );
727  exit(255);
728  }
729 
730  tqt_argv = new char*[ args->count() + 2 ];
731  tqt_argv[ 0 ] = tqstrdup( appName());
732  int i = 0;
733  for(; i < args->count(); i++)
734  {
735  tqt_argv[i+1] = tqstrdup((char *) args->arg(i));
736  }
737  tqt_argv[i+1] = 0;
738 
739  return &tqt_argv;
740 }
741 
742 void
743 TDECmdLineArgs::enable_i18n()
744 {
745  // called twice or too late
746  if (TDEGlobal::_locale)
747  return;
748 
749  if (!TDEGlobal::_instance) {
750  TDEInstance *instance = new TDEInstance(about);
751  (void) instance->config();
752  // Don't delete instance!
753  }
754 }
755 
756 void
757 TDECmdLineArgs::usage(const TQString &error)
758 {
759  assert(TDEGlobal::_locale);
760  TQCString localError = error.local8Bit();
761  if (localError[error.length()-1] == '\n')
762  localError = localError.left(error.length()-1);
763  fprintf(stderr, "%s: %s\n", argv[0], localError.data());
764 
765  TQString tmp = i18n("Use --help to get a list of available command line options.");
766  localError = tmp.local8Bit();
767  fprintf(stderr, "%s: %s\n", argv[0], localError.data());
768  exit(254);
769 }
770 
771 void
772 TDECmdLineArgs::usage(const char *id)
773 {
774  enable_i18n();
775  assert(argsList != 0); // It's an error to call usage(...) without
776  // having done addCmdLineOptions first!
777 
778  TQString optionFormatString = " %1 %2\n";
779  TQString optionFormatStringDef = " %1 %2 [%3]\n";
780  TQString optionHeaderString = i18n("\n%1:\n");
781  TQString tmp;
782  TQString usage;
783 
784  TDECmdLineArgs *args = argsList->last();
785 
786  if (!(args->id) && (args->options) &&
787  (args->options->name) && (args->options->name[0] != '+'))
788  {
789  usage = i18n("[options] ")+usage;
790  }
791 
792  while(args)
793  {
794  if (args->name)
795  {
796  usage = i18n("[%1-options]").arg(args->name)+" "+usage;
797  }
798  args = argsList->prev();
799  }
800 
801  TDECmdLineArgs *appOptions = argsList->last();
802  if (!appOptions->id)
803  {
804  const TDECmdLineOptions *option = appOptions->options;
805  while(option && option->name)
806  {
807  if (option->name[0] == '+')
808  usage = usage + (option->name+1) + " ";
809  else if ( option->name[0] == '!' && option->name[1] == '+' )
810  usage = usage + (option->name+2) + " ";
811 
812  option++;
813  }
814  }
815 
816  printQ(i18n("Usage: %1 %2\n").arg(argv[0]).arg(usage));
817  printQ("\n"+about->shortDescription()+"\n");
818 
819  printQ(optionHeaderString.arg(i18n("Generic options")));
820  printQ(optionFormatString.arg("--help", -25).arg(i18n("Show help about options")));
821 
822  args = argsList->first();
823  while(args)
824  {
825  if (args->name && args->id)
826  {
827  TQString option = TQString("--help-%1").arg(args->id);
828  TQString desc = i18n("Show %1 specific options").arg(args->name);
829 
830  printQ(optionFormatString.arg(option, -25).arg(desc));
831  }
832  args = argsList->next();
833  }
834 
835  printQ(optionFormatString.arg("--help-all",-25).arg(i18n("Show all options")));
836  printQ(optionFormatString.arg("--author",-25).arg(i18n("Show author information")));
837  printQ(optionFormatString.arg("-v, --version",-25).arg(i18n("Show version information")));
838  printQ(optionFormatString.arg("--license",-25).arg(i18n("Show license information")));
839  printQ(optionFormatString.arg("--", -25).arg(i18n("End of options")));
840 
841  args = argsList->first(); // Sets current to 1st.
842 
843  bool showAll = id && (::qstrcmp(id, "all") == 0);
844 
845  if (!showAll)
846  {
847  while(args)
848  {
849  if (!id && !args->id) break;
850  if (id && (::qstrcmp(args->id, id) == 0)) break;
851  args = argsList->next();
852  }
853  }
854 
855  while(args)
856  {
857  bool hasArgs = false;
858  bool hasOptions = false;
859  TQString optionsHeader;
860  if (args->name)
861  optionsHeader = optionHeaderString.arg(i18n("%1 options").arg(TQString::fromLatin1(args->name)));
862  else
863  optionsHeader = i18n("\nOptions:\n");
864 
865  while (args)
866  {
867  const TDECmdLineOptions *option = args->options;
868  TQCString opt = "";
869 //
870  while(option && option->name)
871  {
872  TQString description;
873  TQString descriptionRest;
874  TQStringList dl;
875 
876  // Option header
877  if (option->name[0] == ':')
878  {
879  if (option->description)
880  {
881  optionsHeader = "\n"+i18n(option->description);
882  if (!optionsHeader.endsWith("\n"))
883  optionsHeader.append("\n");
884  hasOptions = false;
885  }
886  option++;
887  continue;
888  }
889 
890  // Free-form comment
891  if (option->name[0] == 0)
892  {
893  if (option->description)
894  {
895  TQString tmp = "\n"+i18n(option->description);
896  if (!tmp.endsWith("\n"))
897  tmp.append("\n");
898  printQ(tmp);
899  }
900  option++;
901  continue;
902  }
903 
904  // Options
905  if (option->description)
906  {
907  description = i18n(option->description);
908  dl = TQStringList::split("\n", description, true);
909  description = dl.first();
910  dl.remove( dl.begin() );
911  }
912  TQCString name = option->name;
913  if (name[0] == '!')
914  name = name.mid(1);
915 
916  if (name[0] == '+')
917  {
918  if (!hasArgs)
919  {
920  printQ(i18n("\nArguments:\n"));
921  hasArgs = true;
922  }
923 
924  name = name.mid(1);
925  if ((name[0] == '[') && (name[name.length()-1] == ']'))
926  name = name.mid(1, name.length()-2);
927  printQ(optionFormatString.arg(QString(name), -25)
928  .arg(description));
929  }
930  else
931  {
932  if (!hasOptions)
933  {
934  printQ(optionsHeader);
935  hasOptions = true;
936  }
937 
938  if ((name.length() == 1) || (name[1] == ' '))
939  name = "-"+name;
940  else
941  name = "--"+name;
942  if (!option->description)
943  {
944  opt = name + ", ";
945  }
946  else
947  {
948  opt = opt + name;
949  if (!option->def)
950  {
951  printQ(optionFormatString.arg(QString(opt), -25)
952  .arg(description));
953  }
954  else
955  {
956  printQ(optionFormatStringDef.arg(QString(opt), -25)
957  .arg(description).arg(option->def));
958  }
959  opt = "";
960  }
961  }
962  for(TQStringList::Iterator it = dl.begin();
963  it != dl.end();
964  ++it)
965  {
966  printQ(optionFormatString.arg("", -25).arg(*it));
967  }
968 
969  option++;
970  }
971  args = argsList->next();
972  if (!args || args->name || !args->id) break;
973  }
974  if (!showAll) break;
975  }
976 
977  exit(254);
978 }
979 
980 //
981 // Member functions
982 //
983 
989 TDECmdLineArgs::TDECmdLineArgs( const TDECmdLineOptions *_options,
990  const char *_name, const char *_id)
991  : options(_options), name(_name), id(_id)
992 {
993  parsedOptionList = 0;
994  parsedArgList = 0;
995  isQt = (::qstrcmp(id, "qt") == 0);
996 }
997 
1001 TDECmdLineArgs::~TDECmdLineArgs()
1002 {
1003  delete parsedOptionList;
1004  delete parsedArgList;
1005  if (argsList)
1006  argsList->removeRef(this);
1007 }
1008 
1009 void
1010 TDECmdLineArgs::clear()
1011 {
1012  delete parsedArgList;
1013  parsedArgList = 0;
1014  delete parsedOptionList;
1015  parsedOptionList = 0;
1016 }
1017 
1018 void
1019 TDECmdLineArgs::reset()
1020 {
1021  if ( argsList ) {
1022  argsList->setAutoDelete( true );
1023  argsList->clear();
1024  delete argsList;
1025  argsList = 0;
1026  }
1027  parsed = false;
1028 }
1029 
1030 void
1031 TDECmdLineArgs::save( TQDataStream &ds) const
1032 {
1033  uint count = 0;
1034  if (parsedOptionList)
1035  parsedOptionList->save( ds );
1036  else
1037  ds << count;
1038 
1039  if (parsedArgList)
1040  parsedArgList->save( ds );
1041  else
1042  ds << count;
1043 }
1044 
1045 void
1046 TDECmdLineArgs::load( TQDataStream &ds)
1047 {
1048  if (!parsedOptionList) parsedOptionList = new TDECmdLineParsedOptions;
1049  if (!parsedArgList) parsedArgList = new TDECmdLineParsedArgs;
1050 
1051  parsedOptionList->load( ds );
1052  parsedArgList->load( ds );
1053 
1054  if (parsedOptionList->count() == 0)
1055  {
1056  delete parsedOptionList;
1057  parsedOptionList = 0;
1058  }
1059  if (parsedArgList->count() == 0)
1060  {
1061  delete parsedArgList;
1062  parsedArgList = 0;
1063  }
1064 }
1065 
1066 void
1067 TDECmdLineArgs::setOption(const TQCString &opt, bool enabled)
1068 {
1069  if (isQt)
1070  {
1071  // Qt does it own parsing.
1072  TQCString arg = "-";
1073  if( !enabled )
1074  arg += "no";
1075  arg += opt;
1076  addArgument(arg);
1077  }
1078  if (!parsedOptionList) {
1079  parsedOptionList = new TDECmdLineParsedOptions;
1080  parsedOptionList->setAutoDelete(true);
1081  }
1082 
1083  if (enabled)
1084  parsedOptionList->replace( opt, new TQCString("t") );
1085  else
1086  parsedOptionList->replace( opt, new TQCString("f") );
1087 }
1088 
1089 void
1090 TDECmdLineArgs::setOption(const TQCString &opt, const char *value)
1091 {
1092  if (isQt)
1093  {
1094  // Qt does it's own parsing.
1095  TQCString arg = "-";
1096  arg += opt;
1097  addArgument(arg);
1098  addArgument(value);
1099 
1100 #ifdef Q_WS_X11
1101  // Hack coming up!
1102  if (arg == "-display")
1103  {
1104  setenv(DISPLAY, value, true);
1105  }
1106 #endif
1107  }
1108  if (!parsedOptionList) {
1109  parsedOptionList = new TDECmdLineParsedOptions;
1110  parsedOptionList->setAutoDelete(true);
1111  }
1112 
1113  parsedOptionList->insert( opt, new TQCString(value) );
1114 }
1115 
1116 TQCString
1117 TDECmdLineArgs::getOption(const char *_opt) const
1118 {
1119  TQCString *value = 0;
1120  if (parsedOptionList)
1121  {
1122  value = parsedOptionList->find(_opt);
1123  }
1124 
1125  if (value)
1126  return (*value);
1127 
1128  // Look up the default.
1129  const char *opt_name;
1130  const char *def;
1131  bool dummy = true;
1132  TQCString opt = _opt;
1133  int result = ::findOption( options, opt, opt_name, def, dummy) & ~4;
1134 
1135  if (result != 3)
1136  {
1137  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs):\n");
1138  fprintf(stderr, "Application requests for getOption(\"%s\") but the \"%s\" option\n",
1139  _opt, _opt);
1140  fprintf(stderr, "has never been specified via addCmdLineOptions( ... )\n\n");
1141 
1142  assert( 0 );
1143  exit(255);
1144  }
1145  return TQCString(def);
1146 }
1147 
1148 QCStringList
1149 TDECmdLineArgs::getOptionList(const char *_opt) const
1150 {
1151  QCStringList result;
1152  if (!parsedOptionList)
1153  return result;
1154 
1155  while(true)
1156  {
1157  TQCString *value = parsedOptionList->take(_opt);
1158  if (!value)
1159  break;
1160  result.prepend(*value);
1161  delete value;
1162  }
1163 
1164  // Reinsert items in dictionary
1165  // WABA: This is rather silly, but I don't want to add restrictions
1166  // to the API like "you can only call this function once".
1167  // I can't access all items without taking them out of the list.
1168  // So taking them out and then putting them back is the only way.
1169  for(QCStringList::ConstIterator it=result.begin();
1170  it != result.end();
1171  ++it)
1172  {
1173  parsedOptionList->insert(_opt, new TQCString(*it));
1174  }
1175  return result;
1176 }
1177 
1178 bool
1179 TDECmdLineArgs::isSet(const char *_opt) const
1180 {
1181  // Look up the default.
1182  const char *opt_name;
1183  const char *def;
1184  bool dummy = true;
1185  TQCString opt = _opt;
1186  int result = ::findOption( options, opt, opt_name, def, dummy) & ~4;
1187 
1188  if (result == 0)
1189  {
1190  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs):\n");
1191  fprintf(stderr, "Application requests for isSet(\"%s\") but the \"%s\" option\n",
1192  _opt, _opt);
1193  fprintf(stderr, "has never been specified via addCmdLineOptions( ... )\n\n");
1194 
1195  assert( 0 );
1196  exit(255);
1197  }
1198 
1199  TQCString *value = 0;
1200  if (parsedOptionList)
1201  {
1202  value = parsedOptionList->find(opt);
1203  }
1204 
1205  if (value)
1206  {
1207  if (result == 3)
1208  return true;
1209  else
1210  return ((*value)[0] == 't');
1211  }
1212 
1213  if (result == 3)
1214  return false; // String option has 'false' as default.
1215 
1216  // We return 'true' as default if the option was listed as '-nofork'
1217  // We return 'false' as default if the option was listed as '-fork'
1218  return (result == 2);
1219 }
1220 
1221 int
1222 TDECmdLineArgs::count() const
1223 {
1224  if (!parsedArgList)
1225  return 0;
1226  return parsedArgList->count();
1227 }
1228 
1229 const char *
1230 TDECmdLineArgs::arg(int n) const
1231 {
1232  if (!parsedArgList || (n >= (int) parsedArgList->count()))
1233  {
1234  fprintf(stderr, "\n\nFAILURE (TDECmdLineArgs): Argument out of bounds\n");
1235  fprintf(stderr, "Application requests for arg(%d) without checking count() first.\n",
1236  n);
1237 
1238  assert( 0 );
1239  exit(255);
1240  }
1241 
1242  return parsedArgList->at(n);
1243 }
1244 
1245 KURL
1246 TDECmdLineArgs::url(int n) const
1247 {
1248  return makeURL( arg(n) );
1249 }
1250 
1251 KURL TDECmdLineArgs::makeURL(const char *_urlArg)
1252 {
1253  const TQString urlArg = TQFile::decodeName(_urlArg);
1254  TQFileInfo fileInfo(urlArg);
1255  if (!fileInfo.isRelative()) { // i.e. starts with '/', on unix
1256  KURL result;
1257  result.setPath(urlArg);
1258  return result; // Absolute path.
1259  }
1260 
1261  if ( KURL::isRelativeURL(urlArg) || fileInfo.exists() ) {
1262  KURL result;
1263  result.setPath( cwd()+'/'+urlArg );
1264  result.cleanPath();
1265  return result; // Relative path
1266  }
1267 
1268  return KURL(urlArg); // Argument is a URL
1269 }
1270 
1271 void
1272 TDECmdLineArgs::addArgument(const char *argument)
1273 {
1274  if (!parsedArgList)
1275  parsedArgList = new TDECmdLineParsedArgs;
1276 
1277  parsedArgList->append(argument);
1278 }
1279 
1280 static const TDECmdLineOptions kde_tempfile_option[] =
1281 {
1282  { "tempfile", I18N_NOOP("The files/URLs opened by the application will be deleted after use"), 0},
1283  TDECmdLineLastOption
1284 };
1285 
1286 void
1287 TDECmdLineArgs::addTempFileOption()
1288 {
1289  TDECmdLineArgs::addCmdLineOptions( kde_tempfile_option, "TDE-tempfile", "tde-tempfile" );
1290 }
1291 
1292 bool TDECmdLineArgs::isTempFileSet()
1293 {
1294  TDECmdLineArgs* args = TDECmdLineArgs::parsedArgs( "tde-tempfile" );
1295  if ( args )
1296  return args->isSet( "tempfile" );
1297  return false;
1298 }
KURL
Represents and parses a URL.
Definition: kurl.h:127
TDECmdLineArgs
A class for command-line argument handling.
Definition: tdecmdlineargs.h:222
TDECmdLineArgs::reset
static void reset()
Reset all option definitions, i.e.
Definition: tdecmdlineargs.cpp:1019
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
TDECmdLineArgs::clear
void clear()
Clear all options and arguments.
Definition: tdecmdlineargs.cpp:1010
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
KStaticDeleter
Little helper class to clean up static objects that are held as pointer.
Definition: kstaticdeleter.h:74
TDEAboutData
This class is used to store information about a program.
Definition: tdeaboutdata.h:182
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::appName
static const char * appName()
Get the appname according to argv[0].
Definition: tdecmdlineargs.cpp:199
TDECmdLineArgs::url
KURL url(int n) const
Read out an argument representing a URL.
Definition: tdecmdlineargs.cpp:1246
TDECmdLineOptions::description
const char * description
The text description of the option as should appear in myapp –help.
Definition: tdecmdlineargs.h:60
TDEAboutData::appName
const char * appName() const
Returns the application&#39;s internal name.
Definition: tdeaboutdata.cpp:237
KStaticDeleter::setObject
KDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
Sets the object to delete and registers the object to be deleted to TDEGlobal.
Definition: kstaticdeleter.h:85
TDECmdLineArgs::getOption
TQCString getOption(const char *option) const
Read out a string option.
Definition: tdecmdlineargs.cpp:1117
TDECmdLineArgs::usage
static void usage(const char *id=0)
Print the usage help to stdout and exit.
Definition: tdecmdlineargs.cpp:772
TDECmdLineArgs::loadAppArgs
static void loadAppArgs(TQDataStream &)
Load arguments from a stream.
Definition: tdecmdlineargs.cpp:264
tdelocale.h
TDECmdLineArgs::parsedArgs
static TDECmdLineArgs * parsedArgs(const char *id=0)
Access parsed arguments.
Definition: tdecmdlineargs.cpp:310
KURL::setPath
void setPath(const TQString &path)
Sets the decoded path of the URL.
Definition: kurl.cpp:2025
TDECmdLineArgs::isTempFileSet
static bool isTempFileSet()
Definition: tdecmdlineargs.cpp:1292
TDECmdLineArgs::makeURL
static KURL makeURL(const char *urlArg)
Used by url().
Definition: tdecmdlineargs.cpp:1251
KURL::cleanPath
void cleanPath(bool cleanDirSeparator=true)
Resolves "." and ".." components in path.
Definition: kurl.cpp:1288
KStdAction::save
TDEAction * save(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
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::TDECmdLineArgs
TDECmdLineArgs(const TDECmdLineOptions *_options, const char *_name, const char *_id)
Constructor.
Definition: tdecmdlineargs.cpp:989
TDECmdLineOptions::def
const char * def
The default value for the option, if it is not specified on the command line.
Definition: tdecmdlineargs.h:65
TDEInstance::config
TDEConfig * config() const
Returns the general config object ("appnamerc").
Definition: kinstance.cpp:210
TDECmdLineArgs::addTempFileOption
static void addTempFileOption()
Add standard option –tempfile.
Definition: tdecmdlineargs.cpp:1287
TDECmdLineArgs::~TDECmdLineArgs
~TDECmdLineArgs()
Destructor.
Definition: tdecmdlineargs.cpp:1001
TDECmdLineArgs::count
int count() const
Read the number of arguments that aren&#39;t options (but, for example, filenames).
Definition: tdecmdlineargs.cpp:1222
TDECmdLineArgs::cwd
static TQString cwd()
Get the CWD (Current Working Directory) associated with the current command line arguments.
Definition: tdecmdlineargs.cpp:194
TDECmdLineArgs::getOptionList
QCStringList getOptionList(const char *option) const
Read out all occurrences of a string option.
Definition: tdecmdlineargs.cpp:1149
TDECmdLineArgs::enable_i18n
static void enable_i18n()
Enable i18n to be able to print a translated error message.
Definition: tdecmdlineargs.cpp:743
TDEApplication::addCmdLineOptions
static void addCmdLineOptions()
Add Qt and KDE command line options to TDECmdLineArgs.
Definition: tdeapplication.cpp:1692
TDECmdLineArgs::isSet
bool isSet(const char *option) const
Read out a boolean option or check for the presence of string option.
Definition: tdecmdlineargs.cpp:1179
TDEInstance
Access to KDE global objects for use in shared libraries.
Definition: kinstance.h:47
TDECmdLineArgs::arg
const char * arg(int n) const
Read out an argument.
Definition: tdecmdlineargs.cpp:1230
TDECmdLineOptions
Structure that holds command line options.
Definition: tdecmdlineargs.h:40

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.8.11
This website is maintained by Timothy Pearson.