summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.72.0/src/option.cpp
blob: 9ad8912b3876301c46e3d5bfe63b59628e08646f (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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
/**
 * @file option.cpp
 * Parses the options from the config file.
 *
 * @author  Ben Gardner
 * @author  Guy Maurel since version 0.62 for uncrustify4Qt
 *          October 2015, 2016
 * @author  Matthew Woehlke since version 0.67
 * @license GPL v2+
 */
#include "option.h"

#include "keywords.h"
#include "option_enum.h"
#include "uncrustify.h"
#include "uncrustify_limits.h"
#include "uncrustify_types.h"
#include "uncrustify_version.h"

#include <fstream>
#include <unordered_map>

#include <cctype>
#include <cstdarg>
#include <cstring>

namespace uncrustify
{

namespace
{

static const char *DOC_TEXT_END = u8R"___(
# Meaning of the settings:
#   Ignore - do not do any changes
#   Add    - makes sure there is 1 or more space/brace/newline/etc
#   Force  - makes sure there is exactly 1 space/brace/newline/etc,
#            behaves like Add in some contexts
#   Remove - removes space/brace/newline/etc
#
#
# - Token(s) can be treated as specific type(s) with the 'set' option:
#     `set tokenType tokenString [tokenString...]`
#
#     Example:
#       `set BOOL __AND__ __OR__`
#
#     tokenTypes are defined in src/token_enum.h, use them without the
#     'CT_' prefix: 'CT_BOOL' => 'BOOL'
#
#
# - Token(s) can be treated as type(s) with the 'type' option.
#     `type tokenString [tokenString...]`
#
#     Example:
#       `type int c_uint_8 Rectangle`
#
#     This can also be achieved with `set TYPE int c_uint_8 Rectangle`
#
#
# To embed whitespace in tokenStrings use the '\' escape character, or quote
# the tokenStrings. These quotes are supported: "'`
#
#
# - Support for the auto detection of languages through the file ending can be
#   added using the 'file_ext' command.
#     `file_ext langType langString [langString..]`
#
#     Example:
#       `file_ext CPP .ch .cxx .cpp.in`
#
#     langTypes are defined in uncrusify_types.h in the lang_flag_e enum, use
#     them without the 'LANG_' prefix: 'LANG_CPP' => 'CPP'
#
#
# - Custom macro-based indentation can be set up using 'macro-open',
#   'macro-else' and 'macro-close'.
#     `(macro-open | macro-else | macro-close) tokenString`
#
#     Example:
#       `macro-open  BEGIN_TEMPLATE_MESSAGE_MAP`
#       `macro-open  BEGIN_MESSAGE_MAP`
#       `macro-close END_MESSAGE_MAP`
#
#
)___";


std::vector<OptionGroup>                         option_groups;
std::unordered_map<std::string, GenericOption *> option_map;

#define LOG_CONFIG(...)                            \
   do { log_config(); LOG_FMT(LNOTE, __VA_ARGS__); \
   } while (0)


//-----------------------------------------------------------------------------
constexpr int option_level(int major, int minor, int patch = 0)
{
   return((major << 20) | (minor << 10) | (patch << 0));
}


//-----------------------------------------------------------------------------
void log_config()
{
   // Print the name of the configuration file only once
   static bool config_name_logged = false;

   if (!config_name_logged)
   {
      LOG_FMT(LNOTE, "log_config: the configuration file is: %s\n",
              cpd.filename.c_str());
      config_name_logged = true;
   }
}


//-----------------------------------------------------------------------------
// This identity function exists so that all Option<T>::str can simply call
// to_string(m_val); this function will be used by Option<string>
std::string to_string(const std::string &in)
{
   return(in);
}

using std::to_string;


//-----------------------------------------------------------------------------
std::string to_lower(const char *in, std::string::size_type size = 0)
{
   std::string out;

   if (size > 0)
   {
      out.reserve(size);
   }

   while (*in)
   {
      out += static_cast<char>(std::tolower(*in));
      ++in;
   }
   return(out);
}


//-----------------------------------------------------------------------------
std::string to_lower(const std::string &in)
{
   return(to_lower(in.data(), in.size()));
}


//-----------------------------------------------------------------------------
bool is_arg_sep(int ch)
{
   return(isspace(ch) || ch == ',' || ch == '=');
}


//-----------------------------------------------------------------------------
bool is_varg_sep(int ch)
{
   return(ch == '.');
}


//-----------------------------------------------------------------------------
std::vector<std::string> split_args(std::string in, const char *filename,
                                    bool (*is_sep)(int))
{
   std::vector<std::string> out;
   std::string::size_type   n = 0;
   std::string::size_type   k = in.size();

   // Parse input string
   while (n < k)
   {
      // Skip leading space
      while (n < k && is_sep(in[n]))
      {
         ++n;
      }

      // Detect comments or trailing space
      if (n >= k || in[n] == '#')
      {
         break;
      }

      // Detect and extract quoted string
      if (const auto *quote = strchr("\'\"`", in[n]))
      {
         const auto start = ++n;

         for ((void)n; in[n] != *quote; ++n)
         {
            if (n < k && in[n] == '\\')
            {
               in.erase(n, 1);
               --k;
            }

            if (n >= k)
            {
               OptionWarning w{ filename };
               w("found unterminated quoted-string");
               return{};
            }
         }

         out.push_back(in.substr(start, n - start));

         if (++n < k && !is_sep(in[n]))
         {
            OptionWarning w{ filename };
            w("unexpected text following quoted-string");
            return{};
         }
         continue;
      }
      // Extract anything else
      const auto start = n;

      for ((void)n; n < k && !is_sep(in[n]); ++n)
      {
         if (in[n] == '\\')
         {
            in.erase(n, 1);
            --k;
         }

         if (n >= k)
         {
            OptionWarning w{ filename };
            w("found unterminated quoted-string");
            return{};
         }
      }

      out.push_back(in.substr(start, n - start));
   }
   return(out);
} // split_args


//-----------------------------------------------------------------------------
bool is_path_relative(const std::string &path)
{
   assert(!path.empty());

#ifdef WIN32
   // Check for partition labels as indication for an absolute path
   // 'X:\path\to\file' style absolute disk path
   if (path.size() > 1 && isalpha(path[0]) && path[1] == ':')
   {
      return(false);
   }

   // Check for double backslashs as indication for a network path
   // '\\server\path\to\file style' absolute UNC path
   if (path.size() > 1 && path[0] == '\\' && path[1] == '\\')
   {
      return(false);
   }
#endif

   // Check for a slash as indication for a filename with leading path
   // '/path/to/file' style absolute path
   return(path[0] != '/');
}


//-----------------------------------------------------------------------------
void print_description(FILE *pfile, std::string description,
                       const char *eol_marker)
{
   // Descriptions always start with a '\n', so skip the first character
   for (std::string::size_type start = 1, length = description.length();
        start != std::string::npos && start < length; ++start)
   {
      // Check for empty line so we can squelch trailing whitespace
      if (description[start] == '\n')
      {
         fprintf(pfile, "#%s", eol_marker);
      }
      else
      {
         const auto end = description.find('\n', start);
         fprintf(pfile, "# %s%s",
                 description.substr(start, end - start).c_str(), eol_marker);
         start = end;
      }
   }
}


//-----------------------------------------------------------------------------
bool process_option_line_compat_0_68(const std::string              &cmd,
                                     const std::vector<std::string> &args,
                                     const char                     *filename)
{
   if (cmd == "sp_cpp_lambda_paren")
   {
      OptionWarning w{ filename, OptionWarning::MINOR };
      w("option '%s' is deprecated; use '%s' instead",
        cmd.c_str(), options::sp_cpp_lambda_square_paren.name());

      UNUSED(options::sp_cpp_lambda_square_paren.read(args[1].c_str()));
      return(true);
   }
   return(false);
} // process_option_line_compat_0_68


bool process_option_line_compat_0_70(const std::string &cmd,
                                     const char        *filename)
{
   if (cmd == "sp_word_brace")                     // Issue #2428
   {
      OptionWarning w{ filename, OptionWarning::MINOR };
      w("option '%s' is deprecated; did you want to use '%s' instead?",
        cmd.c_str(), options::sp_type_brace_init_lst.name());

      //UNUSED(options::sp_type_brace_init_lst.read(args[1].c_str()));
      return(true);
   }
   return(false);
} // process_option_line_compat_0_70

} // namespace

///////////////////////////////////////////////////////////////////////////////

//BEGIN Option<T> and helpers


//-----------------------------------------------------------------------------
OptionWarning::OptionWarning(const char *filename, Severity severity)
{
   if (severity != MINOR)
   {
      ++cpd.error_count;
   }

   if (cpd.line_number != 0)
   {
      fprintf(stderr, "%s:%u: ", filename, cpd.line_number);
   }
   else
   {
      fprintf(stderr, "%s: ", filename);
   }
}


//-----------------------------------------------------------------------------
OptionWarning::OptionWarning(const GenericOption *opt, Severity severity)
{
   if (severity != MINOR)
   {
      ++cpd.error_count;
   }
   fprintf(stderr, "Option<%s>: at %s:%d: ", to_string(opt->type()),
           cpd.filename.c_str(), cpd.line_number);
}


//-----------------------------------------------------------------------------
OptionWarning::~OptionWarning()
{
   fprintf(stderr, "\n");
   log_flush(true);
}


//-----------------------------------------------------------------------------
void OptionWarning::operator()(const char *fmt, ...)
{
   va_list args;

   va_start(args, fmt);
   vfprintf(stderr, fmt, args);
   va_end(args);
}


//-----------------------------------------------------------------------------
void GenericOption::warnUnexpectedValue(const char *actual) const
{
   OptionWarning w{ this };

   auto          values = possibleValues();

   if (values[1] == nullptr)
   {
      w("Expected %s ", *values);
   }
   else
   {
      w("Expected one of ");

      while (*values)
      {
         w("'%s'", *values);

         if (*(++values))
         {
            w(", ");
         }
      }
   }
   w(", for '%s'; got '%s'", name(), actual);
}


//-----------------------------------------------------------------------------
void GenericOption::warnIncompatibleReference(const GenericOption *ref) const
{
   OptionWarning w{ this };

   w("%s references option %s with incompatible type %s",
     name(), ref->name(), to_string(ref->type()));
}


//-----------------------------------------------------------------------------
template<typename T>
bool read_enum(const char *in, Option<T> &out)
{
   assert(in);

   if (convert_string(in, out.m_val))
   {
      return(true);
   }

   if (const auto *const opt = find_option(in))
   {
      if (opt->type() != out.type())
      {
         out.warnIncompatibleReference(opt);
         return(false);
      }
      auto &topt = *static_cast<const Option<T> *>(opt);
      out.m_val = topt();
      return(true);
   }
   out.warnUnexpectedValue(in);
   return(false);
}


//-----------------------------------------------------------------------------
template<typename T>
bool read_number(const char *in, Option<T> &out)
{
   assert(in);

   char       *c;
   const auto val = std::strtol(in, &c, 10);

   if (*c == 0 && out.validate(val))
   {
      out.m_val = static_cast<T>(val);
      return(true);
   }
   bool invert = false;

   if (strchr("-", in[0]))
   {
      invert = true;
      ++in;
   }

   if (const auto *const opt = find_option(in))
   {
      LOG_CONFIG("%s(%d): line_number is %d, option(%s) %s, ref(%s) %s\n",
                 __func__, __LINE__, cpd.line_number,
                 to_string(out.type()), out.name(),
                 to_string(opt->type()), opt->name());

      long tval;

      if (opt->type() == OT_NUM)
      {
         auto &sopt = *static_cast<const Option<signed> *>(opt);
         tval = static_cast<long>(sopt());
      }
      else if (opt->type() == OT_UNUM)
      {
         auto &uopt = *static_cast<const Option<unsigned> *>(opt);
         tval = static_cast<long>(uopt());
      }
      else
      {
         out.warnIncompatibleReference(opt);
         return(false);
      }
      const auto rval = (invert ? -tval : tval);

      if (out.validate(rval))
      {
         out.m_val = static_cast<T>(rval);
         return(true);
      }
      return(false);
   }
   out.warnUnexpectedValue(in);
   return(false);
} // read_number


//-----------------------------------------------------------------------------
template<typename T>
void Option<T>::reset()
{
   m_val = m_default;
}


//-----------------------------------------------------------------------------
template<typename T>
std::string Option<T>::str() const
{
   return(to_string(m_val));
}


//-----------------------------------------------------------------------------
template<typename T>
std::string Option<T>::defaultStr() const
{
   return(m_default != T{} ? to_string(m_default) : std::string{});
}

// Explicit instantiations
template class Option<bool>;
template class Option<iarf_e>;
template class Option<line_end_e>;
template class Option<token_pos_e>;
template class Option<signed>;
template class Option<unsigned>;
template class Option<std::string>;

//END Option<T> and helpers

///////////////////////////////////////////////////////////////////////////////

//BEGIN Option<bool>


//-----------------------------------------------------------------------------
template<>
option_type_e Option<bool>::type() const
{
   return(OT_BOOL);
}


//-----------------------------------------------------------------------------
template<>
const char *const *Option<bool>::possibleValues() const
{
   static char const *values[] = { "true", "false", nullptr };

   return(values);
}


//-----------------------------------------------------------------------------
template<>
bool Option<bool>::read(const char *in)
{
   assert(in);

   if (convert_string(in, m_val))
   {
      return(true);
   }
   bool invert = false;

   if (strchr("~!-", in[0]))
   {
      invert = true;
      ++in;
   }

   if (const auto *const opt = find_option(in))
   {
      if (opt->type() != OT_BOOL)
      {
         warnIncompatibleReference(opt);
         return(false);
      }
      auto &bopt = *static_cast<const Option<bool> *>(opt);
      m_val = (invert ? !bopt() : bopt());
      return(true);
   }
   warnUnexpectedValue(in);
   return(false);
}

//END Option<bool>

///////////////////////////////////////////////////////////////////////////////

//BEGIN Option<iarf_e>


//-----------------------------------------------------------------------------
template<>
option_type_e Option<iarf_e>::type() const
{
   return(OT_IARF);
}


//-----------------------------------------------------------------------------
template<>
const char *const *Option<iarf_e>::possibleValues() const
{
   return(iarf_values);
}


//-----------------------------------------------------------------------------
template<>
bool Option<iarf_e>::read(const char *in)
{
   return(read_enum(in, *this));
}

//END Option<iarf_e>

///////////////////////////////////////////////////////////////////////////////

//BEGIN Option<line_end_e>


//-----------------------------------------------------------------------------
template<>
option_type_e Option<line_end_e>::type() const
{
   return(OT_LINEEND);
}


//-----------------------------------------------------------------------------
template<>
const char *const *Option<line_end_e>::possibleValues() const
{
   return(line_end_values);
}


//-----------------------------------------------------------------------------
template<>
bool Option<line_end_e>::read(const char *in)
{
   return(read_enum(in, *this));
}

//END Option<line_end_e>

///////////////////////////////////////////////////////////////////////////////

//BEGIN Option<token_pos_e>


//-----------------------------------------------------------------------------
template<>
option_type_e Option<token_pos_e>::type() const
{
   return(OT_TOKENPOS);
}


//-----------------------------------------------------------------------------
template<>
const char *const *Option<token_pos_e>::possibleValues() const
{
   return(token_pos_values);
}


//-----------------------------------------------------------------------------
template<>
bool Option<token_pos_e>::read(const char *in)
{
   return(read_enum(in, *this));
}

//END Option<token_pos_e>

///////////////////////////////////////////////////////////////////////////////

//BEGIN Option<signed>


//-----------------------------------------------------------------------------
template<>
option_type_e Option<signed>::type() const
{
   return(OT_NUM);
}


//-----------------------------------------------------------------------------
template<>
const char *const *Option<signed>::possibleValues() const
{
   static char const *values[] = { "number", nullptr };

   return(values);
}


//-----------------------------------------------------------------------------
template<>
bool Option<signed>::read(const char *in)
{
   return(read_number(in, *this));
}

//END Option<signed>

///////////////////////////////////////////////////////////////////////////////

//BEGIN Option<unsigned>


//-----------------------------------------------------------------------------
template<>
option_type_e Option<unsigned>::type() const
{
   return(OT_UNUM);
}


//-----------------------------------------------------------------------------
template<>
const char *const *Option<unsigned>::possibleValues() const
{
   static char const *values[] = { "unsigned number", nullptr };

   return(values);
}


//-----------------------------------------------------------------------------
template<>
bool Option<unsigned>::read(const char *in)
{
   return(read_number(in, *this));
}

//END Option<unsigned>

///////////////////////////////////////////////////////////////////////////////

//BEGIN Option<string>


//-----------------------------------------------------------------------------
template<>
option_type_e Option<std::string>::type() const
{
   return(OT_STRING);
}


//-----------------------------------------------------------------------------
template<>
const char *const *Option<std::string>::possibleValues() const
{
   static char const *values[] = { "string", nullptr };

   return(values);
}


//-----------------------------------------------------------------------------
template<>
bool Option<std::string>::read(const char *in)
{
   m_val = in;
   return(true);
}

//END Option<string>

///////////////////////////////////////////////////////////////////////////////

//BEGIN global functions for options


//-----------------------------------------------------------------------------
void begin_option_group(const char *description)
{
   auto g = OptionGroup{ description, {} };

   option_groups.push_back(g);
}


//-----------------------------------------------------------------------------
void register_option(GenericOption *option)
{
   assert(!option_groups.empty());

   option_groups.back().options.push_back(option);
   option_map.emplace(option->name(), option);
}


//-----------------------------------------------------------------------------
uncrustify::GenericOption *find_option(const char *name)
{
   const auto iter = option_map.find(to_lower(name));

   if (iter != option_map.end())
   {
      return(iter->second);
   }
   return(nullptr);
}


//-----------------------------------------------------------------------------
OptionGroup *get_option_group(size_t i)
{
   if (i >= option_groups.size())
   {
      return(nullptr);
   }
   return(&option_groups[i]);
}


//-----------------------------------------------------------------------------
size_t get_option_count()
{
   return(option_map.size());
}


//-----------------------------------------------------------------------------
void process_option_line(const std::string &config_line, const char *filename,
                         int &compat_level)
{
   // Split line into arguments, and punt if no arguments are present
   auto args = split_args(config_line, filename, is_arg_sep);

   if (args.empty())
   {
      return;
   }
   // Check for necessary arguments
   const auto &cmd = to_lower(args.front());

   if (cmd == "set" || cmd == "file_ext")
   {
      if (args.size() < 3)
      {
         OptionWarning w{ filename };
         w("%s requires at least three arguments", cmd.c_str());
         return;
      }
   }
   else
   {
      if (args.size() < 2)
      {
         OptionWarning w{ filename };
         w("%s requires at least two arguments", cmd.c_str());
         return;
      }
   }

   if (cmd == "type")
   {
      for (size_t i = 1; i < args.size(); ++i)
      {
         add_keyword(args[i], CT_TYPE);
      }
   }
   else if (cmd == "macro-open")
   {
      add_keyword(args[1], CT_MACRO_OPEN);
   }
   else if (cmd == "macro-close")
   {
      add_keyword(args[1], CT_MACRO_CLOSE);
   }
   else if (cmd == "macro-else")
   {
      add_keyword(args[1], CT_MACRO_ELSE);
   }
   else if (cmd == "set")
   {
      const auto token = find_token_name(args[1].c_str());

      if (token != CT_NONE)
      {
         LOG_FMT(LNOTE, "%s:%d set '%s':",
                 filename, cpd.line_number, args[1].c_str());

         for (size_t i = 2; i < args.size(); ++i)
         {
            LOG_FMT(LNOTE, " '%s'", args[i].c_str());
            add_keyword(args[i], token);
         }

         LOG_FMT(LNOTE, "\n");
      }
      else
      {
         OptionWarning w{ filename };
         w("%s: unknown type '%s'", cmd.c_str(), args[1].c_str());
      }
   }
#ifndef EMSCRIPTEN
   else if (cmd == "include")
   {
      auto       this_line_number = cpd.line_number;
      const auto &include_path    = args[1];

      if (include_path.empty())
      {
         OptionWarning w{ filename };
         w("include: path cannot be empty");
      }
      else if (is_path_relative(include_path))
      {
         // include is a relative path to the current config file
         unc_text ut = std::string{ filename };
         ut.resize(static_cast<unsigned>(path_dirname_len(filename)));
         ut.append(include_path);
         UNUSED(load_option_file(ut.c_str(), compat_level));
      }
      else
      {
         // include is an absolute path
         UNUSED(load_option_file(include_path.c_str(), compat_level));
      }
      cpd.line_number = this_line_number;
   }
#endif
   else if (cmd == "file_ext")
   {
      auto *const lang_arg = args[1].c_str();

      for (size_t i = 2; i < args.size(); ++i)
      {
         auto *const lang_name = extension_add(args[i].c_str(), lang_arg);

         if (lang_name)
         {
            LOG_FMT(LNOTE, "%s:%d file_ext '%s' => '%s'\n",
                    filename, cpd.line_number, args[i].c_str(), lang_name);
         }
         else
         {
            OptionWarning w{ filename };
            w("file_ext: unknown language '%s'", lang_arg);
            break;
         }
      }
   }
   else if (cmd == "using")
   {
      auto vargs = split_args(args[1], filename, is_varg_sep);

      if (vargs.size() == 2)
      {
         compat_level = option_level(std::stoi(vargs[0]), std::stoi(vargs[1]));
      }
      else if (vargs.size() == 3)
      {
         compat_level = option_level(std::stoi(vargs[0]),
                                     std::stoi(vargs[1]),
                                     std::stoi(vargs[2]));
      }
      else
      {
         OptionWarning w{ filename };
         w("%s requires a version number in the form MAJOR.MINOR[.PATCH]",
           cmd.c_str());
      }
   }
   else
   {
      // Must be a regular option = value
      if (compat_level < option_level(0, 69))
      {
         if (process_option_line_compat_0_68(cmd, args, filename))
         {
            return;
         }
      }

      if (compat_level < option_level(0, 71))
      {
         if (process_option_line_compat_0_70(cmd, filename))
         {
            return;
         }
      }
      const auto oi = option_map.find(cmd);

      if (oi == option_map.end())
      {
         OptionWarning w{ filename };
         w("unknown option '%s'", args[0].c_str());
      }
      else
      {
         UNUSED(oi->second->read(args[1].c_str()));
      }
   }
} // process_option_line


//-----------------------------------------------------------------------------
bool load_option_file(const char *filename, int compat_level)
{
   cpd.line_number = 0;

#ifdef WIN32
   // "/dev/null" not understood by "fopen" in Windows
   if (strcasecmp(filename, "/dev/null") == 0)
   {
      return(true);
   }
#endif

   std::ifstream in;
   in.open(filename, std::ifstream::in);

   if (!in.good())
   {
      OptionWarning w{ filename };
      w("file could not be opened: %s (%d)\n",
        strerror(errno), errno);
      return(false);
   }
   // Read in the file line by line
   std::string line;

   while (std::getline(in, line))
   {
      ++cpd.line_number;
      process_option_line(line, filename, compat_level);
   }
   return(true);
}


//-----------------------------------------------------------------------------
const char *get_eol_marker()
{
   static char eol[3] = { 0x0A, 0x00, 0x00 };

   const auto  &lines = cpd.newline.get();

   for (size_t i = 0; i < lines.size(); ++i)
   {
      eol[i] = static_cast<char>(lines[i]);
   }

   return(eol);
}


//-----------------------------------------------------------------------------
void save_option_file(FILE *pfile, bool with_doc, bool minimal)
{
   int        non_default_values = 0;
   const char *eol_marker        = get_eol_marker();

   fprintf(pfile, "# %s%s", UNCRUSTIFY_VERSION, eol_marker);

   // Print the options by group
   for (auto &og : option_groups)
   {
      bool first = true;

      for (auto *option : og.options)
      {
         const auto val = option->str();

         if (!option->isDefault())
         {
            ++non_default_values;
         }
         else if (minimal)
         {
            continue;
         }
         //....................................................................

         if (with_doc)
         {
            assert(option->description() != nullptr);
            assert(*option->description() != 0);

            if (first)
            {
               fprintf(pfile, "%s#%s", eol_marker, eol_marker);
               print_description(pfile, og.description, eol_marker);
               fprintf(pfile, "#%s", eol_marker);
            }
            fprintf(pfile, "%s", eol_marker);
            print_description(pfile, option->description(), eol_marker);

            const auto ds = option->defaultStr();

            if (!ds.empty())
            {
               fprintf(pfile, "#%s# Default: %s%s",
                       eol_marker, ds.c_str(), eol_marker);
            }
         }
         first = false;

         const int name_len = static_cast<int>(strlen(option->name()));
         const int pad      = name_len < uncrustify::limits::MAX_OPTION_NAME_LEN
                              ? (uncrustify::limits::MAX_OPTION_NAME_LEN - name_len)
                              : 1;

         fprintf(pfile, "%s%*.s= ", option->name(), pad, " ");

         if (option->type() == OT_STRING)
         {
            fprintf(pfile, "\"%s\"", val.c_str());
         }
         else
         {
            fprintf(pfile, "%s", val.c_str());
         }

         if (with_doc)
         {
            const int val_len = static_cast<int>(val.length());
            fprintf(pfile, "%*.s # ", 8 - val_len, " ");

            for (auto pv = option->possibleValues(); *pv; ++pv)
            {
               fprintf(pfile, "%s%s", *pv, pv[1] ? "/" : "");
            }
         }
         fputs(eol_marker, pfile);
      }
   }

   if (with_doc)
   {
      fprintf(pfile, "%s", DOC_TEXT_END);
   }
   print_keywords(pfile);    // Print custom keywords
   print_extensions(pfile);  // Print custom file extensions

   fprintf(pfile, "# option(s) with 'not default' value: %d%s#%s",
           non_default_values, eol_marker, eol_marker);
} // save_option_file

} // namespace uncrustify