File: parse.c

package info (click to toggle)
neomutt 20251211%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,164 kB
  • sloc: ansic: 172,058; sh: 5,357; perl: 1,121; tcl: 1,065; python: 443; makefile: 48
file content (360 lines) | stat: -rw-r--r-- 9,094 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
/**
 * @file
 * Parse the Command Line
 *
 * @authors
 * Copyright (C) 2025 Richard Russon <rich@flatcap.org>
 *
 * @copyright
 * 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, see <http://www.gnu.org/licenses/>.
 */

/**
 * @page cli_parse Parse the Command Line
 *
 * Parse the Command Line
 */

#include "config.h"
#include <stdbool.h>
#include <sys/param.h> // IWYU pragma: keep
#include <unistd.h>
#include "mutt/lib.h"
#include "objects.h"

/**
 * check_help_mode - Check for help mode
 * @param mode String to check
 * @retval enum #HelpMode, e.g. HM_CONFIG
 */
int check_help_mode(const char *mode)
{
  if (mutt_istr_equal(mode, "shared"))
    return HM_SHARED;
  if (mutt_istr_equal(mode, "help"))
    return HM_HELP;
  if (mutt_istr_equal(mode, "info"))
    return HM_INFO;
  if (mutt_istr_equal(mode, "send"))
    return HM_SEND;
  if (mutt_istr_equal(mode, "tui"))
    return HM_TUI;
  if (mutt_istr_equal(mode, "all"))
    return HM_ALL;
  return 0;
}

/**
 * mop_up - Eat multiple arguments
 * @param[in]  argc  Size of argument array
 * @param[in]  argv  Array of argument strings
 * @param[in]  index Where to start eating
 * @param[out] sa    Array for the results
 * @retval num Number of arguments eaten
 *
 * Some options, like `-A`, can take multiple arguments.
 * e.g. `-A apple` or `-A apple banana cherry`
 *
 * Copy the arguments into an array.
 */
static int mop_up(int argc, char *const *argv, int index, struct StringArray *sa)
{
  int count = 0;
  for (int i = index; i < argc; i++, count++)
  {
    // Stop if we find '--' or '-X'
    if ((argv[i][0] == '-') && (argv[i][1] != '\0'))
      break;

    ARRAY_ADD(sa, mutt_str_dup(argv[i]));
  }

  return count;
}

/**
 * cli_parse - Parse the Command Line
 * @param[in]  argc Number of arguments
 * @param[in]  argv Arguments
 * @param[out] cli  Results
 * @retval true Success
 */
