summaryrefslogtreecommitdiff
path: root/fluid/app/args.cxx
blob: ef765206000180885c15795d5540bfcd84efd808 (plain)
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
//
// Command Line Arguments Handling code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2025 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//

#include "app/args.h"

#include "Fluid.h"

#include <FL/Fl.H>
#include <FL/filename.H>
#include <FL/fl_ask.H>

#include <string.h>
#include "../src/flstring.h"


Args::Args()
: update_file(0),
  compile_file(0),
  compile_strings(0),
  show_version(0)
{
  code_filename[0] = '\0';
  header_filename[0] = '\0';
  autodoc_path[0] = '\0';
}


/**
 Load args from command line into variables.

 \param[in] argc number of arguments in the list
 \param[in] argv pointer to an array of arguments
 \return 0 if the args were handled successfully, -1 if there was an error
    and the usage message was shown.
 */
int Args::load(int argc, char **argv) {
  int i = 1;
  Fl::args_to_utf8(argc, argv); // for MSYS2/MinGW
  if (   (Fl::args(argc, argv, i, arg_cb) == 0)     // unsupported argument found
      || (Fluid.batch_mode && (i != argc - 1))        // .fl filename missing
      || (!Fluid.batch_mode && (i < argc - 1))        // more than one filename found
      || (argv[i] && (argv[i][0] == '-'))) {  // unknown option
    static const char *msg =
    "usage: %s <switches> name.fl\n"
    " -u : update .fl file and exit (may be combined with '-c' or '-cs')\n"
    " -c : write .cxx and .h and exit\n"
    " -cs : write .cxx and .h and strings and exit\n"
    " -o <name> : .cxx output filename, or extension if <name> starts with '.'\n"
    " -h <name> : .h output filename, or extension if <name> starts with '.'\n"
    " --help : brief usage information\n"
    " --version, -v : print fluid version number\n"
    " -d : enable internal debugging\n";
    const char *app_name = 0;
    if ((argc > 0) && argv[0] && argv[0][0])
      app_name = fl_filename_name(argv[0]);
    if (!app_name || !app_name[0])
      app_name = "fluid";
#ifdef _MSC_VER
    fl_message(msg, app_name);
#else
    fprintf(stderr, msg, app_name);
#endif
    return -1;
  }
  return i;
}


int Args::arg_cb(int argc, char** argv, int& i) {
  return Fluid.args.arg(argc, argv, i);
}


/**
 Handle command line arguments.
 \param[in] argc number of arguments in the list
 \param[in] argv pointer to an array of arguments
 \param[inout] i current argument index
 \return number of arguments used; if 0, the argument is not supported
 */
int Args::arg(int argc, char** argv, int& i) {
  if (argv[i][0] != '-')
    return 0;
  if (argv[i][1] == 'd' && !argv[i][2]) {
    Fluid.debug_external_editor = 1;
    i++; return 1;
  }
  if (argv[i][1] == 'u' && !argv[i][2]) {
    update_file++;
    Fluid.batch_mode++;
    i++; return 1;
  }
  if (argv[i][1] == 'c' && !argv[i][2]) {
    compile_file++;
    Fluid.batch_mode++;
    i++; return 1;
  }
  if ((strcmp(argv[i], "-v") == 0) || (strcmp(argv[i], "--version") == 0)) {
    show_version = 1;
    i++; return 1;
  }
  if (argv[i][1] == 'c' && argv[i][2] == 's' && !argv[i][3]) {
    compile_file++;
    compile_strings++;
    Fluid.batch_mode++;
    i++; return 1;
  }
  if (argv[i][1] == 'o' && !argv[i][2] && i + 1 < argc) {
    strlcpy(code_filename, argv[i + 1], FL_PATH_MAX);
    Fluid.batch_mode++;
    i += 2; return 2;
  }
#ifndef NDEBUG
  if ((i + 1 < argc) && (strcmp(argv[i], "--autodoc") == 0)) {
    strlcpy(autodoc_path, argv[i + 1], FL_PATH_MAX);
    i += 2; return 2;
  }
#endif
  if (strcmp(argv[i], "--help") == 0) {
    return 0;
  }
  if (argv[i][1] == 'h' && !argv[i][2]) {
    if ((i + 1 < argc) && (argv[i + 1][0] != '-')) {
      strlcpy(header_filename, argv[i + 1], FL_PATH_MAX);
      Fluid.batch_mode++;
      i += 2;
      return 2;
    } else {
      return 0;
    }
  }
  return 0;
}