match.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * ##/%% variable matching code ripped out of ash shell for code sharing
  3. *
  4. * This code is derived from software contributed to Berkeley by
  5. * Kenneth Almquist.
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. *
  9. * Copyright (c) 1989, 1991, 1993, 1994
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
  13. * was re-ported from NetBSD and debianized.
  14. */
  15. #ifdef STANDALONE
  16. # include <stdbool.h>
  17. # include <stdio.h>
  18. # include <stdlib.h>
  19. # include <string.h>
  20. # include <unistd.h>
  21. # define FAST_FUNC /* nothing */
  22. # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN /* nothing */
  23. # define POP_SAVED_FUNCTION_VISIBILITY /* nothing */
  24. #else
  25. # include "libbb.h"
  26. #endif
  27. #include <fnmatch.h>
  28. #include "match.h"
  29. char* FAST_FUNC scan_and_match(char *string, const char *pattern, unsigned flags)
  30. {
  31. char *loc;
  32. char *end;
  33. unsigned len = strlen(string);
  34. int early_exit;
  35. /* We can stop the scan early only if the string part
  36. * we are matching against is shrinking, and the pattern has
  37. * an unquoted "star" at the corresponding end. There are two cases.
  38. * Case 1:
  39. * "qwerty" does not match against pattern "*zy",
  40. * no point in trying to match "werty", "erty" etc:
  41. */
  42. early_exit = (flags == (SCAN_MOVE_FROM_LEFT + SCAN_MATCH_RIGHT_HALF) && pattern[0] == '*');
  43. if (flags & SCAN_MOVE_FROM_LEFT) {
  44. loc = string;
  45. end = string + len + 1;
  46. } else {
  47. loc = string + len;
  48. end = string - 1;
  49. if (flags == (SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF)) {
  50. /* Case 2:
  51. * "qwerty" does not match against pattern "qz*",
  52. * no point in trying to match "qwert", "qwer" etc:
  53. */
  54. const char *p = pattern + strlen(pattern);
  55. if (--p >= pattern && *p == '*') {
  56. early_exit = 1;
  57. while (--p >= pattern && *p == '\\')
  58. early_exit ^= 1;
  59. }
  60. }
  61. }
  62. while (loc != end) {
  63. char c;
  64. int r;
  65. c = *loc;
  66. if (flags & SCAN_MATCH_LEFT_HALF) {
  67. *loc = '\0';
  68. r = fnmatch(pattern, string, 0);
  69. //bb_error_msg("fnmatch('%s','%s',0):%d", pattern, string, r);
  70. *loc = c;
  71. } else {
  72. r = fnmatch(pattern, loc, 0);
  73. //bb_error_msg("fnmatch('%s','%s',0):%d", pattern, loc, r);
  74. }
  75. if (r == 0) /* match found */
  76. return loc;
  77. if (early_exit) {
  78. #ifdef STANDALONE
  79. printf("(early exit) ");
  80. #endif
  81. break;
  82. }
  83. if (flags & SCAN_MOVE_FROM_LEFT) {
  84. loc++;
  85. } else {
  86. loc--;
  87. }
  88. }
  89. return NULL;
  90. }
  91. #ifdef STANDALONE
  92. int main(int argc, char *argv[])
  93. {
  94. char *string;
  95. char *op;
  96. char *pattern;
  97. char *loc;
  98. setvbuf(stdout, NULL, _IONBF, 0);
  99. if (!argv[1]) {
  100. puts(
  101. "Usage: match <test> [test...]\n\n"
  102. "Where a <test> is the form: <string><op><match>\n"
  103. "This is to test the shell ${var#val} expression type.\n\n"
  104. "e.g. `match 'abc#a*'` -> bc"
  105. );
  106. return 1;
  107. }
  108. while (*++argv) {
  109. size_t off;
  110. unsigned scan_flags;
  111. string = *argv;
  112. off = strcspn(string, "#%");
  113. if (!off) {
  114. printf("invalid format\n");
  115. continue;
  116. }
  117. op = string + off;
  118. scan_flags = pick_scan(op[0], op[1]);
  119. printf("'%s': flags:%x, ", string, scan_flags);
  120. pattern = op + 1;
  121. if (op[0] == op[1])
  122. pattern++;
  123. op[0] = '\0';
  124. loc = scan_and_match(string, pattern, scan_flags);
  125. if (scan_flags & SCAN_MATCH_LEFT_HALF) {
  126. printf("'%s'\n", loc);
  127. } else {
  128. if (loc)
  129. *loc = '\0';
  130. printf("'%s'\n", string);
  131. }
  132. }
  133. return 0;
  134. }
  135. #endif