Example #1
0
qse_char_t* qse_awk_rtx_strtok (
	qse_awk_rtx_t* rtx, const qse_char_t* s, 
	const qse_char_t* delim, qse_cstr_t* tok)
{
	return qse_awk_rtx_strxntok (
		rtx, s, qse_strlen(s), delim, qse_strlen(delim), tok);
}
Example #2
0
File: rex01.c Project: apense/qse
static int rex_main (int argc, qse_char_t* argv[])
{
	qse_rex_t* rex;
	qse_rex_node_t* start;
	qse_cstr_t str;
	qse_cstr_t matstr;
	int n;

	if (argc != 3)
	{
		qse_printf (QSE_T("USAGE: %s pattern string\n"), 
			qse_basename(argv[0]));
		return -1;
	}

	rex = qse_rex_open (QSE_MMGR_GETDFL(), 0, QSE_NULL);
	if (rex == QSE_NULL)
	{
		qse_printf (QSE_T("ERROR: cannot open rex\n"));
		return -1;
	}

	qse_rex_setopt (rex, QSE_REX_STRICT);

	start = qse_rex_comp (rex, argv[1], qse_strlen(argv[1]));
	if (start == QSE_NULL)
	{
		qse_printf (QSE_T("ERROR: cannot compile - %s\n"),
			qse_rex_geterrmsg(rex));
		qse_rex_close (rex);
		return -1;
	}

	str.ptr = argv[2];
	str.len = qse_strlen(argv[2]);

	qse_printf (QSE_T("compile ok\n"));

	n = qse_rex_exec (rex, &str, &str, &matstr);
	if (n <= -1)
	{
		qse_printf (QSE_T("ERROR: cannot execute - %s\n"),
			qse_rex_geterrmsg(rex));
		qse_rex_close (rex);
		return -1;
	}
	if (n >= 1)
	{
		qse_printf (QSE_T("MATCH: [%.*s] beginning from char #%d\n"), 
			(int)matstr.len, matstr.ptr,
			(int)(matstr.ptr - str.ptr + 1));
	}

	qse_rex_close (rex);
	return 0;
}
Example #3
0
File: mod-sed.c Project: apense/qse
static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qse_awk_mod_sym_t* sym)
{
	qse_cstr_t ea;
	int left, right, mid, n;

	left = 0; right = QSE_COUNTOF(fnctab) - 1;

	while (left <= right)
	{
		mid = (left + right) / 2;

		n = qse_strcmp (fnctab[mid].name, name);
		if (n > 0) right = mid - 1; 
		else if (n < 0) left = mid + 1;
		else
		{
			sym->type = QSE_AWK_MOD_FNC;
			sym->u.fnc = fnctab[mid].info;
			return 0;
		}
	}

	ea.ptr = (qse_char_t*)name;
	ea.len = qse_strlen(name);
	qse_awk_seterror (awk, QSE_AWK_ENOENT, &ea, QSE_NULL);
	return -1;
}
Example #4
0
qse_char_t* qse_awk_rtx_strntok (
	qse_awk_rtx_t* rtx, const qse_char_t* s, 
	const qse_char_t* delim, qse_size_t delim_len,
	qse_cstr_t* tok)
{
	return qse_awk_rtx_strxntok (
		rtx, s, qse_strlen(s), delim, delim_len, tok);
}
Example #5
0
File: sll.c Project: DeadZen/qse
static int test1 ()
{
	qse_sll_t* s1;
	qse_sll_node_t* p;
	qse_char_t* x[] =
	{
		QSE_T("this is so good"),
		QSE_T("what the hack"),
		QSE_T("do you like it?")
	};
	int i;

	s1 = qse_sll_open (QSE_MMGR_GETDFL(), 0);	
	if (s1 == QSE_NULL)
	{
		qse_printf (QSE_T("cannot open a string\n"));
		return -1;
	}

	qse_sll_setcopier (s1, QSE_SLL_COPIER_INLINE);
	qse_sll_setscale (s1, QSE_SIZEOF(qse_char_t));

	for (i = 0; i < QSE_COUNTOF(x); i++)
	{
		qse_sll_pushtail (s1, x[i], qse_strlen(x[i]));
	}
	qse_printf (QSE_T("s1 holding [%d] nodes\n"), QSE_SLL_SIZE(s1));
	qse_sll_walk (s1, walk_sll, QSE_NULL);
		

	p = qse_sll_search (s1, QSE_NULL, x[0], qse_strlen(x[0]));
	if (p != QSE_NULL)
	{
		qse_sll_delete (s1, p);
	}
	qse_printf (QSE_T("s1 holding [%d] nodes\n"), QSE_SLL_SIZE(s1));
	qse_sll_walk (s1, walk_sll, QSE_NULL);

	qse_sll_close (s1);
	return 0;
}
Example #6
0
File: sed.c Project: DeadZen/qse
static int expand_wildcards (int argc, qse_char_t* argv[], int glob, xarg_t* xarg)
{
	int i;
	qse_cstr_t tmp;

	for (i = 0; i < argc; i++)
	{
		int x;

		if (glob)
		{
		#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
			int glob_flags = QSE_GLOB_TOLERANT | QSE_GLOB_PERIOD | QSE_GLOB_NOESCAPE | QSE_GLOB_IGNORECASE;
		#else
			int glob_flags = QSE_GLOB_TOLERANT | QSE_GLOB_PERIOD;
		#endif

			x = qse_glob (
				argv[i], collect_into_xarg, xarg,
				glob_flags, xarg->mmgr, qse_getdflcmgr()
			);

			if (x <= -1) return -1;
		}
		else x = 0;

		if (x == 0)
		{
			/* not expanded. just use it as is */
			tmp.ptr = argv[i];
			tmp.len = qse_strlen(argv[i]);
			if (collect_into_xarg (&tmp, xarg) <= -1) return -1;
		}
	}

	return 0;
}
Example #7
0
File: sed.c Project: DeadZen/qse
static int add_script (const qse_char_t* str, int mem)
{
	if (g_script.size >= g_script.capa)
	{
		qse_sed_iostd_t* tmp;

		tmp = QSE_MMGR_REALLOC (
			QSE_MMGR_GETDFL(), 
			g_script.io, 
			QSE_SIZEOF(*g_script.io) * (g_script.capa + 16 + 1));
		if (tmp == QSE_NULL) 
		{
			qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory while processing %s\n"), str);
			return -1;
		}

		g_script.io = tmp;
		g_script.capa += 16;
	} 

	if (mem)
	{
		g_script.io[g_script.size].type = QSE_SED_IOSTD_STR;
		/* though its type is not qualified to be const, 
		 * u.mem.ptr is actually const when used for input */
		g_script.io[g_script.size].u.str.ptr = (qse_char_t*)str;
		g_script.io[g_script.size].u.str.len = qse_strlen(str);
	}
	else
	{
		g_script.io[g_script.size].type = QSE_SED_IOSTD_FILE;
		g_script.io[g_script.size].u.file.path = str;
		g_script.io[g_script.size].u.file.cmgr = g_script_cmgr;
	}
	g_script.size++;
	return 0;
}
Example #8
0
File: awk15.c Project: DeadZen/qse
int main ()
{
	qse_awk_t* awk = QSE_NULL;
	qse_awk_rtx_t* rtx = QSE_NULL;
	qse_awk_val_t* retv;

	qse_awk_parsestd_t psin[2];
	qse_awk_parsestd_t psout;

	int ret;

	qse_openstdsios ();

	awk = qse_awk_openstd (0, QSE_NULL);
	if (awk == QSE_NULL)  
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open awk\n"));
		ret = -1; goto oops;
	}

	psin[0].type = QSE_AWK_PARSESTD_STR;
	psin[0].u.str.ptr = src;
	psin[0].u.str.len = qse_strlen(src);
	psin[1].type = QSE_AWK_PARSESTD_NULL;

	psout.type = QSE_AWK_PARSESTD_STR;
	/* ps.out.u.str.ptr and ps.out.u.str.len are set when qse_awk_parsestd() 
	 * returns success */

	ret = qse_awk_parsestd (awk, psin, &psout);
	if (ret <= -1)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), 
			qse_awk_geterrmsg(awk));
		ret = -1; goto oops;
	}

	qse_printf (QSE_T("DEPARSED SOURCE:\n%s\n"), psout.u.str.ptr);
	qse_printf (QSE_T("=================================\n"));
	qse_fflush (QSE_STDOUT);

	QSE_MMGR_FREE (qse_awk_getmmgr(awk), psout.u.str.ptr);

	rtx = qse_awk_rtx_openstd (
		awk, 
		0,
		QSE_T("awk02"),
		QSE_NULL,  /* stdin */
		QSE_NULL,  /* stdout */
		QSE_NULL   /* default cmgr */
	);
	if (rtx == QSE_NULL) 
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), 
			qse_awk_geterrmsg(awk));
		ret = -1; goto oops;
	}
	
	retv = qse_awk_rtx_loop (rtx);
	if (retv == QSE_NULL)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), 
			qse_awk_rtx_geterrmsg(rtx));
		ret = -1; goto oops;
	}

	qse_awk_rtx_refdownval (rtx, retv);
	ret = 0;

