~ubuntu-branches/ubuntu/trusty/openjade1.3/trusty

« back to all changes in this revision

Viewing changes to lib/CmdLineApp.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2002-04-09 00:01:50 UTC
  • Revision ID: james.westby@ubuntu.com-20020409000150-r9rkyalxlhvf9ba3
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 1996 James Clark
 
2
// See the file COPYING for copying permission.
 
3
 
 
4
// Need option registration method that allows derived class to change
 
5
// option names.
 
6
 
 
7
#ifdef __GNUG__
 
8
#pragma implementation
 
9
#endif
 
10
 
 
11
#include "splib.h"
 
12
#include "CmdLineApp.h"
 
13
#include "CmdLineAppMessages.h"
 
14
#include "MessageArg.h"
 
15
#include "ErrnoMessageArg.h"
 
16
#include "Options.h"
 
17
#include "version.h"
 
18
#include "xnew.h"
 
19
#include "macros.h"
 
20
#include "sptchar.h"
 
21
#include "MessageTable.h"
 
22
#include "CodingSystemKit.h"
 
23
 
 
24
#include "ConsoleOutput.h"
 
25
 
 
26
#include <errno.h>
 
27
#include <string.h>
 
28
#include <stdlib.h>
 
29
#include <ctype.h>
 
30
 
 
31
#ifdef SP_HAVE_LOCALE
 
32
#ifdef SP_HAVE_WCHAR
 
33
#include <locale.h>
 
34
#endif
 
35
#endif
 
36
#ifdef SP_HAVE_SETMODE
 
37
#include <fcntl.h>
 
38
#include <io.h>
 
39
#endif
 
40
 
 
41
#include <sys/types.h>
 
42
#ifdef SP_INCLUDE_UNISTD_H
 
43
#include <unistd.h>
 
44
#endif
 
45
#ifdef SP_INCLUDE_IO_H
 
46
#include <io.h>
 
47
#endif
 
48
 
 
49
#ifdef _MSC_VER
 
50
#include <crtdbg.h>
 
51
#endif
 
52
 
 
53
#ifndef SP_DEFAULT_ENCODING
 
54
#ifdef WIN32
 
55
#define SP_DEFAULT_ENCODING SP_T("WINDOWS")
 
56
#else
 
57
#define SP_DEFAULT_ENCODING  SP_T("IS8859-1")
 
58
#endif
 
59
#endif /* not SP_DEFAULT_ENCODING */
 
60
 
 
61
#ifdef SP_NAMESPACE
 
