Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.1 KiB

  1. /* $Header: /nw/tony/src/stevie/src/RCS/regsub.c,v 1.4 89/03/11 22:43:30 tony Exp $
  2. *
  3. * NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
  4. *
  5. * This is NOT the original regular expression code as written by
  6. * Henry Spencer. This code has been modified specifically for use
  7. * with the STEVIE editor, and should not be used apart from compiling
  8. * STEVIE. If you want a good regular expression library, get the
  9. * original code. The copyright notice that follows is from the
  10. * original.
  11. *
  12. * NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
  13. *
  14. * regsub
  15. *
  16. * Copyright (c) 1986 by University of Toronto.
  17. * Written by Henry Spencer. Not derived from licensed software.
  18. *
  19. * Permission is granted to anyone to use this software for any
  20. * purpose on any computer system, and to redistribute it freely,
  21. * subject to the following restrictions:
  22. *
  23. * 1. The author is not responsible for the consequences of use of
  24. * this software, no matter how awful, even if they arise
  25. * from defects in it.
  26. *
  27. * 2. The origin of this software must not be misrepresented, either
  28. * by explicit claim or by omission.
  29. *
  30. * 3. Altered versions must be plainly marked as such, and must not
  31. * be misrepresented as being the original software.
  32. *
  33. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include "regexp.h"
  37. #include "regmagic.h"
  38. #ifndef CHARBITS
  39. #define UCHARAT(p) ((int)*(unsigned char *)(p))
  40. #else
  41. #define UCHARAT(p) ((int)*(p)&CHARBITS)
  42. #endif
  43. /*
  44. - regsub - perform substitutions after a regexp match
  45. */
  46. void
  47. regsub(prog, source, dest)
  48. regexp *prog;
  49. char *source;
  50. char *dest;
  51. {
  52. register char *src;
  53. register char *dst;
  54. register char c;
  55. register int no;
  56. register size_t len;
  57. if (prog == NULL || source == NULL || dest == NULL) {
  58. regerror("NULL parm to regsub");
  59. return;
  60. }
  61. if (UCHARAT(prog->program) != MAGIC) {
  62. regerror("damaged regexp fed to regsub");
  63. return;
  64. }
  65. src = source;
  66. dst = dest;
  67. while ((c = *src++) != '\0') {
  68. if (c == '&')
  69. no = 0;
  70. else if (c == '\\' && '0' <= *src && *src <= '9')
  71. no = *src++ - '0';
  72. else
  73. no = -1;
  74. if (no < 0) { /* Ordinary character. */
  75. if (c == '\\' && (*src == '\\' || *src == '&'))
  76. c = *src++;
  77. *dst++ = c;
  78. } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  79. len = (size_t)(prog->endp[no] - prog->startp[no]);
  80. strncpy(dst, prog->startp[no], len);
  81. dst += len;
  82. if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
  83. regerror("damaged match string");
  84. return;
  85. }
  86. }
  87. }
  88. *dst++ = '\0';
  89. }