Changeset 4c8f5e7 in mainline
- Timestamp:
- 2011-08-17T17:42:36Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4419c34
- Parents:
- 903bd436
- Location:
- uspace/lib/posix
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/fnmatch.c
r903bd436 r4c8f5e7 33 33 */ 34 34 35 // TODO: clean this up a bit 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 */ 36 43 37 44 #include "stdbool.h" … … 46 53 #include "fnmatch.h" 47 54 48 // TODO: documentation 49 55 /* Returned by _match... functions. */ 50 56 #define INVALID_PATTERN -1 51 57 … … 53 59 * but may be extended for better locale support. 54 60 */ 55 typedef int _coll_elm_t; 56 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 */ 57 66 #define COLL_ELM_INVALID -1 58 67 … … 60 69 * 61 70 * @param str 62 * @return 63 */ 64 static _coll_elm_t _coll_elm_get(const char* str)71 * @return Matching collating element or COLL_ELM_INVALID. 72 */ 73 static coll_elm_t _coll_elm_get(const char* str) 65 74 { 66 75 if (str[0] == '\0' || str[1] != '\0') { … … 75 84 * @return 76 85 */ 77 static _coll_elm_t _coll_elm_char(int c)86 static coll_elm_t _coll_elm_char(int c) 78 87 { 79 88 return c; … … 86 95 * @return 0 if the element doesn't match, or the number of characters matched. 87 96 */ 88 static int _coll_elm_match( _coll_elm_t elm, const char *str)97 static int _coll_elm_match(coll_elm_t elm, const char *str) 89 98 { 90 99 return elm == *str; 91 100 } 92 101 93 static int _coll_elm_between(_coll_elm_t first, _coll_elm_t second, 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, 94 111 const char *str) 95 112 { … … 105 122 * @param buf_sz Read buffer's size. If the buffer is not large enough for 106 123 * the entire string, the string is cut with no error indication. 107 * @return 124 * @param flags Flags modifying the behavior. 125 * @return True on success, false if the pattern is invalid. 108 126 */ 109 127 static bool _get_delimited( … … 171 189 }; 172 190 173 /** 191 /** Compare function for binary search in the _char_classes array. 174 192 * 175 193 * @param key … … 183 201 } 184 202 185 /** 203 /** Returns whether the given character belongs to the specified character class. 186 204 * 187 * @param cname 188 * @param c 189 * @return 205 * @param cname Name of the character class. 206 * @param c Character. 207 * @return True if the character belongs to the class, false otherwise. 190 208 */ 191 209 static bool _is_in_class (const char *cname, int c) … … 205 223 } 206 224 207 /** 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. 208 227 * 209 * @param pattern 210 * @param str 211 * @param flags 212 * @return 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. 213 236 */ 214 237 static int _match_char_class(const char **pattern, const char *str, int flags) … … 225 248 /************** END CHARACTER CLASSES ****************/ 226 249 227 /** 250 /** Reads the next collating element in the pattern, taking into account 251 * locale (if supported) and flags (see fnmatch()). 228 252 * 229 * @param pattern 230 * @param flags 231 * @return 232 */ 233 static _coll_elm_t _next_coll_elm(const char **pattern, int flags) 234 { 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 235 264 const char *p = *pattern; 236 265 const bool noescape = (flags & FNM_NOESCAPE) != 0; … … 257 286 if (!noescape && *p == '\\') { 258 287 p++; 288 if (*p == '\0') { 289 *pattern = p; 290 return COLL_ELM_INVALID; 291 } 259 292 } 260 293 if (pathname && *p == '/') { 261 294 return COLL_ELM_INVALID; 262 295 } 263 296 264 297 *pattern = p + 1; 265 298 return _coll_elm_char(*p); 266 299 } 267 300 268 /** 301 /** Matches the beginning of the given string against a bracket expression 302 * the pattern begins with. 269 303 * 270 * @param pattern 271 * @param str 272 * @param flags 273 * @return 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. 274 311 */ 275 312 static int _match_bracket_expr(const char **pattern, const char *str, int flags) … … 315 352 } 316 353 317 _coll_elm_t current_elm = COLL_ELM_INVALID;354 coll_elm_t current_elm = COLL_ELM_INVALID; 318 355 319 356 while (*p != ']') { … … 322 359 /* Range expression. */ 323 360 p++; 324 _coll_elm_t end_elm = _next_coll_elm(&p, flags);361 coll_elm_t end_elm = _next_coll_elm(&p, flags); 325 362 if (end_elm == COLL_ELM_INVALID) { 326 363 return INVALID_PATTERN; … … 357 394 } 358 395 359 /** 396 /** Matches a portion of the pattern containing no asterisks (*) against 397 * the given string. 360 398 * 361 * @param pattern 362 * @param string 363 * @param flags 364 * @return 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. 365 406 */ 366 407 static bool _partial_match(const char **pattern, const char **string, int flags) … … 456 497 } 457 498 458 /** 499 /** Match string against a pattern. 459 500 * 460 * @param pattern 461 * @param string 462 * @param flags 463 * @return 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. 464 505 */ 465 506 static bool _full_match(const char *pattern, const char *string, int flags) … … 518 559 } 519 560 520 /** 561 /** Transform the entire string to lowercase. 521 562 * 522 * @param s 523 * @return 563 * @param s Input string. 564 * @return Newly allocated copy of the input string with all uppercase 565 * characters folded to their lowercase variants. 524 566 */ 525 567 static char *_casefold(const char *s) -
uspace/lib/posix/locale.c
r903bd436 r4c8f5e7 42 42 #include "string.h" 43 43 44 // TODO: documentation 44 /* Just a very basic dummy implementation. 45 * This should allow code using locales to work properly, but doesn't provide 46 * any localization functionality. 47 * Should be extended/rewritten when or if HelenOS supports locales natively. 48 */ 45 49 46 50 struct __posix_locale { … … 75 79 }; 76 80 77 /** 81 /** Set program locale. 78 82 * 79 * @param category 80 * @param locale 81 * @return 83 * @param category What category to set. 84 * @param locale Locale name. 85 * @return Original locale name on success, NULL on failure. 82 86 */ 83 87 char *posix_setlocale(int category, const char *locale) … … 91 95 } 92 96 93 /** 97 /** Return locale-specific information. 94 98 * 95 * @return 99 * @return Information about the current locale. 96 100 */ 97 101 struct posix_lconv *posix_localeconv(void) … … 101 105 } 102 106 103 /** 107 /** Duplicate locale object. 104 108 * 105 * @param locobj 106 * @return 109 * @param locobj Object to duplicate. 110 * @return Duplicated object. 107 111 */ 108 112 posix_locale_t posix_duplocale(posix_locale_t locobj) … … 121 125 } 122 126 123 /** 127 /** Free locale object. 124 128 * 125 * @param locobj 129 * @param locobj Object to free. 126 130 */ 127 131 void posix_freelocale(posix_locale_t locobj) … … 132 136 } 133 137 134 /** 138 /** Create or modify a locale object. 135 139 * 136 * @param category_mask 137 * @param locale 138 * @param base 139 * @return 140 * @param category_mask Mask of categories to be set or modified. 141 * @param locale Locale to be used. 142 * @param base Object to modify. 0 if new object is to be created. 143 * @return The new/modified locale object. 140 144 */ 141 145 posix_locale_t posix_newlocale(int category_mask, const char *locale, … … 159 163 } 160 164 161 /** 165 /** Set locale for the current thread. 162 166 * 163 * @param newloc 164 * @return 167 * @param newloc Locale to use. 168 * @return The previously set locale or LC_GLOBAL_LOCALE 165 169 */ 166 170 posix_locale_t posix_uselocale(posix_locale_t newloc) 167 171 { 168 172 // TODO 169 return NULL;173 return LC_GLOBAL_LOCALE; 170 174 } 171 175 -
uspace/lib/posix/locale.h
r903bd436 r4c8f5e7 35 35 #ifndef POSIX_LOCALE_H_ 36 36 #define POSIX_LOCALE_H_ 37 38 // TODO: documentation39 37 40 38 #ifndef NULL -
uspace/lib/posix/stdio.c
r903bd436 r4c8f5e7 320 320 321 321 /** 322 *323 * @param buf324 * @param size325 * @param mode326 * @return327 */328 FILE *posix_fmemopen(void *restrict buf, size_t size,329 const char *restrict mode)330 {331 // TODO332 not_implemented();333 }334 335 /**336 *337 * @param bufp338 * @param sizep339 * @return340 */341 FILE *posix_open_memstream(char **bufp, size_t *sizep)342 {343 // TODO344 not_implemented();345 }346 347 /**348 322 * Write error messages to standard error. 349 323 * -
uspace/lib/posix/stdio.h
r903bd436 r4c8f5e7 65 65 extern FILE *posix_freopen(const char *restrict filename, 66 66 const char *restrict mode, FILE *restrict stream); 67 extern FILE *posix_fmemopen(void *restrict buf, size_t size,68 const char *restrict mode);69 extern FILE *posix_open_memstream(char **bufp, size_t *sizep);70 67 71 68 /* Error Messages */ -
uspace/lib/posix/stdlib/strtold.c
r903bd436 r4c8f5e7 48 48 #include "../limits.h" 49 49 50 // FIXME: #include <float.h> 50 #include "../float.h" 51 51 52 52 #ifndef HUGE_VALL -
uspace/lib/posix/unistd.c
r903bd436 r4c8f5e7 88 88 int posix_isatty(int fd) 89 89 { 90 // TODO 90 91 /* Always returns false, because there is no easy way to find 91 92 * out under HelenOS. */
Note:
See TracChangeset
for help on using the changeset viewer.