/* regexp routine definitions
 *
 * Copyright 2001, KUN.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* $Id: pattern.h,v 1.4 2005/04/07 15:25:41 olafs Exp $ */

#ifndef PATTERN_H
#define PATTERN_H

#include <sys/types.h>

typedef long	RegExp;

/*
//------------------------------------------------------------------------------
// Function:
//	RegExp*
//	new_regexp(const char* regexp_txt, int* result_p)
//
// Description:
//	Compile regular expression regexp_txt. A buffer holding the
//	compiled expression is allocated, even if compilation fails.
//	The location pointed to by result_p is set to indicate the
//	compilation result: code < 0 means regular expressions not
//	supported, code 0 means success, and code > 0 indicates an
//	error value.
//
// Return value:
//	Pointer to compiled expression, even in case of failure.
//------------------------------------------------------------------------------
*/

RegExp*
new_regexp(const char* regexp_txt, int* result_p);

/*
//------------------------------------------------------------------------------
// Function:
//	RegExp *
//	empty_regexp();
//
// Description:
//	Prepare for collecting a regular expression consisting of
//	several alternatives.
//	Conceptually, collects an initial "(".
//
// Return value:
//	Pointer to a expression.
//------------------------------------------------------------------------------
*/
RegExp *
empty_regexp();


/*
//------------------------------------------------------------------------------
// Function:
//	int
//	add_alternative_to_regexp(RegExp *regexp, const char* regexp_txt)
//
// Description:
//	Add an alternative to a regular expression as prepared by
//	a previous call to empty_regexp() and add_alternative_to_regexp().
//	Conceptually, adds an alternative operator and the given regexp,
//	resulting in something like "(foo|bar".
//
// Return value:
//	Error code, 0 if ok.
//------------------------------------------------------------------------------
*/
int
add_alternative_to_regexp(RegExp *regexp, const char* regexp_txt);


/*
//------------------------------------------------------------------------------
// Function:
//	void
//	add_alternative_to_regexp(RegExp *regexp, const char* regexp_txt)
//
// Description:
//	Indicates that all alternatives have been added, and
//	prepares the regular expression for use.
//	Conceptually, adds a ")", resulting in something like "(foo|bar)".
//
// Return value:
//	Error code, 0 if ok.
//------------------------------------------------------------------------------
*/
int 
finalize_regexp(RegExp *regexp);

/*
//------------------------------------------------------------------------------
// Function:
//	void
//	delete_regexp(RegExp* regexp)
//
// Description:
//	Delete compiled regular expression regexp.
//------------------------------------------------------------------------------
*/

void
delete_regexp(RegExp* regexp);

/*
//------------------------------------------------------------------------------
// Function:
//	size_t regexp_error(int error, RegExp* regexp, char* buf, size_t sz)
//
// Description:
//	Convert error code associated with regexp to string,
//	and copy at most sz bytes of this string to buf.
//
// Return value:
//	Length of error message.
//------------------------------------------------------------------------------
*/

size_t
regexp_error(int, RegExp*, char*, size_t);

/*
//------------------------------------------------------------------------------
// Function:
//	const char*
//	match_regexp(const char* str, RegExp* regexp)
//
// Description:
//	Match string str with compiled regular expression regexp.
//	This function will try to match the longest prefix of str.
//
// Return value:
//	A pointer to the first character in str not matched by regexp,
//	or NULL if regexp does not match str.
//------------------------------------------------------------------------------
*/

const char*
match_regexp(const char* str, RegExp* regexp);

/*
//------------------------------------------------------------------------------
// Function:
//	const char*
//	match_regexp_all_alternatives(const char* str, RegExp* regexp, int nalts, int *alts);
//
// Description:
//	Match string str with compiled regular expression regexp.
//	This function will try to match the longest prefix of str, and
//	it records all matches of all top-level alternatives that it finds
//      in alts[], like this:
//      3	end position of the match, or -1 to terminate the list
//      5,3,8   the numbers of the alternatives, starting at 1
//      0       to terminate the list of alternatives
//      -1      to terminate the list of matches.
//
// Return value:
//	A pointer to the first character in str not matched by regexp,
//	or NULL if regexp does not match str.
//------------------------------------------------------------------------------
*/
const char*
match_regexp_all_alternatives(const char* str, RegExp* regexp, int nalts, int *alts);
#endif /* PATTERN_H */
