1993-04-08 05:07:16 +04:00
|
|
|
/* ed.h: type and constant definitions for the ed editor. */
|
|
|
|
/*
|
1993-11-12 13:48:43 +03:00
|
|
|
* Copyright (c) 1993 Andrew Moore
|
1993-04-08 05:07:16 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1993-11-12 13:48:43 +03:00
|
|
|
* @(#)ed.h 5.5 (Talke Studio) 3/28/93
|
1993-04-08 05:07:16 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
1993-04-14 16:22:14 +04:00
|
|
|
#include <errno.h>
|
1993-12-01 16:48:47 +03:00
|
|
|
#if defined(BSD) && BSD >= 199103 || defined(__NetBSD__)
|
1993-04-08 05:07:16 +04:00
|
|
|
# include <sys/param.h> /* for MAXPATHLEN */
|
|
|
|
#endif
|
1993-04-28 07:37:34 +04:00
|
|
|
#include <regex.h>
|
1993-05-08 14:49:52 +04:00
|
|
|
#include <signal.h>
|
1993-11-12 13:48:43 +03:00
|
|
|
#ifdef sun
|
|
|
|
# include <limits.h>
|
|
|
|
#endif
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
#define ERR (-2)
|
1993-05-08 14:49:52 +04:00
|
|
|
#define EMOD (-3)
|
|
|
|
#define FATAL (-4)
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
# define MAXPATHLEN 255 /* _POSIX_PATH_MAX */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAXFNAME MAXPATHLEN /* max file name size */
|
1993-05-08 14:49:52 +04:00
|
|
|
#define MINBUFSZ 512 /* minimum buffer size - must be > 0 */
|
1993-04-08 05:07:16 +04:00
|
|
|
#define SE_MAX 30 /* max subexpressions in a regular expression */
|
1993-11-12 13:48:43 +03:00
|
|
|
#ifdef INT_MAX
|
|
|
|
# define LINECHARS INT_MAX /* max chars per line */
|
|
|
|
#else
|
|
|
|
# define LINECHARS MAXINT /* max chars per line */
|
|
|
|
#endif
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
typedef regex_t pattern_t;
|
|
|
|
|
|
|
|
/* Line node */
|
|
|
|
typedef struct line {
|
|
|
|
struct line *next;
|
|
|
|
struct line *prev;
|
|
|
|
off_t seek; /* address of line in scratch buffer */
|
|
|
|
int len; /* length of line */
|
|
|
|
} line_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct undo {
|
|
|
|
|
|
|
|
/* type of undo nodes */
|
|
|
|
#define UADD 0
|
|
|
|
#define UDEL 1
|
1993-04-15 08:58:32 +04:00
|
|
|
#define UMOV 2
|
|
|
|
#define VMOV 3
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
int type; /* command type */
|
|
|
|
line_t *h; /* head of list */
|
|
|
|
line_t *t; /* tail of list */
|
|
|
|
} undo_t;
|
|
|
|
|
|
|
|
#ifndef max
|
1993-05-08 14:49:52 +04:00
|
|
|
# define max(a,b) ((a) > (b) ? (a) : (b))
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif
|
|
|
|
#ifndef min
|
1993-05-08 14:49:52 +04:00
|
|
|
# define min(a,b) ((a) < (b) ? (a) : (b))
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
#define INC_MOD(l, k) ((l)+1 > (k) ? 0 : (l)+1)
|
|
|
|
#define DEC_MOD(l, k) ((l)-1 < 0 ? (k) : (l)-1)
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
#define SKIPBLANKS() while (isspace(*ibufp) && *ibufp != '\n') ibufp++
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* SPL1: disable some interrupts (requires reliable signals) */
|
|
|
|
#define SPL1() mutex++
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* SPL0: enable all interrupts; check sigflags (requires reliable signals) */
|
|
|
|
#define SPL0() \
|
1993-04-08 05:07:16 +04:00
|
|
|
if (--mutex == 0) { \
|
1993-11-23 07:41:44 +03:00
|
|
|
if (sigflags & (1 << SIGHUP)) handle_hup(SIGHUP); \
|
|
|
|
if (sigflags & (1 << SIGINT)) handle_int(SIGINT); \
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
#if defined(sun) || defined(NO_REALLOC_NULL)
|
|
|
|
/* CKBUF: assure at least a minimum size for buffer b */
|
|
|
|
#define CKBUF(b,n,i,err) \
|
|
|
|
if ((i) > (n)) { \
|
|
|
|
int ti = (n); \
|
|
|
|
char *ts; \
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL1(); \
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((b) != NULL) { \
|
|
|
|
if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno)); \
|
|
|
|
sprintf(errmsg, "out of memory"); \
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0(); \
|
1993-05-08 14:49:52 +04:00
|
|
|
return err; \
|
|
|
|
} \
|
|
|
|
} else { \
|
|
|
|
if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno)); \
|
|
|
|
sprintf(errmsg, "out of memory"); \
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0(); \
|
1993-05-08 14:49:52 +04:00
|
|
|
return err; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
(n) = ti; \
|
|
|
|
(b) = ts; \
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0(); \
|
1993-05-08 14:49:52 +04:00
|
|
|
}
|
|
|
|
#else /* NO_REALLOC_NULL */
|
|
|
|
/* CKBUF: assure at least a minimum size for buffer b */
|
|
|
|
#define CKBUF(b,n,i,err) \
|
|
|
|
if ((i) > (n)) { \
|
|
|
|
int ti = (n); \
|
|
|
|
char *ts; \
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL1(); \
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno)); \
|
|
|
|
sprintf(errmsg, "out of memory"); \
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0(); \
|
1993-05-08 14:49:52 +04:00
|
|
|
return err; \
|
|
|
|
} \
|
|
|
|
(n) = ti; \
|
|
|
|
(b) = ts; \
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0(); \
|
1993-05-08 14:49:52 +04:00
|
|
|
}
|
|
|
|
#endif /* NO_REALLOC_NULL */
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
/* requeue: link pred before succ */
|
|
|
|
#define requeue(pred, succ) (pred)->next = (succ), (succ)->prev = (pred)
|
|
|
|
|
|
|
|
/* insqueue: insert elem in circular queue after pred */
|
|
|
|
#define insqueue(elem, pred) \
|
|
|
|
{ \
|
|
|
|
requeue((elem), (pred)->next); \
|
|
|
|
requeue((pred), elem); \
|
|
|
|
}
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* remqueue: remove_lines elem from circular queue */
|
1993-04-08 05:07:16 +04:00
|
|
|
#define remqueue(elem) requeue((elem)->prev, (elem)->next);
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
|
|
|
|
#define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
|
1993-05-08 14:49:52 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
|
|
|
|
#define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
|
1993-05-08 14:49:52 +04:00
|
|
|
|
1993-04-15 08:58:32 +04:00
|
|
|
#ifndef strerror
|
|
|
|
# define strerror(n) sys_errlist[n]
|
|
|
|
#endif
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
#ifndef __P
|
|
|
|
# ifndef __STDC__
|
|
|
|
# define __P(proto) ()
|
|
|
|
# else
|
|
|
|
# define __P(proto) proto
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
1993-11-12 13:48:43 +03:00
|
|
|
/* Local Function Declarations */
|
1993-11-23 07:41:44 +03:00
|
|
|
void add_line_node __P((line_t *));
|
|
|
|
int append_lines __P((long));
|
|
|
|
int apply_subst_template __P((char *, regmatch_t *, int, int));
|
|
|
|
int build_active_list __P((int));
|
|
|
|
int cbc_decode __P((char *, FILE *));
|
|
|
|
int cbc_encode __P((char *, int, FILE *));
|
|
|
|
int check_addr_range __P((long, long));
|
|
|
|
void clear_active_list __P((void));
|
|
|
|
void clear_undo_stack __P((void));
|
|
|
|
int close_sbuf __P((void));
|
|
|
|
int copy_lines __P((long));
|
|
|
|
int delete_lines __P((long, long));
|
|
|
|
void des_error __P((char *));
|
|
|
|
int display_lines __P((long, long, int));
|
|
|
|
line_t *dup_line_node __P((line_t *));
|
|
|
|
int exec_command __P((void));
|
|
|
|
long exec_global __P((int, int));
|
|
|
|
void expand_des_key __P((char *, char *));
|
|
|
|
int extract_addr_range __P((void));
|
|
|
|
char *extract_pattern __P((int));
|
|
|
|
int extract_subst_tail __P((void));
|
|
|
|
char *extract_subst_template __P((void));
|
|
|
|
int flush_des_file __P((FILE *));
|
|
|
|
line_t *get_addressed_line_node __P((long));
|
|
|
|
int get_des_char __P((FILE *));
|
|
|
|
pattern_t *get_compiled_pattern __P((void));
|
|
|
|
char *get_extended_line __P((int *, int));
|
|
|
|
int get_file_line __P((FILE *));
|
|
|
|
char *get_filename __P((void));
|
|
|
|
int get_input_line __P((void));
|
|
|
|
int get_keyword __P((void));
|
|
|
|
long get_line_node_addr __P((line_t *));
|
|
|
|
long get_matching_node_addr __P((pattern_t *, int));
|
|
|
|
long get_marked_node_addr __P((int));
|
|
|
|
char *get_sbuf_line __P((line_t *));
|
|
|
|
int get_shell_command __P((void));
|
|
|
|
void handle_hup __P((int));
|
|
|
|
void handle_int __P((int));
|
|
|
|
void handle_winch __P((int));
|
|
|
|
int has_trailing_escape __P((char *, char *));
|
|
|
|
int hex_to_binary __P((int, int));
|
|
|
|
void init_buffers __P((void));
|
|
|
|
void init_des_cipher __P((void));
|
|
|
|
int is_legal_filename __P((char *));
|
|
|
|
int join_lines __P((long, long));
|
|
|
|
int mark_line_node __P((line_t *, int));
|
|
|
|
int move_lines __P((long));
|
|
|
|
line_t *next_active_node __P(());
|
|
|
|
long next_addr __P((void));
|
|
|
|
int open_sbuf __P((void));
|
|
|
|
void output_line __P((char *, int, long, int));
|
|
|
|
char *parse_char_class __P((char *));
|
|
|
|
int pop_undo_stack __P((void));
|
|
|
|
undo_t *push_undo_stack __P((int, long, long));
|
|
|
|
int put_des_char __P((int, FILE *));
|
|
|
|
char *put_sbuf_line __P((char *));
|
1993-04-08 05:07:16 +04:00
|
|
|
void quit __P((int));
|
1993-11-23 07:41:44 +03:00
|
|
|
long read_file __P((long, char *));
|
|
|
|
int search_and_replace __P((pattern_t *, int));
|
|
|
|
int set_active_node __P((line_t *));
|
|
|
|
void set_des_key __P((char *));
|
|
|
|
void signal_hup __P((int));
|
|
|
|
void signal_int __P((int));
|
|
|
|
char *strip_escapes __P((char *));
|
|
|
|
int substitute_matching_text __P((pattern_t *, line_t *, int));
|
|
|
|
char *translit_text __P((char *, int, int, int));
|
|
|
|
void unmark_line_node __P((line_t *));
|
|
|
|
void unset_active_nodes __P((line_t *, line_t *));
|
|
|
|
long write_file __P((long, long, char *, char *));
|
1993-04-15 08:58:32 +04:00
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
extern int mutex;
|
|
|
|
extern int sigflags;
|