62
namespace SP_NAMESPACE {
 
63
#endif
 
64
 
 
65
static const SP_TCHAR *progName = 0;
 
66
 
 
67
static const SP_TCHAR versionString[] = SP_VERSION;
 
68
 
 
69
static FileOutputByteStream standardOutput(1, 0);
 
70
static FileOutputByteStream standardError(2, 0);
 
71
 
 
72
CmdLineApp::CmdLineApp(const char *requiredInternalCode)
 
73
: errorFile_(0),
 
74
  outputCodingSystem_(0),
 
75
  // Colon at beginning is Posix.2ism that says to return : rather than ? for
 
76
  // missing option argument.
 
77
  optstr_(SP_T(":"), 1),
 
78
  MessageReporter(0),
 
79
  internalCharsetIsDocCharset_(1),
 
80
  codingSystem_(0)
 
81
{
 
82
  initCodingSystem(requiredInternalCode);
 
83
  setMessageStream(makeStdErr());
 
84
  registerOption('b', internalCharsetIsDocCharset_ ? SP_T("bctf") : SP_T("encoding"));
 
85
  registerOption('f', SP_T("error_file"));
 
86
  registerOption('v');
 
87
}
 
88
 
 
89
void CmdLineApp::resetCodingSystemKit()
 
90
{
 
91
  codingSystemKit_ = codingSystemKit_->copy();
 
92
}
 
93
 
 
94
void CmdLineApp::registerOption(AppChar c, const AppChar *argName)
 
95
{
 
96
  optstr_ += c;
 
97
  if (argName) {
 
98
    optstr_ += SP_T(':');
 
99
    optArgNames_.push_back(argName);
 
100
  }
 
101
}
 
102
 
 
103
StringC CmdLineApp::usageString()
 
104
{
 
105
  String<AppChar> result;
 
106
  if (progName)
 
107
    result.assign(progName, tcslen(progName));
 
108
  PackedBoolean hadOption[128];
 
109
  for (int i = 0; i < 128; i++)
 
110
    hadOption[i] = 0;
 
111
  Boolean hadNoArgOption = 0;
 
112
  for (size_t i = 1; i < optstr_.size(); i++) {
 
113
    if (optstr_[i] == 0)
 
114
      break;
 
115
    if (i + 1 < optstr_.size() && optstr_[i + 1] == ':')
 
116
      i++;
 
117
    else if (!hadOption[optstr_[i]]) {
 
118
      hadOption[optstr_[i]] = 1;
 
119
      if (!hadNoArgOption) {
 
120
        hadNoArgOption = 1;
 
121
        result.append(SP_T(" [-"), 3);
 
122
      }
 
123
      result += optstr_[i];
 
124
    }
 
125
  }
 
126
  if (hadNoArgOption)
 
127
    result += SP_T(']');
 
128
  size_t j = 0;
 
129
  for (size_t i = 1; i < optstr_.size(); i++) {
 
130
    if (i + 1 < optstr_.size() && optstr_[i + 1] == ':') {
 
131
      if (!hadOption[optstr_[i]]) {
 
132
        hadOption[optstr_[i]] = 1;
 
133
        result += SP_T(' ');
 
134
        result += SP_T('[');
 
135
        result += SP_T('-');
 
136
        result += optstr_[i];
 
137
        result += SP_T(' ');
 
138
        result.append(optArgNames_[j], tcslen(optArgNames_[j]));
 
139
        result += SP_T(']');
 
140
      }
 
141
      i++;
 
142
      j++;
 
143
    }
 
144
  }
 
145
  result.append(SP_T(" sysid..."), tcslen(SP_T(" sysid...")));
 
146
  result += 0;
 
147
  return convertInput(result.data());
 
148
}
 
149
 
 
150
static
 
151
void ewrite(const char *s)
 
152
{
 
153
  int n = (int)strlen(s);
 
154
  while (n > 0) {
 
155
    int nw = write(2, s, n);
 
156
    if (nw < 0)
 
157
      break;
 
158
    n -= nw;
 
159
    s += nw;
 
160
  }
 
161
}
 
162
 
 
163
static
 
164
#ifdef SP_FANCY_NEW_HANDLER
 
165
int outOfMemory(size_t)
 
166
#else
 
167
void outOfMemory()
 
168
#endif
 
169
{
 
170
  ewrite("SP library: out of memory\n");
 
171
  exit(1);
 
172
#ifdef SP_FANCY_NEW_HANDLER
 
173
  return 0;
 
174
#endif  
 
175
}
 
176
 
 
177
int CmdLineApp::init(int, AppChar **argv)
 
178
{
 
179
#ifndef SP_ANSI_LIB
 
180
  set_new_handler(outOfMemory);
 
181
#endif
 
182
#ifdef SP_HAVE_LOCALE
 
183
#ifdef SP_HAVE_WCHAR
 
184
  setlocale(LC_ALL, "");
 
185
#endif
 
186
#endif
 
187
#ifdef SP_HAVE_SETMODE
 
188
  _setmode(1, _O_BINARY);
 
189
  _setmode(2, _O_BINARY);
 
190
#endif
 
191
  progName = argv[0];
 
192
  if (progName)
 
193
    setProgramName(convertInput(progName));
 
194
  return 0;
 
195
}
 
196
 
 
197
int CmdLineApp::run(int argc, AppChar **argv)
 
198
{
 
199
#ifdef _MSC_VER
 
200
  _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
 
201
#endif
 
202
#ifdef SP_ANSI_LIB
 
203
  try {
 
204
#endif
 
205
  int ret = init(argc, argv);
 
206
  if (ret)
 
207
    return ret;
 
208
  int firstArg;
 
209
  ret = processOptions(argc, argv, firstArg);
 
210
  if (ret)
 
211
    return ret;
 
212
  ret = processArguments(argc - firstArg, argv + firstArg);
 
213
  progName = 0;
 
214
  return ret;
 
215
#ifdef SP_ANSI_LIB
 
216
  }
 
217
catch (
 
218
#ifndef SP_NO_STD_NAMESPACE
 
219
       std::
 
220
#endif
 
221
            bad_alloc) {
 
222
#ifdef SP_FANCY_NEW_HANDLER
 
223
    outOfMemory(0);
 
224
#else
 
225
    outOfMemory();
 
226
#endif
 
227
  }
 
228
  return 1;
 
229
#endif /* SP_ANSI_LIB */
 
230
}    
 
231
 
 
232
int CmdLineApp::processOptions(int argc, AppChar **argv, int &nextArg)
 
233
{
 
234
  AppChar ostr[2];
 
235
  optstr_ += SP_T('\0');
 
236
  Options<AppChar> options(argc, argv, optstr_.data());
 
237
  AppChar opt;
 
238
  while (options.get(opt)) {
 
239
    switch (opt) {
 
240
    case ':':
 
241
      ostr[0] = options.opt();
 
242
      ostr[1] = SP_T('\0');
 
243
      message(CmdLineAppMessages::missingOptionArgError,
 
244
              StringMessageArg(convertInput(ostr)));
 
245
      message(CmdLineAppMessages::usage,
 
246
              StringMessageArg(usageString()));
 
247
      return 1;
 
248
    case '?':
 
249
      ostr[0] = options.opt();
 
250
      ostr[1] = SP_T('\0');
 
251
      message(CmdLineAppMessages::invalidOptionError,
 
252
              StringMessageArg(convertInput(ostr)));
 
253
      message(CmdLineAppMessages::usage,
 
254
              StringMessageArg(usageString()));
 
255
      return 1;
 
256
    default:
 
257
      processOption(opt, options.arg());
 
258
      break;
 
259
    }
 
260
  }
 
261
  nextArg = options.ind();
 
262
  if (errorFile_) {
 
263
    static FileOutputByteStream file;
 
264
    if (!file.open(errorFile_)) {
 
265
      message(CmdLineAppMessages::openFileError,
 
266
              StringMessageArg(convertInput(errorFile_)),
 
267
              ErrnoMessageArg(errno));
 
268
      return 1;
 
269
    }
 
270
    setMessageStream(new EncodeOutputCharStream(&file, codingSystem()));
 
271
  }
 
272
  if (!outputCodingSystem_)
 
273
    outputCodingSystem_ = codingSystem();
 
274
  return 0;
 
275
}
 
276
 
 
277
void CmdLineApp::processOption(AppChar opt, const AppChar *arg)
 
278
{
 
279
  switch (opt) {
 
280
  case 'b':
 
281
    outputCodingSystem_ = lookupCodingSystem(arg);
 
282
    if (!outputCodingSystem_)
 
283
      message(internalCharsetIsDocCharset_
 
284
              ? CmdLineAppMessages::unknownBctf
 
285
              : CmdLineAppMessages::unknownEncoding,
 
286
              StringMessageArg(convertInput(arg)));
 
287
    break;
 
288
  case 'f':
 
289
    errorFile_ = arg;
 
290
    break;
 
291
  case 'v':
 
292
    // print the version number
 
293
    message(CmdLineAppMessages::versionInfo,
 
294
            StringMessageArg(convertInput(versionString)));
 
295
    break;
 
296
  default:
 
297
    CANNOT_HAPPEN();
 
298
  }
 
299
}
 
300
 
 
301
Boolean CmdLineApp::getMessageText(const MessageFragment &frag,
 
302
                                   StringC &text)
 
303
{
 
304
  String<SP_TCHAR> str;
 
305
  if (!MessageTable::instance()->getText(frag, str))
 
306
    return 0;
 
307
#ifdef SP_WIDE_SYSTEM
 
308
  text.assign((const Char *)str.data(), str.size());
 
309
#else
 
310
  str += 0;
 
311
  text = codingSystem()->convertIn(str.data());
 
312
#endif
 
313
  return 1;
 
314
}
 
315
 
 
316
Boolean CmdLineApp::stringMatches(const SP_TCHAR *s, const char *key)
 
317
{
 
318
  for (; *key != '\0'; s++, key++) {
 
319
    if (*s != tolower(*key) && *s != toupper(*key))
 
320
      return 0;
 
321
  }
 
322
  return *s == '\0';
 
323
}
 
324
 
 
325
void CmdLineApp::initCodingSystem(const char *requiredInternalCode)
 
326
{
 
327
  const char *name = requiredInternalCode;
 
328
#ifdef SP_MULTI_BYTE
 
329
  char buf[256];
 
330
  if (!name) {
 
331
    const SP_TCHAR *internalCode = tgetenv(SP_T("SP_SYSTEM_CHARSET"));
 
332
    if (internalCode) {
 
333
      buf[255] = '\0';
 
334
      for (size_t i = 0; i < 255; i++) {
 
335
        buf[i] = internalCode[i];
 
336
        if (buf[i] == '\0')
 
337
          break;
 
338
      }
 
339
      name = buf;
 
340
    }
 
341
  }
 
342
  if (requiredInternalCode)
 
343
    internalCharsetIsDocCharset_ = 0;
 
344
  else {
 
345
    const SP_TCHAR *useInternal = tgetenv(SP_T("SP_CHARSET_FIXED"));
 
346
    if (useInternal
 
347
        && (stringMatches(useInternal, "YES")
 
348
            || stringMatches(useInternal, "1")))
 
349
      internalCharsetIsDocCharset_ = 0;
 
350
  }
 
351
#endif /* SP_MULTI_BYTE */
 
352
  codingSystemKit_ = CodingSystemKit::make(name);
 
353
  const SP_TCHAR *codingName = tgetenv(internalCharsetIsDocCharset_
 
354
                                       ? SP_T("SP_BCTF")
 
355
                                       : SP_T("SP_ENCODING"));
 
356
  if (codingName)
 
357
    codingSystem_ = lookupCodingSystem(codingName);
 
358
#ifdef SP_MULTI_BYTE
 
359
  if (!codingSystem_ && !internalCharsetIsDocCharset_)
 
360
    codingSystem_ = lookupCodingSystem(SP_DEFAULT_ENCODING);
 
361
#endif
 
362
  if (!codingSystem_
 
363
#ifndef SP_WIDE_SYSTEM
 
364
      || codingSystem_->fixedBytesPerChar() > 1
 
365
#endif
 
366
    )
 
367
    codingSystem_ = codingSystemKit_->identityCodingSystem();
 
368
}
 
369
 
 
370
const CodingSystem *
 
371
CmdLineApp::lookupCodingSystem(const AppChar *codingName)
 
372
{
 
373
#define MAX_CS_NAME 50
 
374
  if (tcslen(codingName) < MAX_CS_NAME) {
 
375
    char buf[MAX_CS_NAME];
 
376
    int i;
 
377
    for (i = 0; codingName[i] != SP_T('\0'); i++) {
 
378
      SP_TUCHAR c = codingName[i];
 
379
#ifdef SP_WIDE_SYSTEM
 
380
      if (c > (unsigned char)-1)
 
381
        return 0;
 
382
#endif
 
383
      buf[i] = char(c);
 
384
    }
 
385
    buf[i] = '\0';
 
386
    return codingSystemKit_->makeCodingSystem(buf, internalCharsetIsDocCharset_);
 
387
  }
 
388
  return 0;
 
389
}
 
390
 
 
391
StringC CmdLineApp::convertInput(const SP_TCHAR *s)
 
392
{
 
393
#ifdef SP_WIDE_SYSTEM
 
394
  StringC str(s, wcslen(s));
 
395
#else
 
396
  StringC str(codingSystem()->convertIn(s));
 
397
#endif
 
398
  for (size_t i = 0; i < str.size(); i++)
 
399
    if (str[i] == '\n')
 
400
      str[i] = '\r';
 
401
  return str;
 
402
}
 
403
 
 
404
OutputCharStream *CmdLineApp::makeStdErr()
 
405
{
 
406
  OutputCharStream *os = ConsoleOutput::makeOutputCharStream(2);
 
407
  if (os)
 
408
    return os;
 
409
  return new EncodeOutputCharStream(&standardError, codingSystem());
 
410
}
 
411
 
 
412
OutputCharStream *CmdLineApp::makeStdOut()
 
413
{
 
414
  OutputCharStream *os = ConsoleOutput::makeOutputCharStream(1);
 
415
  if (os)
 
416
    return os;
 
417
  return new EncodeOutputCharStream(&standardOutput, outputCodingSystem_);
 
418
}
 
419
 
 
420
const MessageType2 &CmdLineApp::openFileErrorMessage()
 
421
{
 
422
  return CmdLineAppMessages::openFileError;
 
423
}
 
424
 
 
425
const MessageType2 &CmdLineApp::closeFileErrorMessage()
 
426
{
 
427
  return CmdLineAppMessages::closeFileError;
 
428
}
 
429
 
 
430
#ifdef SP_NAMESPACE
 
431
}
 
432
#endif