Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/fnmatch.c

    r4c8f5e7 rf215bb5f  
    3333 */
    3434
    35 /* This file contains an implementation of the fnmatch() pattern matching
    36  * function. There is more code than necessary to account for the possibility
    37  * of adding POSIX-like locale support to the system in the future. Functions
    38  * that are only necessary for locale support currently simply use single
    39  * characters for "collation elements".
    40  * When (or if) locales are properly implemented, extending this implementation
    41  * will be fairly straightforward.
    42  */
     35// TODO: clean this up a bit
    4336
    4437#include "stdbool.h"
     
    5346#include "fnmatch.h"
    5447
    55 /* Returned by _match... functions. */
     48// TODO: documentation
     49
    5650#define INVALID_PATTERN -1
    5751
     
    5953 * but may be extended for better locale support.
    6054 */
    61 typedef int coll_elm_t;
    62 
    63 /** Return value indicating that the element in question
    64  * is not valid in the current locale. (That is, if locales are supported.)
    65  */
     55typedef int _coll_elm_t;
     56
    6657#define COLL_ELM_INVALID -1
    6758
     
    6960 *
    7061 * @param str
    71  * @return Matching collating element or COLL_ELM_INVALID.
    72  */
    73 static coll_elm_t _coll_elm_get(const char* str)
     62 * @return
     63 */
     64static _coll_elm_t _coll_elm_get(const char* str)
    7465{
    7566        if (str[0] == '\0' || str[1] != '\0') {
     
    8475 * @return
    8576 */
    86 static coll_elm_t _coll_elm_char(int c)
     77static _coll_elm_t _coll_elm_char(int c)
    8778{
    8879        return c;
     
    9586 * @return 0 if the element doesn't match, or the number of characters matched.
    9687 */
    97 static int _coll_elm_match(coll_elm_t elm, const char *str)
     88static int _coll_elm_match(_coll_elm_t elm, const char *str)
    9889{
    9990        return elm == *str;
    10091}
    10192
    102 /** Checks whether a string begins with a collating element in the given range.
    103  *  Ordering depends on the locale (if locales are supported).
    104  *
    105  * @param first First element of the range.
    106  * @param second Last element of the range.
    107  * @param str String to match.
    108  * @return 0 if there is no match, or the number of characters matched.
    109  */
    110 static int _coll_elm_between(coll_elm_t first, coll_elm_t second,
     93static int _coll_elm_between(_coll_elm_t first, _coll_elm_t second,
    11194    const char *str)
    11295{
     
    122105 * @param buf_sz Read buffer's size. If the buffer is not large enough for
    123106 *    the entire string, the string is cut with no error indication.
    124  * @param flags Flags modifying the behavior.
    125  * @return True on success, false if the pattern is invalid.
     107 * @return
    126108 */
    127109static bool _get_delimited(
     
    189171};
    190172
    191 /** Compare function for binary search in the _char_classes array.
     173/**
    192174 *
    193175 * @param key
     
    201183}
    202184
    203 /** Returns whether the given character belongs to the specified character class.
    204  *
    205  * @param cname Name of the character class.
    206  * @param c Character.
    207  * @return True if the character belongs to the class, false otherwise.
     185/**
     186 *
     187 * @param cname
     188 * @param c
     189 * @return
    208190 */
    209191static bool _is_in_class (const char *cname, int c)
     
    223205}
    224206
    225 /** Tries to parse an initial part of the pattern as a character class pattern,
    226  *  and if successful, matches the beginning of the given string against the class.
    227  *
    228  * @param pattern Pointer to the pattern to match. Must begin with a class
    229  *    specifier and is repositioned to the first character after the specifier
    230  *    if successful.
    231  * @param str String to match.
    232  * @param flags Flags modifying the behavior (see fnmatch()).
    233  * @return INVALID_PATTERN if the pattern doesn't start with a valid class
    234  *    specifier, 0 if the beginning of the matched string doesn't belong
    235  *    to the class, or positive number of characters matched.
     207/**
     208 *
     209 * @param pattern
     210 * @param str
     211 * @param flags
     212 * @return
    236213 */
    237214static int _match_char_class(const char **pattern, const char *str, int flags)
     
    248225/************** END CHARACTER CLASSES ****************/
    249226
    250 /** Reads the next collating element in the pattern, taking into account
    251  *  locale (if supported) and flags (see fnmatch()).
    252  *
    253  * @param pattern Pattern.
    254  * @param flags Flags given to fnmatch().
    255  * @return Collating element on success,
    256  *     or COLL_ELM_INVALID if the pattern is invalid.
    257  */
    258 static coll_elm_t _next_coll_elm(const char **pattern, int flags)
    259 {
    260         assert(pattern != NULL);
    261         assert(*pattern != NULL);
    262         assert(**pattern != '\0');
    263 
     227/**
     228 *
     229 * @param pattern
     230 * @param flags
     231 * @return
     232 */
     233static _coll_elm_t _next_coll_elm(const char **pattern, int flags)
     234{
    264235        const char *p = *pattern;
    265236        const bool noescape = (flags & FNM_NOESCAPE) != 0;
     
    286257        if (!noescape && *p == '\\') {
    287258                p++;
    288                 if (*p == '\0') {
    289                         *pattern = p;
    290                         return COLL_ELM_INVALID;
    291                 }
    292259        }
    293260        if (pathname && *p == '/') {
    294261                return COLL_ELM_INVALID;
    295262        }
    296        
     263
    297264        *pattern = p + 1;
    298265        return _coll_elm_char(*p);
    299266}
    300267
    301 /** Matches the beginning of the given string against a bracket expression
    302  *  the pattern begins with.
    303  *
    304  * @param pattern Pointer to the beginning of a bracket expression in a pattern.
    305  *     On success, the pointer is moved to the first character after the
    306  *     bracket expression.
    307  * @param str Unmatched part of the string.
    308  * @param flags Flags given to fnmatch().
    309  * @return INVALID_PATTERN if the pattern is invalid, 0 if there is no match
    310  *     or the number of matched characters on success.
     268/**
     269 *
     270 * @param pattern
     271 * @param str
     272 * @param flags
     273 * @return
    311274 */
    312275static int _match_bracket_expr(const char **pattern, const char *str, int flags)
     
    352315        }
    353316       
    354         coll_elm_t current_elm = COLL_ELM_INVALID;
     317        _coll_elm_t current_elm = COLL_ELM_INVALID;
    355318       
    356319        while (*p != ']') {
     
    359322                        /* Range expression. */
    360323                        p++;
    361                         coll_elm_t end_elm = _next_coll_elm(&p, flags);
     324                        _coll_elm_t end_elm = _next_coll_elm(&p, flags);
    362325                        if (end_elm == COLL_ELM_INVALID) {
    363326                                return INVALID_PATTERN;
     
    394357}
    395358
    396 /** Matches a portion of the pattern containing no asterisks (*) against
    397  *  the given string.
    398  *
    399  * @param pattern Pointer to the unmatched portion of the pattern.
    400  *     On success, the pointer is moved to the first asterisk, or to the
    401  *     terminating nul character, whichever occurs first.
    402  * @param string Pointer to the input string. On success, the pointer is moved
    403  *     to the first character that wasn't explicitly matched.
    404  * @param flags Flags given to fnmatch().
    405  * @return True if the entire subpattern matched. False otherwise.
     359/**
     360 *
     361 * @param pattern
     362 * @param string
     363 * @param flags
     364 * @return
    406365 */
    407366static bool _partial_match(const char **pattern, const char **string, int flags)
     
    497456}
    498457
    499 /** Match string against a pattern.
    500  *
    501  * @param pattern Pattern.
    502  * @param string String to match.
    503  * @param flags Flags given to fnmatch().
    504  * @return True if the string matched the pattern, false otherwise.
     458/**
     459 *
     460 * @param pattern
     461 * @param string
     462 * @param flags
     463 * @return
    505464 */
    506465static bool _full_match(const char *pattern, const char *string, int flags)
     
    537496                        end = string;
    538497                } else {
    539                         end = strchrnul(string, pathname ? '/' : '\0');
     498                        end= strchrnul(string, pathname ? '/' : '\0');
    540499                }
    541500
     
    559518}
    560519
    561 /** Transform the entire string to lowercase.
    562  *
    563  * @param s Input string.
    564  * @return Newly allocated copy of the input string with all uppercase
    565  *     characters folded to their lowercase variants.
     520/**
     521 *
     522 * @param s
     523 * @return
    566524 */
    567525static char *_casefold(const char *s)
Note: See TracChangeset for help on using the changeset viewer.