-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopensearch
executable file
·3447 lines (3082 loc) · 117 KB
/
opensearch
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
#!/usr/bin/env sh
### Script: opensearch
##
## Open Search を利用する。
##
## Metadata:
##
## id - 6343b25a-bc74-43b4-8e61-e38b9d95523c
## author - <qq542vev at https://purl.org/meta/me/>
## version - 0.1.2
## date - 2023-07-10
## since - 2020-04-20
## copyright - Copyright (C) 2020-2022 qq542vev. Some rights reserved.
## license - <CC-BY at https://creativecommons.org/licenses/by/4.0/>
## package - shell-opensearch
##
## See Also:
##
## * <Project homepage at https://github.com/qq542vev/shell-opensearch>
## * <Bag report at https://github.com/qq542vev/shell-opensearch/issues>
##
## Help Output:
##
## ------ Text ------
## Usage:
## opensearch [OPTION]... [COMMAND]
##
## Options:
## -c, --config-directory DIRECTORY
## 設定ファイルのディレクトリを指定する
## -h, --help このヘルプを表示して終了する
## -v, --version バージョン情報を表示して終了する
##
## Commands:
## add OpenSearch description document を追加する
## contact OpenSearch description document の作成者に連絡する
## list OpenSearch description document の一覧を表示する
## search OpenSearch description document を元に検索を行う
## show OpenSearch description document を表示する
## update OpenSearch description document を更新する
## validate OpenSearch description document を検査する
##
## Exit Status:
## 0 - successful termination
## 64 - command line usage error
## 65 - data format error
## 66 - cannot open input
## 67 - addressee unknown
## 68 - host name unknown
## 69 - service unavailable
## 70 - internal software error
## 71 - system error (e.g., can't fork)
## 72 - critical OS file missing
## 73 - can't create (user) output file
## 74 - input/output error
## 75 - temp failure; user is invited to retry
## 76 - remote error in protocol
## 77 - permission denied
## 78 - configuration error
## 129 - received SIGHUP
## 130 - received SIGINT
## 131 - received SIGQUIT
## 143 - received SIGTERM
## ------------------
readonly 'VERSION=opensearch 0.1.1'
set -efu
umask '0022'
readonly "LC_ALL_ORG=${LC_ALL-}"
LC_ALL='C'
IFS=$(printf ' \t\n_'); IFS="${IFS%_}"
PATH="${PATH-}${PATH:+:}$(command -p getconf 'PATH')"
UNIX_STD='2003' # HP-UX POSIX mode
XPG_SUS_ENV='ON' # AIX POSIX mode
XPG_UNIX98='OFF' # AIX UNIX 03 mode
POSIXLY_CORRECT='1' # GNU Coreutils POSIX mode
COMMAND_MODE='unix2003' # macOS UNIX 03 mode
export 'LC_ALL' 'IFS' 'PATH' 'UNIX_STD' 'XPG_SUS_ENV' 'XPG_UNIX98' 'POSIXLY_CORRECT' 'COMMAND_MODE'
readonly 'EX_OK=0' # successful termination
readonly 'EX__BASE=64' # base value for error messages
readonly 'EX_USAGE=64' # command line usage error
readonly 'EX_DATAERR=65' # data format error
readonly 'EX_NOINPUT=66' # cannot open input
readonly 'EX_NOUSER=67' # addressee unknown
readonly 'EX_NOHOST=68' # host name unknown
readonly 'EX_UNAVAILABLE=69' # service unavailable
readonly 'EX_SOFTWARE=70' # internal software error
readonly 'EX_OSERR=71' # system error (e.g., can't fork)
readonly 'EX_OSFILE=72' # critical OS file missing
readonly 'EX_CANTCREAT=73' # can't create (user) output file
readonly 'EX_IOERR=74' # input/output error
readonly 'EX_TEMPFAIL=75' # temp failure; user is invited to retry
readonly 'EX_PROTOCOL=76' # remote error in protocol
readonly 'EX_NOPERM=77' # permission denied
readonly 'EX_CONFIG=78' # configuration error
readonly 'EX__MAX=78' # maximum listed value
trap 'case "${?}" in 0) end_call;; *) end_call "${EX_SOFTWARE}";; esac' 0 # EXIT
trap 'end_call 129' 1 # SIGHUP
trap 'end_call 130' 2 # SIGINT
trap 'end_call 131' 3 # SIGQUIT
trap 'end_call 143' 15 # SIGTERM
alias org_lc='LC_ALL="${LC_ALL_ORG}"'
### Function: end_call
##
## 一時ディレクトリを削除しスクリプトを終了する。
##
## Parameters:
##
## $1 - 終了ステータス。
##
## Returns:
##
## $1 の終了ステータス。
end_call() {
trap '' 0 # EXIT
rm -fr -- ${tmpDir:+"${tmpDir}"}
exit "${1:-0}"
}
### Function: option_error
##
## エラーメッセージを出力する。
##
## Parameters:
##
## $1 - エラーメッセージ。
##
## Returns:
##
## 終了コード64。
option_error() {
printf '%s: %s\n' "${0##*/}" "${1}" >&2
printf "詳細については '%s' を実行してください。\\n" "${0##*/} --help" >&2
end_call "${EX_USAGE}"
}
### Function: regex_match
##
## 文字列が正規表現に一致するか検査する。
##
## Parameters:
##
## $1 - 検査する文字列。
## $@ - 正規表現。
##
## Returns:
##
## 0か1の真理値。
regex_match() {
awk -- '
BEGIN {
for(i = 2; i < ARGC; i++) {
if(ARGV[1] !~ ARGV[i]) {
exit 1
}
}
exit
}
' ${@+"${@}"} || return 1
return 0
}
### Function: append_array_posix
##
## 配列風文字列に要素を追加する。
##
## Parameters:
##
## $1 - 結果を代入する変数名。
## $@ - 追加する要素。
append_array_posix() {
while [ 2 -le "${#}" ]; do
__append_array_posix "${1}" "${2}"
eval "shift 2; set -- '${1}'" '${@+"${@}"}'
done
}
### Function: __append_array_posix
##
## 配列風文字列に要素を追加する。
##
## Parameters:
##
## $1 - 結果を代入する変数名。
## $2 - 追加する要素。
__append_array_posix() {
set "${1}" "${2-}" ''
until [ "${2#*\'}" '=' "${2}" ]; do
set -- "${1}" "${2#*\'}" "${3}${2%%\'*}'\"'\"'"
done
eval "${1}=\"\${${1}-}\${${1}:+ }'\${3}\${2}'\""
}
### Function: awkv_escape
##
## 文字列内の AWK 特殊文字をエスケープする。
##
## Parameters:
##
## $1 - 結果を代入する変数名。
## $2 - エスケープする文字列。
##
## See Also:
##
## * <4.2 POSIX 準拠シェル 用 at https://qiita.com/ko1nksm/items/a22c0ce88e39c9f2dcb0#42-posix-%E6%BA%96%E6%8B%A0%E3%82%B7%E3%82%A7%E3%83%AB-%E7%94%A8>
awkv_escape() {
set -- "${1}" "${2}" ''
until [ "${2#*\\}" '=' "${2}" ]; do
set -- "${1}" "${2#*\\}" "${3}${2%%\\*}\\\\"
done
set -- "${1}" "${3}${2}"
case "${2}" in
@*) set -- "${1}" "\\100${2#?}";;
esac
eval "${1}=\${2}"
}
add_parameter() {
case "${2}" in
'{'?*'}'*=*)
set -- "${1}" "${2}" "${2%%\}*}" "${2#*\}}"
append_array_posix "${1}" "${3}}${4%%=*}" "${4#*=}"
;;
*) append_array_posix "${1}" "{${OPENSEARCH_NS}}${2%%=*}" "${2#*=}";;
esac
}
template_parameter_check() {
awk -- "$(
cat <<-'__EOF__'
### Function: uri_check
##
## 文字列が URI であるか検査する。
##
## Parameters:
##
## uri - 検査する文字列。
## relative - 相対 URI を許可する。
##
## Returns:
##
## 0か1の真理値。
function uri_check(uri, relative) {
if(relative) {
return (uri ~ /^([A-Za-z][A-Za-z0-9+.-]*:)?(\/\/(([!$&-.0-;=A-Z_a-z~]|%[0-9A-Fa-f]{2})*@)?(\[(((([0-9A-F]{1,4}:){6}|::([0-9A-F]{1,4}:){5}|([0-9A-F]{1,4})?::([0-9A-F]{1,4}:){4}|(([0-9A-F]{1,4}:){0,1}[0-9A-F]{1,4})?::([0-9A-F]{1,4}:){3}|(([0-9A-F]{1,4}:){0,2}[0-9A-F]{1,4})?::([0-9A-F]{1,4}:){2}|(([0-9A-F]{1,4}:){0,3}[0-9A-F]{1,4})?::[0-9A-F]{1,4}:|(([0-9A-F]{1,4}:){0,4}[0-9A-F]{1,4})?::)([0-9A-F]{1,4}:[0-9A-F]{1,4}|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(([0-9A-F]{1,4}:){0,5}[0-9A-F]{1,4})?::[0-9A-F]{1,4}|(([0-9A-F]{1,4}:){0,6}[0-9A-F]{1,4})?::)|v[0-9A-F]+\.[!$&-.0-;=A-Z_a-z~]+)\]|([A-Za-z0-9._~!$&'()*+,;=-]|%[0-9A-Fa-f]{2})*)(:[0-9]*)?(\/([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})*)*|\/(([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})+(\/([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})*)*)?|([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})+(\/([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})*)*|)(\?([!$&-;=?-Z_a-z~]|%[0-9A-Fa-f]{2})*)?(#([!$&-;=?-Z_a-z~]|%[0-9A-Fa-f]{2})*)?$/)
} else {
return (uri ~ /^[A-Za-z][A-Za-z0-9+.-]*:(\/\/(([!$&-.0-;=A-Z_a-z~]|%[0-9A-Fa-f]{2})*@)?(\[(((([0-9A-F]{1,4}:){6}|::([0-9A-F]{1,4}:){5}|([0-9A-F]{1,4})?::([0-9A-F]{1,4}:){4}|(([0-9A-F]{1,4}:){0,1}[0-9A-F]{1,4})?::([0-9A-F]{1,4}:){3}|(([0-9A-F]{1,4}:){0,2}[0-9A-F]{1,4})?::([0-9A-F]{1,4}:){2}|(([0-9A-F]{1,4}:){0,3}[0-9A-F]{1,4})?::[0-9A-F]{1,4}:|(([0-9A-F]{1,4}:){0,4}[0-9A-F]{1,4})?::)([0-9A-F]{1,4}:[0-9A-F]{1,4}|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(([0-9A-F]{1,4}:){0,5}[0-9A-F]{1,4})?::[0-9A-F]{1,4}|(([0-9A-F]{1,4}:){0,6}[0-9A-F]{1,4})?::)|v[0-9A-F]+\.[!$&-.0-;=A-Z_a-z~]+)\]|([A-Za-z0-9._~!$&'()*+,;=-]|%[0-9A-Fa-f]{2})*)(:[0-9]*)?(\/([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})*)*|\/(([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})+(\/([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})*)*)?|([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})+(\/([!$&-.0-;=@-Z_a-z~]|%[0-9A-Fa-f]{2})*)*|)(\?([!$&-;=?-Z_a-z~]|%[0-9A-Fa-f]{2})*)?(#([!$&-;=?-Z_a-z~]|%[0-9A-Fa-f]{2})*)?$/)
}
}
BEGIN {
parameter = ARGV[1]
if(match(parameter, /^\{[^}]*\}/)) {
if(!uri_check(substr(parameter, 2, RLENGTH - 2))) {
exit 1
}
parameter = substr(parameter, RLENGTH + 1)
}
exit !(parameter ~ /=/)
}
__EOF__
)" "${1}"
}
# @getoptions
parser_definition() {
setup REST abbr:true error:option_error plus:true no:0 help:usage \
-- 'Usage:' " ${2##*/} [OPTION]... [COMMAND]" \
'' 'Options:'
param configDir -c --config-directory init:'configDir="${HOME}/.shell-opensearch"' pattern:'?*' var:DIRECTORY -- '設定ファイルのディレクトリを指定する'
disp :usage -h --help -- 'このヘルプを表示して終了する'
disp VERSION -v --version -- 'バージョン情報を表示して終了する'
msg -- '' 'Commands:'
cmd add -- 'OpenSearch description document を追加する'
cmd contact -- 'OpenSearch description document の作成者に連絡する'
cmd list -- 'OpenSearch description document の一覧を表示する'
cmd search -- 'OpenSearch description document を元に検索を行う'
cmd show -- 'OpenSearch description document を表示する'
cmd update -- 'OpenSearch description document を更新する'
cmd validate -- 'OpenSearch description document を検査する'
msg -- '' 'Exit Status:' \
' 0 - successful termination' \
' 64 - command line usage error' \
' 65 - data format error' \
' 66 - cannot open input' \
' 67 - addressee unknown' \
' 68 - host name unknown' \
' 69 - service unavailable' \
' 70 - internal software error' \
" 71 - system error (e.g., can't fork)" \
' 72 - critical OS file missing' \
" 73 - can't create (user) output file" \
' 74 - input/output error' \
' 75 - temp failure; user is invited to retry' \
' 76 - remote error in protocol' \
' 77 - permission denied' \
' 78 - configuration error' \
' 129 - received SIGHUP' \
' 130 - received SIGINT' \
' 131 - received SIGQUIT' \
' 143 - received SIGTERM'
}
parser_definition_add() {
setup REST abbr:true error:option_error plus:true no:0 help:usage_add \
-- 'Usage:' \
" ${2##*/} add [OPTION]... NAME[,NAME]... URL" \
" ${2##*/} add [OPTION]... FILE_PATH URL" \
'' 'Options:'
disp :usage_add -h --help -- 'このヘルプを表示して終了する'
}
parser_definition_contact() {
setup REST abbr:true error:option_error plus:true no:0 help:usage_contact \
-- 'Usage:' \
" ${2##*/} contact [OPTION]... NAME[,NAME]..." \
" ${2##*/} contact [OPTION]... OPENSEARCH_FILE" \
'' 'Options:'
option extarnal -e --{no-}extarnal init:@no on: no:'printf "%s\\n"' var:COMMAND -- '外部プログラムを起動する'
flag skipFlag -s --skip-error init:@no -- 'エラーをスキップして次のファイルを処理する'
disp :usage_contact -h --help -- 'このヘルプを表示して終了する'
}
parser_definition_list() {
setup REST abbr:true error:option_error plus:true no:0 help:usage_list \
-- 'Usage:' \
" ${2##*/} list [OPTION]... NAME[,NAME]..." \
" ${2##*/} list [OPTION]... OPENSEARCH_FILE" \
'' 'Options:'
flag pathFlag -p --{no-}path init:@no -- '名前ではなくパスを表示する'
disp :usage_list -h --help -- 'このヘルプを表示して終了する'
}
parser_definition_search() {
setup REST abbr:true error:option_error plus:true no:0 help:usage_search \
-- 'Usage:' \
" ${2##*/} search [OPTION]... NAME[,NAME]... [KEYWORD]..." \
" ${2##*/} search [OPTION]... OPENSEARCH_FILE [KEYWORD]..." \
'' 'Options:'
option extarnal -e --{no-}extarnal init:@no on: no:'printf "%s\\n"' var:COMMAND -- '外部プログラムを起動する'
param :'add_parameter "parameters" "${OPTARG}"' -p --template-parameter validate:'template_parameter_check "${OPTARG}"' var:'[{NAMESPACE_URI}]NAME=[VALUE]' -- 'テンプレートパラメータを指定する'
flag parameters --no-template-parameter init:@no on: no: -- '-p, --template-parameter をリセットする'
flag skipFlag -s --skip-error init:@no -- 'エラーをスキップして次のファイルを処理する'
param mimeType -t --accept-type init:='text/html,application/xhtml+xml,application/atom+xml,application/rss+xml,application/xml,text/plain,text/*,*/*' var:'MIME_TYPE[,MIME_TYPE]...' validate:"regex_match \"\${OPTARG}\" \"^(application|audio|font|example|image|message|model|multipart|text|video|x-[!#-'*+.0-9A-Z^_-z|~-]+|\*)/[!#-'*+.0-9A-Z\^_-z|~-]+(,(application|audio|font|example|image|message|model|multipart|text|video|x-[!#-'*+.0-9A-Z^_-z|~-]+|\*)/[!#-'*+.0-9A-Z\^_-z|~-]+)*$\"" -- '希望するリソースの MIME タイプを指定する'
disp :usage_search -h --help -- 'このヘルプを表示して終了する'
}
parser_definition_show() {
setup REST abbr:true error:option_error plus:true no:0 help:usage_show \
-- 'Usage:' \
" ${2##*/} show [OPTION]... NAME[,NAME]... [OPENSEARCH_ELEMENT]..." \
" ${2##*/} show [OPTION]... OPENSEARCH_FILE [OPENSEARCH_ELEMENT]..." \
'' 'Options:'
param format -f --format init:='text' pattern:'text | xml | normalized-xml' var:'text | xml | normalized-xml' -- '表示形式を指定する'
option indent -i --{no-}indent-space init:@on on:2 no:0 validate:'regex_match "${OPTARG}" "^0$|^[1-9][0-9]*$"' var:'NUMBER' -- 'インデントを行う'
flag skipFlag -s --skip-error init:@no -- 'エラーをスキップして次のファイルを処理する'
disp :usage_show -h --help -- 'このヘルプを表示して終了する'
}
parser_definition_update() {
setup REST abbr:true error:option_error plus:true no:0 help:usage_update \
-- 'Usage:' \
" ${2##*/} update [OPTION]... NAME[,NAME]..." \
" ${2##*/} update [OPTION]... OPENSEARCH_FILE" \
'' 'Options:'
param :'add_parameter "parameters" "${OPTARG}"' -p --template-parameter validate:'template_parameter_check "${OPTARG}"' var:'[{NAMESPACE_URI}]NAME=VALUE' -- 'テンプレートパラメータを指定する'
flag parameters --no-template-parameter init:@no on: no: -- '-p, --template-parameter をリセットする'
flag skipFlag -s --skip-error init:@no -- 'エラーをスキップして次のファイルを処理する'
disp :usage_update -h --help -- 'このヘルプを表示して終了する'
}
parser_definition_validate() {
setup REST abbr:true error:option_error plus:true no:0 help:usage_validate \
-- 'Usage:' \
" ${2##*/} validate [OPTION]... NAME[,NAME]..." \
" ${2##*/} validate [OPTION]... OPENSEARCH_FILE" \
'' 'Options:'
flag errorFlag -e --{no-}error init:@no no: -- 'print verbose error messages on stderr'
flag quietFlag -q --{no-}quiet init:@no no: -- 'do not list files (return result code only)'
disp :usage_validate -h --help -- 'このヘルプを表示して終了する'
}
# @end
# @gengetoptions parser -i parser_definition parse "${1}"
# Generated by getoptions (BEGIN)
# URL: https://github.com/ko1nksm/getoptions (v3.3.0)
configDir="${HOME}/.shell-opensearch"
REST=''
parse() {
OPTIND=$(($#+1))
while OPTARG= && [ $# -gt 0 ]; do
set -- "${1%%\=*}" "${1#*\=}" "$@"
while [ ${#1} -gt 2 ]; do
case $1 in (*[!a-zA-Z0-9_-]*) break; esac
case '--config-directory' in
"$1") OPTARG=; break ;;
$1*) OPTARG="$OPTARG --config-directory"
esac
case '--help' in
"$1") OPTARG=; break ;;
$1*) OPTARG="$OPTARG --help"
esac
case '--version' in
"$1") OPTARG=; break ;;
$1*) OPTARG="$OPTARG --version"
esac
break
done
case ${OPTARG# } in
*\ *)
eval "set -- $OPTARG $1 $OPTARG"
OPTIND=$((($#+1)/2)) OPTARG=$1; shift
while [ $# -gt "$OPTIND" ]; do OPTARG="$OPTARG, $1"; shift; done
set "Ambiguous option: $1 (could be $OPTARG)" ambiguous "$@"
option_error "$@" >&2 || exit $?
echo "$1" >&2
exit 1 ;;
?*)
[ "$2" = "$3" ] || OPTARG="$OPTARG=$2"
shift 3; eval 'set -- "${OPTARG# }"' ${1+'"$@"'}; OPTARG= ;;
*) shift 2
esac
case $1 in
--?*=*) OPTARG=$1; shift
eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'}
;;
--no-*|--without-*) unset OPTARG ;;
-[c]?*) OPTARG=$1; shift
eval 'set -- "${OPTARG%"${OPTARG#??}"}" "${OPTARG#??}"' ${1+'"$@"'}
;;
-[hv]?*) OPTARG=$1; shift
eval 'set -- "${OPTARG%"${OPTARG#??}"}" -"${OPTARG#??}"' ${1+'"$@"'}
OPTARG= ;;
+*) unset OPTARG ;;
esac
case $1 in
'-c'|'--config-directory')
[ $# -le 1 ] && set "required" "$1" && break
OPTARG=$2
case $OPTARG in ?*) ;;
*) set "pattern:?*" "$1"; break
esac
configDir="$OPTARG"
shift ;;
'-h'|'--help')
usage
exit 0 ;;
'-v'|'--version')
echo "${VERSION}"
exit 0 ;;
--)
while [ $# -gt 0 ]; do
REST="${REST} \"\${$(($OPTIND-$#))}\""
shift
done
break ;;
[-+]?*) set "unknown" "$1"; break ;;
*)
case $1 in 'add'|'contact'|'list'|'search'|'show'|'update'|'validate') ;;
*) set "notcmd" "$1"; break
esac
while [ $# -gt 0 ]; do
REST="${REST} \"\${$(($OPTIND-$#))}\""
shift
done
break ;;
esac
shift
done
[ $# -eq 0 ] && { OPTIND=1; unset OPTARG; return 0; }
case $1 in
unknown) set "Unrecognized option: $2" "$@" ;;
noarg) set "Does not allow an argument: $2" "$@" ;;
required) set "Requires an argument: $2" "$@" ;;
pattern:*) set "Does not match the pattern (${1#*:}): $2" "$@" ;;
notcmd) set "Not a command: $2" "$@" ;;
*) set "Validation error ($1): $2" "$@"
esac
option_error "$@" >&2 || exit $?
echo "$1" >&2
exit 1
}
usage() {
cat<<'GETOPTIONSHERE'
Usage:
opensearch [OPTION]... [COMMAND]
Options:
-c, --config-directory DIRECTORY
設定ファイルのディレクトリを指定する
-h, --help このヘルプを表示して終了する
-v, --version バージョン情報を表示して終了する
Commands:
add OpenSearch description document を追加する
contact OpenSearch description document の作成者に連絡する
list OpenSearch description document の一覧を表示する
search OpenSearch description document を元に検索を行う
show OpenSearch description document を表示する
update OpenSearch description document を更新する
validate OpenSearch description document を検査する
Exit Status:
0 - successful termination
64 - command line usage error
65 - data format error
66 - cannot open input
67 - addressee unknown
68 - host name unknown
69 - service unavailable
70 - internal software error
71 - system error (e.g., can't fork)
72 - critical OS file missing
73 - can't create (user) output file
74 - input/output error
75 - temp failure; user is invited to retry
76 - remote error in protocol
77 - permission denied
78 - configuration error
129 - received SIGHUP
130 - received SIGINT
131 - received SIGQUIT
143 - received SIGTERM
GETOPTIONSHERE
}
# Generated by getoptions (END)
# @end
argument_count_check() {
if [ "${1}" -lt "${2:-0}" ]; then
printf "%s: 引数が不足しています。\\n" "${0##*/}" >&2
printf "詳細については '%s' を実行してください。\\n" "${0##*/} --help" >&2
end_call "${EX_USAGE}"
elif [ -n "${3:-}" ] && [ "${3}" -lt "${1}" ]; then
printf "%s: 引数が超過しています。\\n" "${0##*/}" >&2
printf "詳細については '%s' を実行してください。\\n" "${0##*/} --help" >&2
end_call "${EX_USAGE}"
fi
}
argument_to_filepath() {
eval "${1}=\"\${${1}-}\""
set -- "${1}" "${2}" "$(
cat <<-'__EOF__'
### Function: shell_argument
##
## 安全な Shell Script 用の引数を生成する。
##
## Parameters:
##
## string - 引数となる文字列。
##
## Returns:
##
## Shell Script 用の引数。
function shell_argument(string) {
gsub(/'+/, "'\"&\"'", string)
return "'" string "'"
}
BEGIN {
for(i = 1; i < ARGC; i++) {
printf("%s ", shell_argument(ARGV[i]))
}
}
__EOF__
)"
case "${2}" in
/* | .[,/]* | ..[,/]* | *[,/]. | *[,/].. | *[,/].[,/]* | *[,/]..[,/]*) append_array_posix "${1}" "${2}";;
*)
while :; do
case "${2%%,*}" in
'' | */) eval append_array_posix '"${1}"' "$(find -L -- "${configDir}/${2%%,*}" -name '*.xml' -type f -exec awk -- "${3}" '{}' '+' 2>'/dev/null')";;
*) append_array_posix "${1}" "${configDir}/${2%%,*}.xml";;
esac
case "${2}" in
*,*) set -- "${1}" "${2#*,}" "${3}";;
*) break;;
esac
done
;;
esac
}
download() {
case "${2}" in 'file:///')
set -- "${1}" "${2#file://}"
set -- "${1}" "${2%%#*}"
set -- "${1}" "${2%%\?*}"
(
cd -- '/'
cat -- "${2}" >"${1}"
)
return 0
esac
if command -v 'wget' >'/dev/null'; then
wget --header "Accept: ${3-*/*}" --no-verbose --output-document "${1}" -- "${2}"
elif command -v 'curl' >'/dev/null'; then
curl --compressed --fail --header "Accept: ${3-*/*}" --location --output "${1}" --silent --show-error -- "${2}"
else
printf "%s: 'wget' または 'curl' が必要です。\\n" "${0##*/}" >&2
printf "詳細については '%s' を実行してください。\\n" "${0##*/} --help" >&2
end_call "${EX_UNAVAILABLE}"
fi
}
opensearch_add() {
# @gengetoptions parser -i parser_definition_add parse_add "${1}"
# Generated by getoptions (BEGIN)
# URL: https://github.com/ko1nksm/getoptions (v3.3.0)
REST=''
parse_add() {
OPTIND=$(($#+1))
while OPTARG= && [ $# -gt 0 ]; do
set -- "${1%%\=*}" "${1#*\=}" "$@"
while [ ${#1} -gt 2 ]; do
case $1 in (*[!a-zA-Z0-9_-]*) break; esac
case '--help' in
"$1") OPTARG=; break ;;
$1*) OPTARG="$OPTARG --help"
esac
break
done
case ${OPTARG# } in
*\ *)
eval "set -- $OPTARG $1 $OPTARG"
OPTIND=$((($#+1)/2)) OPTARG=$1; shift
while [ $# -gt "$OPTIND" ]; do OPTARG="$OPTARG, $1"; shift; done
set "Ambiguous option: $1 (could be $OPTARG)" ambiguous "$@"
option_error "$@" >&2 || exit $?
echo "$1" >&2
exit 1 ;;
?*)
[ "$2" = "$3" ] || OPTARG="$OPTARG=$2"
shift 3; eval 'set -- "${OPTARG# }"' ${1+'"$@"'}; OPTARG= ;;
*) shift 2
esac
case $1 in
--?*=*) OPTARG=$1; shift
eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'}
;;
--no-*|--without-*) unset OPTARG ;;
-[h]?*) OPTARG=$1; shift
eval 'set -- "${OPTARG%"${OPTARG#??}"}" -"${OPTARG#??}"' ${1+'"$@"'}
OPTARG= ;;
+*) unset OPTARG ;;
esac
case $1 in
'-h'|'--help')
usage_add
exit 0 ;;
--)
shift
while [ $# -gt 0 ]; do
REST="${REST} \"\${$(($OPTIND-$#))}\""
shift
done
break ;;
[-+]?*) set "unknown" "$1"; break ;;
*)
REST="${REST} \"\${$(($OPTIND-$#))}\""
esac
shift
done
[ $# -eq 0 ] && { OPTIND=1; unset OPTARG; return 0; }
case $1 in
unknown) set "Unrecognized option: $2" "$@" ;;
noarg) set "Does not allow an argument: $2" "$@" ;;
required) set "Requires an argument: $2" "$@" ;;
pattern:*) set "Does not match the pattern (${1#*:}): $2" "$@" ;;
notcmd) set "Not a command: $2" "$@" ;;
*) set "Validation error ($1): $2" "$@"
esac
option_error "$@" >&2 || exit $?
echo "$1" >&2
exit 1
}
usage_add() {
cat<<'GETOPTIONSHERE'
Usage:
opensearch add [OPTION]... NAME[,NAME]... URL
opensearch add [OPTION]... FILE_PATH URL
Options:
-h, --help このヘルプを表示して終了する
GETOPTIONSHERE
}
# Generated by getoptions (END)
# @end
parse_add ${@+"${@}"}
eval "set -- ${REST}"
argument_count_check "${#}" '2' '2'
argument_to_filepath 'xmlFiles' "${1}"
url="${2}"
downloadXML="${tmpDir}/download.xml"
downloadHTML="${tmpDir}/download.html"
awkScript=$(
cat <<-'__EOF__'
### Function: uri_path_remove_dot_segments
##
## パス文字列内からドットセグメントを除去する。
##
## Parameters:
##
## path - パス文字列。
##
## Returns:
##
## ドットセグメントが除去された文字列。
##
## See Also:
##
## * <5.2.4. Remove Dot Segments at https://www.rfc-editor.org/rfc/rfc3986#section-5.2.4>
function uri_path_remove_dot_segments(path, result) {
if(!index(path, ".")) {
return path
}
result = ""
while(path != "") {
if(index(path, "./") == 1) {
path = substr(path, 3)
} else if(index(path, "../") == 1) {
path = substr(path, 4)
} else if(index(path, "/./") == 1) {
path = substr(path, 3)
} else if(path == "/.") {
path = "/"
} else if(index(path, "/../") == 1) {
path = substr(path, 4)
gsub(/\/?[^\/]*$/, "", result)
} else if(path == "/..") {
path = "/"
gsub(/\/?[^\/]*$/, "", result)
} else if(path == "." || path == "..") {
path = ""
} else {
match(path, /^.[^\/]*/)
result = result substr(path, RSTART, RLENGTH)
path = substr(path, RLENGTH + 1)
}
}
return result
}
### Function: uri_parse
##
## URI の構成要素を分離する。
##
## Parameters:
##
## uri - 分離する URI。
## element - 構成要素格納する配列。
function uri_parse(uri, element, pattern,key,i,auth,result) {
split("^[^:/?#]*: ^//[^/?#]* ^[^?#]* ^\\?[^#]* ^#.* ^[^@/?#]*@ ^[^:/?#]* ^:[^/?#]*", pattern, " ")
split("scheme authority path query fragment userinfo host port", key, " ")
split("", element)
for(i = 1; i <= 5; i++) {
element[key[i]] = ""
if(match(uri, pattern[i])) {
element[key[i]] = substr(uri, RSTART, RLENGTH)
uri = substr(uri, RSTART + RLENGTH)
}
}
element["userinfo"] = ""
element["host"] = ""
element["port"] = ""
if(element["authority"]) {
auth = substr(element["authority"], 3)
for(i = 6; i <= 8; i++) {
if(match(auth, pattern[i])) {
element[key[i]] = substr(auth, RSTART, RLENGTH)
auth = substr(auth, RSTART + RLENGTH)
}
}
}
}
### Function: uri_relative_to_absolute
##
## 相対 URI を絶対 URI に変換する
##
## Parameters:
##
## uri - 相対 URI。
## baseURIElements - 基底 URI の各要素の配列。
##
## See Also:
##
## * <5.2.2. Transform References at https://www.rfc-editor.org/rfc/rfc3986#section-5.2.2>
function uri_relative_to_absolute(uri, baseURIElements, elements,names,count,result,i) {
uri_parse(uri, elements)
count = split("scheme authority path query fragment", names, " ")
result = ""
for(i = 1; i <= count; i++) {
if(elements[names[i]] == "") {
result = result baseURIElements[names[i]]
} else {
if(names[i] == "path" && index(elements["path"], "/") != 1) {
gsub("/[^/]*$", "/", baseURIElements["path"])
elements["path"] = baseURIElements["path"] elements[names[i]]
}
for(; i <= count; i++) {
if(names[i] == "path") {
elements["path"] = uri_path_remove_dot_segments(elements["path"])
}
result = result elements[names[i]]
}
}
}
return result
}
BEGIN {
uri_parse(baseURL, baseURIElements)
}
$0 != "" {
printf("%s\n", uri_relative_to_absolute($0, baseURIElements))
}
__EOF__
)
download \
"${downloadXML}" "${url}" \
"${OPENSEARCH_MIME_TYPES}, text/html, application/xhtml+xml"
eval "set -- ${xmlFiles}"
if opensearch_file_check --quiet "${downloadXML}"; then
for dest in ${@+"${@}"}; do
destDir=$(dirname "${dest}"; printf '_')
mkdir -p -- "${destDir%?_}"
cat -- "${downloadXML}" >"${dest}"
done
exit
elif xml format --html --quiet "${downloadXML}" | tee -- "${downloadHTML}" | xml validate --quiet -; then
baseURL=$(
{
xml select --text -N "xhtml=${XHTML_NS}" --template \
--match '(/html/head/base | /xhtml:html/xhtml:head/xhtml:base)/@href[1]' \
--value-of 'normalize-space()' "${downloadHTML}" \
|| printf '%s' "${url}"
} | awk -v "baseURL=${url}" -- "${awkScript}"
)
xml select --text -N "xhtml=${XHTML_NS}" --template \
--match '(/html/head/link | /xhtml:html/xhtml:head/xhtml:link)[@href]' \
--match "self::*[normalize-space(@type) = '${OPENSEARCH_MIME_TYPE}']" \
--match 'self::*[contains(concat(" ", translate(normalize-space(@rel), "SEARCH", "search"), " "), " search ")]' \
--value-of 'normalize-space(@href)' --nl "${downloadHTML}" \
| awk -v "baseURL=${url}" -- "${awkScript}" \
| while read -r linkURL; do
rm -f -- "${downloadXML}"
if
download "${downloadXML}" "${linkURL}" && xml validate --quiet "${downloadXML}" \
&& opensearch_normalize "${downloadXML}" | tee -- "${tmpDir}/normalized.xml" | opensearch_file_check --quiet -
then
: >"${tmpDir}/success"
for dest in ${@+"${@}"}; do
destDir=$(dirname "${dest}"; printf '_')
mkdir -p -- "${destDir%?_}"
cat -- "${tmpDir}/normalized.xml" >"${dest}"
done
break
fi
done
if [ '!' -f "${tmpDir}/success" ]; then
printf "%s: '%s' は OpenSearch description document へのリンクが見つかりません。\\n" "${0##*/}" "${url}" >&2
printf "詳細については '%s' を実行してください。\\n" "${0##*/} --help" >&2
end_call "${EX_DATAERR}"
fi
else
printf "%s: '%s' は 互換性のないファイル形式です。\\n" "${0##*/}" "${url}" >&2
printf "詳細については '%s' を実行してください。\\n" "${0##*/} --help" >&2
end_call "${EX_DATAERR}"
fi
}
opensearch_contact() {
# @gengetoptions parser -i parser_definition_contact parse_contact "${1}"
# Generated by getoptions (BEGIN)
# URL: https://github.com/ko1nksm/getoptions (v3.3.0)
extarnal='printf "%s\\n"'
skipFlag='0'
REST=''
parse_contact() {
OPTIND=$(($#+1))
while OPTARG= && [ $# -gt 0 ]; do
set -- "${1%%\=*}" "${1#*\=}" "$@"
while [ ${#1} -gt 2 ]; do
case $1 in (*[!a-zA-Z0-9_-]*) break; esac
case '--extarnal' in
"$1") OPTARG=; break ;;
$1*) OPTARG="$OPTARG --extarnal"
esac
case '--no-extarnal' in
"$1") OPTARG=; break ;;
$1*) OPTARG="$OPTARG --no-extarnal"
esac
case '--skip-error' in
"$1") OPTARG=; break ;;
$1*) OPTARG="$OPTARG --skip-error"
esac
case '--help' in
"$1") OPTARG=; break ;;
$1*) OPTARG="$OPTARG --help"
esac
break
done
case ${OPTARG# } in
*\ *)
eval "set -- $OPTARG $1 $OPTARG"
OPTIND=$((($#+1)/2)) OPTARG=$1; shift
while [ $# -gt "$OPTIND" ]; do OPTARG="$OPTARG, $1"; shift; done
set "Ambiguous option: $1 (could be $OPTARG)" ambiguous "$@"
option_error "$@" >&2 || exit $?
echo "$1" >&2
exit 1 ;;
?*)
[ "$2" = "$3" ] || OPTARG="$OPTARG=$2"
shift 3; eval 'set -- "${OPTARG# }"' ${1+'"$@"'}; OPTARG= ;;
*) shift 2