match.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. *loc = c;
  70. } else {
  71. r = fnmatch(pattern, loc, 0);
  72. }
  73. if (r == 0) /* match found */
  74. return loc;
  75. if (early_exit) {
  76. #ifdef STANDALONE
  77. printf("(early exit) ");
  78. #endif
  79. break;
  80. }
  81. if (flags & SCAN_MOVE_FROM_LEFT) {
  82. loc++;
  83. } else {
  84. loc--;
  85. }
  86. }
  87. return NULL;
  88. }
  89. #ifdef STANDALONE
  90. int main(int argc, char *argv[])
  91. {
  92. char *string;
  93. char *op;
  94. char *pattern;
  95. char *loc;
  96. setvbuf(stdout, NULL, _IONBF, 0);
  97. if (!argv[1]) {
  98. puts(
  99. "Usage: match <test> [test...]\n\n"
  100. "Where a <test> is the form: <string><op><match>\n"
  101. "This is to test the shell ${var#val} expression type.\n\n"
  102. "e.g. `match 'abc#a*'` -> bc"
  103. );
  104. return 1;
  105. }
  106. while (*++argv) {
  107. size_t off;
  108. unsigned scan_flags;
  109. string = *argv;
  110. off = strcspn(string, "#%");
  111. if (!off) {
  112. printf("invalid format\n");
  113. continue;
  114. }
  115. op = string + off;
  116. scan_flags = pick_scan(op[0], op[1]);
  117. printf("'%s': flags:%x, ", string, scan_flags);
  118. pattern = op + 1;
  119. if (op[0] == op[1])
  120. pattern++;
  121. op[0] = '\0';
  122. loc = scan_and_match(string, pattern, scan_flags);
  123. if (scan_flags & SCAN_MATCH_LEFT_HALF) {
  124. printf("'%s'\n", loc);
  125. } else {
  126. if (loc)
  127. *loc = '\0';
  128. printf("'%s'\n", string);
  129. }
  130. }
  131. return 0;
  132. }
  133. #endif