3
0

match.c 3.3 KB

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