2016-11-17 13:35:34 +00:00
|
|
|
From ba0520650ae7f9f63e48ba9fb3a94297aebe2d0c Mon Sep 17 00:00:00 2001
|
2016-03-17 13:10:03 +00:00
|
|
|
From: Martin Sehnoutka <msehnout@redhat.com>
|
2016-11-17 13:35:34 +00:00
|
|
|
Date: Wed, 7 Sep 2016 14:22:21 +0200
|
2018-07-25 11:28:40 +00:00
|
|
|
Subject: [PATCH 14/59] Add support for square brackets in ls.
|
2016-03-17 13:10:03 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
ls.c | 222 +++++++++++++++++++++++++++++++++++++++++++++----------------------
|
|
|
|
1 file changed, 150 insertions(+), 72 deletions(-)
|
|
|
|
|
|
|
|
diff --git a/ls.c b/ls.c
|
2016-11-17 13:35:34 +00:00
|
|
|
index 616b2d9..b840136 100644
|
2016-03-17 13:10:03 +00:00
|
|
|
--- a/ls.c
|
|
|
|
+++ b/ls.c
|
|
|
|
@@ -246,7 +246,7 @@ vsf_filename_passes_filter(const struct mystr* p_filename_str,
|
2011-11-15 12:34:41 +00:00
|
|
|
int ret = 0;
|
|
|
|
char last_token = 0;
|
|
|
|
int must_match_at_current_pos = 1;
|
2016-11-17 13:35:34 +00:00
|
|
|
-
|
2011-11-15 12:34:41 +00:00
|
|
|
+ int matched = 0;
|
2016-11-17 13:35:34 +00:00
|
|
|
|
2011-11-15 12:34:41 +00:00
|
|
|
str_copy(&filter_remain_str, p_filter_str);
|
2016-11-17 13:35:34 +00:00
|
|
|
|
2016-03-17 13:10:03 +00:00
|
|
|
@@ -276,7 +276,7 @@ vsf_filename_passes_filter(const struct mystr* p_filename_str,
|
2011-11-15 12:34:41 +00:00
|
|
|
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)
|
2016-03-17 13:10:03 +00:00
|
|
|
@@ -294,94 +294,172 @@ vsf_filename_passes_filter(const struct mystr* p_filename_str,
|
2011-11-15 12:34:41 +00:00
|
|
|
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)
|
2014-07-04 11:38:27 +00:00
|
|
|
+
|
2011-11-15 12:34:41 +00:00
|
|
|
+ matched = 0;
|
|
|
|
+ do {
|
|
|
|
+ if (!str_isempty(&s_match_needed_str))
|
2014-07-04 11:38:27 +00:00
|
|
|
{
|
|
|
|
- /* Fail */
|
|
|
|
- goto out;
|
2011-11-15 12:34:41 +00:00
|
|
|
+ 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)
|
2014-07-04 11:38:27 +00:00
|
|
|
+ {
|
2011-11-15 12:34:41 +00:00
|
|
|
+ goto out;
|
2014-07-04 11:38:27 +00:00
|
|
|
+ }
|
2011-11-15 12:34:41 +00:00
|
|
|
+ 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);
|
|
|
|
}
|
2014-07-04 11:38:27 +00:00
|
|
|
- indexx = locate_result.index;
|
|
|
|
- if (must_match_at_current_pos && indexx > 0)
|
2011-11-15 12:34:41 +00:00
|
|
|
+ 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;
|
|
|
|
}
|
2014-07-04 11:38:27 +00:00
|
|
|
- if (!must_match_at_current_pos && last_token == 0)
|
2011-11-15 12:34:41 +00:00
|
|
|
+ else if (last_token == '{')
|
|
|
|
{
|
2014-07-04 11:38:27 +00:00
|
|
|
- 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);
|
2011-11-15 12:34:41 +00:00
|
|
|
+ struct str_locate_result end_brace =
|
|
|
|
+ str_locate_char(&filter_remain_str, '}');
|
|
|
|
+ must_match_at_current_pos = 1;
|
|
|
|
+ if (end_brace.found)
|
|
|
|
+ {
|
2014-07-04 11:38:27 +00:00
|
|
|
+ int entire = (*iters == 1 && last_token == '{');
|
|
|
|
|
|
|
|
- if (locate_result.found)
|
2011-11-15 12:34:41 +00:00
|
|
|
+ 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))
|
2014-07-04 11:38:27 +00:00
|
|
|
+ {
|
2011-11-15 12:34:41 +00:00
|
|
|
+ str_empty(&new_filter_str);
|
2014-07-04 11:38:27 +00:00
|
|
|
+ if (!matched && !entire)
|
2011-11-15 12:34:41 +00:00
|
|
|
+ {
|
|
|
|
+ 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, ',');
|
2014-07-04 11:38:27 +00:00
|
|
|
+ }
|
2011-11-15 12:34:41 +00:00
|
|
|
+ goto out;
|
|
|
|
+ }
|
|
|
|
+ else if (str_isempty(&name_remain_str) ||
|
|
|
|
+ str_get_char_at(&name_remain_str, 0) != '{')
|
2014-07-04 11:38:27 +00:00
|
|
|
{
|
|
|
|
- ret = 1;
|
2011-11-15 12:34:41 +00:00
|
|
|
+ goto out;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ str_right(&name_remain_str, &temp_str,
|
|
|
|
+ str_getlen(&name_remain_str) - 1);
|
|
|
|
+ str_copy(&name_remain_str, &temp_str);
|
|
|
|
}
|
2014-07-04 11:38:27 +00:00
|
|
|
- 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))
|
|
|
|
- {
|
2011-11-15 12:34:41 +00:00
|
|
|
- goto out;
|
|
|
|
}
|
2014-07-04 11:38:27 +00:00
|
|
|
- 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)
|
2011-11-15 12:34:41 +00:00
|
|
|
+ else if (last_token == '[')
|
|
|
|
{
|
2014-07-04 11:38:27 +00:00
|
|
|
- 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))
|
2011-11-15 12:34:41 +00:00
|
|
|
+ struct str_locate_result end_sqb =
|
|
|
|
+ str_locate_char(&filter_remain_str, ']');
|
|
|
|
+ must_match_at_current_pos = 1;
|
|
|
|
+ if (end_sqb.found)
|
2016-03-17 13:10:03 +00:00
|
|
|
{
|
|
|
|
- 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))
|
2011-11-15 12:34:41 +00:00
|
|
|
+ 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);)
|
2014-07-04 11:38:27 +00:00
|
|
|
{
|
|
|
|
- ret = 1;
|
|
|
|
- goto out;
|
2011-11-15 12:34:41 +00:00
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+ }
|
2014-07-04 11:38:27 +00:00
|
|
|
}
|
|
|
|
- str_copy(&brace_list_str, &temp_str);
|
|
|
|
- str_split_char(&brace_list_str, &temp_str, ',');
|
2011-11-15 12:34:41 +00:00
|
|
|
+ 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);
|
2014-07-04 11:38:27 +00:00
|
|
|
}
|
|
|
|
- goto out;
|
|
|
|
- }
|
|
|
|
- else if (str_isempty(&name_remain_str) ||
|
|
|
|
- str_get_char_at(&name_remain_str, 0) != '{')
|
|
|
|
- {
|
|
|
|
- goto out;
|
2011-11-15 12:34:41 +00:00
|
|
|
}
|
|
|
|
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.
|
2016-03-17 13:10:03 +00:00
|
|
|
--
|
2018-07-25 11:28:40 +00:00
|
|
|
2.14.4
|
2016-03-17 13:10:03 +00:00
|
|
|
|