[comp.sys.amiga] mg2a patch: case sensitive search

jw@sics.se (Johan Widen) (06/21/88)

Here is a patch that enables mg to perform case sensitive search
(and exact replace). The default is still case fold search and
the strange case hack replace. You turn this off by calling
	set-case-fold-search
with an argument.

The patch also modifies regex.c so that it can be used without the
alloca() function.

*** :re_search.c.old	Wed Jun 15 16:52:24 1988
--- re_search.c	Mon Jun 20 19:10:10 1988
***************
*** 68,77 ****
  what you give them.   Help stamp out software-hoarding!
  */
  
- #ifdef	REGEX
  #include	"def.h"
  #include	"macro.h"
  
  #define SRCH_BEGIN	(0)			/* Search sub-codes.	*/
  #define SRCH_FORW	(-1)
  #define SRCH_BACK	(-2)
--- 68,79 ----
  what you give them.   Help stamp out software-hoarding!
  */
  
  #include	"def.h"
  #include	"macro.h"
  
+ int	casefoldsearch = TRUE;		 /* Does search ignore case ? */
+ 
+ #ifdef	REGEX
  #define SRCH_BEGIN	(0)			/* Search sub-codes.	*/
  #define SRCH_FORW	(-1)
  #define SRCH_BACK	(-2)
***************
*** 81,87 ****
  
  char	re_pat[NPAT];			/* Regex pattern		*/
  int	re_srch_lastdir = SRCH_NOPR;	 /* Last search flags. */
- int	casefoldsearch = TRUE;		 /* Does search ignore case ? */
  
  /* Indexed by a character, gives the upper case equivalent of the character */
  
--- 83,88 ----
***************
*** 532,563 ****
  }
  
  
- 
- /* Cause case to not matter in searches.  This is the default.	If
-  * called with argument cause case to matter.
-  */
- setcasefold(f, n) {
- 
-   if (f & FFARG) {
-     casefoldsearch = FALSE;
-     ewprintf("Case-fold-search unset");
-   }
-   else {
-     casefoldsearch = TRUE;
-     ewprintf("Case-fold-search set");
-   }
- 
-   /* Invalidate the regular expression pattern since I'm too lazy
-    * to recompile it.
-    */
- 
-   re_pat[0] = '\0';
- 
-   return(TRUE);
- 
- } /* end setcasefold */
- 
- 
  /* Delete all lines after dot that contain a string matching regex
   */
  delmatchlines(f, n) {
--- 533,538 ----
***************
*** 704,706 ****
--- 679,709 ----
    return(TRUE);
  }
  #endif
+ 
+ 
+ 
+ /* Cause case to not matter in searches.  This is the default.	If
+  * called with argument cause case to matter.
+  */
+ setcasefold(f, n) {
+ 
+   if (f & FFARG) {
+     casefoldsearch = FALSE;
+     ewprintf("Case-fold-search unset");
+   }
+   else {
+     casefoldsearch = TRUE;
+     ewprintf("Case-fold-search set");
+   }
+ 
+   /* Invalidate the regular expression pattern since I'm too lazy
+    * to recompile it.
+    */
+ 
+ #ifdef REGEX
+   re_pat[0] = '\0';
+ #endif
+ 
+   return(TRUE);
+ 
+ } /* end setcasefold */
*** :search.c.old	Wed Jun 15 16:51:26 1988
--- search.c	Mon Jun 20 19:47:01 1988
***************
*** 13,18 ****
--- 13,20 ----
  #include	"macro.h"
  #endif
  
+ extern int	casefoldsearch;
+ 
  #define SRCH_BEGIN	(0)			/* Search sub-codes.	*/
  #define SRCH_FORW	(-1)
  #define SRCH_BACK	(-2)
***************
*** 442,447 ****
--- 444,450 ----
  	register int	rcnt = 0;	/* Replacements made so far	*/
  	register int	plen;		/* length of found string	*/
  	char		news[NPAT];	/* replacement string		*/
