#include "declarations.h"

//	match(" this_is_A_tag = This_is_a_value ", "([ \t]*)([a-z_]*)[ =:\t]*([a-z_]*)(.*$)", 5);
/*
 * http://burks.brighton.ac.uk/burks/language/ml/mosmllib/regex.htm
 * http://www.boost.org/libs/regex/syntax.htm
 * http://www.metahtml.org/298381227007537/documentation/regex/welcome.mhtml
 */

regmatch_t * match(char *string, char *pattern, int nmatch)
{
   int status, eflag;
   regex_t re;
   regmatch_t *pmatch;
   char buf[120], *ps;
   
	nmatch++;
	pmatch=calloc(nmatch+1, sizeof(regmatch_t));

   if (status=regcomp(&re, pattern, REG_EXTENDED|REG_ICASE|REG_NEWLINE) != 0)  {
   	regerror(status, &re, buf, 120);
   	/*
   	 * WARNING update for proper logging/exit later
   	 */
   	return NULL;
   }

   ps=string;
   eflag=0;

	status = regexec(&re, ps, nmatch, pmatch, eflag);
	/*
	 * if the first subset is zero, there are no matches, for some reason
	 * (my lack of clue), regexec always returns 0 instead of REG_NOMATCH
	 */
	regfree(&re);
	if(pmatch[0].rm_so !=-1)
		return pmatch;
   
   return NULL;

/* never reached, legacy code for reminder */
		/*
		 * make sure we have two valid entries (kinda skip the middle?)
		 */
		
/*		if ((pmatch[2].rm_so + pmatch[2].rm_eo) &&
			 (pmatch[4].rm_so + pmatch[4].rm_eo)) {

			sz1=pmatch[2].rm_eo - pmatch[2].rm_so;
			sz2=pmatch[4].rm_eo - pmatch[4].rm_so;
			tag=calloc(sz1+1, 1);
			value=calloc(sz2+1, 1);
			memcpy(tag, ps+pmatch[2].rm_so, sz1);
			memcpy(value, ps+pmatch[4].rm_so, sz2);
		}
*/
}
