-
Notifications
You must be signed in to change notification settings - Fork 27
/
txt2regex.sh
executable file
·1563 lines (1401 loc) · 41.9 KB
/
txt2regex.sh
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
#!/bin/bash
# txt2regex.sh - Regular Expressions "wizard" made with Bash builtins
#
# Website : https://aurelio.net/projects/txt2regex/
# Author : Aurelio Jargas ([email protected])
# License : GPL
# Requires: bash >= 3.0
#
# shellcheck disable=SC1117,SC2034
# SC1117 because it was obsoleted in shellcheck >0.5
# SC2034 because it considers unused vars that I load with eval (ax_*)
#
# Please, read the README file.
#
# $STATUS:
# 0 beginning of the regex
# 1 defining regex
# 12 choosing subregex
# 2 defining quantifier
# 3 really quit?
# 4 choosing session programs
# 9 end of the regex
#
# 20001019 ** 1st version
# 20001026 ++ lots of changes and tests
# 20001028 ++ improvements, public release
# 20001107 ++ bash version check (thanks eliphas)
# 20001113 ++ php support, Progs command
# 20010223 ++ i18n, --all, freshmeat announce (oh no!)
# 20010223 v0.1
# 20010420 ++ id.po, \lfunction_name, s/regexp/regex/ig
# 20010423 ++ --nocolor, --history, Usage(), doNextHist{,Args}()
# ++ flags: interactive, color, allprogs
# ++ .oO(¤user parameters history)
# 20010424 v0.2
# 20010606 ++ option --whitebg
# -- grep from $progs to fit on 24 lines by default
# 20010608 -- clear command (not bash), ++ Clear()
# -- stty command (not bash), ++ $LINES
# -- *Progs*(), ++ Choice(), ChoiceRefresh()
# ++ POSIX character classes [[:abc:]]
# ++ special combinations inside []
# ++ $HUMAN improved with getString, getNumber, Choice
# ++ detailed --help, moved to sourceforge
# 20010613 v0.3
# 20010620 -- seq command (not bash), ++ sek()
# 20010613 v0.3.1
# 20010731 ++ Reset: "RegEx prog :" with automatic length
# ++ new progs: postgres, javascript, vbscript, procmail
# ++ ax_prog: new item: escape char - escape is ok now
# ++ improved meta knowledge on perl, tcl and gawk
# 20010802 v0.4
# 20010821 ++ ShowMeta(), new option: --showmeta
# 20010824 ++ getMeta(), ShowInfo(), new option: --showinfo, $cR color
# 20010828 ++ getItemIndex(), getLargestItem()
# <> Clear(): using \033c, ALL: using for((;;)) ksh syntax
# <> vi == Nvi
# 20010828 v0.5
# 20010831 ++ group & or support- cool!, clearN()
# ++ nice groups balance check -> ((2)), use $COLUMNS
# <> TopTitle(): BLOAT, 3 lines, smart, arrays
# <> Menu(): s/stupid recursion/while/
# ++ Z status to handle 0,menu,0 situation
# <> s/eval/${!var}/
# 20010903 <> Choice: fixed outrange answers
# ++ trapping ^c do clearEnd, ++ new prog: mysql
# ++ history now works with Choice() menus
# ++ history appears when quitting
# 20010905 v0.6
# 20020225 ++ "really quit?" message, ++ --version
# 20020304 <> --history just shows final RE on STDOUT
# ++ --make, --prog, printError()
# ++ groups are now quantifiable
# ++ ready_(date[123], hour[123], number[123])
# 20020304 v0.7
# 20040928 <> bash version test (works in 3.x and newer)
# 20040928 v0.8
# 20040929 <> --help split into individual messages (helps i18n)
# 20051229 <> fixed bug on bash3 for eval contents (thanks Marcus Habermehl)
# 20121221 ** moved to GitHub, please see the Git history from now on
# Every command in this script is a Bash builtin. This is by design.
# Make sure we don't break that rule in future code by strictly
# disallowing any system command.
export PATH=
TEXTDOMAIN=txt2regex
TEXTDOMAINDIR=po
VERSION=0.10b
printError() {
printf '%s: ' $"ERROR"
# shellcheck disable=SC2059
printf "$@"
exit 1
}
case "$BASH_VERSION" in
[3-9].*)
: # do nothing
;;
*)
printError 'Bash version >=3.0 required, but you have %s\n' "$BASH_VERSION"
;;
esac
Usage() {
# Ugly code, but isolates in $"..." only the strings that need
# translation and tries to keep the option descriptions aligned even
# when long words are used as meta vars.
printf '%s txt2regex [--nocolor|--whitebg] [--all|--prog %s]\n' \
$"usage:" $"PROGRAMS"
printf '%s txt2regex --showmeta\n' \
$"usage:"
printf '%s txt2regex --showinfo %s [--nocolor]\n' \
$"usage:" $"PROGRAM"
printf '%s txt2regex --history %s [--all|--prog %s]\n' \
$"usage:" $"VALUE" $"PROGRAMS"
printf '%s txt2regex --make %s [--all|--prog %s]\n' \
$"usage:" $"LABEL" $"PROGRAMS"
printf '\n'
printf '%s\n' $"Options:"
printf ' %-22s%s\n' '--all' \
$"Select all the available programs"
printf ' %-22s%s\n' '--nocolor' \
$"Do not use colors"
printf ' %-22s%s\n' '--whitebg' \
$"Adjust colors for white background terminals"
printf ' %-22s%s\n' '--prog '$"PROGRAMS" \
$"Specify which programs to use, separated by commas"
printf '\n'
printf ' %-22s%s\n' '--showmeta' \
$"Print a metacharacters table featuring all the programs"
printf ' %-22s%s\n' '--showinfo '$"PROGRAM" \
$"Print regex-related info about the specified program"
printf ' %-22s%s\n' '--history '$"VALUE" \
$"Print a regex from the given history data"
printf ' %-22s%s\n' '--make '$"LABEL" \
$"Print a ready regex for the specified label"
printf '\n'
printf ' %-22s%s\n' '-V, --version' \
$"Print the program version and quit"
printf ' %-22s%s\n' '-h, --help' \
$"Print the help message and quit"
printf '\n'
exit "${1:-0}" # $1 is the exit code (default is 0)
}
# The defaults
is_interactive=1
use_colors=1
has_white_background=0
has_not_supported=0
mode_show_meta=0
mode_show_info=0
GRP1=0
GRP2=0
# Here's the default list of programs shown.
# Edit here or use --prog to overwrite it.
progs=(python egrep grep sed vim emacs)
### IMPORTANT DATA ###
# To generate this array:
# grep version: tests/regex-tester.txt | sort | cut -d ' ' -f 1
allprogs=(
awk
chicken
ed
egrep
emacs
expect
find
gawk
grep
javascript
lex
mawk
mysql
perl
php
postgres
procmail
python
sed
tcl
vi
vim
)
# To generate this array:
# grep version: tests/regex-tester.txt | sort | sed "s/.* version: //;s/.*/'&'/"
allversions=(
'awk version 20121220'
'CHICKEN 4.12.0'
'GNU Ed 1.10'
'grep (GNU grep) 3.1'
'GNU Emacs 25.2.2'
'expect version 5.45.4'
'find (GNU findutils) 4.7.0-git'
'GNU Awk 4.1.4'
'grep (GNU grep) 3.1'
'node v8.10.0'
'flex 2.6.4'
'mawk 1.3.3 Nov 1996'
'mysql Ver 14.14 Distrib 5.7.29'
'perl v5.26.1'
'PHP 7.2.24-0ubuntu0.18.04.4'
'psql (PostgreSQL) 10.12'
'procmail v3.23pre 2001/09/13'
'Python 3.6.9'
'sed (GNU sed) 4.4'
'tcl 8.6'
'nvi 1.81.6-13'
'VIM - Vi IMproved 8.0 (2016 Sep 12)'
)
label_names=(
date
date2
date3
hour
hour2
hour3
number
number2
number3
)
label_descriptions=(
'date LEVEL 1: mm/dd/yyyy: matches from 00/00/0000 to 99/99/9999'
'date LEVEL 2: mm/dd/yyyy: matches from 00/00/1000 to 19/39/2999'
'date LEVEL 3: mm/dd/yyyy: matches from 00/00/1000 to 12/31/2999'
'hour LEVEL 1: hh:mm: matches from 00:00 to 99:99'
'hour LEVEL 2: hh:mm: matches from 00:00 to 29:59'
'hour LEVEL 3: hh:mm: matches from 00:00 to 23:59'
'number LEVEL 1: integer, positive and negative'
'number LEVEL 2: level 1 plus optional float point'
'number LEVEL 3: level 2 plus optional commas, like: 34,412,069.90'
)
label_data=(
# date
'26521652165¤:2¤2¤/¤:2¤2¤/¤:2¤4'
'24161214161214165¤01¤:2¤/¤0123¤:2¤/¤12¤:2¤3'
'2(2161|2141)121(2161|4161|2141)1214165¤0¤:2¤1¤012¤/¤0¤:2¤12¤:2¤3¤01¤/¤12¤:2¤3'
# hour
'2652165¤:2¤2¤:¤:2¤2'
'24161214161¤012¤:2¤:¤012345¤:2'
'2(4161|2141)1214161¤01¤:2¤2¤0123¤:¤012345¤:2'
# number
'24264¤+-¤:2'
'24264(2165)2¤+-¤:2¤.¤:2¤2'
'24266(2165)3(2165)2¤+-¤:2¤3¤,¤:2¤3¤.¤:2¤2'
)
#date3 : perl: (0[0-9]|1[012])/(0[0-9]|[12][0-9]|3[01])/[12][0-9]{3}
#hour3 : perl: ([01][0-9]|2[0123]):[012345][0-9]
#number3: perl: [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
### -- ###
getItemIndex() { # item, array_items
local item="$1"
local i=0
shift
while [ -n "$1" ]; do
[ "$1" == "$item" ] && printf '%d\n' "$i" && return
i=$((i + 1))
shift
done
}
validateProgramNames() {
local name
for name in "$@"; do
[ -z "$(getItemIndex "$name" "${allprogs[@]}")" ] &&
printError '%s: %s\n' $"unknown program" "$name"
done
}
# Parse command line options
while [ $# -gt 0 ]; do
case "$1" in
--history)
[ -z "$2" ] && Usage 1
history="$2"
shift
is_interactive=0
use_colors=0
hists="0${history%%¤*}"
histargs="¤${history#*¤}"
[ "${hists#0}" == "${histargs#¤}" ] && unset histargs
;;
--make)
shift
is_interactive=0
use_colors=0
label_name="${1%1}" # final 1 is optional (date1 == date)
label_index=$(getItemIndex "$label_name" "${label_names[@]}")
# Sanity check
[ -z "$label_index" ] &&
printError '%s: "%s": %s\n%s %s\n' \
'--make' "$1" $"invalid argument" \
$"valid names:" "${label_names[*]}"
# Set history data
hist="${label_data[$label_index]}"
hists="0${hist%%¤*}"
histargs="¤${hist#*¤}"
printf '\n### %s\n\n' "${label_descriptions[$label_index]}"
;;
--prog)
[ -z "$2" ] && Usage 1
shift
eval "progs=(${1//,/ })"
validateProgramNames "${progs[@]}"
;;
--nocolor)
use_colors=0
;;
--whitebg)
has_white_background=1
;;
--showmeta)
mode_show_meta=1
;;
--showinfo)
[ -z "$2" ] && Usage 1
infoprog="$2"
shift
mode_show_info=1
validateProgramNames "$infoprog"
;;
--all)
progs=("${allprogs[@]}")
;;
-V | --version)
printf 'txt2regex %s\n' "$VERSION"
exit 0
;;
-h | --help)
Usage 0
;;
*)
printf '%s: %s\n\n' "$1" $"invalid option"
Usage 1
;;
esac
shift
done
set -o noglob
### The Regex show
S0_txt=(
$"start to match"
$"on the line beginning"
$"in any part of the line"
)
S0_re=(
''
'^'
''
)
S1_txt=(
$"followed by"
$"any character"
$"a specific character"
$"a literal string"
$"an allowed characters list"
$"a forbidden characters list"
$"a special combination"
$"a POSIX combination (locale aware)"
$"a ready regex (not implemented)"
$"anything"
)
S1_re=(
''
'.'
''
''
''
''
''
''
''
'.*'
)
S2_txt=(
$"how many times (repetition)"
$"one"
$"zero or one (optional)"
$"zero or more"
$"one or more"
$"exactly N"
$"up to N"
$"at least N"
)
# COMBO
combo_txt=(
$"uppercase letters"
$"lowercase letters"
$"numbers"
$"underscore"
$"space"
$"TAB"
)
combo_re=(
'A-Z'
'a-z'
'0-9'
'_'
' '
'@'
)
#TODO use all posix components?
posix_txt=(
$"letters"
$"lowercase letters"
$"uppercase letters"
$"numbers"
$"letters and numbers"
$"hexadecimal numbers"
$"whitespaces (space and TAB)"
$"graphic chars (not-whitespace)"
)
posix_re=(
'alpha'
'lower'
'upper'
'digit'
'alnum'
'xdigit'
'blank'
'graph'
)
# Title (line 1)
# shellcheck disable=SC2256
tit1_txt=(
$"quit"
$"reset"
$"color"
$"programs"
''
''
''
''
''
'^txt2regex$'
)
tit1_cmd=(
'.'
'0'
'*'
'/'
''
''
''
''
''
''
)
# Title (line 2-3)
tit2_txt=(
$"or"
$"open group"
$"close group"
''
''
''
''
''
''
$"not supported"
)
tit2_cmd=(
'|'
'('
')'
''
''
''
''
''
''
'!!'
)
# S2_* arrays: The list of quantifiers (to be used when STATUS=2)
# Every array will be named S2_<prog>: S2_awk, S2_ed, S2_egrep, ...
# The array index refers to the menu item in the "repetition" screen.
# To update this data:
# make test-regex
# grep ' S2 .*OK$' tests/regex-tester.txt
#
while read -r prog_id data; do
# Set the S2_<prog> array for each line. Example:
# S2_egrep=('-' '-' '?' '*' '+' '{@}' '{1,@}' '{@,}')
read -r -a "S2_$prog_id" <<< "$data"
done << 'EOD'
awk - - ? * + !! !! !!
chicken - - ? * + {@} {1,@} {@,}
ed - - \? * \+ \{@\} \{1,@\} \{@,\}
egrep - - ? * + {@} {1,@} {@,}
emacs - - ? * + \\{@\\} \\{1,@\\} \\{@,\\}
expect - - ? * + {@} {1,@} {@,}
find - - ? * + {@} {1,@} {@,}
gawk - - ? * + {@} {1,@} {@,}
grep - - \? * \+ \{@\} \{1,@\} \{@,\}
javascript - - ? * + {@} {1,@} {@,}
lex - - ? * + {@} {1,@} {@,}
mawk - - ? * + !! !! !!
mysql - - ? * + {@} {1,@} {@,}
perl - - ? * + {@} {1,@} {@,}
php - - ? * + {@} {1,@} {@,}
postgres - - ? * + {@} {1,@} {@,}
procmail - - ? * + !! !! !!
python - - ? * + {@} {1,@} {@,}
sed - - \? * \+ \{@\} \{1,@\} \{@,\}
tcl - - ? * + {@} {1,@} {@,}
vi - - \{0,1\} * \{1,\} \{@\} \{1,@\} \{@,\}
vim - - \= * \+ \{@} \{1,@} \{@,}
EOD
# ax_* arrays: Extra regex-related data for all the programs.
# Every array will be named ax_<prog>: ax_awk, ax_ed, ax_egrep, ...
# To check how this data is used in this source code, search for
# something like 'ax_.*5'.
#
# To update this data:
# make test-regex
# grep -E ' ax123 .+OK$' tests/regex-tester.txt # 1,2,3
# grep -E ' a\.b +OK$' tests/regex-tester.txt # 4
# grep -E ' ax5 .+OK$' tests/regex-tester.txt # 5
# grep -E ' ax6 ' tests/regex-tester.txt # 6
# grep -E ' ax7 ' tests/regex-tester.txt # 7
# grep -E ' ax8 ' tests/regex-tester.txt # 8
#
# In PHP, we're using \\ instead of \ as the escape metacharacter
# because it works consistently, being it inside single or double
# quotes. Using only \ would work in some cases, but not in others:
# The literal + is matched by: \+ \\+ [+] [\+] [\\+]
# The literal \ is matched by: \\\\ [\\\\]
#
while read -r prog_id data; do
# Set the ax_<prog> array for each line. Example:
# ax_awk=('' '|' '(' ')' '\' '\.*[---()|+?^$' '\' 'P' '\t')
read -r -a "ax_$prog_id" <<< "$data"
done << 'EOD'
awk - | ( ) \ \.*[---()|+?^$ \ P \t
chicken - | ( ) \\ \.*[---()|+?^$ \ P \t
ed - \| \( \) \ \.*[---------- - P -
egrep - | ( ) \ \.*[-{-(-|+?^$ - P -
emacs - \\| \\( \\) \\ \.*[------+?-- \ P \t
expect - | ( ) \ \.*[-{}()|+?^$ \ P \t
find - | ( ) \ \.*[-{-(-|+?^$ - P -
gawk - | ( ) \ \.*[---(-|+?^$ \ P \t
grep - \| \( \) \ \.*[---------- - P -
javascript - | ( ) \ \.*[---()|+?^$ \ - \t
lex - | ( ) \ \.*[-{}()|+?-- \ P \t
mawk - | ( ) \ \.*[---()|+?^$ \ - \t
mysql - | ( ) \\ \.*[---(-|+?^$ \ P \t
perl - | ( ) \ \.*[-{-()|+?^$ \ P \t
php - | ( ) \\ \.*[-{-()|+?^$ \ P \t
postgres - | ( ) \ \.*[---()|+?^$ \ P \t
procmail - | ( ) \ \.*[---()|+?^$ - - -
python - | ( ) \ \.*[-{-()|+?^$ \ - \t
sed - \| \( \) \ \.*[---------- - P \t
tcl - | ( ) \ \.*[-{}()|+?^$ \ P \t
vi - !! \( \) \ \.*[---------- - P -
vim - \| \( \) \ \.*[---------- \ P \t
EOD
# \.*[]{}()|+?^$ -=false
# [0] Unused
# [1] Which is the metacharacter for alternatives?
# [2,3] Which are the metacharacters for grouping?
# [4] Which is the escape metacharacter?
# [5] Which chars of \.*[]{}()|+?^$ need to be escaped to be matched as
# literals? Note that txt2regex has menus to insert all of those as
# metacharacters (except $), so in user input they will always be
# literal. For ^ and $, some tools consider them literal when not in
# their special start/end position (marked here as -).
# [6] To match '\' inside [], do you need to escape it? If yes, use '\'.
# [7] Has support for [[:POSIX:]] character classes? If yes, use 'P'.
# [8] Does \t inside [] match a tab? If yes, use '\t'.
ColorOnOff() {
# The colors: Normal, Prompt, Bold, Important
[ "$use_colors" -eq 0 ] && return
if [ -n "$cN" ]; then
unset cN cP cB cI cR
elif [ "$has_white_background" -eq 0 ]; then
cN=$(printf '\033[m') # normal
cP=$(printf '\033[1;31m') # red
cB=$(printf '\033[1;37m') # white
cI=$(printf '\033[1;33m') # yellow
cR=$(printf '\033[7m') # reverse
else
cN=$(printf '\033[m') # normal
cP=$(printf '\033[31m') # red
cB=$(printf '\033[32m') # green
cI=$(printf '\033[34m') # blue
cR=$(printf '\033[7m') # reverse
fi
}
# Emulate the 'seq N' command
sek() {
local z="$1"
local a=1
while [ "$a" -le "$z" ]; do
printf '%d\n' "$a"
a=$((a + 1))
done
}
# Is the $1 char present in the $2 text?
charInText() {
local char="$1"
local text="$2"
local i
for ((i = 0; i < ${#text}; i++)); do
[ "${text:$i:1}" == "$char" ] && return 0
done
return 1
}
# Remove all duplicated chars from the $1 text
uniqChars() {
local text="$1"
local text_uniq=''
local i
for ((i = 0; i < ${#text}; i++)); do
charInText "${text:$i:1}" "$text_uniq" ||
text_uniq="$text_uniq${text:$i:1}"
done
printf '%s\n' "$text_uniq"
}
# Escape each $1 in $2 using $3
escapeChars() {
local special_chars="$1"
local text="$2"
local escape_char="${3:-\\}"
local escaped_text
local i
local this_char
for ((i = 0; i < ${#text}; i++)); do
this_char=${text:$i:1}
if charInText "$this_char" "$special_chars"; then
if [ "$this_char$this_char" == "$escape_char" ]; then
# Special case: this_char=\ and escape_char=\\
# The normal escaping (see the next else) would make \\\
# (which is wrong). Here we ensure \\\\ is produced.
escaped_text="$escaped_text$escape_char$escape_char"
else
# normal escaping
escaped_text="$escaped_text$escape_char$this_char"
fi
else
# no escaping
escaped_text="$escaped_text$this_char"
fi
done
printf '%s\n' "$escaped_text"
}
getLargestItem() {
local largest
while [ -n "$1" ]; do
[ ${#1} -gt ${#largest} ] && largest="$1"
shift
done
printf '%s\n' "$largest"
}
# Used to get values from the S2_* and ax_* metachar arrays
getMeta() { # var-name index
local m="$1[$2]"
m=${!m}
# Remove all non-metacharacters: @ ! -
# Those are used only internally as markers
m=${m//[@!-]/}
# Remove when getting '?' or '+' for 'vi', since they are unsupported
# and the current values are workarounds using '{}'
[ "$1" == S2_vi ] && { [ "$2" -eq 2 ] || [ "$2" -eq 4 ]; } && m=''
printf '%s\n' "$m"
}
ShowMeta() {
local i g1 g2 prog progsize
progsize=$(getLargestItem "${allprogs[@]}")
for ((i = 0; i < ${#allprogs[@]}; i++)); do
prog=${allprogs[$i]}
g1=$(getMeta "ax_$prog" 2)
g2=$(getMeta "ax_$prog" 3)
printf "\n%-${#progsize}s" "$prog" # name
printf '%7s' "$(getMeta "S2_$prog" 4)" # +
printf '%7s' "$(getMeta "S2_$prog" 2)" # ?
printf '%7s' "$(getMeta "S2_$prog" 5)" # {}
printf '%7s' "$(getMeta "ax_$prog" 1)" # |
printf '%8s' "$g1$g2" # ()
printf ' %s' "${allversions[$i]}" # version
done
printf '\n\n%s\n\n' $"NOTE: . [] [^] and * are the same on all programs."
}
ShowInfo() {
local prog="$1"
local escmeta
local index
local i
local metas
local needesc
local posix=$"NO"
local tabinlist=$"NO"
local txtsize
local ver
local -a data
local -a txt
# Getting data
index=$(getItemIndex "$prog" "${allprogs[@]}")
ver="${allversions[$index]}"
escmeta=$(getMeta "ax_$prog" 4)
needesc=$(getMeta "ax_$prog" 5)
[ "$(getMeta "ax_$prog" 7)" == 'P' ] && posix=$"YES"
[ "$(getMeta "ax_$prog" 8)" == '\t' ] && tabinlist=$"YES"
# Metacharacters list
# printf arguments: + ? {} | ( )
metas="$(
printf '. [] [^] * %s %s %s %s %s%s' \
"$(getMeta "S2_$prog" 4)" \
"$(getMeta "S2_$prog" 2)" \
"$(getMeta "S2_$prog" 5)" \
"$(getMeta "ax_$prog" 1)" \
"$(getMeta "ax_$prog" 2)" \
"$(getMeta "ax_$prog" 3)"
)"
# Populating cool i18n arrays
# shellcheck disable=SC2256
txt=(
$"program"
$"metas"
$"esc meta"
$"need esc"
$"\t in []"
'[:POSIX:]'
)
data=(
"$prog: $ver"
"$metas"
"$escmeta"
"${needesc//-/}"
"$tabinlist"
"$posix"
)
# Show me! show me! show me!
ColorOnOff
printf '\n'
txtsize=$(getLargestItem "${txt[@]}")
for ((i = 0; i < ${#txt[@]}; i++)); do
printf "%s %${#txtsize}s %s %s\n" \
"$cR" "${txt[$i]}" "${cN:-:}" "${data[$i]}"
done
printf '\n'
}
if [ "$mode_show_meta" -eq 1 ]; then
ShowMeta
exit 0
fi
if [ "$mode_show_info" -eq 1 ]; then
ShowInfo "$infoprog"
exit 0
fi
# Screen size/positioning issues
ScreenSize() {
# Note that those are all global variables
x_regex=1
y_regex=4
x_hist=3
y_hist=$((y_regex + ${#progs[*]} + 1))
x_prompt=3
y_prompt=$((y_regex + ${#progs[*]} + 2))
x_menu=3
y_menu=$((y_prompt + 2))
x_prompt2=15
y_max=$((y_menu + ${#S1_txt[*]}))
# The defaults case not exported
: ${LINES:=25}
: ${COLUMNS:=80}
#TODO automatic check when selecting programs
if [ "$is_interactive" -eq 1 ] && [ $LINES -lt "$y_max" ]; then
printError '\n%s\n%s\n%s\n' \
"$(
printf \
$"Your terminal has %d lines, but txt2regex needs at least %d lines." \
"$LINES" "$y_max"
)" \
$"Increase the number of lines or select less programs using --prog." \
$"If this line number detection is incorrect, export the LINES variable."
fi
}
_eol=$(printf '\033[0K') # clear trash until EOL
# The cool control chars functions
gotoxy() {
[ "$is_interactive" -eq 1 ] && printf '\033[%d;%dH' "$2" "$1"
}
clearEnd() {
[ "$is_interactive" -eq 1 ] && printf '\033[0J'
}
clearN() {
[ "$is_interactive" -eq 1 ] && printf '\033[%dX' "$1"
}
Clear() {
[ "$is_interactive" -eq 1 ] && printf '\033c'
}
# Ideas: tab between, $cR on cmd, yellow-white-yellow
printTitleCmd() {
printf '[%s%s%s]%s ' "$cI" "$1" "$cN" "$2"
}
TopTitle() {
gotoxy 1 1
local color
local cmd
local i
local j
local showme
local txt
[ "$is_interactive" -eq 0 ] && return
# 1st line: aplication commands
for ((i = 0; i < 10; i++)); do
showme=0
txt=${tit1_txt[$i]}
cmd=${tit1_cmd[$i]}
case $i in
[01])
showme=1
;;
2)
[ "$use_colors" -eq 1 ] && showme=1
;;
3)
[ "$STATUS" -eq 0 ] && showme=1
;;
9)
gotoxy $((COLUMNS - ${#txt})) 1
printf '%s\n' "$txt"
;;
esac
if [ $showme -eq 1 ]; then
printTitleCmd "$cmd" "$txt"
else
clearN $((${#txt} + 3))
fi
done
# 2nd line: grouping and or
if [ "$STATUS" -eq 0 ]; then
printf %s "$_eol"
else
if [ "$STATUS" -eq 1 ]; then
for i in 0 1 2; do
txt=${tit2_txt[$i]}
cmd=${tit2_cmd[$i]}
showme=1
[ $i -eq 2 ] && [ $GRP1 -eq $GRP2 ] && showme=0
if [ $showme -eq 1 ]; then
printTitleCmd "$cmd" "$txt"
else
clearN $((${#txt} + 3))
fi
done
else # delete commands only
clearN $((${#tit2_txt[0]} + 5 + ${#tit2_txt[1]} + 5 + ${#tit2_txt[2]} + 5))
fi
# open groups
gotoxy $((COLUMNS - GRP1 - GRP2 - ${#GRP1})) 2
color="$cP"
[ "$GRP1" -eq "$GRP2" ] && color="$cB"
for ((j = 0; j < GRP1; j++)); do printf '%s(%s' "$color" "$cN"; done
[ $GRP1 -gt 0 ] && printf %s "$GRP1"
for ((j = 0; j < GRP2; j++)); do printf '%s)%s' "$color" "$cN"; done
fi
# 3rd line: legend
txt=${tit2_txt[9]}
cmd=${tit2_cmd[9]}
gotoxy $((COLUMNS - ${#txt} - ${#cmd} - 1)) 3
if [ "$has_not_supported" -eq 1 ]; then
printf '%s%s%s %s' "$cB" "$cmd" "$cN" "$txt"
else
clearN $((${#txt} + ${#cmd} + 1))
fi
}
doMenu() {
local i
local -a Menui
eval "Menui=(\"\${$1[@]}\")"
menu_n=$((${#Menui[*]} - 1)) # ini (global var)
if [ "$is_interactive" -eq 1 ]; then
# history
gotoxy $x_hist $y_hist
printf ' %s.oO(%s%s%s)%s%s(%s%s%s)%s%s\n' \
"$cP" "$cN" "$REPLIES" "$cP" "$cN" \
"$cP" "$cN" "$uins" "$cP" "$cN" \
"$_eol"
# title
gotoxy $x_menu $y_menu
printf '%s%s:%s%s\n' "$cI" "${Menui[0]}" "$cN" "$_eol"
# itens
for i in $(sek $menu_n); do
printf ' %s%d%s) %s%s\n' "$cB" "$i" "$cN" "${Menui[$i]}" "$_eol"
i=$((i + 1))
done
clearEnd
# prompt
gotoxy $x_prompt $y_prompt
printf '%s[1-%d]:%s %s' "$cP" "$menu_n" "$cN" "$_eol"
read -r -n 1
else
doNextHist
REPLY=$hist
fi
}
Menu() {
local name="$1"
local ok=0
while [ $ok -eq 0 ]; do
doMenu "$name"
case "$REPLY" in
[1-9])
[ "$REPLY" -gt "$menu_n" ] && continue
ok=1
REPLIES="$REPLIES$REPLY"
;;
.)
ok=1
LASTSTATUS=$STATUS
STATUS=3
;;
0)
ok=1
STATUS=Z
;;
\*)
ColorOnOff
TopTitle
;;
[\(\)\|])
[ "$STATUS" -ne 1 ] && continue
[ "$REPLY" == ')' ] &&
{ [ $GRP1 -gt 0 ] && [ $GRP1 -eq $GRP2 ] || [ $GRP1 -eq 0 ]; } &&