OSDN Git Service

The options passed to portsreinstall in the builder chroot environment by portsreinst...
[portsreinstall/current.git] / lib / libstr.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - String processing -
5 # Copyright (C) 2013-2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Escape characters for the extended regular expression (by stream interface) =============
10 str_escape_regexp_filter ()
11 {
12         sed 's/\\/\\\\/g;s/|/\\|/g;s/\./\\./g;s|/|\\/|g;s/\[/\\[/g;s/\]/\\]/g;s/[(]/\\(/g;s/[)]/\\)/g;s/\+/\\+/g;s/\?/\\?/g;s/\*/\\*/g;s/\^/\\^/g;s/\$/\\$/g'
13 }
14
15 # ============= Escape characters for the extended regular expression (by argument interface) =============
16 str_escape_regexp ()
17 {
18         echo "$*" | str_escape_regexp_filter
19 }
20
21 # ============= Escape characters for replacement used with the extended regular expression (by stream interface) =============
22 str_escape_replaceval_filter ()
23 {
24         sed 's/\\/\\\\/g;s/&/\\&/g;s|/|\\/|g'
25 }
26
27 # ============= Escape characters for replacement used with the extended regular expression (by argument interface) =============
28 str_escape_replaceval ()
29 {
30         echo -n "$*" | str_escape_replaceval_filter
31 }
32
33 # ============= Convert a glob pattern into a pattern for the extended regular expression =============
34 str_convert_glob_to_regexp_pattern ()
35 {
36         sed 's/\+/\\+/g;s/\./\\./g;s|/|\\/|g;s/\*/.*/g;s/\?/g./;s/\[\!/[^/g;s/^/^/;s/$/$/'
37 }
38
39 # ============= Convert a ports glob pattern into a pattern for the extended regular expression =============
40 str_convert_portsglob_to_regexp_pattern ()
41 {
42         local glob_pattern regexp_pattern
43         glob_pattern=$1
44         if expr "$glob_pattern" : '^:' > /dev/null 2>&1
45         then
46                 regexp_pattern=`echo "$glob_pattern" | sed 's/^://'`
47         else
48                 regexp_pattern=`echo "$glob_pattern" | str_convert_glob_to_regexp_pattern`
49         fi
50         echo "$regexp_pattern"
51 }
52
53 # ============= Compose a part of sentence by concatenating items in a list as a logical product =============
54 str_linearize_list_and ()
55 {
56         echo "$*" | sed -E 's/^ +//; s/ +$//; s/ +/, /g; s/, ([^ ]+)$/ and \1/'
57 }
58
59 # ============= Manipulate a flavored origin name from a diretory path =============
60 str_dirpath_to_origin ()
61 {
62         local dbpath portname catpath catname
63         dbpath=$1
64         portname=`basename "$dbpath"`
65         catpath=`dirname "$dbpath"`
66         catname=`basename "$catpath"`
67         echo "$catname/$portname"
68 }
69
70 # ============= Concatenate a space/linefeed separated list to a apart of sentence =============
71 str_concat_items_for_sentence ()
72 {
73         sed 's/$/,/' | tr '\n' ' ' | sed -E 's/, $//;s/, ([^ ]*)$/ and \1/'
74 }
75
76 # ============= Regularize a file path for df and /etc/fstab (by stream interface) =============
77 str_regularize_df_path_filter ()
78 {
79         sed 's|//*|/|g;s|/$||;s/[[:space:]]/\\040/g'
80 }
81
82 # ============= Regularize a file path for df and /etc/fstab (by argument interface) =============
83 str_regularize_df_path ()
84 {
85         echo -n "$*" | str_regularize_df_path_filter
86 }