[comp.os.minix] fixes for sed

n62@np1.hep.nl (Klamer Schutte) (03/29/89)

Here are some patches to program sed.
There were bugs in the following fields:
1 strcmp was called with a NULL pointer
2 the regular expression . was interpreted as .*
i was getting the idea the only thing sed was tested for is s/^X// !
The changes in sedexec.c from static to Static is for debugging purposes;
When not static debugging (using mdb ) is easier. Not neccessary to have
the program running!

When patch is run on the source of sed (sed.c, as mailed by ast to me;
probably the same as on the pc distribution ) a shar file is created;
this file unshars to 3 files: sed.h; sedcomp.c and sedexec.c. (reason :
on a single floppy ST it is very difficult to compile a 40k+ file! )

The program is tested on a ST; but i do not know a reason it will not
work on a PC.
Compile, run, enjoy! 

Klamer Schutte (.signature at end )

PS the context diff is created on a bsd4.3 system using diff -b -c;
   But this should be compatible with minix patch.
-----------------------------------------------------------------------
*** sed.c	Wed Mar 29 17:13:44 1989
--- sed.shar	Wed Mar 29 17:10:00 1989
***************
*** 1,28 ****
! /* sed - stream editor		Author: Eric S. Raymond */
! 
! /* This used to be three different files with the following makefile:
!  * (Note the chmem).
! 
! CFLAGS=	-F -T.
! 
! OBJS=   sedcomp.s sedexec.s
! 
! sed: 	$(OBJS)
!         cc -T. -o sed $(OBJS)
! 	@chmem =13312 sed
! 
! $(OBJS):	sed.h
! 
!  * If you want longer lines: increase MAXBUF.
!  * If you want scripts with more text: increase POOLSIZE.
!  * If you want more commands per script: increase MAXCMDS.
!  */
! 
! #include <stdio.h>
! #include <ctype.h>
! 
! /*+++++++++++++++*/
! 
  /* sed.h -- types and constants for the stream editor */
  
  /* data area sizes used by both modules */
--- 1,18 ----
! #! /bin/sh
! # This is a shell archive, meaning:
! # 1. Remove everything above the #! /bin/sh line.
! # 2. Save the resulting text in a file.
! # 3. Execute the file with /bin/sh (not csh) to create the files:
! #	sed.h
! #	sedcomp.c
! #	sedexec.c
! # This archive created: Wed Mar 29 17:10:00 1989
! export PATH; PATH=/bin:$PATH
! if test -f 'sed.h'
! then
! 	echo shar: will not over-write existing file "'sed.h'"
! else
! cat << \SHAR_EOF > 'sed.h'
  /* sed.h -- types and constants for the stream editor */
  
  /* data area sizes used by both modules */
***************
*** 98,110 ****
  
  /* sed.h ends here */
  
! #define CMASK  0xFF	/* some char type should have been unsigned char? */
  
- /*+++++++++++++++*/
- 
  /* sed - stream editor		Author: Eric S. Raymond */
  
  /*
     The stream editor compiles its command input	 (from files or -e options)
     into an internal form using compile() then executes the compiled form using
     execute(). Main() just initializes data structures, interprets command line
--- 88,108 ----
  
  /* sed.h ends here */
  
! SHAR_EOF
! fi # end of overwriting check
! if test -f 'sedcomp.c'
! then
! 	echo shar: will not over-write existing file "'sedcomp.c'"
! else
! cat << \SHAR_EOF > 'sedcomp.c'
  
  /* sed - stream editor		Author: Eric S. Raymond */
  
  /*
+  * If you want longer lines: increase MAXBUF.
+  * If you want scripts with more text: increase POOLSIZE.
+  * If you want more commands per script: increase MAXCMDS.
+ 
     The stream editor compiles its command input	 (from files or -e options)
     into an internal form using compile() then executes the compiled form using
     execute(). Main() just initializes data structures, interprets command line
***************
*** 120,128 ****
     The operation of execute() is described in its source module.
  */
  
! /* #include <stdio.h>		/* uses getc, fprintf, fopen, fclose */
  extern FILE *fopen();		/* should this be in stdio.h? */
! /* #include "sed.h"		/* command type struct and name defines */
  
  /* imported functions */
  extern int strcmp();		/* test strings for equality */
--- 118,126 ----
     The operation of execute() is described in its source module.
  */
  
