defs.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /** $MirOS: src/usr.bin/sed/defs.h,v 1.3 2017/11/20 01:23:56 tg Exp $ */
  2. /* $OpenBSD: defs.h,v 1.8 2017/01/20 10:26:16 krw Exp $ */
  3. /*-
  4. * Copyright (c) 1992 Diomidis Spinellis.
  5. * Copyright (c) 1992, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * This code is derived from software contributed to Berkeley by
  9. * Diomidis Spinellis of Imperial College, University of London.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * from: @(#)defs.h 8.1 (Berkeley) 6/6/93
  36. */
  37. /*
  38. * Types of address specifications
  39. */
  40. enum e_atype {
  41. AT_RE, /* Line that match RE */
  42. AT_LINE, /* Specific line */
  43. AT_LAST, /* Last line */
  44. };
  45. /*
  46. * Format of an address
  47. */
  48. struct s_addr {
  49. enum e_atype type; /* Address type */
  50. union {
  51. u_long l; /* Line number */
  52. regex_t *r; /* Regular expression */
  53. } u;
  54. };
  55. /*
  56. * Substitution command
  57. */
  58. struct s_subst {
  59. int n; /* Occurrence to subst. */
  60. int p; /* True if p flag */
  61. char *wfile; /* NULL if no wfile */
  62. int wfd; /* Cached file descriptor */
  63. regex_t *re; /* Regular expression */
  64. size_t maxbref; /* Largest backreference. */
  65. u_long linenum; /* Line number. */
  66. char *new; /* Replacement text */
  67. };
  68. /*
  69. * An internally compiled command.
  70. * Initialy, label references are stored in t, on a second pass they
  71. * are updated to pointers.
  72. */
  73. struct s_command {
  74. struct s_command *next; /* Pointer to next command */
  75. struct s_addr *a1, *a2; /* Start and end address */
  76. char *t; /* Text for : a c i r w */
  77. union {
  78. struct s_command *c; /* Command(s) for b t { */
  79. struct s_subst *s; /* Substitute command */
  80. u_char *y; /* Replace command array */
  81. int fd; /* File descriptor for w */
  82. } u;
  83. char code; /* Command code */
  84. u_int nonsel:1; /* True if ! */
  85. u_int inrange:1; /* True if in range */
  86. };
  87. /*
  88. * Types of command arguments recognised by the parser
  89. */
  90. enum e_args {
  91. EMPTY, /* d D g G h H l n N p P q x = \0 */
  92. TEXT, /* a c i */
  93. NONSEL, /* ! */
  94. GROUP, /* { */
  95. ENDGROUP, /* } */
  96. COMMENT, /* # */
  97. BRANCH, /* b t */
  98. LABEL, /* : */
  99. RFILE, /* r */
  100. WFILE, /* w */
  101. SUBST, /* s */
  102. TR /* y */
  103. };
  104. /*
  105. * Structure containing things to append before a line is read
  106. */
  107. struct s_appends {
  108. enum {AP_STRING, AP_FILE} type;
  109. char *s;
  110. size_t len;
  111. };
  112. enum e_spflag {
  113. APPEND, /* Append to the contents. */
  114. REPLACE, /* Replace the contents. */
  115. };
  116. /*
  117. * Structure for a space (process, hold, otherwise).
  118. */
  119. typedef struct {
  120. char *space; /* Current space pointer. */
  121. size_t len; /* Current length. */
  122. int deleted; /* If deleted. */
  123. int append_newline; /* If originally terminated by \n. */
  124. char *back; /* Backing memory. */
  125. size_t blen; /* Backing memory length. */
  126. } SPACE;
  127. /*
  128. * Error severity codes:
  129. */
  130. #define FATAL 1 /* Exit immediately with 1 */
  131. #define COMPILE 2 /* Print error, count and finish script */
  132. /*
  133. * Round up to the nearest multiple of _POSIX2_LINE_MAX
  134. */
  135. #define ROUNDLEN(x) \
  136. (((x) + _POSIX2_LINE_MAX - 1) & ~(_POSIX2_LINE_MAX - 1))