xref: /csrg-svn/contrib/ed/re.c (revision 60663)
157698Sbostic /*-
2*60663Sbostic  * Copyright (c) 1992, 1993
3*60663Sbostic  *	The Regents of the University of California.  All rights reserved.
457698Sbostic  *
557698Sbostic  * This code is derived from software contributed to Berkeley by
657698Sbostic  * Rodney Ruddock of the University of Guelph.
757698Sbostic  *
857698Sbostic  * %sccs.include.redist.c%
957698Sbostic  */
1057698Sbostic 
1157698Sbostic #ifndef lint
12*60663Sbostic static char sccsid[] = "@(#)re.c	8.1 (Berkeley) 05/31/93";
1357698Sbostic #endif /* not lint */
1457698Sbostic 
1557710Sbostic #include <sys/types.h>
1657710Sbostic 
1757710Sbostic #include <regex.h>
1857710Sbostic #include <setjmp.h>
1957710Sbostic #include <stdio.h>
2057710Sbostic #include <stdlib.h>
2157710Sbostic #include <string.h>
2257710Sbostic 
2358315Sbostic #ifdef DBI
2458315Sbostic #include <db.h>
2558315Sbostic #endif
2658315Sbostic 
2757698Sbostic #include "ed.h"
2857710Sbostic #include "extern.h"
2957698Sbostic 
3057698Sbostic /*
3157698Sbostic  * This finds the n-th occurrence of an RE in a line. If '^' was at the start
3257710Sbostic  * of the RE then look once (in case n=1). There is no standard RE interface
3357710Sbostic  * to do this.  Returns 0 for success.  NOTE: the #ifdef REG_STARTEND is if
3457710Sbostic  * the regex package has the BSD extensions to it.
3557698Sbostic  */
3657698Sbostic int
3757698Sbostic #ifdef REG_STARTEND
regexec_n(reprecomp,strg,num_subexp,reprematch,flags,n,len,pass)3857698Sbostic regexec_n(reprecomp, strg, num_subexp, reprematch, flags, n, len, pass)
3957698Sbostic #else
4057698Sbostic regexec_n(reprecomp, strg, num_subexp, reprematch, flags, n, offset, pass)
4157698Sbostic #endif
4257710Sbostic 	regex_t *reprecomp;
4357710Sbostic 	char *strg;
4457710Sbostic 	size_t num_subexp;
4557710Sbostic 	regmatch_t reprematch[];
4657710Sbostic 	int flags, n;
4757698Sbostic #ifdef REG_STARTEND
4857710Sbostic 	size_t len;
4957698Sbostic #else
5057710Sbostic 	size_t *offset;
5157698Sbostic #endif
5257710Sbostic 	int pass; /* if pass == 0 .rm_so user set, else set default */
5357698Sbostic {
5459475Sbostic 	int l_cnt, l_flag=0;
5557698Sbostic #ifndef REG_STARTEND
5659915Sbostic 	char *l_offset=strg, *l_end;
5757698Sbostic #endif
5857698Sbostic 
5957710Sbostic 	if (n <= 0)
6057710Sbostic 		return (REG_NOMATCH);
6157698Sbostic #ifdef REG_STARTEND
6257710Sbostic 	flags = (flags | REG_STARTEND);
6357710Sbostic 	if (pass)
6457710Sbostic 		reprematch[0].rm_so = 0;
6557710Sbostic 	reprematch[0].rm_eo = len;
6657698Sbostic #else
6757710Sbostic 	strg = &strg[offset];
6859915Sbostic 	l_end = &strg[strlen(strg)];
6957698Sbostic #endif
7057710Sbostic 	for (l_cnt = 0;;) {
7157710Sbostic 		if (regexec(reprecomp,
7257710Sbostic 		    strg, num_subexp, reprematch, flags) == 0)
7357710Sbostic 			l_cnt++;
7457710Sbostic 		else
7557710Sbostic 			return (REG_NOMATCH);
7659915Sbostic 
7757710Sbostic 		if (l_cnt >= n)
7857710Sbostic 			break;
7957698Sbostic #ifdef REG_STARTEND
8059915Sbostic 		if (reprematch[0].rm_so == reprematch[0].rm_eo)
8159915Sbostic 			reprematch[0].rm_eo++;
8257710Sbostic 		reprematch[0].rm_so = reprematch[0].rm_eo;
8359915Sbostic 		if (reprematch[0].rm_so == len)
8459915Sbostic 			return (REG_NOMATCH);
8557710Sbostic 		reprematch[0].rm_eo = len;
8657698Sbostic #else
8757710Sbostic 		strg = &strg[reprematch[0].rm_eo];
8859915Sbostic 		if (strg == l_end)
8959915Sbostic                         return (REG_NOMATCH);
9057698Sbostic #endif
9157710Sbostic 		/* if a "^" started the current RE we only loop once */
9257710Sbostic 		if (RE_sol)
9357710Sbostic 			return (REG_NOMATCH);
9457710Sbostic 	}
9557698Sbostic #ifndef REG_STARTEND
9657710Sbostic 	*offset = (size_t) (strg - l_offset);
9757698Sbostic #endif
9857710Sbostic 	return (0);		/* success */
9957710Sbostic }
10057698Sbostic 
10157698Sbostic /*
10257698Sbostic  * Replace in the line specified at the found locations with the
10357698Sbostic  * specified replacement. There is no standard RE interface to do
10457698Sbostic  * this.
10557698Sbostic  */
10657710Sbostic char *
10757698Sbostic #ifdef REG_STARTEND
10857710Sbostic re_replace(line, num_subexp, repmatch, replacer)
10957698Sbostic #else
11057710Sbostic re_replace(line, num_subexp, repmatch, replacer, offset)
11157698Sbostic #endif
11257710Sbostic 	char *line;
11357710Sbostic 	size_t num_subexp;
11457710Sbostic 	regmatch_t repmatch[];
11557710Sbostic 	char *replacer;
11657698Sbostic #ifndef REG_STARTEND
11757710Sbostic 	size_t offset;
11857698Sbostic #endif
11957698Sbostic {
12057710Sbostic 	static char *l_prev_r = NULL;
12157710Sbostic 	static int l_prev_r_flag = 0;
12257710Sbostic 	regoff_t l_len_before, l_len_whole, l_slen[RE_SEC];
12357710Sbostic 	int l_cnt, l_len_new = 0, l_new_rm_eo = 0;
12457710Sbostic 	char *l_string, *l_head;
12557698Sbostic 
12657710Sbostic 	if (l_prev_r_flag == 0) {
12757710Sbostic 		l_prev_r_flag = 1;
12857710Sbostic 		l_prev_r = NULL;
12957710Sbostic 	}
13057710Sbostic 	l_head = replacer;
13157710Sbostic 	/* Length of what stays the same before. */
13257710Sbostic 	l_len_before = (repmatch[0].rm_so);
13357710Sbostic 	l_len_whole = strlen(line);
13457710Sbostic 	if (num_subexp > RE_SEC - 1)
13557710Sbostic 		num_subexp = RE_SEC - 1;
13657710Sbostic 	for (l_cnt = 0; l_cnt <= num_subexp; l_cnt++)
13757710Sbostic 		l_slen[l_cnt] =
13857710Sbostic 		    (repmatch[l_cnt].rm_eo) - (repmatch[l_cnt].rm_so);
13957698Sbostic 
14057710Sbostic 	/*
14157710Sbostic 	 * l_slen[0] == len of what is to be replaced.
14257710Sbostic 	 * l_slen[1-9] == len of each backref.
14357710Sbostic 	 */
14459915Sbostic 	if ((*replacer == '%') && (replacer[1] == '\0')) {
14557710Sbostic 		l_string = calloc(l_len_whole - l_slen[0] +
14657710Sbostic 		    (strlen(l_prev_r)) + 2, sizeof(char));
14757710Sbostic 		if (l_string == NULL) {
14857710Sbostic 			/* *errnum = -1; */
14957710Sbostic 			strcpy(help_msg, "out of memory error");
15057710Sbostic 			return (NULL);
15157710Sbostic 		}
15257698Sbostic #ifdef REG_STARTEND
15359483Sbostic 		memmove(l_string, line, (int) l_len_before);
15457698Sbostic #else
15559483Sbostic 		memmove(l_string, line, (int) l_len_before + offset);
15657698Sbostic #endif
15757698Sbostic #ifdef REG_STARTEND
15857710Sbostic 		l_string[l_len_before] = '\0';
15957698Sbostic #else
16057710Sbostic 		l_string[l_len_before + offset] = '\0';
16157698Sbostic #endif
16257710Sbostic 		strcat(l_string, l_prev_r);
16359915Sbostic 		l_new_rm_eo = strlen(l_string);
16457698Sbostic #ifdef REG_STARTEND
16557710Sbostic 		strcat(l_string, &line[repmatch[0].rm_eo]);
16657698Sbostic #else
16757710Sbostic 		strcat(l_string, &line[repmatch[0].rm_eo + offset]);
16857698Sbostic #endif
16959915Sbostic 		repmatch[0].rm_eo = l_new_rm_eo;
17057710Sbostic 		return (l_string);
17157710Sbostic 	}
17257698Sbostic 
17357710Sbostic 	/* Figure out length of new line first. */
17457710Sbostic 	while (*replacer != '\0') {
17557710Sbostic 		/* Add in the length of the RE match. */
17657710Sbostic 		if (*replacer == '&')
17757710Sbostic 			l_len_new = l_len_new + l_slen[0];
17857710Sbostic 		/* Add in the length of a backref. */
17957710Sbostic 		else if (*replacer == '\\') {
18057710Sbostic 			replacer++;
18157710Sbostic 			if ((*replacer > '0') &&
18257710Sbostic 			    (*replacer < ('9' + 1)) &&
18357710Sbostic 			    (repmatch[*replacer - '0'].rm_so > -1))
18457710Sbostic 				/* -1 - -1 = 0 */
18557710Sbostic 				l_len_new = l_len_new + l_slen[*replacer - '0'];
18657710Sbostic 			else
18757710Sbostic 				l_len_new++;
18857710Sbostic 		} else
18957710Sbostic 			l_len_new++;
19057710Sbostic 		replacer++;
19157710Sbostic 	}
19257698Sbostic 
19357710Sbostic 	/* Create the line of an appropriate length. */
19457710Sbostic 	l_string =
19557710Sbostic 	    calloc(l_len_whole - l_slen[0] + l_len_new + 2, sizeof(char));
19657710Sbostic 	if (l_string == NULL) {
19757710Sbostic 		strcpy(help_msg, "out of memory error");
19857710Sbostic 		return (NULL);
19957710Sbostic 	}
20057710Sbostic 	if (l_prev_r != NULL)
20157710Sbostic 		free(l_prev_r);
20257710Sbostic 	l_prev_r = calloc(l_len_new + 2, sizeof(char));
20357710Sbostic 	if (l_prev_r == NULL) {
20457710Sbostic 		strcpy(help_msg, "out of memory error");
20557710Sbostic 		return (NULL);
20657710Sbostic 	}
20757710Sbostic 	/* Copy over what doesn't change before the chars to be replaced. */
20857698Sbostic #ifdef REG_STARTEND
20959483Sbostic 	memmove(l_string, line, (size_t)l_len_before);
21057698Sbostic #else
21159483Sbostic 	memmove(l_string, line, l_len_before + offset);
21257698Sbostic #endif
21357698Sbostic #ifdef REG_STARTEND
21457710Sbostic 	l_string[l_len_before] = '\0';
21557698Sbostic #else
21657710Sbostic 	l_string[l_len_before + offset] = '\0';
21757698Sbostic #endif
21857710Sbostic 	l_prev_r[0] = '\0';
21957698Sbostic 
22057710Sbostic 	/* Make the replacement. */
22157710Sbostic 	replacer = l_head;
22257710Sbostic 	while (*replacer != '\0') {
22357710Sbostic 		/* Put what matched the RE into the replacement. */
22457710Sbostic 		if (*replacer == '&') {
22557698Sbostic #ifdef REG_STARTEND
22657710Sbostic 			strncat(l_string,
22757710Sbostic 			    &line[repmatch[0].rm_so], (int)l_slen[0]);
22857710Sbostic 			strncat(l_prev_r,
22957710Sbostic 			    &line[repmatch[0].rm_so], (int) l_slen[0]);
23057698Sbostic #else
23157710Sbostic 			strncat(l_string,
23257710Sbostic 			    &line[repmatch[0].rm_so + offset], (int) l_slen[0]);
23357710Sbostic 			strncat(l_prev_r,
23457710Sbostic 			    &line[repmatch[0].rm_so + offset], (int) l_slen[0]);
23557698Sbostic #endif
23657710Sbostic 		} else if (*replacer == '\\') {
23757710Sbostic 			/* Likely a backref to be included. */
23857710Sbostic 			replacer++;
23957710Sbostic 			if ((*replacer > '0') && (*replacer < ('9' + 1)) &&
24057710Sbostic 			    (repmatch[*replacer - '0'].rm_so > -1)) {
24157698Sbostic #ifdef REG_STARTEND
24257710Sbostic 				strncat(l_string,
24357710Sbostic 				    &line[repmatch[*replacer - '0'].rm_so],
24457710Sbostic 				    (int) l_slen[*replacer - '0']);
24557710Sbostic 				strncat(l_prev_r,
24657710Sbostic 				    &line[repmatch[*replacer - '0'].rm_so],
24757710Sbostic 				    (int) l_slen[*replacer - '0']);
24857698Sbostic #else
24957710Sbostic 				strncat(l_string,
25057710Sbostic 				    &line[repmatch[*replacer - '0'].rm_so +
25157710Sbostic 				    offset], (int) l_slen[*replacer - '0']);
25257710Sbostic 				strncat(l_prev_r,
25357710Sbostic 				    &line[repmatch[*replacer - '0'].rm_so +
25457710Sbostic 				    offset], (int) l_slen[*replacer - '0']);
25557698Sbostic #endif
25657710Sbostic 			}
25757710Sbostic 			/* Put the replacement in. */
25857710Sbostic 			else {
25957710Sbostic 				strncat(l_string, replacer, 1);
26057710Sbostic 				strncat(l_prev_r, replacer, 1);
26157710Sbostic 			}
26257710Sbostic 		}
26357710Sbostic 		/* Put the replacement in. */
26457710Sbostic 		else {
26557710Sbostic 			strncat(l_string, replacer, 1);
26657710Sbostic 			strncat(l_prev_r, replacer, 1);
26757710Sbostic 		}
26857710Sbostic 		replacer++;
26957710Sbostic 	}
27057698Sbostic 
27157710Sbostic 	l_new_rm_eo = strlen(l_string);
27257698Sbostic 
27357710Sbostic 	/* Copy over what was after the chars to be replaced to the new line. */
27457698Sbostic #ifdef REG_STARTEND
27557710Sbostic 	strcat(l_string, &line[repmatch[0].rm_eo]);
27657698Sbostic #else
27757710Sbostic 	strcat(l_string, &line[repmatch[0].rm_eo + offset]);
27857698Sbostic #endif
27957698Sbostic 
28057710Sbostic 	repmatch[0].rm_eo = l_new_rm_eo;	/* Update rm_eo. */
28157698Sbostic #ifndef REG_STARTEND
28257710Sbostic 	offset += l_new_rm_eo;			/* Update offset. */
28357698Sbostic #endif
28457710Sbostic 	return (l_string);			/* Return the new line. */
28557710Sbostic }
286