! #include <stdio.h>		/* uses getc, fprintf, fopen, fclose */
  extern FILE *fopen();		/* should this be in stdio.h? */
! #include "sed.h"		/* command type struct and name defines */
  
  /* imported functions */
  extern int strcmp();		/* test strings for equality */
***************
*** 500,506 ****
  		if (nwfiles >= WFILES) ABORT(TMWFI);
  		fp=gettext(fname[nwfiles]=fp);	/* filename will be in pool */
  		for(i = nwfiles-1; i >= 0; i--) /* match it in table */
! 			if (strcmp(fname[nwfiles], fname[i]) == 0)
  			{
  				cmdp->fout = fout[i];
  				return(0);
--- 498,505 ----
  		if (nwfiles >= WFILES) ABORT(TMWFI);
  		fp=gettext(fname[nwfiles]=fp);	/* filename will be in pool */
  		for(i = nwfiles-1; i >= 0; i--) /* match it in table */
! 			if ((fname[i] != 0) && 
! 			    (strcmp(fname[nwfiles], fname[i]) == 0))
  			{
  				cmdp->fout = fout[i];
  				return(0);
***************
*** 639,644 ****
--- 638,644 ----
  
  		case '.':	/* match any char except newline */
  			*ep++ = CDOT;
+ 			break;
  
  		case '*':	/* 0..n repeats of previous pattern */
  			if (lastep == NULL)	/* if * isn't first on line */
***************
*** 832,838 ****
  {
  	register label	*rp;
  	for(rp = lablst; rp < ptr; rp++)
! 		if (strcmp(rp->name, ptr->name) == 0)
  			return(rp);
  	return(NULL);
  }
--- 832,838 ----
  {
  	register label	*rp;
  	for(rp = lablst; rp < ptr; rp++)
! 		if ((rp->name != NULL) && (strcmp(rp->name, ptr->name) == 0))
  			return(rp);
  	return(NULL);
  }
***************
*** 913,924 ****
  int n;
  {
  /* Flush buffers and exit. */
    _cleanup();
    exit(n);
  }
  
- /*+++++++++++++++*/
- 
  /*
     sedexec.c -- execute compiled form of stream editor commands
  
--- 913,932 ----
  int n;
  {
  /* Flush buffers and exit. */
+ #ifndef	ATARI_ST
    _cleanup();
+ #endif
    exit(n);
  }
+ SHAR_EOF
+ fi # end of overwriting check
+ if test -f 'sedexec.c'
+ then
+ 	echo shar: will not over-write existing file "'sedexec.c'"
+ else
+ cat << \SHAR_EOF > 'sedexec.c'
+ /* sed - stream editor		Author: Eric S. Raymond */
  
  /*
     sedexec.c -- execute compiled form of stream editor commands
  
***************
*** 933,942 ****
     readout() and memcmp() are output and string-comparison utilities.  
  */
  
! /* #include <stdio.h>	/* {f}puts, {f}printf, getc/putc, f{re}open, fclose */
  extern FILE *fopen();
! /* #include <ctype.h>	/* for isprint(), isdigit(), toascii() macros */
! /* #include "sed.h"	/* command type structures & miscellaneous constants */
  
  extern char	*strcpy();	/* used in dosub */
  
--- 941,952 ----
     readout() and memcmp() are output and string-comparison utilities.  
  */
  
! #define	Static		/* Not static but debugable ! */
! 
! #include <stdio.h>	/* {f}puts, {f}printf, getc/putc, f{re}open, fclose */
  extern FILE *fopen();
! #include <ctype.h>	/* for isprint(), isdigit(), toascii() macros */
! #include "sed.h"	/* command type structures & miscellaneous constants */
  
  extern char	*strcpy();	/* used in dosub */
  
***************
*** 961,991 ****
  #define TRUE	 1
  #define FALSE	 0
  
! static char LTLMSG[]	= "sed: line too long\n";
  
! static char	*spend;	 /* current end-of-line-buffer pointer */
! static long	lnum = 0L;	/* current source line number */
  
  /* append buffer maintenance */
! static sedcmd	*appends[MAXAPPENDS];	/* array of ptrs to a,i,c commands */
! static sedcmd	**aptr = appends;	/* ptr to current append */
  
  /* genbuf and its pointers */
! static char	genbuf[GENSIZ];
! static char	*loc1;
! static char	*loc2;
! static char	*locs;
  
  /* command-logic flags */
! static int	lastline;	       /* do-line flag */
! static int	jump;		   /* jump to cmd's link address if set */
! static int	delete;		 /* delete command flag */
  
  /* tagged-pattern tracking */
! static char	*bracend[MAXTAGS];	/* tagged pattern start pointers */
! static char	*brastart[MAXTAGS];	/* tagged pattern end pointers */
  
! static int	anysub;		/* true if any s on current line succeeded */
  
  
  void execute()
--- 971,1001 ----
  #define TRUE	 1
  #define FALSE	 0
  
! Static char LTLMSG[]	= "sed: line too long\n";
  
! Static char	*spend;	 /* current end-of-line-buffer pointer */
! Static long	lnum = 0L;	/* current source line number */
  
  /* append buffer maintenance */
! Static sedcmd	*appends[MAXAPPENDS];	/* array of ptrs to a,i,c commands */
! Static sedcmd	**aptr = appends;	/* ptr to current append */
  
  /* genbuf and its pointers */
! Static char	genbuf[GENSIZ];
! Static char	*loc1;
! Static char	*loc2;
! Static char	*locs;
  
  /* command-logic flags */
! Static int	lastline;	       /* do-line flag */
! Static int	jump;		   /* jump to cmd's link address if set */
! Static int	delete;		 /* delete command flag */
  
  /* tagged-pattern tracking */
! Static char	*bracend[MAXTAGS];	/* tagged pattern start pointers */
! Static char	*brastart[MAXTAGS];	/* tagged pattern end pointers */
  
! Static int	anysub;		/* true if any s on current line succeeded */
  
  
  void execute()
***************
*** 1053,1059 ****
  	}
  }
  