+ 	int		exact = (f & FFARG) | ! casefoldsearch;
  
  #ifndef NO_MACRO
  	if(macrodef) {
***************
*** 469,475 ****
  		update();
  		switch (getkey(FALSE)) {
  		case ' ':
! 			if (lreplace((RSIZE) plen, news, f) == FALSE)
  				return (FALSE);
  			rcnt++;
  			break;
--- 472,478 ----
  		update();
  		switch (getkey(FALSE)) {
  		case ' ':
! 			if (lreplace((RSIZE) plen, news, exact) == FALSE)
  				return (FALSE);
  			rcnt++;
  			break;
***************
*** 475,481 ****
  			break;
  
  		case '.':
! 			if (lreplace((RSIZE) plen, news, f) == FALSE)
  				return (FALSE);
  			rcnt++;
  			goto stopsearch;
--- 478,484 ----
  			break;
  
  		case '.':
! 			if (lreplace((RSIZE) plen, news, exact) == FALSE)
  				return (FALSE);
  			rcnt++;
  			goto stopsearch;
***************
*** 487,493 ****
  
  		case '!':
  			do {
! 				if (lreplace((RSIZE) plen, news, f) == FALSE)
  					return (FALSE);
  				rcnt++;
  			} while (forwsrch() == TRUE);
--- 490,496 ----
  
  		case '!':
  			do {
! 				if (lreplace((RSIZE) plen, news, exact) == FALSE)
  					return (FALSE);
  				rcnt++;
  			} while (forwsrch() == TRUE);
***************
*** 626,632 ****
  /*
   * Compare two characters.
   * The "bc" comes from the buffer.
!  * It has its case folded out. The
   * "pc" is from the pattern.
   */
  static int
--- 629,635 ----
  /*
   * Compare two characters.
   * The "bc" comes from the buffer.
!  * It has its case folded out if casefoldsearch is TRUE. The
   * "pc" is from the pattern.
   */
  static int
***************
*** 635,644 ****
  {
  	bc = CHARMASK(bc);
  	pc = CHARMASK(pc);
! 	if (bc == pc) return TRUE;
! 	if (ISUPPER(bc)) return TOLOWER(bc) == pc;
! 	if (ISUPPER(pc)) return bc == TOLOWER(pc);
! 	return FALSE;
  }
  
  /*
--- 638,651 ----
  {
  	bc = CHARMASK(bc);
  	pc = CHARMASK(pc);
! 	if (casefoldsearch) {
! 		if (bc == pc) return TRUE;
! 		if (ISUPPER(bc)) return TOLOWER(bc) == pc;
! 		if (ISUPPER(pc)) return bc == TOLOWER(pc);
! 		return FALSE;
! 	} else {
! 		return (bc == pc);
! 	}
  }
  
  /*
*** :keymap.c.old	Wed Jun 15 16:49:46 1988
--- keymap.c	Mon Jun 20 19:12:58 1988
***************
*** 177,191 ****
  extern	int	space_to_tabstop();
  #endif
  
- #ifdef	REGEX
  /*
   * Defined by "re_search.c"
   */
  extern	int	re_forwsearch();	/* Regex search forward		 */
  extern	int	re_backsearch();	/* Regex search backwards	 */
  extern	int	re_searchagain();	/* Repeat regex search command	 */
  extern	int	re_queryrepl();		/* Regex query replace		 */
- extern	int	setcasefold();		/* Set case fold in searches	 */
  extern	int	delmatchlines();	/* Delete all lines matching	 */
  extern	int	delnonmatchlines();	/* Delete all lines not matching */
  extern	int	cntmatchlines();	/* Count matching lines		 */
--- 177,190 ----
  extern	int	space_to_tabstop();
  #endif
  
  /*
   * Defined by "re_search.c"
   */
+ #ifdef	REGEX
  extern	int	re_forwsearch();	/* Regex search forward		 */
  extern	int	re_backsearch();	/* Regex search backwards	 */
  extern	int	re_searchagain();	/* Repeat regex search command	 */
  extern	int	re_queryrepl();		/* Regex query replace		 */
  extern	int	delmatchlines();	/* Delete all lines matching	 */
  extern	int	delnonmatchlines();	/* Delete all lines not matching */
  extern	int	cntmatchlines();	/* Count matching lines		 */
***************
*** 191,196 ****
--- 190,196 ----
  extern	int	cntmatchlines();	/* Count matching lines		 */
  extern	int	cntnonmatchlines();	/* Count nonmatching lines	 */
  #endif
+ extern	int	setcasefold();		/* Set case fold in searches	 */
  
  /*
   * Defined by "region.c".
***************
*** 1022,1030 ****
  	{backsearch,	"search-backward"},
  	{forwsearch,	"search-forward"},
  	{selfinsert,	"self-insert-command"},
- #ifdef	REGEX
  	{setcasefold,	"set-case-fold-search"},
- #endif
  	{set_default_mode, "set-default-mode"},
  	{setfillcol,	"set-fill-column"},
  	{setmark,	"set-mark-command"},
--- 1022,1028 ----
*** :regex.c.old	Wed Jun 15 16:51:52 1988
--- regex.c	Mon Jun 20 20:36:03 1988
***************
*** 51,57 ****
     if (done)
       return;
  
!    bzero (re_syntax_table, sizeof re_syntax_table);
  
     for (c = 'a'; c <= 'z'; c++)
       re_syntax_table[c] = Sword;
--- 51,57 ----
     if (done)
       return;
  
!    bzero(re_syntax_table, sizeof re_syntax_table);
  
     for (c = 'a'; c <= 'z'; c++)
       re_syntax_table[c] = Sword;
***************
*** 371,377 ****
  
  	  PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  	  /* Clear the whole map */
! 	  bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  	  /* Read in characters and ranges, setting map bits */
  	  while (1)
  	    {
--- 371,377 ----
  
  	  PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  	  /* Clear the whole map */
! 	  bzero(b, (1 << BYTEWIDTH) / BYTEWIDTH);
  	  /* Read in characters and ranges, setting map bits */
  	  while (1)
  	    {
***************
*** 626,632 ****
    unsigned char *stackb[NFAILURES];
    unsigned char **stackp = stackb;
  
!   bzero (fastmap, (1 << BYTEWIDTH));
    bufp->fastmap_accurate = 1;
    bufp->can_be_null = 0;
  
--- 626,632 ----
    unsigned char *stackb[NFAILURES];
    unsigned char **stackp = stackb;
  
!   bzero(fastmap, (1 << BYTEWIDTH));
    bufp->fastmap_accurate = 1;
    bufp->can_be_null = 0;
  
***************
*** 941,947 ****
--- 941,951 ----
      If a failure happens and the innermost failure point is dormant,
      it discards that failure point and tries the next one. */
  
+ #ifdef NO_ALLOCA
+   char **stackb = (char **) malloc (2 * NFAILURES * sizeof (char *));
+ #else
    char **stackb = (char **) alloca (2 * NFAILURES * sizeof (char *));
+ #endif
    char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];
  
    /* Information on the "contents" of registers.
***************
*** 1046,1051 ****
--- 1050,1058 ----
  		}
  	      regs->start[0] = pos;
  	    }
+ #ifdef NO_ALLOCA
+ 	  free((char *) stackb);
+ #endif
  	  if (d - string1 >= 0 && d - string1 <= size1)
  	    return d - string1 - pos;
  	  else
***************
*** 1101,1107 ****
  		if (mcnt > dend2 - d2)
  		  mcnt = dend2 - d2;
  		/* Compare that many; failure if mismatch, else skip them. */
! 		if (translate ? bcmp_translate (d, d2, mcnt, translate) : bcmp (d, d2, mcnt))
  		  goto fail;
  		d += mcnt, d2 += mcnt;
  	      }
--- 1108,1114 ----
  		if (mcnt > dend2 - d2)
  		  mcnt = dend2 - d2;
  		/* Compare that many; failure if mismatch, else skip them. */
! 		if (translate ? bcmp_translate (d, d2, mcnt, translate) : bcmp(d, d2, mcnt))
  		  goto fail;
  		d += mcnt, d2 += mcnt;
  	      }
***************
*** 1173,1182 ****
  	case on_failure_jump:
  	  if (stackp == stacke)
  	    {
  	      char **stackx = (char **) alloca (2 * (stacke - stackb) * sizeof (char *));
! 	      bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  	      stackp += stackx - stackb;
  	      stacke = stackx + 2 * (stacke - stackb);
  	      stackb = stackx;
  	    }
  	  mcnt = *p++ & 0377;
--- 1180,1196 ----
  	case on_failure_jump:
  	  if (stackp == stacke)
  	    {
+ #ifdef NO_ALLOCA
+ 	      char **stackx = (char **) malloc (2 * (stacke - stackb) * sizeof (char *));
+ #else
  	      char **stackx = (char **) alloca (2 * (stacke - stackb) * sizeof (char *));
! #endif
! 	      bcopy(stackb, stackx, (stacke - stackb) * sizeof (char *));
  	      stackp += stackx - stackb;
  	      stacke = stackx + 2 * (stacke - stackb);
+ #ifdef NO_ALLOCA
+ 	      free((char *) stackb);
+ #endif
  	      stackb = stackx;
  	    }
  	  mcnt = *p++ & 0377;
***************
*** 1240,1249 ****
  	case dummy_failure_jump:
  	  if (stackp == stacke)
  	    {
  	      char **stackx = (char **) alloca (2 * (stacke - stackb) * sizeof (char *));
! 	      bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  	      stackp += stackx - stackb;
  	      stacke = stackx + 2 * (stacke - stackb);
  	      stackb = stackx;
  	    }
  	  *stackp++ = 0;
--- 1254,1270 ----
  	case dummy_failure_jump:
  	  if (stackp == stacke)
  	    {
+ #ifdef NO_ALLOCA
+ 	      char **stackx = (char **) malloc (2 * (stacke - stackb) * sizeof (char *));
+ #else
  	      char **stackx = (char **) alloca (2 * (stacke - stackb) * sizeof (char *));
! #endif
! 	      bcopy(stackb, stackx, (stacke - stackb) * sizeof (char *));
  	      stackp += stackx - stackb;
  	      stacke = stackx + 2 * (stacke - stackb);
+ #ifdef NO_ALLOCA
+ 	      free((char *) stackb);
+ #endif
  	      stackb = stackx;
  	    }
  	  *stackp++ = 0;
***************
*** 1398,1403 ****
--- 1419,1427 ----
  	}
        else break;   /* Matching at this starting point really fails! */
      }
