PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/ed/ed.h

https://bitbucket.org/freebsd/freebsd-base
C Header | 273 lines | 201 code | 26 blank | 46 comment | 22 complexity | a10048cb4af1b40ca31abdc28ad0eec9 MD5 | raw file
  1. /* ed.h: type and constant definitions for the ed editor. */
  2. /*-
  3. * Copyright (c) 1993 Andrew Moore
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. * @(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp
  28. * $FreeBSD$
  29. */
  30. #include <sys/param.h>
  31. #include <errno.h>
  32. #include <limits.h>
  33. #include <regex.h>
  34. #include <signal.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #define ERR (-2)
  40. #define EMOD (-3)
  41. #define FATAL (-4)
  42. #define MINBUFSZ 512 /* minimum buffer size - must be > 0 */
  43. #define SE_MAX 30 /* max subexpressions in a regular expression */
  44. #ifdef INT_MAX
  45. # define LINECHARS INT_MAX /* max chars per line */
  46. #else
  47. # define LINECHARS MAXINT /* max chars per line */
  48. #endif
  49. /* gflags */
  50. #define GLB 001 /* global command */
  51. #define GPR 002 /* print after command */
  52. #define GLS 004 /* list after command */
  53. #define GNP 010 /* enumerate after command */
  54. #define GSG 020 /* global substitute */
  55. typedef regex_t pattern_t;
  56. /* Line node */
  57. typedef struct line {
  58. struct line *q_forw;
  59. struct line *q_back;
  60. off_t seek; /* address of line in scratch buffer */
  61. int len; /* length of line */
  62. } line_t;
  63. typedef struct undo {
  64. /* type of undo nodes */
  65. #define UADD 0
  66. #define UDEL 1
  67. #define UMOV 2
  68. #define VMOV 3
  69. int type; /* command type */
  70. line_t *h; /* head of list */
  71. line_t *t; /* tail of list */
  72. } undo_t;
  73. #ifndef max
  74. # define max(a,b) ((a) > (b) ? (a) : (b))
  75. #endif
  76. #ifndef min
  77. # define min(a,b) ((a) < (b) ? (a) : (b))
  78. #endif
  79. #define INC_MOD(l, k) ((l) + 1 > (k) ? 0 : (l) + 1)
  80. #define DEC_MOD(l, k) ((l) - 1 < 0 ? (k) : (l) - 1)
  81. /* SPL1: disable some interrupts (requires reliable signals) */
  82. #define SPL1() mutex++
  83. /* SPL0: enable all interrupts; check sigflags (requires reliable signals) */
  84. #define SPL0() \
  85. if (--mutex == 0) { \
  86. if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \
  87. if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \
  88. }
  89. /* STRTOL: convert a string to long */
  90. #define STRTOL(i, p) { \
  91. if (((i = strtol(p, &p, 10)) == LONG_MIN || i == LONG_MAX) && \
  92. errno == ERANGE) { \
  93. errmsg = "number out of range"; \
  94. i = 0; \
  95. return ERR; \
  96. } \
  97. }
  98. #if defined(sun) || defined(NO_REALLOC_NULL)
  99. /* REALLOC: assure at least a minimum size for buffer b */
  100. #define REALLOC(b,n,i,err) \
  101. if ((i) > (n)) { \
  102. size_t ti = (n); \
  103. char *ts; \
  104. SPL1(); \
  105. if ((b) != NULL) { \
  106. if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
  107. fprintf(stderr, "%s\n", strerror(errno)); \
  108. errmsg = "out of memory"; \
  109. SPL0(); \
  110. return err; \
  111. } \
  112. } else { \
  113. if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
  114. fprintf(stderr, "%s\n", strerror(errno)); \
  115. errmsg = "out of memory"; \
  116. SPL0(); \
  117. return err; \
  118. } \
  119. } \
  120. (n) = ti; \
  121. (b) = ts; \
  122. SPL0(); \
  123. }
  124. #else /* NO_REALLOC_NULL */
  125. /* REALLOC: assure at least a minimum size for buffer b */
  126. #define REALLOC(b,n,i,err) \
  127. if ((i) > (n)) { \
  128. size_t ti = (n); \
  129. char *ts; \
  130. SPL1(); \
  131. if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
  132. fprintf(stderr, "%s\n", strerror(errno)); \
  133. errmsg = "out of memory"; \
  134. SPL0(); \
  135. return err; \
  136. } \
  137. (n) = ti; \
  138. (b) = ts; \
  139. SPL0(); \
  140. }
  141. #endif /* NO_REALLOC_NULL */
  142. /* REQUE: link pred before succ */
  143. #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred)
  144. /* INSQUE: insert elem in circular queue after pred */
  145. #define INSQUE(elem, pred) \
  146. { \
  147. REQUE((elem), (pred)->q_forw); \
  148. REQUE((pred), elem); \
  149. }
  150. /* REMQUE: remove_lines elem from circular queue */
  151. #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw);
  152. /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
  153. #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
  154. /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
  155. #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
  156. /* Local Function Declarations */
  157. void add_line_node(line_t *);
  158. int append_lines(long);
  159. int apply_subst_template(const char *, regmatch_t *, int, int);
  160. int build_active_list(int);
  161. int cbc_decode(unsigned char *, FILE *);
  162. int cbc_encode(unsigned char *, int, FILE *);
  163. int check_addr_range(long, long);
  164. void clear_active_list(void);
  165. void clear_undo_stack(void);
  166. int close_sbuf(void);
  167. int copy_lines(long);
  168. int delete_lines(long, long);
  169. int display_lines(long, long, int);
  170. line_t *dup_line_node(line_t *);
  171. int exec_command(void);
  172. long exec_global(int, int);
  173. int extract_addr_range(void);
  174. char *extract_pattern(int);
  175. int extract_subst_tail(int *, long *);
  176. char *extract_subst_template(void);
  177. int filter_lines(long, long, char *);
  178. line_t *get_addressed_line_node(long);
  179. pattern_t *get_compiled_pattern(void);
  180. char *get_extended_line(int *, int);
  181. char *get_filename(void);
  182. int get_keyword(void);
  183. long get_line_node_addr(line_t *);
  184. long get_matching_node_addr(pattern_t *, int);
  185. long get_marked_node_addr(int);
  186. char *get_sbuf_line(line_t *);
  187. int get_shell_command(void);
  188. int get_stream_line(FILE *);
  189. int get_tty_line(void);
  190. void handle_hup(int);
  191. void handle_int(int);
  192. void handle_winch(int);
  193. int has_trailing_escape(char *, char *);
  194. int hex_to_binary(int, int);
  195. void init_buffers(void);
  196. int is_legal_filename(char *);
  197. int join_lines(long, long);
  198. int mark_line_node(line_t *, int);
  199. int move_lines(long);
  200. line_t *next_active_node(void);
  201. long next_addr(void);
  202. int open_sbuf(void);
  203. char *parse_char_class(char *);
  204. int pop_undo_stack(void);
  205. undo_t *push_undo_stack(int, long, long);
  206. const char *put_sbuf_line(const char *);
  207. int put_stream_line(FILE *, const char *, int);
  208. int put_tty_line(const char *, int, long, int);
  209. void quit(int);
  210. long read_file(char *, long);
  211. long read_stream(FILE *, long);
  212. int search_and_replace(pattern_t *, int, int);
  213. int set_active_node(line_t *);
  214. void signal_hup(int);
  215. void signal_int(int);
  216. char *strip_escapes(char *);
  217. int substitute_matching_text(pattern_t *, line_t *, int, int);
  218. char *translit_text(char *, int, int, int);
  219. void unmark_line_node(line_t *);
  220. void unset_active_nodes(line_t *, line_t *);
  221. long write_file(char *, const char *, long, long);
  222. long write_stream(FILE *, long, long);
  223. /* global buffers */
  224. extern char stdinbuf[];
  225. extern char *ibuf;
  226. extern char *ibufp;
  227. extern int ibufsz;
  228. /* global flags */
  229. extern int isbinary;
  230. extern int isglobal;
  231. extern int modified;
  232. extern int mutex;
  233. extern int sigflags;
  234. /* global vars */
  235. extern long addr_last;
  236. extern long current_addr;
  237. extern const char *errmsg;
  238. extern long first_addr;
  239. extern int lineno;
  240. extern long second_addr;
  241. extern long u_addr_last;
  242. extern long u_current_addr;
  243. extern long rows;
  244. extern int cols;
  245. extern int newline_added;
  246. extern int scripted;
  247. extern int patlock;