bool cli_parse(int argc, char *const *argv, struct CommandLine *cli)
{
  if ((argc < 1) || !argv || !cli)
    return false;

  // Any leading non-options must be addresses
  int count = mop_up(argc, argv, 1, &cli->send.addresses);
  if (count > 0)
  {
    cli->send.is_set = true;
    argc -= count;
    argv += count;
  }

  bool rc = true;

  opterr = 0; // We'll handle the errors
// Always initialise getopt() or the tests will fail
#if defined(BSD) || defined(__APPLE__)
  optreset = 1;
  optind = 1;
#else
  optind = 0;
#endif

  while (rc && (argc > 1) && (optind < argc))
  {
    int opt = getopt(argc, argv, "+:A:a:b:Cc:Dd:Ee:F:f:Gg:H:hi:l:m:nOpQ:RSs:TvyZz");
    switch (opt)
    {
      // ------------------------------------------------------------
      // Shared
      case 'F': // user config file
      {
        ARRAY_ADD(&cli->shared.user_files, mutt_str_dup(optarg));
        cli->shared.is_set = true;
        break;
      }
      case 'n': // no system config file
      {
        cli->shared.disable_system = true;
        cli->shared.is_set = true;
        break;
      }
      case 'e': // enter commands
      {
        ARRAY_ADD(&cli->shared.commands, mutt_str_dup(optarg));
        cli->shared.is_set = true;
        break;
      }
      case 'm': // mbox type
      {
        buf_strcpy(&cli->shared.mbox_type, optarg);
        cli->shared.is_set = true;
        break;
      }
      case 'd': // log level
      {
        buf_strcpy(&cli->shared.log_level, optarg);
        cli->shared.is_set = true;
        break;
      }
      case 'l': // log file
      {
        buf_strcpy(&cli->shared.log_file, optarg);
        cli->shared.is_set = true;
        break;
      }

      // ------------------------------------------------------------
      // Help
      case 'h': // help
      {
        if (optind < argc)
        {
          cli->help.mode = check_help_mode(argv[optind]);
          if (cli->help.mode != HM_NONE)
            optind++;
        }
        cli->help.help = true;
        cli->help.is_set = true;
        break;
      }
      case 'v': // version, license
      {
        if (cli->help.version)
          cli->help.license = true;
        else
          cli->help.version = true;

        cli->help.is_set = true;
        break;
      }

      // ------------------------------------------------------------
      // Info
      case 'A': // alias lookup
      {
        ARRAY_ADD(&cli->info.alias_queries, mutt_str_dup(optarg));
        // '-A' can take multiple arguments
        optind += mop_up(argc, argv, optind, &cli->info.alias_queries);
        cli->info.is_set = true;
        break;
      }
      case 'D': // dump config, dump changed
      {
        if (cli->info.dump_config)
          cli->info.dump_changed = true;
        else
          cli->info.dump_config = true;

        cli->info.is_set = true;
        break;
      }
      case 'O': // one-liner help
      {
        cli->info.show_help = true;
        cli->info.is_set = true;
        break;
      }
      case 'Q': // query config&cli->send.attach
      {
        ARRAY_ADD(&cli->info.queries, mutt_str_dup(optarg));
        // '-Q' can take multiple arguments
        optind += mop_up(argc, argv, optind, &cli->info.queries);
        cli->info.is_set = true;
        break;
      }
      case 'S': // hide sensitive
      {
        cli->info.hide_sensitive = true;
        cli->info.is_set = true;
        break;
      }

      // ------------------------------------------------------------
      // Send
      case 'a': // attach file
      {
        ARRAY_ADD(&cli->send.attach, mutt_str_dup(optarg));
        // `-a` can take multiple arguments
        optind += mop_up(argc, argv, optind, &cli->send.attach);
        cli->send.is_set = true;
        break;
      }
      case 'b': // bcc:
      {
        ARRAY_ADD(&cli->send.bcc_list, mutt_str_dup(optarg));
        cli->send.is_set = true;
        break;
      }
      case 'C': // crypto
      {
        cli->send.use_crypto = true;
        cli->send.is_set = true;
        break;
      }
      case 'c': // cc:
      {
        ARRAY_ADD(&cli->send.cc_list, mutt_str_dup(optarg));
        cli->send.is_set = true;
        break;
      }
      case 'E': // edit file
      {
        cli->send.edit_infile = true;
        cli->send.is_set = true;
        break;
      }
      case 'H': // draft file
      {
        buf_strcpy(&cli->send.draft_file, optarg);
        cli->send.is_set = true;
        break;
      }
      case 'i': // include file
      {
        buf_strcpy(&cli->send.include_file, optarg);
        cli->send.is_set = true;
        break;
      }
      case 's': // subject:
      {
        buf_strcpy(&cli->send.subject, optarg);
        cli->send.is_set = true;
        break;
      }

      // ------------------------------------------------------------
      // TUI
      case 'f': // start folder
      {
        buf_strcpy(&cli->tui.folder, optarg);
        cli->tui.is_set = true;
        break;
      }
      case 'G': // list newsgroups
      {
        cli->tui.start_nntp = true;
        cli->tui.is_set = true;
        break;
      }
      case 'g': // news server
      {
        cli->tui.start_nntp = true;
        buf_strcpy(&cli->tui.nntp_server, optarg);
        cli->tui.is_set = true;
        break;
      }
      case 'p': // postponed
      {
        cli->tui.start_postponed = true;
        cli->tui.is_set = true;
        break;
      }
      case 'R': // read-only
      {
        cli->tui.read_only = true;
        cli->tui.is_set = true;
        break;
      }
      case 'y': // browser
      {
        cli->tui.start_browser = true;
        cli->tui.is_set = true;
        break;
      }
      case 'Z': // new mail
      {
        cli->tui.start_new_mail = true;
        cli->tui.is_set = true;
        break;
      }
      case 'z': // any mail
      {
        cli->tui.start_any_mail = true;
        cli->tui.is_set = true;
        break;
      }

      // ------------------------------------------------------------
      case -1: // end of options
      {
        for (int i = optind; i < argc; i++)
        {
          ARRAY_ADD(&cli->send.addresses, mutt_str_dup(argv[i]));
        }
        cli->send.is_set = true;
        optind = argc; // finish parsing
        break;
      }
      default: // error
      {
        if (opt == '?')
          mutt_warning("Invalid option: %c", optopt);
        else if (opt == ':')
          mutt_warning("Option %c requires an argument", optopt);

        cli->help.help = true;
        cli->help.is_set = true;
        rc = false; // stop parsing
        break;
      }
    }
  }

  return rc;
}