+ #ifdef NO_ALLOCA
+   free((char *) stackb);
+ #endif
    return -1;	     /* Failure to match */
  }
  
*** :sysdef.h.old	Wed Jun 15 16:53:54 1988
--- sysdef.h	Mon Jun 20 21:17:48 1988
***************
*** 16,21 ****
--- 16,22 ----
  #endif
  
  #define	VARARGS
+ #define LOCAL_VARARGS
  #define	DPROMPT				/* we always want delayed prompts */
  #define	KBLOCK	4096			/* Kill grow.			*/
  #define	GOOD	0			/* Good exit status.		*/
***************
*** 47,52 ****
--- 48,55 ----
  #define	MAXPATH	128	/* longest expected directory path	*/
  
  #define	bcopy(src,dest,len) movmem(src,dest,len)
+ #define	bcmp(a,b,len) memcmp(a,b,len)
+ #define bzero(dest, len) setmem(dest, len)
  
  #define fncmp Strcmp
  

-- 
Johan Widen
SICS, PO Box 1263, S-164 28 KISTA, SWEDEN
Tel: +46 8 752 15 32	Ttx: 812 61 54 SICS S	Fax: +46 8 751 72 30
Internet: jw@sics.se or {mcvax,munnari,ukc,unido}!enea!sics.se!jw