oops:
	if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
	if (awk != QSE_NULL) qse_awk_close (awk);

	qse_closestdsios ();
	return -1;
}
Example #9
0
qse_size_t qse_awk_inttostr (
	qse_awk_t* awk, qse_awk_int_t value, 
	int radix, const qse_char_t* prefix, qse_char_t* buf, qse_size_t size)
{
	qse_awk_int_t t, rem;
	qse_size_t len, ret, i;
	qse_size_t prefix_len;

	prefix_len = (prefix != QSE_NULL)? qse_strlen(prefix): 0;

	t = value;
	if (t == 0)
	{
		/* zero */
		if (buf == QSE_NULL) 
		{
			/* if buf is not given, 
			 * return the number of bytes required */
			return prefix_len + 1;
		}

		if (size < prefix_len+1) 
		{
			/* buffer too small */
			return (qse_size_t)-1;
		}

		for (i = 0; i < prefix_len; i++) buf[i] = prefix[i];
		buf[prefix_len] = QSE_T('0');
		if (size > prefix_len+1) buf[prefix_len+1] = QSE_T('\0');
		return prefix_len+1;
	}

	/* non-zero values */
	len = prefix_len;
	if (t < 0) { t = -t; len++; }
	while (t > 0) { len++; t /= radix; }

	if (buf == QSE_NULL)
	{
		/* if buf is not given, return the number of bytes required */
		return len;
	}

	if (size < len) return (qse_size_t)-1; /* buffer too small */
	if (size > len) buf[len] = QSE_T('\0');
	ret = len;

	t = value;
	if (t < 0) t = -t;

	while (t > 0) 
	{
		rem = t % radix;
		if (rem >= 10)
			buf[--len] = (qse_char_t)rem + QSE_T('a') - 10;
		else
			buf[--len] = (qse_char_t)rem + QSE_T('0');
		t /= radix;
	}

	if (value < 0) 
	{
		for (i = 1; i <= prefix_len; i++) 
		{
			buf[i] = prefix[i-1];
			len--;
		}
		buf[--len] = QSE_T('-');
	}
	else
	{
		for (i = 0; i < prefix_len; i++) buf[i] = prefix[i];
	}

	return ret;
}
Example #10
0
File: awk07.c Project: DeadZen/qse
static int awk_main (int argc, qse_char_t* argv[])
{
	qse_awk_t* awk = QSE_NULL;
	qse_awk_rtx_t* rtx = QSE_NULL;
	qse_awk_parsestd_t psin[2];
	qse_awk_val_t* rtv = QSE_NULL;
	qse_awk_val_t* arg = QSE_NULL;
	int ret = -1, opt;

	/* this structure is passed to qse_awk_rtx_makemapvalwithdata() */
	qse_awk_val_map_data_t md[] =
	{
		{ { QSE_T("f0"), 2 }, QSE_AWK_VAL_MAP_DATA_STR, QSE_T("linux") },
		{ { QSE_T("f1"), 2 }, QSE_AWK_VAL_MAP_DATA_STR, QSE_T("openvms") },
		{ { QSE_T("f2"), 2 }, QSE_AWK_VAL_MAP_DATA_STR, QSE_T("hpux") },
		{ { QSE_NULL,    0 }, 0, QSE_NULL } /* last item */
	};

	/* create a standard awk object */
	awk = qse_awk_openstd (0, QSE_NULL);
	if (awk == QSE_NULL)  
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open awk\n"));
		goto oops;
	}

	/* get the awk's trait */
	qse_awk_getopt (awk, QSE_AWK_TRAIT, &opt);
	/* change the trait value to disallow BEGIN, END, pattern-action blocks */
	opt &= ~QSE_AWK_PABLOCK;
	/* update the trait */
	qse_awk_setopt (awk, QSE_AWK_TRAIT, &opt);

	/* prepare a script to parse */
	psin[0].type = QSE_AWK_PARSESTD_STR;
	psin[0].u.str.ptr = src;
	psin[0].u.str.len = qse_strlen(src);
	psin[1].type = QSE_AWK_PARSESTD_NULL;

	/* parse the script */
	if (qse_awk_parsestd (awk, psin, QSE_NULL) <= -1)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
		goto oops;
	}

	/* create a standard runtime context */
	rtx = qse_awk_rtx_openstd (
		awk, 
		0,
		QSE_T("awk06"),
		QSE_NULL, /* stdin */
		QSE_NULL, /* stdout */
		QSE_NULL  /* default cmgr */
	
	);
	if (rtx == QSE_NULL) 
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
		goto oops;
	}
	
	/* create a map value to pass as an argument */
	arg = qse_awk_rtx_makemapvalwithdata (rtx, md);
	if (arg == QSE_NULL)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
		goto oops;
	}
	qse_awk_rtx_refupval (rtx, arg);
	
	/* execute the dump function in the awk script */
	rtv = qse_awk_rtx_call (rtx, QSE_T("dump"), &arg, 1);
	if (rtv == QSE_NULL)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
		goto oops;
	}

	if (qse_awk_rtx_getvaltype (rtx, rtv) == QSE_AWK_VAL_MAP)
	{
		/* if a returned value is a map, 
		 * traverse the map and print the key/value pairs. */

		qse_awk_val_map_itr_t itr;
		qse_awk_val_map_itr_t* iptr;

		/* get the iterator to the first key/value pair */
		iptr = qse_awk_rtx_getfirstmapvalitr (rtx, rtv, &itr);
		while (iptr)
		{
			qse_cstr_t str;

			/* #QSE_AWK_VAL_MAP_ITR_VAL returns the value part */
			str.ptr = qse_awk_rtx_valtostrdup (
				rtx, QSE_AWK_VAL_MAP_ITR_VAL(iptr), &str.len);
			if (str.ptr == QSE_NULL)
			{
				qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
				goto oops;
			}
	
			/* #QSE_AWK_VAL_MAP_ITR_KEY returns the key part */
			qse_printf (QSE_T("ret [%.*s]=[%.*s]\n"), 
				(int)QSE_AWK_VAL_MAP_ITR_KEY(iptr)->len, 
				QSE_AWK_VAL_MAP_ITR_KEY(iptr)->ptr,
				(int)str.len, str.ptr
			);
			qse_awk_rtx_freemem (rtx, str.ptr);
			
			/* get the iterator to the next key/value pair */
			iptr = qse_awk_rtx_getnextmapvalitr (rtx, rtv, &itr);
		}
	}
	else
	{
		/* if it is a plain value, convert it to a string
		 * and print it */
		qse_cstr_t str;

		str.ptr = qse_awk_rtx_valtostrdup (rtx, rtv, &str.len);
		if (str.ptr == QSE_NULL)
		{
			qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
			goto oops;
		}
	
		qse_printf (QSE_T("ret [%.*s]\n"), (int)str.len, str.ptr);
		qse_awk_rtx_freemem (rtx, str.ptr);
	}

	ret = 0;

