Recovered from cvs revision 2007-05-17 03:03:00
This commit is contained in:
@ -1,11 +1,9 @@
|
||||
/*
|
||||
* $Id: Awk.cpp,v 1.12 2007/05/14 08:40:13 bacon Exp $
|
||||
* $Id: Awk.cpp,v 1.15 2007/05/16 14:44:13 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <ase/awk/StdAwk.hpp>
|
||||
#include <ase/cmn/str.h>
|
||||
|
||||
#include <ase/utl/ctype.h>
|
||||
#include <ase/utl/stdio.h>
|
||||
#include <ase/utl/main.h>
|
||||
|
||||
@ -22,6 +20,9 @@ public:
|
||||
TestAwk (): srcInName(ASE_NULL), srcOutName(ASE_NULL),
|
||||
numConInFiles(0), numConOutFiles(0)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
heap = ASE_NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
~TestAwk ()
|
||||
@ -29,6 +30,30 @@ public:
|
||||
close ();
|
||||
}
|
||||
|
||||
int open ()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
ASE_ASSERT (heap == ASE_NULL);
|
||||
heap = HeapCreate (0, 1000000, 1000000);
|
||||
if (heap == ASE_NULL) return -1;
|
||||
#endif
|
||||
|
||||
return StdAwk::open ();
|
||||
}
|
||||
|
||||
void close ()
|
||||
{
|
||||
StdAwk::close ();
|
||||
|
||||
numConInFiles = 0;
|
||||
numConOutFiles = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
HeapDestroy (heap);
|
||||
heap = ASE_NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
int addConsoleInput (const char_t* file)
|
||||
{
|
||||
if (numConInFiles < ASE_COUNTOF(conInFile))
|
||||
@ -114,13 +139,19 @@ protected:
|
||||
|
||||
ssize_t readSource (Source& io, char_t* buf, size_t len)
|
||||
{
|
||||
if (len <= 0) return -1;
|
||||
FILE* fp = (FILE*)io.getHandle();
|
||||
ssize_t n = 0;
|
||||
|
||||
// TOOD: read more characters...
|
||||
cint_t c = ase_fgetc ((FILE*)io.getHandle());
|
||||
if (c == ASE_CHAR_EOF) return 0;
|
||||
buf[0] = (ase_char_t)c;
|
||||
return 1;
|
||||
while (n < len)
|
||||
{
|
||||
ase_cint_t c = ase_fgetc (fp);
|
||||
if (c == ASE_CHAR_EOF) break;
|
||||
|
||||
buf[n++] = c;
|
||||
if (c == ASE_T('\n')) break;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
ssize_t writeSource (Source& io, char_t* buf, size_t len)
|
||||
@ -146,164 +177,6 @@ protected:
|
||||
return len;
|
||||
}
|
||||
|
||||
// pipe io handlers
|
||||
int openPipe (Pipe& io)
|
||||
{
|
||||
Awk::Pipe::Mode mode = io.getMode();
|
||||
FILE* fp = NULL;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case Awk::Pipe::READ:
|
||||
fp = ase_popen (io.getName(), ASE_T("r"));
|
||||
break;
|
||||
case Awk::Pipe::WRITE:
|
||||
fp = ase_popen (io.getName(), ASE_T("w"));
|
||||
break;
|
||||
}
|
||||
|
||||
if (fp == NULL) return -1;
|
||||
|
||||
io.setHandle (fp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int closePipe (Pipe& io)
|
||||
{
|
||||
fclose ((FILE*)io.getHandle());
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t readPipe (Pipe& io, char_t* buf, size_t len)
|
||||
{
|
||||
FILE* fp = (FILE*)io.getHandle();
|
||||
if (ase_fgets (buf, len, fp) == ASE_NULL)
|
||||
{
|
||||
if (ferror(fp)) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ase_strlen(buf);
|
||||
}
|
||||
|
||||
ssize_t writePipe (Pipe& io, char_t* buf, size_t len)
|
||||
{
|
||||
FILE* fp = (FILE*)io.getHandle();
|
||||
size_t left = len;
|
||||
|
||||
while (left > 0)
|
||||
{
|
||||
if (*buf == ASE_T('\0'))
|
||||
{
|
||||
#if defined(ASE_CHAR_IS_WCHAR) && defined(__linux)
|
||||
if (fputc ('\0', fp) == EOF)
|
||||
#else
|
||||
if (ase_fputc (*buf, fp) == ASE_CHAR_EOF)
|
||||
#endif
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
left -= 1; buf += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined(ASE_CHAR_IS_WCHAR) && defined(__linux)
|
||||
/* fwprintf seems to return an error with the file
|
||||
* pointer opened by popen, as of this writing.
|
||||
* anyway, hopefully the following replacement
|
||||
* will work all the way. */
|
||||
int n = fprintf (fp, "%.*ls", left, buf);
|
||||
if (n >= 0)
|
||||
{
|
||||
size_t x;
|
||||
for (x = 0; x < left; x++)
|
||||
{
|
||||
if (buf[x] == ASE_T('\0')) break;
|
||||
}
|
||||
n = x;
|
||||
}
|
||||
#else
|
||||
int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf);
|
||||
#endif
|
||||
|
||||
if (n < 0 || n > left) return -1;
|
||||
left -= n; buf += n;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int flushPipe (Pipe& io) { return ::fflush ((FILE*)io.getHandle()); }
|
||||
|
||||
// file io handlers
|
||||
int openFile (File& io)
|
||||
{
|
||||
Awk::File::Mode mode = io.getMode();
|
||||
FILE* fp = NULL;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case Awk::File::READ:
|
||||
fp = ase_fopen (io.getName(), ASE_T("r"));
|
||||
break;
|
||||
case Awk::File::WRITE:
|
||||
fp = ase_fopen (io.getName(), ASE_T("w"));
|
||||
break;
|
||||
case Awk::File::APPEND:
|
||||
fp = ase_fopen (io.getName(), ASE_T("a"));
|
||||
break;
|
||||
}
|
||||
|
||||
if (fp == NULL) return -1;
|
||||
|
||||
io.setHandle (fp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int closeFile (File& io)
|
||||
{
|
||||
fclose ((FILE*)io.getHandle());
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t readFile (File& io, char_t* buf, size_t len)
|
||||
{
|
||||
FILE* fp = (FILE*)io.getHandle();
|
||||
if (ase_fgets (buf, len, fp) == ASE_NULL)
|
||||
{
|
||||
if (ferror(fp)) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ase_strlen(buf);
|
||||
}
|
||||
|
||||
ssize_t writeFile (File& io, char_t* buf, size_t len)
|
||||
{
|
||||
FILE* fp = (FILE*)io.getHandle();
|
||||
size_t left = len;
|
||||
|
||||
while (left > 0)
|
||||
{
|
||||
if (*buf == ASE_T('\0'))
|
||||
{
|
||||
if (ase_fputc (*buf, fp) == ASE_CHAR_EOF) return -1;
|
||||
left -= 1; buf += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf);
|
||||
if (n < 0 || n > left) return -1;
|
||||
left -= n; buf += n;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int flushFile (File& io) { return ::fflush ((FILE*)io.getHandle()); }
|
||||
|
||||
// console io handlers
|
||||
int openConsole (Console& io)
|
||||
{
|
||||
@ -373,14 +246,18 @@ protected:
|
||||
{
|
||||
ConTrack* t = (ConTrack*)io.getHandle();
|
||||
FILE* fp = t->handle;
|
||||
ssize_t n = 0;
|
||||
|
||||
if (ase_fgets (buf, len, fp) == ASE_NULL)
|
||||
while (n < len)
|
||||
{
|
||||
if (ferror(fp)) return -1;
|
||||
return 0;
|
||||
ase_cint_t c = ase_fgetc (fp);
|
||||
if (c == ASE_CHAR_EOF) break;
|
||||
|
||||
buf[n++] = c;
|
||||
if (c == ASE_T('\n')) break;
|
||||
}
|
||||
|
||||
return ase_strlen(buf);
|
||||
return n;
|
||||
}
|
||||
|
||||
ssize_t writeConsole (Console& io, char_t* buf, size_t len)
|
||||
@ -456,39 +333,34 @@ protected:
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// primitive operations
|
||||
void* allocMem (size_t n) { return ::malloc (n); }
|
||||
void* reallocMem (void* ptr, size_t n) { return ::realloc (ptr, n); }
|
||||
void freeMem (void* ptr) { ::free (ptr); }
|
||||
|
||||
bool_t isUpper (cint_t c) { return ase_isupper (c); }
|
||||
bool_t isLower (cint_t c) { return ase_islower (c); }
|
||||
bool_t isAlpha (cint_t c) { return ase_isalpha (c); }
|
||||
bool_t isDigit (cint_t c) { return ase_isdigit (c); }
|
||||
bool_t isXdigit (cint_t c) { return ase_isxdigit (c); }
|
||||
bool_t isAlnum (cint_t c) { return ase_isalnum (c); }
|
||||
bool_t isSpace (cint_t c) { return ase_isspace (c); }
|
||||
bool_t isPrint (cint_t c) { return ase_isprint (c); }
|
||||
bool_t isGraph (cint_t c) { return ase_isgraph (c); }
|
||||
bool_t isCntrl (cint_t c) { return ase_iscntrl (c); }
|
||||
bool_t isPunct (cint_t c) { return ase_ispunct (c); }
|
||||
cint_t toUpper (cint_t c) { return ase_toupper (c); }
|
||||
cint_t toLower (cint_t c) { return ase_tolower (c); }
|
||||
|
||||
real_t pow (real_t x, real_t y)
|
||||
void* allocMem (size_t n)
|
||||
{
|
||||
return ::pow (x, y);
|
||||
#ifdef _WIN32
|
||||
return ::HeapAlloc (heap, 0, n);
|
||||
#else
|
||||
return ::malloc (n);
|
||||
#endif
|
||||
}
|
||||
|
||||
int vsprintf (char_t* buf, size_t size, const char_t* fmt, va_list arg)
|
||||
{
|
||||
return ase_vsprintf (buf, size, fmt, arg);
|
||||
void* reallocMem (void* ptr, size_t n)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (ptr == NULL)
|
||||
return ::HeapAlloc (heap, 0, n);
|
||||
else
|
||||
return ::HeapReAlloc (heap, 0, ptr, n);
|
||||
#else
|
||||
return ::realloc (ptr, n);
|
||||
#endif
|
||||
}
|
||||
|
||||
void vdprintf (const char_t* fmt, va_list arg)
|
||||
{
|
||||
ase_vfprintf (stderr, fmt, arg);
|
||||
void freeMem (void* ptr)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
::HeapFree (heap, 0, ptr);
|
||||
#else
|
||||
::free (ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
@ -506,6 +378,10 @@ private:
|
||||
|
||||
size_t numConOutFiles;
|
||||
const char_t* conOutFile[128];
|
||||
|
||||
#ifdef _WIN32
|
||||
void* heap;
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifndef NDEBUG
|
||||
@ -683,15 +559,16 @@ int awk_main (int argc, ase_char_t* argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" int ase_main (int argc, ase_char_t* argv[])
|
||||
int ase_main (int argc, ase_achar_t* argv[])
|
||||
{
|
||||
|
||||
int n;
|
||||
|
||||
#if defined(__linux) && defined(_DEBUG)
|
||||
mtrace ();
|
||||
#endif
|
||||
|
||||
n = awk_main (argc, argv);
|
||||
n = ase_runmain (argc,argv,awk_main);
|
||||
|
||||
#if defined(__linux) && defined(_DEBUG)
|
||||
muntrace ();
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk.c,v 1.6 2007/05/13 14:57:43 bacon Exp $
|
||||
* $Id: awk.c,v 1.8 2007/05/16 09:15:14 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <ase/awk/awk.h>
|
||||
@ -46,13 +46,6 @@ struct mmgr_data_t
|
||||
};
|
||||
#endif
|
||||
|
||||
#if defined(vms) || defined(__vms)
|
||||
/* it seems that the main function should be placed in the main object file
|
||||
* in OpenVMS. otherwise, the first function in the main object file seems
|
||||
* to become the main function resulting in program start-up failure. */
|
||||
#include <ase/utl/main.c>
|
||||
#endif
|
||||
|
||||
#ifndef NDEBUG
|
||||
void ase_assert_abort (void)
|
||||
{
|
||||
@ -1096,7 +1089,7 @@ static int awk_main (int argc, ase_char_t* argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ase_main (int argc, ase_char_t* argv[])
|
||||
int ase_main (int argc, ase_achar_t* argv[])
|
||||
{
|
||||
int n;
|
||||
|
||||
@ -1109,7 +1102,7 @@ int ase_main (int argc, ase_char_t* argv[])
|
||||
#endif
|
||||
*/
|
||||
|
||||
n = awk_main (argc, argv);
|
||||
n = ase_runmain (argc, argv, awk_main);
|
||||
|
||||
#if defined(__linux) && defined(_DEBUG)
|
||||
muntrace ();
|
||||
|
Reference in New Issue
Block a user