! static int selected(ipc)
  /* is current command selected */
  sedcmd	*ipc;
  {
--- 1063,1069 ----
  	}
  }
  
! Static int selected(ipc)
  /* is current command selected */
  sedcmd	*ipc;
  {
***************
*** 1105,1111 ****
  	return ipc->flags.allbut ? !sel : sel;
  }
  
! static int match(expbuf, gf)	/* uses genbuf */
  /* match RE at expbuf against linebuf; if gf set, copy linebuf from genbuf */
  char	*expbuf;
  {
--- 1115,1121 ----
  	return ipc->flags.allbut ? !sel : sel;
  }
  
! Static int match(expbuf, gf)	/* uses genbuf */
  /* match RE at expbuf against linebuf; if gf set, copy linebuf from genbuf */
  char	*expbuf;
  {
***************
*** 1159,1165 ****
  	return(FALSE);
  }
  
! static int advance(lp, ep)
  /* attempt to advance match pointer by one pattern element */
  register char	*lp;	    /* source (linebuf) ptr */
  register char	*ep;	    /* regular expression element ptr */
--- 1169,1175 ----
  	return(FALSE);
  }
  
! Static int advance(lp, ep)
  /* attempt to advance match pointer by one pattern element */
  register char	*lp;	    /* source (linebuf) ptr */
  register char	*ep;	    /* regular expression element ptr */
***************
*** 1300,1306 ****
  		}
  }
  
! static int substitute(ipc)
  /* perform s command */
  sedcmd	*ipc;			   /* ptr to s command struct */
  {
--- 1310,1316 ----
  		}
  }
  
! Static int substitute(ipc)
  /* perform s command */
  sedcmd	*ipc;			   /* ptr to s command struct */
  {
***************
*** 1320,1326 ****
  	return(TRUE);			   /* we succeeded */
  }
  
! static void dosub(rhsbuf)	       /* uses linebuf, genbuf, spend */
  /* generate substituted right-hand side (of s command) */
  char	*rhsbuf;	/* where to put the result */
  {
--- 1330,1336 ----
  	return(TRUE);			   /* we succeeded */
  }
  
! Static void dosub(rhsbuf)	       /* uses linebuf, genbuf, spend */
  /* generate substituted right-hand side (of s command) */
  char	*rhsbuf;	/* where to put the result */
  {
***************
*** 1358,1364 ****
  	spend = lp-1;
  }
  
! static char *place(asp, al1, al2)	       /* uses genbuf */
  /* place chars at *al1...*(al1 - 1) at asp... in genbuf[] */
  register char	*asp, *al1, *al2;
  {
--- 1368,1374 ----
  	spend = lp-1;
  }
  
