File: CommandLineSettings.cc

package info (click to toggle)
xtide 2.6.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,996 kB
  • ctags: 2,617
  • sloc: cpp: 26,266; ansic: 8,105; makefile: 152; yacc: 113; sh: 54; lex: 54
file content (440 lines) | stat: -rw-r--r-- 12,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// $Id: CommandLineSettings.cc,v 1.7 2003/02/20 15:19:56 flaterco Exp $
/*  CommandLineSettings  Settings from command line.

    Copyright (C) 1998  David Flater.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "common.hh"

// Fodder for a next generation getopt.

CommandLineSettings::Argdesc switches[] = {

  // XTide switches

  {"b",  CommandLineSettings::temporal}, // Begin time
  {"e",  CommandLineSettings::temporal}, // End time
  {"f",  CommandLineSettings::format},   // Format
  {"l",  CommandLineSettings::text},     // Location
  {"m",  CommandLineSettings::mode},     // Mode
  {"ml", CommandLineSettings::number},   // Mark level (with units appended)
  {"o",  CommandLineSettings::text},     // Output file
  {"s",  CommandLineSettings::temporal}, // Step size

  {"el", CommandLineSettings::boolean},  // Extra lines
  {"fe", CommandLineSettings::boolean},  // Flat Earth
  {"in", CommandLineSettings::boolean},  // Infer constituents
  {"nf", CommandLineSettings::boolean},  // No fill
  {"ns", CommandLineSettings::boolean},  // No sun moon
  {"tl", CommandLineSettings::boolean},  // Top lines
  {"v",  CommandLineSettings::boolean},  // Print version & exit
  {"z",  CommandLineSettings::boolean},  // Zulu time
  {"suck", CommandLineSettings::boolean}, // Undocumented test mode

  {"bg", CommandLineSettings::color},    // Background color
  {"bc", CommandLineSettings::color},    // Button color
  {"cw", CommandLineSettings::number},   // Clock width
  {"df", CommandLineSettings::text},     // Date format
  {"Dc", CommandLineSettings::color},    // Datum color
  {"dc", CommandLineSettings::color},    // Daytime color
  {"ec", CommandLineSettings::color},    // Ebb color
  {"fc", CommandLineSettings::color},    // Flood color
  {"fg", CommandLineSettings::color},    // Foreground color
  {"ga", CommandLineSettings::number},   // Graph aspect
  {"gh", CommandLineSettings::number},   // Graph height
  {"gl", CommandLineSettings::number},   // Globe center longitude
  {"gw", CommandLineSettings::number},   // Graph width
  {"hf", CommandLineSettings::text},     // Hour format
  {"lw", CommandLineSettings::number},   // Line width
  {"mc", CommandLineSettings::color},    // Mark color
  {"Mc", CommandLineSettings::color},    // Mean Tide Level color
  {"nc", CommandLineSettings::color},    // Nighttime color
  {"tf", CommandLineSettings::text},     // Time format
  {"th", CommandLineSettings::number},   // TTY height
  {"tw", CommandLineSettings::number},   // TTY width
  {"u",  CommandLineSettings::unit},     // Units

  // Switches passed through to X11
  // Most of these others are unsupported, discouraged, deprecated,
  // untested, considered harmful...  You get the picture.

  // This no-op is used to clobber -geometry in xxContext.
  {"XX", CommandLineSettings::number},

  {"foreground", CommandLineSettings::color},    // X background color
  {"background", CommandLineSettings::color},    // X background color
  {"display", CommandLineSettings::text},        // X display
  {"fn", CommandLineSettings::text},             // X font
  {"font", CommandLineSettings::text},           // X font
  {"bd", CommandLineSettings::color},            // X border color
  {"bordercolor", CommandLineSettings::color},   // X border color
  {"bw", CommandLineSettings::number},           // X border width
  {"borderwidth", CommandLineSettings::number},  // X border width
  {"iconic", CommandLineSettings::boolean},      // X start iconic
  {"name", CommandLineSettings::text},           // X window name
  {"rv", CommandLineSettings::boolean},          // X reverse video
  {"reverse", CommandLineSettings::boolean},     // X reverse video
  {"selectionTimeout", CommandLineSettings::number},  // X timeout
  {"synchronous", CommandLineSettings::boolean}, // X synchronous mode
  {"title", CommandLineSettings::text},          // X window title
  {"xnllanguage", CommandLineSettings::text},    // X window language
  {"xrm", CommandLineSettings::text},            // X resource string

  {NULL, CommandLineSettings::boolean} // Stop
};

#define duplicated_code_block {        \
      if (temprest) {                  \
	if (rest1) {                   \
          while (rest1) {              \
            CommandLineSettings::arglist *t = rest1->next;  \
            delete rest1;              \
            rest1 = t;                 \
          }                            \
          while (temprest) {           \
            CommandLineSettings::arglist *t = temprest->next;  \
            delete temprest;           \
            temprest = t;              \
          }                            \
	  return NULL;                 \
	} else {                       \
	  rest1 = temprest;            \
	  mycookedswitch = name;       \
	}                              \
      }                                \
}

// The goal is to disambiguate the argument string.
// Inputs:
//   int argc, char **argv   The usual
//   int argi                Index to the argument now being looked at
// Returns:
//   arglist*  Disambiguated argument list if valid.  Null if not.

CommandLineSettings::arglist *CommandLineSettings::disambiguate
(int argc, char **argv, int argi) {

  char *argii = argv[argi];
  if (*argii != '-' && *argii != '+')
    return NULL;
  int isplus = (*argii == '+');
  argii++;

  Dstr mycookedswitch;
  CommandLineSettings::arglist *rest1=NULL, *temprest;
  for (unsigned settingslooper = 0; switches[settingslooper].name != NULL;
       settingslooper++) {
    if (isplus && switches[settingslooper].typ != CommandLineSettings::boolean)
      continue;
    char *name = switches[settingslooper].name;
    if (!strncmp (argii, name, strlen(name))) {
      // Try 1:  concatenated argument
      temprest = check_arg (argc, argv, argi, argii + strlen(name),
			    switches[settingslooper].typ);
      duplicated_code_block;
      // Try 2:  separate argument
      if ((*(argii + strlen(name)) == '\0') && (argi + 1 < argc)) {
	temprest = check_arg (argc, argv, argi+1, argv[argi+1],
			      switches[settingslooper].typ);
        duplicated_code_block;
      }
    }
  }

  if (rest1) {
    // check_arg is supposed to allocate one for us with a null
    // switch.
    assert (rest1->svitch.isNull());
    if (isplus)
      mycookedswitch *= '+';
    else
      mycookedswitch *= '-';
    rest1->svitch = mycookedswitch;
    if (isplus) {
      if (rest1->arg == "y")
	rest1->arg = "n";
      else if (rest1->arg == "n")
	rest1->arg = "y";
      else
	assert (0);
    }
  }
  return rest1; // Too easy.
}

// The goal is still to disambiguate the argument string.
// char *argii   Index to the current character position in argv[argi].
// argtype t     The type of argument we expect to find there.

CommandLineSettings::arglist *CommandLineSettings::check_arg
(int argc, char **argv, int argi, char *argii, CommandLineSettings::argtype t)
{
  Dstr mycookedarg;
  switch (t) {

  case CommandLineSettings::boolean:
    switch (*argii) {
    case '\0':
    case 'y':
    case 'Y':
      mycookedarg = "y";
      break;
    case 'n':
    case 'N':
      mycookedarg = "n";
      break;
    default:
      return NULL;
    }
    break;

  case CommandLineSettings::temporal:
  case CommandLineSettings::number:
    {
      float a;
      if (sscanf (argii, "%f", &a) == 1) {
        mycookedarg = argii;
        break;
      }
    }
    return NULL;

  case CommandLineSettings::mode:
    if (strlen (argii) != 1)
      return NULL;
    switch (*argii) {
    case 'b':
    case 'c':
    case 'C':
    case 'g':
    case 'l':
    case 'm':
    case 'p':
    case 'r':
    case 's':
      mycookedarg = argii;
      break;
    default:
      return NULL;
    }
    break;

  case CommandLineSettings::format:
    if (strlen (argii) != 1)
      return NULL;
    switch (*argii) {
    case 'h':
    case 'p':
    case 't':
      mycookedarg = argii;
      break;
    default:
      return NULL;
    }
    break;

  case CommandLineSettings::unit:
    if (!strcmp (argii, "ft") ||
        !strcmp (argii, "m") ||
        !strcmp (argii, "x")) {
      mycookedarg = argii;
      break;
    }
    return NULL;

  case CommandLineSettings::color:
    // Parsecolor wants to print warnings...  not worth fixing.
    if (isalpha(*argii) || *argii == '#') {
      mycookedarg = argii;
      break;
    }
    return NULL;

  case CommandLineSettings::text:
    if (*argii == '\0')
      return NULL; // They probably didn't mean that.
    mycookedarg = argii;
    break;

  default:
    assert (0);
  }

  CommandLineSettings::arglist *rest = NULL;
  if (++argi < argc) {
    rest = disambiguate (argc, argv, argi);
    if (!rest)
      return NULL;
  }
  CommandLineSettings::arglist *myarg = new CommandLineSettings::arglist;
  // myarg->svitch intentionally left blank
  myarg->arg = mycookedarg;
  myarg->next = rest;
  return myarg; // Easy.
}

CommandLineSettings::CommandLineSettings (int argc, char **argv) {
  static CommandLineSettings::arglist *args = NULL;
  if (!args)
    if (argc > 1) {
      args = disambiguate (argc, argv, 1);
      if (!args)
        barf (BAD_OR_AMBIGUOUS_COMMAND_LINE);
    }

  if (findarg ("-v", args)) {
    Dstr line;
    version_string (line);
    fprintf (stderr, "%s\n", line.aschar());
    exit (0);
  }
  {
    int a;
    for (a=0; a<numcolors; a++) {
      Dstr aname ("-");
      aname += colorarg[a];
      colors[a] = findarg (aname.aschar(), args);
    }
  }

  el = findarg("-el",args);
  fe = findarg("-fe",args);
  u = findarg("-u",args);
  z = findarg("-z",args);
  df = findarg("-df",args);
  hf = findarg("-hf",args);
  tf = findarg("-tf",args);
  tl = findarg("-tl",args);
  nf = findarg("-nf",args);
  ns = findarg("-ns",args);
  in = findarg("-in",args);

  b = findarg("-b",args);
  e = findarg("-e",args);
  f = findarg("-f",args);
  m = findarg("-m",args);
  s = findarg("-s",args);
  o = findarg("-o",args);

  l_len = 0;
  {
    struct lnode *templ, *last;
    l = last = NULL;
    for (CommandLineSettings::arglist *i = args; i; i=i->next) {
      if (i->svitch == "-l") {
        l_len++;
	templ = new lnode;
	templ->name = i->arg;
	templ->next = NULL;
        if (last) {
          last->next = templ;
          last = templ;
        } else {
  	  l = last = templ;
        }
      }
    }
  }
  if (l_len == 0) {
    char *defloc = getenv("XTIDE_DEFAULT_LOCATION");
    if (defloc) {
      l = new lnode;
      l->name = defloc;
      l->next = NULL;
      l_len = 1;
    }
  }

  char *t;
  if ((t = findarg ("-ga", args))) {
    ga_isnull = 0;
    ga = getposdouble (t);
  }
  if ((t = findarg ("-gl", args))) {
    gl_isnull = 0;
    gl = getgldouble (t);
  }
  if ((t = findarg ("-lw", args))) {
    lw_isnull = 0;
    lw = getposdouble (t);
  }
  if ((t = findarg ("-gh", args))) {
    gh_isnull = 0;
    gh = max (mingheight, getposint (t));
  }
  if ((t = findarg ("-gw", args))) {
    gw_isnull = 0;
    gw = max (mingwidth, getposint (t));
  }
  if ((t = findarg ("-th", args))) {
    th_isnull = 0;
    th = max (minttyheight, getposint (t));
  }
  if ((t = findarg ("-tw", args))) {
    tw_isnull = 0;
    tw = max (minttywidth, getposint (t));
  }
  if ((t = findarg ("-cw", args))) {
    cw_isnull = 0;
    cw = max (mingwidth, getposint (t));
  }

  ml_isnull = 1;
  if ((t = findarg ("-ml", args))) {
    double v;
    Dstr uts;
    ml_isnull = 0;
    int i;
    for (i=0; i<strlen(t); i++) {
      char c = t[i];
      if (!((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.'
      || c == 'e' || c == 'E'))
        break;
    }
    uts = t + i;
    if (sscanf (t, "%lf", &v) != 1) {
      Dstr details ("The offending input was '");
      details += t;
      details += "'.";
      barf (NOT_A_NUMBER, details);
    }
    ml = PredictionValue (PredictionValue::Unit (uts), v);
  }
}

CommandLineSettings::~CommandLineSettings () {
  while (l) {
    struct lnode *templ = l;
    l = l->next;
    delete templ;
  }
}

char *
CommandLineSettings::findarg (char *arg, CommandLineSettings::arglist *args) {
  while (args) {
    if (args->svitch == arg)
      return args->arg.aschar();
    args = args->next;
  }
  return NULL;
}

char
*CommandLineSettings::settingsid() {
  return "command line";
}