[comp.os.minix] ed part 2 of 2

ast@cs.vu.nl (Andy Tanenbaum) (09/14/87)

: This is a shar archive.  Extract with sh, not csh.
: This archive ends with exit, so do not worry about trailing junk.
: --------------------------- cut here --------------------------
PATH=/bin:/usr/bin
echo Extracting \o\m\a\t\c\h\.\c
sed 's/^X//' > \o\m\a\t\c\h\.\c << '+ END-OF-FILE '\o\m\a\t\c\h\.\c
X#include <stdio.h>
X#include "tools.h"
X
X/*
X * Match one pattern element, pointed at by pat, with the character at
X * **linp.  Return non-zero on match.  Otherwise, return 0.  *Linp is
X * advanced to skip over the matched character; it is not advanced on
X * failure.  The amount of advance is 0 for patterns that match null
X * strings, 1 otherwise.  "boln" should point at the position that will
X * match a BOL token.
X */
Xomatch(linp, pat, boln)
Xchar	**linp;
XTOKEN	*pat;
Xchar	*boln;
X{
X	
X	register int	advance;
X
X	advance = -1;
X
X	if (**linp)
X	{
X		switch (pat->tok)
X		{
X		case LITCHAR:
X			if (**linp == pat->lchar)
X				advance = 1;
X			break;
X
X		case BOL:
X			if (*linp = boln)
X				advance = 0;
X			break;
X
X		case ANY:
X			if (**linp != '\n')
X				advance = 1;
X			break;
X
X		case EOL:
X			if (**linp == '\n')
X				advance = 0;
X			break;
X
X		case CCL:
X			if( testbit( **linp, pat->bitmap))
X				advance = 1;
X			break;
X
X		case NCCL:
X			if (!testbit (**linp, pat->bitmap))
X				advance = 1;
X			break;
X
X		default:
X			printf("omatch: can't happen\n");
X		}
X	}
X	if (advance >= 0)
X		*linp += advance;
X
X	return (++advance);
X}
+ END-OF-FILE omatch.c
chmod 'u=rw,g=r,o=r' \o\m\a\t\c\h\.\c
set `sum \o\m\a\t\c\h\.\c`
sum=$1
case $sum in
11662)	:;;
*)	echo 'Bad sum in '\o\m\a\t\c\h\.\c >&2
esac
echo Extracting \o\p\t\p\a\t\.\c
sed 's/^X//' > \o\p\t\p\a\t\.\c << '+ END-OF-FILE '\o\p\t\p\a\t\.\c
X/*
X * Copyright 1987 Brian Beattie Rights Reserved.
X *
X * Permission to copy and/or distribute granted under the
X * following conditions:
X *
X * 1). No charge may be made other than resonable charges
X *	for reproduction.
X *
X * 2). This notice must remain intact.
X *
X * 3). No further restrictions may be added.
X *
X */
X#include <stdio.h>
X#include "tools.h"
X#include "ed.h"
X
XTOKEN	*
Xoptpat(oldpat)
XTOKEN	*oldpat;
X{
X	char	delim, str[MAXPAT], *cp;
X
X	delim = *inptr++;
X	cp = str;
X	while(*inptr != delim && *inptr != NL)
X	{
X		if(*inptr == ESCAPE)
X			if(inptr[1] == '/' || inptr[1] == '?')
X				inptr++;
X		*cp++ = *inptr++;
X	}
X
X	*cp = EOS;
X	if(*str == EOS)
X		return(oldpat);
X	if(oldpat)
X		unmakepat(oldpat);
X	return(getpat(str));
X}
+ END-OF-FILE optpat.c
chmod 'u=rw,g=r,o=r' \o\p\t\p\a\t\.\c
set `sum \o\p\t\p\a\t\.\c`
sum=$1
case $sum in
05262)	:;;
*)	echo 'Bad sum in '\o\p\t\p\a\t\.\c >&2
esac
echo Extracting \s\e\t\.\c
sed 's/^X//' > \s\e\t\.\c << '+ END-OF-FILE '\s\e\t\.\c
X/*
X * Copyright 1987 Brian Beattie Rights Reserved.
X *
X * Permission to copy and/or distribute granted under the
X * following conditions:
X *
X * 1). No charge may be made other than resonable charges
X *	for reproduction.
X *
X * 2). This notice must remain intact.
X *
X * 3). No further restrictions may be added.
X *
X */
X#include <stdio.h>
X#include "tools.h"
X#include "ed.h"
X
Xstruct tbl {
X	char	*t_str;
X	int	*t_ptr;
X	int	t_val;
X} *t, tbl[] = {
X	"number",	&nflg,		TRUE,
X	"nonumber",	&nflg,		FALSE,
X	"list",		&lflg,		TRUE,
X	"nolist",	&lflg,		FALSE,
X	"eightbit",	&eightbit,	TRUE,
X	"noeightbit",	&eightbit,	FALSE,
X	0
X};
X
Xset()
X{
X	char	word[16];
X	int	i;
X
X	inptr++;
X	if(toupper(*inptr) != 'T')
X	{
X		if(*inptr != SP && *inptr != HT && *inptr != NL)
X			return(ERR);
X	} else
X		inptr++;
X
X	if(*inptr == NL)
X		return(show("all"));
X		/* skip white space */
X	while(*inptr == SP || *inptr == HT)
X		inptr++;
X
X	for(i = 0; *inptr != SP && *inptr != HT && *inptr != NL;)
X		word[i++] = *inptr++;
X	word[i] = EOS;
X	for(t = tbl; t->t_str; t++)
X	{
X		if(strcmp(word,t->t_str) == 0)
X		{
X			*t->t_ptr = t->t_val;
X			return(0);
X		}
X	}
X}
X
Xshow()
X{
X	extern int	version;
X
X	printf("ed version %d.%d\n",version/100,version%100);
X	printf("number %s, list %s\n",nflg?"ON":"OFF",lflg?"ON":"OFF");
X	return(0);
X}
+ END-OF-FILE set.c
chmod 'u=rw,g=r,o=r' \s\e\t\.\c
set `sum \s\e\t\.\c`
sum=$1
case $sum in
09185)	:;;
*)	echo 'Bad sum in '\s\e\t\.\c >&2
esac
echo Extracting \s\e\t\b\u\f\.\c
sed 's/^X//' > \s\e\t\b\u\f\.\c << '+ END-OF-FILE '\s\e\t\b\u\f\.\c
X/*
X * Copyright 1987 Brian Beattie Rights Reserved.
X *
X * Permission to copy and/or distribute granted under the
X * following conditions:
X *
X * 1). No charge may be made other than resonable charges
X *	for reproduction.
X *
X * 2). This notice must remain intact.
X *
X * 3). No further restrictions may be added.
X *
X */
X#include <stdio.h>
X#include "tools.h"
X#include "ed.h"
X
Xrelink(a, x, y, b)
XLINE	*a, *x, *y, *b;
X{
X	x->l_prev = a;
X	y->l_next = b;
X}
X
Xclrbuf()
X{
X	del(1, lastln);
X}
X
Xsetbuf()
X{
X	relink(&line0, &line0, &line0, &line0);
X	curln = lastln = 0;
X}
+ END-OF-FILE setbuf.c
chmod 'u=rw,g=r,o=r' \s\e\t\b\u\f\.\c
set `sum \s\e\t\b\u\f\.\c`
sum=$1
case $sum in
35684)	:;;
*)	echo 'Bad sum in '\s\e\t\b\u\f\.\c >&2
esac
echo Extracting \s\u\b\s\t\.\c
sed 's/^X//' > \s\u\b\s\t\.\c << '+ END-OF-FILE '\s\u\b\s\t\.\c
X/*
X * Copyright 1987 Brian Beattie Rights Reserved.
X *
X * Permission to copy and/or distribute granted under the
X * following conditions:
X *
X * 1). No charge may be made other than resonable charges
X *	for reproduction.
X *
X * 2). This notice must remain intact.
X *
X * 3). No further restrictions may be added.
X *
X */
X#include <stdio.h>
X#include "tools.h"
X#include "ed.h"
Xsubst(pat, sub, gflg, pflag)
XTOKEN	*pat;
Xchar	*sub;
Xint	gflg, pflag;
X{
X	int	lin, chngd, nchngd;
X	char	*txtptr, *txt;
X	char	*lastm, *m, *new, buf[MAXLINE];
X
X	if(line1 <= 0)
X		return( ERR );
X	nchngd = 0;		/* reset count of lines changed */
X	for(lin = line1; lin <= line2; lin++)
X	{
X		txt = txtptr = gettxt(lin);
X		new = buf;
X		chngd = 0;
X		lastm = NULL;
X		while(*txtptr)
X		{
X			if(gflg || !chngd)
X				m = amatch(txtptr, pat, txt);
X			else
X				m = NULL;
X			if(m != NULL && lastm != m)
X			{
X				chngd++;
X				new = catsub(txtptr, m, sub, new,
X						buf+MAXLINE);
X				lastm = m;
X			}
X			if(m == NULL || m == txtptr)
X			{
X				*new++ = *txtptr++;
X			} else {
X				txtptr = m;
X			}
X		}
X		if(chngd)
X		{
X			if(new >= buf+MAXLINE)
X				return( ERR );
X			*new++ = EOS;
X			del(lin,lin);
X			ins(buf);
X			nchngd++;
X			if(pflag)
X				doprnt(curln, curln);
X		}
X	}
X	if(nchngd == 0 && !gflg)
X	{
X		printf("string not found\n");
X		return(ERR);
X	}
X	return( nchngd );
X}
+ END-OF-FILE subst.c
chmod 'u=rw,g=r,o=r' \s\u\b\s\t\.\c
set `sum \s\u\b\s\t\.\c`
sum=$1
case $sum in
38978)	:;;
*)	echo 'Bad sum in '\s\u\b\s\t\.\c >&2
esac
echo Extracting \s\y\s\t\e\m\.\c
sed 's/^X//' > \s\y\s\t\e\m\.\c << '+ END-OF-FILE '\s\y\s\t\e\m\.\c
X#define SHELL	"/bin/sh"
X
Xsystem(c)
Xchar *c; {
X	int pid, status;
X	
X	switch (pid = fork()) {
X	case -1:
X		return -1;
X	case 0:
X		execl(SHELL, "sh", "-c", c, (char *) 0);
X		exit(-1);
X	default:
X		while (wait(&status) != pid)
X			;
X	}
X	return status;
X}
+ END-OF-FILE system.c
chmod 'u=rw,g=r,o=r' \s\y\s\t\e\m\.\c
set `sum \s\y\s\t\e\m\.\c`
sum=$1
case $sum in
07747)	:;;
*)	echo 'Bad sum in '\s\y\s\t\e\m\.\c >&2
esac
echo Extracting \t\o\o\l\s\.\h
sed 's/^X//' > \t\o\o\l\s\.\h << '+ END-OF-FILE '\t\o\o\l\s\.\h
Xstatic char	tools_h[] =
X"$Header: tools.h,v 2.1 85/11/14 11:30:00 beattie Exp $";
X/*
X *	#defines for non-printing ASCII characters
X */
X
X#define NUL	0x00	/* ^@ */
X#define EOS	0x00	/* end of string */
X#define SOH	0x01	/* ^A */
X#define STX	0x02	/* ^B */
X#define ETX	0x03	/* ^C */
X#define EOT	0x04	/* ^D */
X#define ENQ	0x05	/* ^E */
X#define ACK	0x06	/* ^F */
X#define BEL	0x07	/* ^G */
X#define BS	0x08	/* ^H */
X#define HT	0x09	/* ^I */
X#define LF	0x0a	/* ^J */
X#define NL	'\n'
X#define VT	0x0b	/* ^K */
X#define FF	0x0c	/* ^L */
X#define CR	0x0d	/* ^M */
X#define SO	0x0e	/* ^N */
X#define SI	0x0f	/* ^O */
X#define DLE	0x10	/* ^P */
X#define DC1	0x11	/* ^Q */
X#define DC2	0x12	/* ^R */
X#define DC3	0x13	/* ^S */
X#define DC4	0x14	/* ^T */
X#define NAK	0x15	/* ^U */
X#define SYN	0x16	/* ^V */
X#define ETB	0x17	/* ^W */
X#define CAN	0x18	/* ^X */
X#define EM	0x19	/* ^Y */
X#define SUB	0x1a	/* ^Z */
X#define ESC	0x1b	/* ^[ */
X#define FS	0x1c	/* ^\ */
X#define GS	0x1d	/* ^] */
X#define RS	0x1e	/* ^^ */
X#define US	0x1f	/* ^_ */
X#define SP	0x20	/* space */
X#define DEL	0x7f	/* DEL*/
X
X
X#define TRUE	1
X#define FALSE	0
X#define ERR	-2
X
X
X/*	Definitions of meta-characters used in pattern matching
X *	routines.  LITCHAR & NCCL are only used as token identifiers;
X *	all the others are also both token identifier and actual symbol
X *	used in the regular expression.
X */
X
X
X#define BOL	'^'
X#define EOL	'$'
X#define ANY	'.'
X#define LITCHAR	'L'
X#define	ESCAPE	'\\'
X#define CCL	'['	/* Character class: [...] */
X#define CCLEND	']'
X#define NEGATE	'~'
X#define NCCL	'!'	/* Negative character class [^...] */
X#define CLOSURE	'*'
X#define OR_SYM	'|'
X#define DITTO	'&'
X
X/* Largest permitted size for an expanded character class.  (i.e. the class
X * [a-z] will expand into 26 symbols; [a-z0-9] will expand into 36.)
X */
X#define CLS_SIZE	128
X
X/*
X *	Tokens are used to hold pattern templates. (see makepat())
X */
Xtypedef	char	BITMAP;
X
Xtypedef struct token {
X	char		tok;
X	char		lchar;
X	BITMAP		*bitmap;
X	struct token	*next;
X} TOKEN;
X
X#define TOKSIZE sizeof (TOKEN)
X
X/*
X *	An absolute maximun for strings.
X */
X
X#define MAXSTR	132	/* Maximum numbers of characters in a line */
X
X
Xextern	char	*matchs();
Xextern	char	*amatch();
Xextern	char	*in_string();
Xextern	TOKEN	*getpat();
Xextern	int	esc();
Xextern	char	*dodash();
Xextern	TOKEN	*makepat();
Xextern	int	unmakepat();
Xextern	int	insert();
Xextern	int	delete();
Xextern	int	isalphanum();
Xextern	char	*stoupper();
Xextern	int	pr_tok();
Xextern	int	pr_line();
Xextern	BITMAP	*makebitmap();
X
X/* macros */
X#define max(a,b)	((a>b)?a:b)
X#define min(a,b)	((a<b)?a:b)
X#define toupper(c)	(c>='a'&&c<='z'?c-32:c)
+ END-OF-FILE tools.h
chmod 'u=rw,g=r,o=r' \t\o\o\l\s\.\h
set `sum \t\o\o\l\s\.\h`
sum=$1
case $sum in
24721)	:;;
*)	echo 'Bad sum in '\t\o\o\l\s\.\h >&2
esac
echo Extracting \u\n\m\k\p\a\t\.\c
sed 's/^X//' > \u\n\m\k\p\a\t\.\c << '+ END-OF-FILE '\u\n\m\k\p\a\t\.\c
X#include <stdio.h>
X#include "tools.h"
X
X/* Free up the memory usde for token string */
Xunmakepat(head)
XTOKEN	*head;
X{
X
X	register TOKEN	*old_head;
X
X	while (head)
X	{
X		switch (head->tok)
X		{
X		case CCL:
X		case NCCL:
X			free(head->bitmap);
X				/* fall through to default */
X
X		default:
X			old_head = head;
X			head = head->next;
X			free (old_head);
X			break;
X		}
X	}
X}
+ END-OF-FILE unmkpat.c
chmod 'u=rw,g=r,o=r' \u\n\m\k\p\a\t\.\c
set `sum \u\n\m\k\p\a\t\.\c`
sum=$1
case $sum in
43211)	:;;
*)	echo 'Bad sum in '\u\n\m\k\p\a\t\.\c >&2
esac
:4lecpa