oops:
	/* clear the return value */
	if (rtv) qse_awk_rtx_refdownval (rtx, rtv);

	/* dereference the argument */
	if (arg) qse_awk_rtx_refdownval (rtx, arg);

	/* destroy a runtime context */
	if (rtx) qse_awk_rtx_close (rtx);

	/* destroy the processor */
	if (awk) qse_awk_close (awk);

	return ret;
}
Example #11
0
File: awk05.c Project: DeadZen/qse
static int awk_main (int argc, qse_char_t* argv[])
{
	qse_awk_t* awk = QSE_NULL;
	qse_awk_rtx_t* rtx = QSE_NULL;
	qse_awk_parsestd_t psin[2];
	qse_char_t* str;
	qse_size_t len;
	qse_awk_val_t* rtv;
	qse_awk_val_t* arg[2] = { QSE_NULL, QSE_NULL };
	int ret = -1, i, opt;

	/* create an awk object */
	awk = qse_awk_openstd (0, QSE_NULL);
	if (awk == QSE_NULL)  
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open awk\n"));
		goto oops;
	}

	/* get the awk's trait */
	qse_awk_getopt (awk, QSE_AWK_TRAIT, &opt);
	/* change the trait value to disallow BEGIN, END, pattern-action blocks */
	opt &= ~QSE_AWK_PABLOCK;
	/* update the trait */
	qse_awk_setopt (awk, QSE_AWK_TRAIT, &opt);

	/* prepare an awk script to parse */
	psin[0].type = QSE_AWK_PARSESTD_STR;
	psin[0].u.str.ptr = src;
	psin[0].u.str.len = qse_strlen(src);
	psin[1].type = QSE_AWK_PARSESTD_NULL;

	/* parse the script */
	if  (qse_awk_parsestd (awk, psin, QSE_NULL) <= -1)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
		goto oops;
	}

	/* create a runtime context */
	rtx = qse_awk_rtx_openstd (
		awk, 
		0,
		QSE_T("awk04"), 
		QSE_NULL, /* stdin */
		QSE_NULL, /* stdout */
		QSE_NULL  /* default cmgr */
	);
	if (rtx == QSE_NULL) 
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
		goto oops;
	}
	
	/* create the first argument to the pow function to call */
	arg[0] = qse_awk_rtx_makeintval (rtx, 50);
	if (arg[0] == QSE_NULL)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
		goto oops;
	}
	qse_awk_rtx_refupval (rtx, arg[0]);

	/* create the second argument to the pow function to call */
	arg[1] = qse_awk_rtx_makeintval (rtx, 3);
	if (arg[1] == QSE_NULL)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
		goto oops;
	}
	qse_awk_rtx_refupval (rtx, arg[1]);

	/* call the pow function */
	rtv = qse_awk_rtx_call (rtx, QSE_T("pow"), arg, 2);
	if (rtv == QSE_NULL)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
		goto oops;
	}

	/* duplicate the return value to a string */
	str = qse_awk_rtx_valtostrdup (rtx, rtv, &len);

	/* clear the return value */
	qse_awk_rtx_refdownval (rtx, rtv);

	if (str == QSE_NULL)
	{
		qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
		goto oops;
	}

	qse_printf (QSE_T("[%.*s]\n"), (int)len, str);

	/* destroy the duplicated string  */
	qse_awk_rtx_freemem (rtx, str);

	ret = 0;

oops:
	/* dereference all arguments */
	for (i = 0; i < QSE_COUNTOF(arg); i++)
	{
		if (arg[i]) qse_awk_rtx_refdownval (rtx, arg[i]);
	}

	/* destroy a runtime context */
	if (rtx) qse_awk_rtx_close (rtx);

	/* destroy the processor */
	if (awk) qse_awk_close (awk);

	return ret;
}