Ignore:
File:
1 edited

Legend:

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

    r6921178 rf215bb5  
    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
    68 /**
    69  * Get collating element matching a string.
    70  *
    71  * @param str String representation of the element.
    72  * @return Matching collating element or COLL_ELM_INVALID.
    73  */
    74 static coll_elm_t _coll_elm_get(const char* str)
     59/** Get collating element matching a string.
     60 *
     61 * @param str
     62 * @return
     63 */
     64static _coll_elm_t _coll_elm_get(const char* str)
    7565{
    7666        if (str[0] == '\0' || str[1] != '\0') {
     
    8070}
    8171
    82 /**
    83  * Get collating element matching a single character.
    84  *
    85  * @param c Character representation of the element.
    86  * @return Matching collating element.
    87  */
    88 static coll_elm_t _coll_elm_char(int c)
     72/** Get collating element matching a single character.
     73 *
     74 * @param c
     75 * @return
     76 */
     77static _coll_elm_t _coll_elm_char(int c)
    8978{
    9079        return c;
    9180}
    9281
    93 /**
    94  * Match collating element with a beginning of a string.
    95  *
    96  * @param elm Collating element to match.
    97  * @param str String which beginning should match the element.
     82/** Match collating element with a beginning of a string.
     83 *
     84 * @param elm
     85 * @param str
    9886 * @return 0 if the element doesn't match, or the number of characters matched.
    9987 */
    100 static int _coll_elm_match(coll_elm_t elm, const char *str)
     88static int _coll_elm_match(_coll_elm_t elm, const char *str)
    10189{
    10290        return elm == *str;
    10391}
    10492
    105 /**
    106  * Checks whether a string begins with a collating element in the given range.
    107  * Ordering depends on the locale (if locales are supported).
    108  *
    109  * @param first First element of the range.
    110  * @param second Last element of the range.
    111  * @param str String to match.
    112  * @return 0 if there is no match, or the number of characters matched.
    113  */
    114 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,
    11594    const char *str)
    11695{
     
    11897}
    11998
    120 /**
    121  * Read a string delimited by [? and ?].
     99/** Read a string delimited by [? and ?].
    122100 *
    123101 * @param pattern Pointer to the string to read from. Its position is moved
     
    127105 * @param buf_sz Read buffer's size. If the buffer is not large enough for
    128106 *    the entire string, the string is cut with no error indication.
    129  * @param flags Flags modifying the behavior.
    130  * @return True on success, false if the pattern is invalid.
     107 * @return
    131108 */
    132109static bool _get_delimited(
     
    195172
    196173/**
    197  * Compare function for binary search in the _char_classes array.
    198  *
    199  * @param key Key of the searched element.
    200  * @param elem Element of _char_classes array.
    201  * @return Ordering indicator (-1 less than, 0 equal, 1 greater than).
     174 *
     175 * @param key
     176 * @param elem
     177 * @return
    202178 */
    203179static int _class_compare(const void *key, const void *elem)
     
    208184
    209185/**
    210  * Returns whether the given character belongs to the specified character class.
    211  *
    212  * @param cname Name of the character class.
    213  * @param c Character.
    214  * @return True if the character belongs to the class, false otherwise.
     186 *
     187 * @param cname
     188 * @param c
     189 * @return
    215190 */
    216191static bool _is_in_class (const char *cname, int c)
     
    231206
    232207/**
    233  * Tries to parse an initial part of the pattern as a character class pattern,
    234  * and if successful, matches the beginning of the given string against the class.
    235  *
    236  * @param pattern Pointer to the pattern to match. Must begin with a class
    237  *    specifier and is repositioned to the first character after the specifier
    238  *    if successful.
    239  * @param str String to match.
    240  * @param flags Flags modifying the behavior (see fnmatch()).
    241  * @return INVALID_PATTERN if the pattern doesn't start with a valid class
    242  *    specifier, 0 if the beginning of the matched string doesn't belong
    243  *    to the class, or positive number of characters matched.
     208 *
     209 * @param pattern
     210 * @param str
     211 * @param flags
     212 * @return
    244213 */
    245214static int _match_char_class(const char **pattern, const char *str, int flags)
     
    257226
    258227/**
    259  * Reads the next collating element in the pattern, taking into account
    260  * locale (if supported) and flags (see fnmatch()).
    261  *
    262  * @param pattern Pattern.
    263  * @param flags Flags given to fnmatch().
    264  * @return Collating element on success,
    265  *     or COLL_ELM_INVALID if the pattern is invalid.
    266  */
    267 static coll_elm_t _next_coll_elm(const char **pattern, int flags)
    268 {
    269         assert(pattern != NULL);
    270         assert(*pattern != NULL);
    271         assert(**pattern != '\0');
    272 
     228 *
     229 * @param pattern
     230 * @param flags
     231 * @return
     232 */
     233static _coll_elm_t _next_coll_elm(const char **pattern, int flags)
     234{
    273235        const char *p = *pattern;
    274236        const bool noescape = (flags & FNM_NOESCAPE) != 0;
     
    295257        if (!noescape && *p == '\\') {
    296258                p++;
    297                 if (*p == '\0') {
    298                         *pattern = p;
    299                         return COLL_ELM_INVALID;
    300                 }
    301259        }
    302260        if (pathname && *p == '/') {
    303261                return COLL_ELM_INVALID;
    304262        }
    305        
     263
    306264        *pattern = p + 1;
    307265        return _coll_elm_char(*p);
     
    309267
    310268/**
    311  * Matches the beginning of the given string against a bracket expression
    312  * the pattern begins with.
    313  *
    314  * @param pattern Pointer to the beginning of a bracket expression in a pattern.
    315  *     On success, the pointer is moved to the first character after the
    316  *     bracket expression.
    317  * @param str Unmatched part of the string.
    318  * @param flags Flags given to fnmatch().
    319  * @return INVALID_PATTERN if the pattern is invalid, 0 if there is no match
    320  *     or the number of matched characters on success.
     269 *
     270 * @param pattern
     271 * @param str
     272 * @param flags
     273 * @return
    321274 */
    322275static int _match_bracket_expr(const char **pattern, const char *str, int flags)
     
    362315        }
    363316       
    364         coll_elm_t current_elm = COLL_ELM_INVALID;
     317        _coll_elm_t current_elm = COLL_ELM_INVALID;
    365318       
    366319        while (*p != ']') {
     
    369322                        /* Range expression. */
    370323                        p++;
    371                         coll_elm_t end_elm = _next_coll_elm(&p, flags);
     324                        _coll_elm_t end_elm = _next_coll_elm(&p, flags);
    372325                        if (end_elm == COLL_ELM_INVALID) {
    373326                                return INVALID_PATTERN;
     
    405358
    406359/**
    407  * Matches a portion of the pattern containing no asterisks (*) against
    408  * the given string.
    409  *
    410  * @param pattern Pointer to the unmatched portion of the pattern.
    411  *     On success, the pointer is moved to the first asterisk, or to the
    412  *     terminating nul character, whichever occurs first.
    413  * @param string Pointer to the input string. On success, the pointer is moved
    414  *     to the first character that wasn't explicitly matched.
    415  * @param flags Flags given to fnmatch().
    416  * @return True if the entire subpattern matched. False otherwise.
     360 *
     361 * @param pattern
     362 * @param string
     363 * @param flags
     364 * @return
    417365 */
    418366static bool _partial_match(const char **pattern, const char **string, int flags)
     
    509457
    510458/**
    511  * Match string against a pattern.
    512  *
    513  * @param pattern Pattern.
    514  * @param string String to match.
    515  * @param flags Flags given to fnmatch().
    516  * @return True if the string matched the pattern, false otherwise.
     459 *
     460 * @param pattern
     461 * @param string
     462 * @param flags
     463 * @return
    517464 */
    518465static bool _full_match(const char *pattern, const char *string, int flags)
     
    549496                        end = string;
    550497                } else {
    551                         end = strchrnul(string, pathname ? '/' : '\0');
     498                        end= strchrnul(string, pathname ? '/' : '\0');
    552499                }
    553500
     
    572519
    573520/**
    574  * Transform the entire string to lowercase.
    575  *
    576  * @param s Input string.
    577  * @return Newly allocated copy of the input string with all uppercase
    578  *     characters folded to their lowercase variants.
     521 *
     522 * @param s
     523 * @return
    579524 */
    580525static char *_casefold(const char *s)
     
    591536 * Filename pattern matching.
    592537 *
    593  * @param pattern Pattern to match the string against.
    594  * @param string Matched string.
    595  * @param flags Flags altering the matching of special characters
    596  *     (mainly for dot and slash).
    597  * @return Zero if the string matches the pattern, FNM_NOMATCH otherwise.
     538 * @param pattern
     539 * @param string
     540 * @param flags
     541 * @return
    598542 */
    599543int posix_fnmatch(const char *pattern, const char *string, int flags)
     
    624568}
    625569
    626 // FIXME: put the testcases to the app/tester after fnmatch is included into libc
     570// FIXME: put the testcases somewhere else
    627571
    628572#if 0
Note: See TracChangeset for help on using the changeset viewer.