! Static char *place(asp, al1, al2)	       /* uses genbuf */
  /* place chars at *al1...*(al1 - 1) at asp... in genbuf[] */
  register char	*asp, *al1, *al2;
  {
***************
*** 1371,1377 ****
  	return(asp);
  }
  
! static void listto(p1, fp)
  /* write a hex dump expansion of *p1... to fp */
  register char	*p1;	    /* the source */
  FILE	    *fp;	    /* output stream to write to */
--- 1381,1387 ----
  	return(asp);
  }
  
! Static void listto(p1, fp)
  /* write a hex dump expansion of *p1... to fp */
  register char	*p1;	    /* the source */
  FILE	    *fp;	    /* output stream to write to */
***************
*** 1396,1402 ****
  	putc('\n', fp);
  }
  
! static void truncated(h) int h;
  {
  	static long last = 0L;
  
--- 1406,1412 ----
  	putc('\n', fp);
  }
  
! Static void truncated(h) int h;
  {
  	static long last = 0L;
  
***************
*** 1408,1414 ****
  	fprintf(stderr, " truncated to %d characters\n", MAXBUF);
  }
  
! static void command(ipc)
  /* execute compiled command pointed at by ipc */
  sedcmd	*ipc;
  {
--- 1418,1424 ----
  	fprintf(stderr, " truncated to %d characters\n", MAXBUF);
  }
  
! Static void command(ipc)
  /* execute compiled command pointed at by ipc */
  sedcmd	*ipc;
  {
***************
*** 1604,1610 ****
  	}
  }
  
! static void openfile(file) char *file;
  /* Replace stdin by given file */
  {
  	if (freopen(file, "r", stdin)==NULL) {
--- 1614,1620 ----
  	}
  }
  
! Static void openfile(file) char *file;
  /* Replace stdin by given file */
  {
  	if (freopen(file, "r", stdin)==NULL) {
***************
*** 1615,1621 ****
  
  static int c;	/* Will be the next char to read, a kind of lookahead */
  
! static void get()
  /* Read next character into c treating all argument files as run through cat */
  {
  	while ((c=getchar())==EOF && --eargc>=0)
--- 1625,1631 ----
  
  static int c;	/* Will be the next char to read, a kind of lookahead */
  
! Static void get()
  /* Read next character into c treating all argument files as run through cat */
  {
  	while ((c=getchar())==EOF && --eargc>=0)
***************
*** 1622,1628 ****
  		openfile(*eargv++);
  }
  
! static void initget()
  /* Initialise character input */
  {
  	if (--eargc>=0) openfile(*eargv++);	/* else input == stdin */
--- 1632,1638 ----
  		openfile(*eargv++);
  }
  
! Static void initget()
  /* Initialise character input */
  {
  	if (--eargc>=0) openfile(*eargv++);	/* else input == stdin */
***************
*** 1629,1635 ****
  	get();
  }
  
! static char *getline(buf)
  /* get next line of text to be edited, return pointer to end */
  register char	*buf;	   /* where to send the input */
  {
--- 1639,1645 ----
  	get();
  }
  
! Static char *getline(buf)
  /* get next line of text to be edited, return pointer to end */
  register char	*buf;	   /* where to send the input */
  {
***************
*** 1651,1657 ****
  	return buf;
  }
  
! static int memcmp(a, b, count)
  /* return TRUE if *a... == *b... for count chars, FALSE otherwise */
  register char	*a, *b;
  {
--- 1661,1667 ----
  	return buf;
  }
  
! Static int memcmp(a, b, count)
  /* return TRUE if *a... == *b... for count chars, FALSE otherwise */
  register char	*a, *b;
  {
***************
*** 1661,1667 ****
  	return(TRUE);		   /* compare succeeded */
  }
  
! static void readout()
  /* write file indicated by r command to output */
  {
  	register int	t;	/* hold input char or EOF */
--- 1671,1677 ----
  	return(TRUE);		   /* compare succeeded */
  }
  
! Static void readout()
  /* write file indicated by r command to output */
  {
  	register int	t;	/* hold input char or EOF */
***************
*** 1688,1690 ****
--- 1698,1704 ----
  
  /* sedexec.c ends here */
  
+ SHAR_EOF
+ fi # end of overwriting check
+ #	End of shell archive
+ exit 0
-- 
________________________________________________________________________________
Klamer Schutte			mcvax!nikhefh!n62	      n62@nikhefh.hep.nl