⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ed.c

📁 操作系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
	if (nlines == 0) line2 = lastln;	if (*inptr != ' ' && *inptr != HT && *inptr != NL) return(ERR);	if ((fptr = getfn()) == NULL) return(ERR);	if ((err = doread(line2, fptr)) < 0) return(err);	fchanged = TRUE;	break;      case 's':	if (*inptr == 'e') return(set());	while (*inptr == SP || *inptr == HT) inptr++;	if ((subpat = optpat()) == NULL) return (ERR);	if ((gflag = getrhs(rhs)) < 0) return (ERR);	if (*inptr == 'p') pflag++;	if (deflt(curln, curln) < 0) return (ERR);	if ((nchng = subst(subpat, rhs, gflag, pflag)) < 0) return (ERR);	if (nchng) fchanged = TRUE;	break;      case 't':	if ((line3 = getone()) < 0) return(ERR);	if (deflt(curln, curln) < 0) return (ERR);	if (transfer(line3) < 0) return (ERR);	fchanged = TRUE;	break;      case 'W':      case 'w':	apflg = (c == 'W');	if (*inptr != ' ' && *inptr != HT && *inptr != NL) return(ERR);	if ((fptr = getfn()) == NULL) return(ERR);	if (deflt(1, lastln) < 0) return(ERR);	if (dowrite(line1, line2, fptr, apflg) < 0) return (ERR);	fchanged = FALSE;	break;      case 'x':	if (*inptr == NL && nlines == 0 && !glob) {		if ((fptr = getfn()) == NULL) return(ERR);		if (dowrite(1, lastln, fptr, 0) >= 0) return (EOF);	}	return(ERR);      case 'z':	if (deflt(curln, curln) < 0) return(ERR);	switch (*inptr) {	    case '-':		if (doprnt(line1 - 21, line1) < 0) return(ERR);		break;	    case '.':		if (doprnt(line1 - 11, line1 + 10) < 0) return(ERR);		break;	    case '+':	    case '\n':		if (doprnt(line1, line1 + 21) < 0) return(ERR);		break;	}	break;      default:	return(ERR);}  return(0);}int dolst(line1, line2)int line1, line2;{  int oldlflg = lflg, p;  lflg = 1;  p = doprnt(line1, line2);  lflg = oldlflg;  return p;}/*	dodash.c	*//* #include <stdio.h> *//* #include "tools.h" *//*	Expand the set pointed to by *src into dest. *	Stop at delim.  Return 0 on error or size of *	character class on success.  Update *src to *	point at delim.  A set can have one element *	{x} or several elements ( {abcdefghijklmnopqrstuvwxyz} *	and {a-z} are equivalent ).  Note that the dash *	notation is expanded as sequential numbers. *	This means (since we are using the ASCII character *	set) that a-Z will contain the entire alphabet *	plus the symbols: [\]^_`.  The maximum number of *	characters in a character class is defined by maxccl. */char *dodash(delim, src, map)int delim;char *src, *map;{  register int first, last;  char *start;  start = src;  while (*src && *src != delim) {	if (*src != '-') setbit(esc(&src), map, 1);	else if (src == start || *(src + 1) == delim)		setbit('-', map, 1);	else {		src++;		if (*src < *(src - 2)) {			first = *src;			last = *(src - 2);		} else {			first = *(src - 2);			last = *src;		}		while (++first <= last) setbit(first, map, 1);	}	src++;  }  return(src);}/*	doglob.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */int doglob(){  int lin, stat;  char *cmd;  LINE *ptr;  cmd = inptr;  while (1) {	ptr = getptr(1);	for (lin = 1; lin <= lastln; lin++) {		if (ptr->l_stat & LGLOB) break;		ptr = ptr->l_next;	}	if (lin > lastln) break;	ptr->l_stat &= ~LGLOB;	curln = lin;	inptr = cmd;	if ((stat = getlst()) < 0) return(stat);	if ((stat = docmd(1)) < 0) return (stat);  }  return(curln);}/*	doprnt.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */int doprnt(from, to)int from, to;{  int i;  LINE *lptr;  from = from < 1 ? 1 : from;  to = to > lastln ? lastln : to;  if (to != 0) {	lptr = getptr(from);	for (i = from; i <= to; i++) {		prntln(lptr->l_buff, lflg, (nflg ? i : 0));		lptr = lptr->l_next;	}	curln = to;  }  return(0);}void prntln(str, vflg, lin)char *str;int vflg, lin;{  if (lin) printf("%7d ", lin);  while (*str && *str != NL) {	if (*str < ' ' || *str >= 0x7f) {		switch (*str) {		    case '\t':			if (vflg)				putcntl(*str, stdout);			else				putc(*str, stdout);			break;		    case DEL:			putc('^', stdout);			putc('?', stdout);			break;		    default:			putcntl(*str, stdout);			break;		}	} else		putc(*str, stdout);	str++;  }  if (vflg) putc('$', stdout);  putc('\n', stdout);}void putcntl(c, stream)char c;FILE *stream;{  putc('^', stream);  putc((c & 31) | '@', stream);}/*	doread.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */extern int diag;int doread(lin, fname)int lin;char *fname;{  FILE *fp;  int err;  long bytes;  int lines;  static char str[MAXLINE];  err = 0;  nonascii = nullchar = truncated = 0;  if (diag) printf("\"%s\" ", fname);  if ((fp = fopen(fname, "r")) == NULL) {	printf("file open err\n");	return(ERR);  }  curln = lin;  for (lines = 0, bytes = 0; (err = egets(str, MAXLINE, fp)) > 0;) {	bytes += strlen(str);	if (ins(str) < 0) {		printf("file insert error\n");		err++;		break;	}	lines++;  }  fclose(fp);  if (err < 0) return(err);  if (diag) {	printf("%d lines %ld bytes", lines, bytes);	if (nonascii) printf(" [%d non-ascii]", nonascii);	if (nullchar) printf(" [%d nul]", nullchar);	if (truncated) printf(" [%d lines truncated]", truncated);	printf("\n");  }  return(err);}/*	dowrite.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */int dowrite(from, to, fname, apflg)int from, to;char *fname;int apflg;{  FILE *fp;  int lin, err;  int lines;  long bytes;  char *str;  LINE *lptr;  err = 0;  lines = bytes = 0;  if (diag) printf("\"%s\" ", fname);  if ((fp = fopen(fname, (apflg ? "a" : "w"))) == NULL) {	printf("file open error\n");	return(ERR);  }  lptr = getptr(from);  for (lin = from; lin <= to; lin++) {	str = lptr->l_buff;	lines++;	bytes += strlen(str) + 1;	if (fputs(str, fp) == EOF) {		printf("file write error\n");		err++;		break;	}	fputc('\n', fp);	lptr = lptr->l_next;  }  if (diag) printf("%d lines %ld bytes\n", lines, bytes);  fclose(fp);  return(err);}/*	ed.c	*//* Copyright 1987 Brian Beattie Rights Reserved. * * Permission to copy and/or distribute granted under the * following conditions: * * 1). No charge may be made other than resonable charges *	for reproduction. * * 2). This notice must remain intact. * * 3). No further restrictions may be added. * *//* #include <stdio.h> *//* #include <signal.h> *//* #include "tools.h" *//* #include "ed.h" */#include <setjmp.h>jmp_buf env;LINE line0;int curln = 0;int lastln = 0;char *inptr;static char inlin[MAXLINE];int nflg, lflg;int line1, line2, nlines;extern char fname[];int version = 1;int diag = 1;void intr(sig)int sig;{  printf("?\n");  longjmp(env, 1);}int main(argc, argv)int argc;char **argv;{  int stat, i, doflush;  set_buf();  doflush = isatty(1);  if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 0) {	diag = 0;	argc--;	argv++;  }  if (argc > 1) {	for (i = 1; i < argc; i++) {		if (doread(0, argv[i]) == 0) {			curln = 1;			strcpy(fname, argv[i]);			break;		}	}  }  while (1) {	setjmp(env);	if (signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, intr);	if (doflush) fflush(stdout);	if (fgets(inlin, sizeof(inlin), stdin) == NULL) {		break;	}	if (*inlin == '!') {		for (inptr = inlin; *inptr != NL; inptr++);		*inptr = EOS;		System(inlin + 1);		continue;	}	inptr = inlin;	if (getlst() >= 0)		if ((stat = ckglob()) != 0) {			if (stat >= 0 && (stat = doglob()) >= 0) {				curln = stat;				continue;			}		} else {			if ((stat = docmd(0)) >= 0) {				if (stat == 1) doprnt(curln, curln);				continue;			}		}	if (stat == EOF) {		exit(0);	}	if (stat == FATAL) {		fputs("FATAL ERROR\n", stderr);		exit(1);	}	printf("?\n");  }  return(0);}/*	egets.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */int eightbit = 1;		/* save eight bit */int nonascii, nullchar, truncated;int egets(str, size, stream)char *str;int size;FILE *stream;{  int c, count;  char *cp;  for (count = 0, cp = str; size > count;) {	c = getc(stream);	if (c == EOF) {		*cp++ = '\n';		*cp = EOS;		if (count) {			printf("[Incomplete last line]\n");		}		return(count);	}	if (c == NL) {		*cp++ = c;		*cp = EOS;		return(++count);	}	if (c > 127) {		if (!eightbit)	/* if not saving eighth bit */			c = c & 127;	/* strip eigth bit */		nonascii++;	/* count it */	}	if (c) {		*cp++ = c;	/* not null, keep it */		count++;	} else		nullchar++;	/* count nulls */  }  str[count - 1] = EOS;  if (c != NL) {	printf("truncating line\n");	truncated++;	while ((c = getc(stream)) != EOF)		if (c == NL) break;  }  return(count);}/*	esc.c	*//* #include <stdio.h> *//* #include "tools.h" *//* Map escape sequences into their equivalent symbols.  Returns the * correct ASCII character.  If no escape prefix is present then s * is untouched and *s is returned, otherwise **s is advanced to point * at the escaped character and the translated character is returned. */int esc(s)char **s;{  register int rval;  if (**s != ESCAPE) {	rval = **s;  } else {	(*s)++;	switch (toupper(**s)) {	    case '\000':	rval = ESCAPE;	break;	    case 'S':	rval = ' ';	break;	    case 'N':	rval = '\n';	break;	    case 'T':	rval = '\t';	break;	    case 'B':	rval = '\b';	break;	    case 'R':	rval = '\r';	break;	    default:	rval = **s;	break;	}  }  return(rval);}/*	find.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */int find(pat, dir)TOKEN *pat;int dir;{  int i, num;  char lin[MAXLINE];  LINE *ptr;  num = curln;  ptr = getptr(curln);  num = (dir ? nextln(num) : prevln(num));  ptr = (dir ? ptr->l_next : ptr->l_prev);  for (i = 0; i < lastln; i++) {	if (num == 0) {		num = (dir ? nextln(num) : prevln(num));		ptr = (dir ? ptr->l_next : ptr->l_prev);	}	strcpy(lin, ptr->l_buff);	strcat(lin, "\n");	if (matchs(lin, pat, 0)) {		return(num);	}	num = (dir ? nextln(num) : prevln(num));	ptr = (dir ? ptr->l_next : ptr->l_prev);  }  return(ERR);}/*	getfn.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */extern char fname[MAXFNAME];int nofname;char *getfn(){  static char file[256];  char *cp;  if (*inptr == NL) {	nofname = TRUE;	strcpy(file, fname);  } else {	nofname = FALSE;	while (*inptr == SP || *inptr == HT) inptr++;	cp = file;	while (*inptr && *inptr != NL && *inptr != SP && *inptr != HT) {		*cp++ = *inptr++;	}	*cp = '\0';	if (strlen(file) == 0) {		printf("bad file name\n");		return(NULL);	}  }  if (strlen(file) == 0) {	printf("no file name\n");	return(NULL);  }  return(file);}/*	getlst.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */int getlst(){  int num;  line2 = 0;  for (nlines = 0; (num = getone()) >= 0;) {	line1 = line2;	line2 = num;	nlines++;	if (*inptr != ',' && *inptr != ';') break;	if (*inptr == ';') curln = num;	inptr++;  }  nlines = min(nlines, 2);  if (nlines == 0) line2 = curln;  if (nlines <= 1) line1 = line2;  if (num == ERR)	return(num);  else	return(nlines);}/*	getnum.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */int mark['z' - 'a' + 1];int getnum(first)int first;{  TOKEN *srchpat;  int num;  char c;  while (*inptr == SP || *inptr == HT) inptr++;  if (*inptr >= '0' && *inptr <= '9') {	/* line number */	for (num = 0; *inptr >= '0' && *inptr <= '9';) {		num = (num * 10) + *inptr - '0';		inptr++;	}	return num;  }  switch (c = *inptr) {      case '.':	inptr++;	return(curln);      case '$':	inptr++;	return(lastln);      case '/':      case '?':	srchpat = optpat();	if (*inptr == c) inptr++;	return(find(srchpat, c == '/' ? 1 : 0));      case '-':      case '+':	return(first ? curln : 1);      case '\'':	inptr++;	if (*inptr < 'a' || *inptr > 'z') return(EOF);	return mark[*inptr++ - 'a'];      default:	return(first ? EOF : 1);/* unknown address */  }}/*	getone.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */#define FIRST 1#define NOTFIRST 0int getone(){  int c, i, num;  if ((num = getnum(FIRST)) >= 0) {	while (1) {		while (*inptr == SP || *inptr == HT) inptr++;		if (*inptr != '+' && *inptr != '-') break;		c = *inptr++;		if ((i = getnum(NOTFIRST)) < 0) return(i);		if (c == '+') {			num += i;		} else {			num -= i;		}	}  }  return(num > lastln ? ERR : num);}/*	getpat.c	*//* #include <stdio.h> *//* #include "tools.h" *//* Translate arg into a TOKEN string */TOKEN * getpat(arg)char *arg;{  return(makepat(arg, '\000'));}/*	getptr.c	*//* #include <stdio.h> *//* #include "tools.h" *//* #include "ed.h" */LINE * getptr(num)int num;{  LINE *ptr;  int j;  if (2 * num > lastln && num <= lastln) {	/* high line numbers */	ptr = line0.l_prev;	for (j = lastln; j > num; j--) ptr = ptr->l_prev;  } else {			/* low line numbers */	ptr = &line0;	for (j = 0; j < num; j++) ptr = ptr->l_next;  }  return(ptr);}/*	getrhs.c	*//* #include <stdio.h> *//* #include "tools.h" */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -