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
|
/***************************************************************************
* *
* _____ ____ *
* | __ \ / __ \ _ _ _____ *
* | | \ \ / / \_\ | | | | _ \ *
* | | \ \| | | | | | |_| | *
* | | | || | | | | | ___/ *
* | | / /| | __ | | | | _ \ *
* | |__/ / \ \__/ / | |___| | |_| | *
* |_____/ \____/ |_____|_|_____/ *
* *
* Wiimms source code library *
* *
***************************************************************************
* *
* Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
* *
***************************************************************************
* *
* This library 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 library 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. *
* *
* See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
* *
***************************************************************************/
#include <ctype.h>
#include <getopt.h>
#include <string.h>
#include "dclib-basics.h"
#include "dclib-ui.h"
#include "dclib-debug.h"
//
///////////////////////////////////////////////////////////////////////////////
/////////////// register options ///////////////
///////////////////////////////////////////////////////////////////////////////
enumError RegisterOptionByIndex
(
const InfoUI_t * iu, // valid pointer
int opt_index, // index of option (OPT_*)
int level, // the level of registration
bool is_env // true: register environment pre setting
)
{
ASSERT(iu);
DASSERT(iu->opt_info);
DASSERT(iu->opt_used);
DASSERT(iu->opt_index);
if ( level <= 0 )
return ERR_OK;
if ( opt_index >= 0
&& opt_index < iu->n_opt_total
&& opt_index < UIOPT_INDEX_SIZE )
{
TRACE("OPT: RegisterOptionByIndex(,%02x,%d,%d) opt_index=%02x,%s\n",
opt_index, level, is_env, opt_index,
iu->opt_info[opt_index].long_name );
u8 * obj = iu->opt_used + opt_index;
u32 count = *obj;
if (is_env)
{
if ( count < 0x7f )
{
count += level;
*obj = count < 0x7f ? count : 0x7f;
}
}
else
{
if ( count < 0x80 )
count = 0x80;
count += level;
*obj = count < 0xff ? count : 0xff;
}
return ERR_OK;
}
PRINT("OPT: RegisterOptionbyIndex(,%02x/%02x/%02x,%d,%d) => WARNING\n",
opt_index, iu->n_opt_total, UIOPT_INDEX_SIZE, level, is_env );
return ERR_WARNING;
}
///////////////////////////////////////////////////////////////////////////////
enumError RegisterOptionByName
(
const InfoUI_t * iu, // valid pointer
int opt_name, // short name of GO_* value of option
int level, // the level of registration
bool is_env // true: register environment pre setting
)
{
ASSERT(iu);
DASSERT(iu->opt_index);
if ( opt_name >= 0 && opt_name < UIOPT_INDEX_SIZE )
return RegisterOptionByIndex(iu,iu->opt_index[opt_name],level,is_env);
PRINT("OPT: RegisterOptionbyName(,%02x,%d,%d) => WARNING\n",
opt_name, level, is_env );
return ERR_WARNING;
}
///////////////////////////////////////////////////////////////////////////////
enumError VerifySpecificOptions ( const InfoUI_t * iu, const KeywordTab_t * cmd )
{
ASSERT(iu);
ASSERT(cmd);
ASSERT( cmd->id > 0 && cmd->id < iu->n_cmd );
const InfoCommand_t * ic = iu->cmd_info + cmd->id;
ASSERT(ic->opt_allowed);
u8 * allow = ic->opt_allowed;
u8 * active = iu->opt_used;
TRACE("ALLOWED SPECIFIC OPTIONS:\n");
TRACE_HEXDUMP(10,0,0,-20,allow,iu->n_opt_specific);
TRACE("ACTIVE SPECIFIC OPTIONS:\n");
TRACE_HEXDUMP(10,0,0,-20,active,iu->n_opt_specific);
enumError err = ERR_OK;
int idx;
for ( idx = 0; idx < iu->n_opt_specific; idx++, allow++, active++ )
{
if (!*allow)
{
if ( *active & 0x80 )
{
const InfoOption_t * io = iu->opt_info + idx;
if (io->short_name)
ERROR0(ERR_SEMANTIC,"Command '%s' doesn't allow the option --%s (-%c).\n",
cmd->name1, io->long_name, io->short_name );
else
ERROR0(ERR_SEMANTIC,"Command '%s' doesn't allow the option --%s.\n",
cmd->name1, io->long_name );
err = ERR_SEMANTIC;
}
else
*active = 0; // clear environ settings
}
}
return err;
}
///////////////////////////////////////////////////////////////////////////////
int GetOptionCount ( const InfoUI_t * iu, int option )
{
ASSERT(iu);
ASSERT(iu->opt_info);
ASSERT(iu->opt_used);
if ( option > 0 && option < iu->n_opt_total )
{
TRACE("GetOptionCount(,%02x) option=%s => %d\n",
option, iu->opt_info[option].long_name,
iu->opt_used[option] & UIOPT_USED_MASK );
return iu->opt_used[option] & UIOPT_USED_MASK;
}
TRACE("GetOptionCount(,%02x) => INVALID [-1]\n",option);
return -1;
}
///////////////////////////////////////////////////////////////////////////////
void DumpUsedOptions ( const InfoUI_t * iu, FILE * f, int indent )
{
ASSERT(iu);
ASSERT(iu->opt_info);
ASSERT(iu->opt_used);
TRACE("OptionUsed[]:\n");
TRACE_HEXDUMP16(9,0,iu->opt_used,iu->n_opt_total+1);
if (!f)
return;
indent = NormalizeIndent(indent);
int i;
for ( i = 0; i < iu->n_opt_total; i++ )
{
const u8 opt = iu->opt_used[i];
if (opt)
fprintf(f,"%*s%s %s %2u* [%02x,%s]\n",
indent, "",
i <= iu->n_opt_specific ? "CMD" : "GLB",
opt & 0x80 ? "PRM" : "ENV",
opt & UIOPT_USED_MASK,
i, iu->opt_info[i].long_name );
}
};
///////////////////////////////////////////////////////////////////////////////
void WarnDepractedOptions ( const struct InfoUI_t * iu )
{
DASSERT(iu);
uint i, count = 0;
char buf[PATH_MAX];
char *dest = buf;
for ( i = 0; i < iu->n_opt_total; i++ )
{
if ( iu->opt_used[i] )
{
const InfoOption_t *io = iu->opt_info + i;
if ( io->deprecated )
{
if (io->long_name)
{
count++;
dest = snprintfE(dest,buf+sizeof(buf)," --%s",io->long_name);
}
else if (io->short_name)
{
count++;
dest = snprintfE(dest,buf+sizeof(buf)," -%c",io->short_name);
}
}
}
}
if (count)
ERROR0(ERR_WARNING,
"Attention: Deprecated option%s:%s\n",
count == 1 ? "" : "s", buf );
}
///////////////////////////////////////////////////////////////////////////////
enumError CheckEnvOptions ( ccp varname, check_opt_func func )
{
TRACE("CheckEnvOptions(%s,%p)\n",varname,func);
ccp env = getenv(varname);
if ( !env || !*env )
return ERR_OK;
PRINT("env[%s] = %s\n",varname,env);
#if 1 // [[new]] with ArgManager_t
ArgManager_t am = {0};
AppendArgManager(&am,ProgInfo.progname,0,false);
ScanQuotedArgManager(&am,env,true);
#ifdef DEBUG
for ( int i = 0; i < am.argc; i++ )
printf(" [%02u] |%s|\n",i,am.argv[i]);
#endif
enumError stat = func(am.argc,am.argv,true);
#else // [[old]]
const int envlen = strlen(env);
char * buf = MALLOC(envlen+1);
char * dest = buf;
int argc = 1; // argv[0] = ProgInfo.progname
ccp src = env;
while (*src)
{
while ( *src > 0 && *src <= ' ' ) // skip blanks & control
src++;
if (!*src)
break;
argc++;
while ( *(u8*)src > ' ' )
*dest++ = *src++;
*dest++ = 0;
DASSERT( dest <= buf+envlen+1 );
}
TRACE("argc = %d\n",argc);
char ** argv = MALLOC((argc+1)*sizeof(*argv));
argv[0] = (char*)ProgInfo.progname;
argv[argc] = 0;
dest = buf;
int i;
for ( i = 1; i < argc; i++ )
{
TRACE("argv[%d] = %s\n",i,dest);
argv[i] = dest;
while (*dest)
dest++;
dest++;
DASSERT( dest <= buf+envlen+1 );
}
enumError stat = func(argc,argv,true);
#endif
// don't free() because of possible pointers into argv[]
if (stat)
fprintf(stderr,
"Error while scanning the environment variable '%s'\n",varname);
return stat;
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// text helpers ///////////////
///////////////////////////////////////////////////////////////////////////////
void DumpText
(
FILE * f, // output file, if NULL: 'pbuf' must be set
char * pbuf, // output buffer, ignored if 'f' not NULL
char * pbuf_end, // end of output buffer
ccp text, // source text
int text_len, // >=0: length of text, <0: use strlen(text)
bool is_makedoc, // true: write makedoc format
ccp end // write this text at the end of all
)
{
ASSERT(f||pbuf);
ccp tie = is_makedoc ? " \\" : "";
char buf[100], *buf_end = buf + 70, *dest = buf;
bool set_apos = false;
bool brace_active = false;
if (!text)
text = "";
if ( text_len < 0 )
text_len = strlen(text) ;
ccp text_end = text + text_len;
while ( text < text_end )
{
char * dest_break = 0;
ccp text_break = 0;
bool eol = false;
while ( !eol && text < text_end && dest < buf_end )
{
if ( *text >= 1 && *text <= 3 )
{
// \1 : Text only for built in help
// \2 : Text only for ui.def
// \3 : Text for all (default)
const bool skip = *text == ( is_makedoc ? 1 : 2 );
//PRINT("CH=%d, SKIP=%d\n",*text,skip);
text++;
if (skip)
while ( text < text_end && (u8)*text > 3 )
text++;
continue;
}
switch (*text)
{
case 0:
// ignore
break;
case ' ':
if ( dest > buf )
{
dest_break = dest;
text_break = text;
}
*dest++ = *text++;
break;
case '\n':
eol = true;
*dest++ = '\\';
*dest++ = 'n';
text++;
dest_break = 0;
text_break = 0;
break;
case '\\':
case '"':
*dest++ = '\\';
*dest++ = *text++;
break;
case '{':
if (is_makedoc)
*dest++ = *text++;
else if ( *++text == '{' )
*dest++ = *text++;
else
{
brace_active = true;
ccp ptr = text;
while ( ptr < text_end && *ptr > ' ' && *ptr != '}' )
ptr++;
set_apos = *ptr != '}';
if (set_apos)
*dest++ = '\'';
}
break;
case '}':
if ( is_makedoc || !brace_active )
*dest++ = *text++;
else if ( *++text == '}' )
*dest++ = *text++;
else
{
brace_active = false;
if (set_apos)
{
set_apos = false;
*dest++ = '\'';
}
}
break;
case '@':
case '$':
if (!is_makedoc)
{
if ( text[1] == *text )
*dest++ = *text++;
text++;
break;
}
// fall through
default:
*dest++ = *text++;
}
DASSERT( dest < buf_end + 10 );
}
if ( dest >= buf_end && dest_break )
{
dest = dest_break;
text = text_break;
}
if ( dest > buf && text < text_end )
{
*dest = 0;
if (f)
fprintf(f,"\t\"%s\"%s\n",buf,tie);
else
pbuf = snprintfE(pbuf,pbuf_end,"\t\"%s\"%s\n",buf,tie);
dest = buf;
}
}
*dest = 0;
if (f)
fprintf(f,"\t\"%s\"%s",buf,end);
else
snprintf(pbuf,pbuf_end-pbuf,"\t\"%s\"%s",buf,end);
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// print options ///////////////
///////////////////////////////////////////////////////////////////////////////
static void print_single_option
(
FILE * f, // valid output stream
int indent, // normalized indent of output
const InfoOption_t * io, // option to print
uint opt_fw, // field width for options names
uint disp_fw // total field width of displayed output
)
{
DASSERT(f);
DASSERT(io);
if ( !io->short_name && !io->long_name )
{
fputc('\n',f);
return;
}
ccp col_reset, col_param;
if (IsFileColorized(f))
{
fputs(GetTextMode(1,TTM_COL_OPTION),f);
col_param = io->param ? GetTextMode(1,TTM_COL_PARAM) : "";
col_reset = TermTextModeReset;
}
else
col_reset = col_param = "";
ccp iop = io->param;
ccp sep = !iop ? ""
: !io->optional_parm ? " "
: iop[0] == '=' || iop[0] == '[' && iop[0] == '=' ? ""
: "=";
int len;
if (!io->short_name)
len = fprintf(f,"%*s --%s%s%s%s ",
indent, "",
io->long_name,
sep,
col_param, iop ? iop : "" );
else if (!io->long_name)
len = fprintf(f,"%*s -%c%s%s%s ",
indent, "",
io->short_name,
iop ? " " : "",
col_param, iop ? iop : "" );
else
len = fprintf(f,"%*s -%c --%s%s%s%s ",
indent, "",
io->short_name,
io->long_name,
sep,
col_param, iop ? iop : "" );
len -= strlen(col_param);
if ( len > opt_fw )
{
fprintf(f,"%s\n",col_reset);
PutLines(f,opt_fw,disp_fw,0,0,io->help,0);
}
else
{
fputs(col_reset,f);
PutLines(f,opt_fw,disp_fw,len,0,io->help,0);
}
}
///////////////////////////////////////////////////////////////////////////////
static inline bool is_opt_selected
(
const InfoUI_t * iu, // valid pointer
const InfoOption_t * io, // active option
bool use_filter // true: user filter
)
{
DASSERT(io);
return use_filter
? ( iu->opt_used[io->id] & 0x80 ) != 0
: !io->hidden;
}
///////////////////////////////////////////////////////////////////////////////
static void print_help_options
(
const InfoUI_t * iu, // valid pointer
FILE * f, // valid output stream
bool filter_opt, // true: filter options by 'iu->opt_used'
int indent, // indent of output
uint beg_index, // index of first option to print
uint end_index, // index of last option to print +1
ccp title // NULL or title text
)
{
DASSERT(iu);
if ( end_index > iu->n_opt_total )
end_index = iu->n_opt_total;
if ( beg_index >= end_index || !f )
return;
if (title)
{
const ColorMode_t colmode = GetFileColorized(f);
fprintf(f,"%*s%s%s:%s\n\n",
indent,"",
GetTextMode(colmode,TTM_COL_HEADING), title,
GetTextMode(colmode,TTM_RESET) );
}
// const int fw = GetTermWidth(80,40) - 1;
const int fw = GetGoodTermWidth(0,false) - 1;
const InfoOption_t *io_beg = iu->opt_info + beg_index;
const InfoOption_t *io_end = iu->opt_info + end_index;
const InfoOption_t *io;
// calculate the base option field width
int base_opt_fw = 0;
for ( io = io_beg; io < io_end; io++ )
{
if ( io->long_name && is_opt_selected(iu,io,filter_opt) )
{
int len = strlen(io->long_name);
if (io->param)
len += 1 + strlen(io->param);
if ( base_opt_fw < len )
base_opt_fw = len;
}
}
const int max_fw = 2 + (fw+5)/8;
if ( base_opt_fw > max_fw )
base_opt_fw = max_fw;
// calculate the option field width
int opt_fw = 0;
for ( io = io_beg; io < io_end; io++ )
{
if ( io->long_name && is_opt_selected(iu,io,filter_opt) )
{
int len = strlen(io->long_name);
if (io->param)
len += 1 + strlen(io->param);
if ( len <= base_opt_fw && opt_fw < len )
opt_fw = len;
}
}
opt_fw += indent + 9;
for ( io = io_beg; io < io_end; io++ )
{
if ( is_opt_selected(iu,io,filter_opt) )
{
if (io->separator)
fputc('\n',f);
print_single_option(f,indent,io,opt_fw,fw);
}
}
fputc('\n',f);
}
///////////////////////////////////////////////////////////////////////////////
void PrintHelpOptions
(
const InfoUI_t * iu, // valid pointer
FILE * f, // valid output stream
int indent // indent of output
)
{
DASSERT(iu);
bool filter_opt = false;
uint i;
for ( i = 1; i < iu->n_opt_total; i++ )
{
const u8 used = iu->opt_used[i];
if ( used & 0x80 && ( used != 0x81 || !iu->opt_info[i].ignore ) )
{
filter_opt = true;
break;
}
}
fputc('\n',f);
if (filter_opt)
{
print_help_options( iu, f, true, indent,
1, iu->n_opt_total,
"Selected options with common description" );
}
else
{
print_help_options( iu, f, false, indent,
iu->n_opt_specific+1, iu->n_opt_total,
"Global options" );
print_help_options( iu, f, false, indent,
1, iu->n_opt_specific,
"Command specific options with common description" );
}
}
///////////////////////////////////////////////////////////////////////////////
void PrintHelpCommands
(
const InfoUI_t * iu, // valid pointer
FILE * f, // valid output stream
int indent, // indent of output
ccp help_cmd // NULL or name of help command
)
{
DASSERT(iu);
if ( !iu->n_cmd )
return;
uint col_len;
ccp col_head, col_cmd, col_reset;
if ( IsFileColorized(f))
{
col_head = GetTextMode(1,TTM_COL_HEADING);
col_cmd = GetTextMode(1,TTM_COL_CMD);
col_reset = TermTextModeReset;
col_len = strlen(col_cmd) + strlen(col_reset);
}
else
{
col_head = col_cmd = col_reset = "";
col_len = 0;
}
fprintf(f,"\n%*s%sCommands:%s\n\n", indent,"", col_head, col_reset );
// const int fw = GetTermWidth(80,40) - 1;
const int fw = GetGoodTermWidth(0,false) - 1;
int fw1 = 0, fw2 = 0;
const InfoCommand_t * ic;
for ( ic = iu->cmd_info; ic->name1; ic++ )
if (!ic->hidden)
{
const int len = strlen(ic->name1);
if ( fw1 < len )
fw1 = len;
if ( ic->name2 )
{
const int len = strlen(ic->name2);
if ( fw2 < len )
fw2 = len;
}
}
const int fw12 = fw1 + ( fw2 ? fw2 + 3 : 0 );
for ( ic = iu->cmd_info+1; ic->name1; ic++ )
if (!ic->hidden)
{
if (ic->separator)
fputc('\n',f);
int len;
if ( ic->name2 )
len = fprintf(f,"%*s %s%-*s%s | %s%-*s%s : ",
indent, "",
col_cmd, fw1, ic->name1, col_reset,
col_cmd, fw2, ic->name2, col_reset )
- 2*col_len;
else
len = fprintf(f,"%*s %s%-*s%s : ",
indent, "",
col_cmd, fw12, ic->name1, col_reset )
- col_len;
PutLines(f,indent+len,fw,len,0,ic->help,0);
}
if (help_cmd)
fprintf(f,
"\n%*sType '%s %s command' to get command specific help.\n\n",
indent, "", iu->tool_name, help_cmd );
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// print help ///////////////
///////////////////////////////////////////////////////////////////////////////
void PrintHelp
(
const InfoUI_t * iu, // valid pointer
FILE * f, // valid output stream
int indent, // indent of output
ccp help_cmd, // NULL or name of help command
ccp info, // NULL or additional text
ccp base_uri, // NULL or base URI for a external help link
ccp first_param // NULL or first param of argv[]
)
{
ASSERT(iu);
int cmd = 0;
if ( iu->n_cmd && first_param && *first_param )
{
int cmd_stat;
const KeywordTab_t * ct = ScanKeyword(&cmd_stat,first_param,iu->cmd_tab);
if (ct)
cmd = ct->id;
else
{
char buf[100];
StringCat2S(buf,sizeof(buf),"+",first_param);
ct = ScanKeyword(&cmd_stat,buf,iu->cmd_tab);
if (ct)
cmd = ct->id;
else
{
char key_buf[12];
char *dest = key_buf;
char *end = key_buf + sizeof(key_buf) - 1;
while ( *first_param && dest < end )
*dest++ = toupper((int)*first_param++);
*dest = 0;
const int key_len = dest - key_buf;
if ( key_len >= 3 )
{
if (!memcmp(key_buf,"OPTIONS",key_len) )
{
PrintHelpOptions(iu,f,indent);
return;
}
if ( !memcmp(key_buf,"COMMANDS",key_len)
|| !memcmp(key_buf,"CMD",key_len) )
{
PrintHelpCommands(iu,f,indent,help_cmd);
return;
}
}
}
}
}
PrintHelpCmd(iu,f,indent,cmd,help_cmd,info,base_uri);
}
///////////////////////////////////////////////////////////////////////////////
void PrintHelpCmd
(
const InfoUI_t * iu, // valid pointer
FILE * f, // valid output stream
int indent, // indent of output
int cmd, // index of command
ccp help_cmd, // NULL or name of help command
ccp info, // NULL or poiner to additional text
ccp base_uri // NULL or base URI for a external help link
)
{
ASSERT(iu);
if (!f)
return;
indent = NormalizeIndent(indent);
if ( cmd < 0 || cmd >= iu->n_cmd )
cmd = 0;
const InfoCommand_t * ic = iu->cmd_info + cmd;
// const int fw = GetTermWidth(80,40) - 1;
const int fw = GetGoodTermWidth(0,false) - 1;
uint col_len;
ccp col_head, col_reset;
if (IsFileColorized(f))
{
col_head = GetTextMode(1,TTM_COL_HEADING);
col_reset = GetTextMode(1,TTM_RESET);
col_len = strlen(col_head) + strlen(col_reset);
}
else
{
col_head = col_reset = "";
col_len = 0;
}
char iobuf[10000];
if (!cmd)
snprintf(iobuf,sizeof(iobuf),"%s",iu->tool_name );
else if (ic->name2)
snprintf(iobuf,sizeof(iobuf),"%s %s|%s",iu->tool_name,ic->name1,ic->name2);
else
snprintf(iobuf,sizeof(iobuf),"%s %s",iu->tool_name,ic->name1);
int len = fprintf(f,"\n%*s%s : ",indent,"",iobuf) - 1;
PutLines(f,indent+len,fw,len,0, ic->xhelp ? ic->xhelp : ic->help, 0);
len = fprintf(f,"\n%*s%sSyntax:%s ", indent, "", col_head, col_reset ) - 1 - col_len;
PutLines(f,indent+len,fw,len,0,ic->syntax,0);
fputc('\n',f);
if (!cmd)
PrintHelpCommands(iu,f,indent,help_cmd);
if ( ic->n_opt )
{
fprintf(f,"%*s%s%sptions:%s\n\n",
indent,"", col_head,
!cmd && iu->n_cmd ? "Global o" : "O",
col_reset );
const InfoOption_t ** iop;
// calculate the base option field width
int base_opt_fw = 0;
for ( iop = ic->opt; *iop; iop++ )
{
const InfoOption_t * io = *iop;
if (io->long_name)
{
int len = strlen(io->long_name);
if (io->param)
len += 1 + strlen(io->param);
if ( base_opt_fw < len )
base_opt_fw = len;
}
}
const int max_fw = 2 + (fw+5)/8;
if ( base_opt_fw > max_fw )
base_opt_fw = max_fw;
// calculate the option field width
int opt_fw = 0;
for ( iop = ic->opt; *iop; iop++ )
{
const InfoOption_t * io = *iop;
if (io->long_name)
{
int len = strlen(io->long_name);
if (io->param)
len += 1 + strlen(io->param);
if ( len <= base_opt_fw && opt_fw < len )
opt_fw = len;
}
}
opt_fw += indent + 9;
for ( iop = ic->opt; *iop; iop++ )
print_single_option(f,indent,*iop,opt_fw,fw);
fputc('\n',f);
}
if (info)
fputs(info,f);
if ( base_uri && *base_uri )
{
if (!cmd)
fprintf(f,"%*sMore help is available at %s%s\n\n",
indent, "", base_uri, iu->tool_name );
else if (!ic->hidden)
{
char *dest = iobuf;
ccp src;
for ( src = ic->name1; *src; src++ )
*dest++ = *src == '_' ? '-' : tolower((int)*src);
*dest = 0;
fprintf(f,"%*sMore help is available at %scmd/%s/%s\n\n",
indent, "", base_uri, iu->tool_name, iobuf );
}
}
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// END ///////////////
///////////////////////////////////////////////////////////////////////////////
|