qse/ase/awk/extio.c

97 lines
1.6 KiB
C
Raw Normal View History

2006-06-16 07:37:27 +00:00
/*
2006-06-16 11:24:32 +00:00
* $Id: extio.c,v 1.2 2006-06-16 11:24:32 bacon Exp $
2006-06-16 07:37:27 +00:00
*/
#include <xp/awk/awk_i.h>
#ifndef XP_AWK_STAND_ALONE
#include <xp/bas/assert.h>
#include <xp/bas/string.h>
#include <xp/bas/memory.h>
#endif
/* TODO: a lot of todo's here... */
#include <tchar.h>
#include <stdio.h>
int xp_awk_readcmd (xp_awk_run_t* run, const xp_char_t* cmd, int* errnum)
{
xp_awk_cmd_t* p = run->extio.incmd;
while (p != XP_NULL)
{
if (xp_strcmp(p->name,cmd) == 0) break;
p = p->next;
}
if (p == XP_NULL)
{
p = (xp_awk_cmd_t*) xp_malloc (xp_sizeof(xp_awk_cmd_t));
if (p == XP_NULL)
{
2006-06-16 11:24:32 +00:00
*errnum = XP_AWK_ENOMEM;
2006-06-16 07:37:27 +00:00
return -1;
}
p->name = xp_strdup (cmd);
if (p->name == XP_NULL)
{
xp_free (p);
2006-06-16 11:24:32 +00:00
*errnum = XP_AWK_ENOMEM;
2006-06-16 07:37:27 +00:00
return -1;
}
p->handle = XP_NULL;
p->next = run->extio.incmd;
run->extio.incmd = p;
}
if (p->handle == XP_NULL)
{
p->handle = (void*) _tpopen (p->name, XP_T("r"));
2006-06-16 11:24:32 +00:00
if (p->handle == NULL)
2006-06-16 07:37:27 +00:00
{
2006-06-16 11:24:32 +00:00
/* this is treated as pipe open error.
* the return value of getline should be -1
*/
2006-06-16 07:37:27 +00:00
return -1;
}
}
{
xp_char_t buf[1024];
if (_fgetts (buf, xp_countof(buf), p->handle) == XP_NULL) return 0;
xp_printf(XP_TEXT("%s"), buf);
}
return 1;
}
int xp_awk_closecmd (xp_awk_run_t* run, const xp_char_t* cmd, int* errnum)
{
xp_awk_cmd_t* p = run->extio.incmd, * px = XP_NULL;
while (p != XP_NULL)
{
if (xp_strcmp(p->name,cmd) == 0)
{
fclose ((FILE*)p->handle);
p->handle = XP_NULL;
//if (opt_remove_closed_cmd)
//{
if (px != XP_NULL) px->next = p->next;
else run->extio.incmd = p->next;
xp_free (p->name);
xp_free (p);
//}
}
px = p;
p = p->next;
}
return -1;
}