* Copyright (c) 1992 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Rodney Ruddock of the University of Guelph.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)sub.c 5.3 (Berkeley) %G%";
* The substitute command. It's big because of the backward compatability.
static int l_count2
= 1, l_global
= 0, l_print
= 0;
static int l_first_pass_flag
= 0;
static char *l_match
= NULL
, *l_repl
= NULL
;
LINE
*l_s_ret
, *l_temp_line
, *l_temp_line2
, *l_kval
, *l_last
;
int l_s_flag
, l_count
, l_matched
, l_nflag
, l_cnt
, yy
, l_sr_flag
= 0;
char *l_match2
= NULL
, *l_local
= NULL
, *l_local_temp
= NULL
;
if (start_default
&& End_default
)
start_default
= End_default
= 0;
l_sl
= ss
= getc(inputt
);
if (l_first_pass_flag
== 0)
l_match2
= get_pattern(l_sl
, inputt
, errnum
, 0);
if ((*errnum
== -2) && (l_sl
!= '\n'))
if ((l_match2
== NULL
) ||
(strlen(l_match2
) > 4) || (l_first_pass_flag
== 0))
for (yy
= 0; yy
< (strlen(l_match2
)); yy
++) {
l_print
= (l_print
) ? 0 : 1;
l_global
= (l_global
) ? 0 : 1;
strcpy(help_msg
, "illegal modifier to s");
l_repl
= get_pattern(ss
, inputt
, errnum
, 1);
if (sigint_flag
&& (!sigspecial
))
if ((*errnum
== -1) && (ss
== '\n'))
/* Note, \n still in stream for next getc. */
while (((ss
= getc(inputt
)) != '\n') && (ss
!= EOF
))
l_print
= (l_print
| (int) 1);
l_print
= (l_print
| (int) 2);
l_print
= (l_print
| (int) 4);
if ((ss
> ('0' - 1)) && (ss
< ('9' + 1)))
l_count2
= dig_num_conv(inputt
, errnum
);
"illegal command option");
if ((RE_flag
== 0) && (l_match
[1] == '\0')) {
if ((l_sr_flag
== 0) && (l_match
[1] || (RE_patt
== NULL
))) {
int l_m_len
= 2 + strlen(l_match
);
RE_patt
= malloc(sizeof(char) * (l_m_len
));
bcopy(l_match
, RE_patt
, l_m_len
);
if (sigint_flag
&& (!sigspecial
))
RE_sol
= (l_match
[1] == '^') ? 1 : 0;
(regfree(&RE_comp
), l_err
= regcomp(&RE_comp
, &l_match
[1], 0))) {
regerror(l_err
, &RE_comp
, help_msg
, 128);
if (sigint_flag
&& (!sigspecial
))
get_line(current
->handle
, current
->len
);
if (sigint_flag
&& (!sigspecial
))
RE_match
[0].rm_so
= RE_match
[0].rm_eo
;
l_matched
= regexec_n(&RE_comp
, l_local
,
(size_t)RE_SEC
, RE_match
, 0, l_count
,
(size_t)current
->len
, 0);
l_matched
= regexec_n(&RE_comp
, l_local
,
(size_t)RE_SEC
, RE_match
, 0, l_count
,
if ((l_s_flag
== 0) && (g_flag
== 0))
* The l_local passed into re_replace is not
* freed in re_replace because it is "text",
* the global line holder, for the first pass
* through this loop. The value returned by
* re_replace is a new string (with the first
* replacement in it). If the 'g' flag was
* set with substitute then this new string
* is passed in for the second pass and can
* be freed once re_replace is done with it.
* (...and so on for the rest of the 'g'
* passes. RE_match[0].rm_eo is changed in
* re_replace to be the new location of the
* next character immediately after the
* replacement since it is likely the
* position of that character has changed
* because of the replacement.
l_local
= re_replace(l_local
,
(size_t)(RE_SEC
- 1), RE_match
, &l_repl
[1]);
l_local
= re_replace(l_local
,
(size_t)(RE_SEC
- 1), RE_match
, &l_repl
[1],
if (l_local
[RE_match
[0].rm_eo
] == '\0')
l_temp_line
= current
->above
;
l_temp_line2
= current
->below
;
* Make the new string the one for this line. Check if
if (l_local
[l_cnt
] == '\n' || l_local
[l_cnt
] == '\0') {
if (l_local
[l_cnt
] == '\0')
l_s_ret
= malloc(sizeof(LINE
));
strcpy(help_msg
, "out of memory error");
(l_s_ret
->len
) = strlen(l_local
);
(l_s_ret
->handle
) = add_line(l_local
, l_s_ret
->len
);
(l_s_ret
->above
) = l_temp_line
;
u_add_stk(&(l_temp_line
->below
));
(l_temp_line
->below
) = l_s_ret
;
if ((l_local
[l_cnt
] == '\0') && (l_nflag
== 1))
l_local
= &(l_local
[l_cnt
+ 1]);
(l_s_ret
->below
) = l_temp_line2
;
ku_chk(current
, current
, l_kval
->below
);
if (l_temp_line2
== NULL
)
u_add_stk(&(l_temp_line2
->above
));
(l_temp_line2
->above
) = current
;
if (sigint_flag
&& (!sigspecial
))
if (l_local_temp
!= text
)
current
= current
->below
;
} while (current
!= (End
->below
));
strcpy(help_msg
, "no matches found for substitution");
if (l_print
== (l_print
| (int) 1))
if (l_print
== (l_print
| (int) 2))
if (l_print
== (l_print
| (int) 4))
regcomp(&RE_comp
, &RE_patt
[1], 0);