#ifndef _STRMANIP_H
#define _STRMANIP_H

/*
 * Perform regular-expression substitution on a string.  The source
 * string (of length sourcelen) is searched for the given extended
 * regular expression (egrep syntax).  If the RE was not found, *found
 * is set to zero and a malloc'ed copy of the original string is
 * returned.  If it was found, *found is set nonzero, the string
 * matched by the RE is replaced by "sub" according to the rules
 * below, and the result is returned in a malloc'ed string of
 * appropriate size.  In all cases the length of the returned string
 * is placed in *newlen.
 *
 * If the string contains any null bytes (\0), it is assumed to be a
 * binary string and regular expression replacement is not performed.
 *
 * If found and newlen are not needed, NULL pointers can be given for
 * either of these arguments.
 *
 * In all cases, it is the caller's responsibility to free the
 * returned string.  BEWARE: it is easy to forget to free the string
 * and wind up with memory leaks!
 *
 * If ignore_case is nonzero, case is ignored when matching the RE.
 *
 * If global is zero, only the first-found match for the RE is
 * replaced.  if global is nonzero, every (non-overlapping) match is
 * replaced.
 *
 * Certain characters in the substitution string are treated specially:
 *
 *	&	is replaced by the matched string.
 *	\0	is replaced by the matched string.
 *	\n	if n = 1-9, is replaced by the nth matched
 *		parenthesized substring.  It is an error to use \n if
 *		there are fewer than n matched subexpressions.
 *	\&	is replaced by &
 *	\\	is replaced by \
 *
 * NOTE: to make the lab simpler to debug, if any errors are found in
 * the regular expression an error message is printed and the program
 * is immediately terminated.
 */
extern char* substitute_re(char* source, unsigned int sourcelen, char* regex,
  char* sub, int ignore_case, int global, int* found, unsigned int* newlen);

#endif /* _STRMANIP_H */
