summaryrefslogtreecommitdiff
path: root/0014-Add-support-for-square-brackets-in-ls.patch
blob: 27f537444e1af5ade1a45637e01e692c88744f7f (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
From ba0520650ae7f9f63e48ba9fb3a94297aebe2d0c Mon Sep 17 00:00:00 2001
From: Martin Sehnoutka <msehnout@redhat.com>
Date: Wed, 7 Sep 2016 14:22:21 +0200
Subject: [PATCH 14/59] Add support for square brackets in ls.

---
 ls.c | 222 +++++++++++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 150 insertions(+), 72 deletions(-)

diff --git a/ls.c b/ls.c
index 616b2d9..b840136 100644
--- a/ls.c
+++ b/ls.c
@@ -246,7 +246,7 @@ vsf_filename_passes_filter(const struct mystr* p_filename_str,
   int ret = 0;
   char last_token = 0;
   int must_match_at_current_pos = 1;
-
+  int matched = 0;
 
   str_copy(&filter_remain_str, p_filter_str);
 
@@ -276,7 +276,7 @@ vsf_filename_passes_filter(const struct mystr* p_filename_str,
     static struct mystr s_match_needed_str;
     /* Locate next special token */
     struct str_locate_result locate_result =
-      str_locate_chars(&filter_remain_str, "*?{");
+      str_locate_chars(&filter_remain_str, "*?{[");
     (*iters)++;
     /* Isolate text leading up to token (if any) - needs to be matched */
     if (locate_result.found)
@@ -294,94 +294,172 @@ vsf_filename_passes_filter(const struct mystr* p_filename_str,
       str_empty(&filter_remain_str);
       last_token = 0;
     }
-    if (!str_isempty(&s_match_needed_str))
-    {
-      /* Need to match something.. could be a match which has to start at
-       * current position, or we could allow it to start anywhere
-       */
-      unsigned int indexx;
-      locate_result = str_locate_str(&name_remain_str, &s_match_needed_str);
-      if (!locate_result.found)
+
+    matched = 0;
+    do {
+      if (!str_isempty(&s_match_needed_str))
       {
-        /* Fail */
-        goto out;
+        if (!matched)
+        {
+          matched = 1;
+        }
+        /* Need to match something.. could be a match which has to start at
+         * current position, or we could allow it to start anywhere
+         */
+        unsigned int indexx;
+        locate_result = str_locate_str(&name_remain_str, &s_match_needed_str);
+        if (!locate_result.found)
+        {
+          /* Fail */
+          goto out;
+        }
+        indexx = locate_result.index;
+        if (must_match_at_current_pos && indexx > 0)
+        {
+          goto out;
+        }
+        if (!must_match_at_current_pos && last_token == 0)
+        {
+          struct mystr last_str = INIT_MYSTR;
+          str_mid_to_end(&name_remain_str, &last_str,
+            str_getlen(&name_remain_str) - str_getlen(&s_match_needed_str));
+          locate_result = str_locate_str(&last_str, &s_match_needed_str);
+          str_free(&last_str);
+
+          if (locate_result.found)
+          {
+            ret = 1;
+          }
+          goto out;
+        }
+        /* Chop matched string out of remainder */
+        str_mid_to_end(&name_remain_str, &temp_str,
+                       indexx + str_getlen(&s_match_needed_str));
+        str_copy(&name_remain_str, &temp_str);
       }
-      indexx = locate_result.index;
-      if (must_match_at_current_pos && indexx > 0)
+      if (last_token == '?')
       {
-        goto out;
+        if (str_isempty(&name_remain_str))
+        {
+          goto out;
+        }
+        str_right(&name_remain_str, &temp_str, str_getlen(&name_remain_str) - 1);
+        str_copy(&name_remain_str, &temp_str);
+        must_match_at_current_pos = 1;
       }
-      if (!must_match_at_current_pos && last_token == 0)
+      else if (last_token == '{')
       {
-        struct mystr last_str = INIT_MYSTR;
-        str_mid_to_end(&name_remain_str, &last_str,
-          str_getlen(&name_remain_str) - str_getlen(&s_match_needed_str));
-        locate_result = str_locate_str(&last_str, &s_match_needed_str);
-        str_free(&last_str);
+        struct str_locate_result end_brace =
+          str_locate_char(&filter_remain_str, '}');
+        must_match_at_current_pos = 1;
+        if (end_brace.found)
+        {
+          int entire = (*iters == 1 && last_token == '{');
 
-        if (locate_result.found)
+          str_split_char(&filter_remain_str, &temp_str, '}');
+          str_copy(&brace_list_str, &filter_remain_str);
+          str_copy(&filter_remain_str, &temp_str);
+          str_split_char(&brace_list_str, &temp_str, ',');
+          while (!str_isempty(&brace_list_str))
+          {
+            str_empty(&new_filter_str);
+            if (!matched && !entire)
+            {
+              str_append_char(&new_filter_str, '*');
+            }
+            str_append_str(&new_filter_str, &brace_list_str);
+            str_append_str(&new_filter_str, &filter_remain_str);
+            if (vsf_filename_passes_filter(&name_remain_str, &new_filter_str,
+                                           iters))
+            {
+              ret = 1;
+              goto out;
+            }
+            str_copy(&brace_list_str, &temp_str);
+            str_split_char(&brace_list_str, &temp_str, ',');
+          }
+          goto out;
+        }
+        else if (str_isempty(&name_remain_str) ||
+                 str_get_char_at(&name_remain_str, 0) != '{')
         {
-          ret = 1;
+          goto out;
+        }
+        else
+        {
+          str_right(&name_remain_str, &temp_str,
+                    str_getlen(&name_remain_str) - 1);
+          str_copy(&name_remain_str, &temp_str);
         }
-        goto out;
-      }
-      /* Chop matched string out of remainder */
-      str_mid_to_end(&name_remain_str, &temp_str,
-                     indexx + str_getlen(&s_match_needed_str));
-      str_copy(&name_remain_str, &temp_str);
-    }
-    if (last_token == '?')
-    {
-      if (str_isempty(&name_remain_str))
-      {
-        goto out;
       }
-      str_right(&name_remain_str, &temp_str, str_getlen(&name_remain_str) - 1);
-      str_copy(&name_remain_str, &temp_str);
-      must_match_at_current_pos = 1;
-    }
-    else if (last_token == '{')
-    {
-      struct str_locate_result end_brace =
-        str_locate_char(&filter_remain_str, '}');
-      must_match_at_current_pos = 1;
-      if (end_brace.found)
+      else if (last_token == '[')
       {
-        str_split_char(&filter_remain_str, &temp_str, '}');
-        str_copy(&brace_list_str, &filter_remain_str);
-        str_copy(&filter_remain_str, &temp_str);
-        str_split_char(&brace_list_str, &temp_str, ',');
-        while (!str_isempty(&brace_list_str))
+        struct str_locate_result end_sqb =
+          str_locate_char(&filter_remain_str, ']');
+        must_match_at_current_pos = 1;
+        if (end_sqb.found)
         {
-          str_copy(&new_filter_str, &brace_list_str);
-          str_append_str(&new_filter_str, &filter_remain_str);
-          if (vsf_filename_passes_filter(&name_remain_str, &new_filter_str,
-                                         iters))
+          unsigned int cur_pos;
+          char stch, ench;
+          const char *p_brace;
+
+          str_split_char(&filter_remain_str, &temp_str, ']');
+          str_copy(&brace_list_str, &filter_remain_str);
+          str_copy(&filter_remain_str, &temp_str);
+          p_brace = str_getbuf(&brace_list_str);
+          for (cur_pos = 0; cur_pos < str_getlen(&brace_list_str);)
           {
-            ret = 1;
-            goto out;
+            stch = p_brace[cur_pos];
+            // char vers. range
+            if (cur_pos + 2 < str_getlen(&brace_list_str) &&
+                p_brace[cur_pos+1] == '-')
+            {
+              ench = p_brace[cur_pos+2];
+              cur_pos += 3;
+            }
+            else
+            {
+              ench = stch;
+              cur_pos++;
+            }
+            // expand char[s]
+            for (;stch <= ench && !str_isempty(&brace_list_str); stch++)
+            {
+              str_empty(&new_filter_str);
+              if (!matched)
+              {
+                str_append_char(&new_filter_str, '*');
+              }
+              str_append_char(&new_filter_str, stch);
+              str_append_str(&new_filter_str, &filter_remain_str);
+              if (vsf_filename_passes_filter(&name_remain_str, &new_filter_str,
+                                             iters))
+              {
+                ret = 1;
+                goto out;
+              }
+            }
           }
-          str_copy(&brace_list_str, &temp_str);
-          str_split_char(&brace_list_str, &temp_str, ',');
+          goto out;
+        }
+        else if (str_isempty(&name_remain_str) ||
+                 str_get_char_at(&name_remain_str, 0) != '[')
+        {
+          goto out;
+        }
+        else
+        {
+          str_right(&name_remain_str, &temp_str,
+                    str_getlen(&name_remain_str) - 1);
+          str_copy(&name_remain_str, &temp_str);
         }
-        goto out;
-      }
-      else if (str_isempty(&name_remain_str) ||
-               str_get_char_at(&name_remain_str, 0) != '{')
-      {
-        goto out;
       }
       else
       {
-        str_right(&name_remain_str, &temp_str,
-                  str_getlen(&name_remain_str) - 1);
-        str_copy(&name_remain_str, &temp_str);
+        must_match_at_current_pos = 0;
       }
-    }
-    else
-    {
-      must_match_at_current_pos = 0;
-    }
+    } while (locate_result.found &&
+             str_getlen(&name_remain_str) > 0 && last_token != '*');
   }
   /* Any incoming string left means no match unless we ended on the correct
    * type of wildcard.
-- 
2.14.4