started writing qse_strfmt()/qse_strxfmt() and the famaily

This commit is contained in:
2013-10-24 14:55:15 +00:00
parent 636967779d
commit 198bd714b0
14 changed files with 875 additions and 733 deletions

View File

@ -1,7 +1,6 @@
#include <qse/http/stdhttpd.h>
#include <qse/xli/stdxli.h>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
#include <qse/cmn/str.h>
#include <qse/cmn/mem.h>
@ -48,6 +47,9 @@
# include <sys/resource.h>
#endif
#define QSE_STDOUT qse_getstdout()
#define QSE_STDERR qse_getstderr()
/* --------------------------------------------------------------------- */
static qse_httpd_t* g_httpd = QSE_NULL;
@ -853,8 +855,8 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
cfg->xcfg[i] = qse_httpd_strntombsdup (httpd, ((qse_xli_str_t*)pair->val)->ptr, ((qse_xli_str_t*)pair->val)->len);
if (cfg->xcfg[i] == QSE_NULL)
{
/*qse_printf (QSE_T("ERROR in copying - %s\n"), qse_httpd_geterrmsg (httpd));*/
qse_printf (QSE_T("ERROR in copying\n"));
/*qse_putstrf (QSE_T("ERROR in copying - %s\n"), qse_httpd_geterrmsg (httpd));*/
qse_putstrf (QSE_T("ERROR in copying\n"));
return -1;
}
}
@ -870,7 +872,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
duptmp = qse_xli_dupflatstr (httpd_xtn->xli, (qse_xli_str_t*)pair->val, &duplen, &count);
if (duptmp == QSE_NULL)
{
qse_printf (QSE_T("ERROR: memory failure in copying index\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying index\n"));
return -1;
}
@ -879,7 +881,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (cfg->index.files == QSE_NULL)
{
qse_printf (QSE_T("ERROR: memory failure in copying index\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying index\n"));
return -1;
}
@ -908,7 +910,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
cgi = qse_httpd_callocmem (httpd, QSE_SIZEOF(*cgi));
if (cgi == QSE_NULL)
{
qse_printf (QSE_T("ERROR: memory failure in copying cgi\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying cgi\n"));
return -1;
}
@ -917,7 +919,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (!cgi->spec)
{
qse_httpd_freemem (httpd, cgi);
qse_printf (QSE_T("ERROR: memory failure in copying cgi name\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying cgi name\n"));
return -1;
}
if (pair->val->type == QSE_XLI_STR)
@ -940,7 +942,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
{
qse_httpd_freemem (httpd, cgi->spec);
qse_httpd_freemem (httpd, cgi);
qse_printf (QSE_T("ERROR: memory failure in copying cgi shebang\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying cgi shebang\n"));
return -1;
}
}
@ -982,7 +984,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
auth_rule = qse_httpd_callocmem (httpd, QSE_SIZEOF(*auth_rule));
if (auth_rule == QSE_NULL)
{
qse_printf (QSE_T("ERROR: memory failure in copying auth-rule\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying auth-rule\n"));
return -1;
}
@ -993,7 +995,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (!auth_rule->spec)
{
qse_httpd_freemem (httpd, auth_rule);
qse_printf (QSE_T("ERROR: memory failure in copying auth-rule\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying auth-rule\n"));
return -1;
}
}
@ -1032,7 +1034,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
mime = qse_httpd_callocmem (httpd, QSE_SIZEOF(*mime));
if (mime == QSE_NULL)
{
qse_printf (QSE_T("ERROR: memory failure in copying mime\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying mime\n"));
return -1;
}
@ -1041,7 +1043,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (!mime->spec)
{
qse_httpd_freemem (httpd, mime);
qse_printf (QSE_T("ERROR: memory failure in copying mime\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying mime\n"));
return -1;
}
@ -1050,7 +1052,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
{
qse_httpd_freemem (httpd, mime->spec);
qse_httpd_freemem (httpd, mime);
qse_printf (QSE_T("ERROR: memory failure in copying mime\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying mime\n"));
return -1;
}
@ -1097,7 +1099,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
acc = qse_httpd_callocmem (httpd, QSE_SIZEOF(*acc));
if (acc == QSE_NULL)
{
qse_printf (QSE_T("ERROR: memory failure in copying acc\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying acc\n"));
return -1;
}
@ -1108,7 +1110,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (!acc->spec)
{
qse_httpd_freemem (httpd, acc);
qse_printf (QSE_T("ERROR: memory failure in copying access\n"));
qse_putstrf (QSE_T("ERROR: memory failure in copying access\n"));
return -1;
}
}
@ -1129,7 +1131,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
/* TODO: perform more sanity check */
if (qse_mbschr (cfg->xcfg[XCFG_AUTH], QSE_MT(':')) == QSE_NULL)
{
qse_printf (QSE_T("WARNING: no colon in the auth string - [%hs]\n"), cfg->xcfg[XCFG_AUTH]);
qse_putstrf (QSE_T("WARNING: no colon in the auth string - [%hs]\n"), cfg->xcfg[XCFG_AUTH]);
}
#endif
@ -1205,8 +1207,8 @@ static int load_server_config (qse_httpd_t* httpd, qse_httpd_server_t* server, q
server_xtn->scfg[i] = qse_httpd_strntombsdup (httpd, ((qse_xli_str_t*)pair->val)->ptr, ((qse_xli_str_t*)pair->val)->len);
if (server_xtn->scfg[i] == QSE_NULL)
{
/*qse_printf (QSE_T("ERROR in copying - %s\n"), qse_httpd_geterrmsg (httpd));*/
qse_printf (QSE_T("ERROR in copying\n"));
/*qse_putstrf (QSE_T("ERROR in copying - %s\n"), qse_httpd_geterrmsg (httpd));*/
qse_putstrf (QSE_T("ERROR in copying\n"));
return -1;
}
}
@ -1329,7 +1331,7 @@ static qse_httpd_server_t* attach_server (qse_httpd_t* httpd, int num, qse_xli_l
if (pair == QSE_NULL || pair->val->type != QSE_XLI_STR)
{
/* TOOD: logging */
qse_printf (QSE_T("WARNING: no value or invalid value specified for bind\n"));
qse_putstrf (QSE_T("WARNING: no value or invalid value specified for bind\n"));
return QSE_NULL;
}
@ -1337,7 +1339,7 @@ static qse_httpd_server_t* attach_server (qse_httpd_t* httpd, int num, qse_xli_l
if (qse_strntonwad (((qse_xli_str_t*)pair->val)->ptr, ((qse_xli_str_t*)pair->val)->len, &dope.nwad) <= -1)
{
/* TOOD: logging */
qse_printf (QSE_T("WARNING: invalid value for bind - %s\n"), ((qse_xli_str_t*)pair->val)->ptr);
qse_putstrf (QSE_T("WARNING: invalid value for bind - %s\n"), ((qse_xli_str_t*)pair->val)->ptr);
return QSE_NULL;
}
@ -1351,7 +1353,7 @@ static qse_httpd_server_t* attach_server (qse_httpd_t* httpd, int num, qse_xli_l
if (xserver == QSE_NULL)
{
/* TODO: logging */
qse_printf (QSE_T("WARNING: failed to attach server\n"));
qse_putstrf (QSE_T("WARNING: failed to attach server\n"));
return QSE_NULL;
}
@ -1474,7 +1476,7 @@ static int open_config_file (qse_httpd_t* httpd)
httpd_xtn->xli = qse_xli_openstd (0, 0);
if (httpd_xtn->xli == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("Cannot open xli\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("Cannot open xli\n"));
return -1;
}
@ -1486,7 +1488,7 @@ static int open_config_file (qse_httpd_t* httpd)
{
if (qse_xli_definepair (httpd_xtn->xli, defs[i].name, &defs[i].scm) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("Cannot define %s - %s\n"), defs[i].name, qse_xli_geterrmsg(httpd_xtn->xli));
qse_sio_putstrf (QSE_STDERR, QSE_T("Cannot define %s - %s\n"), defs[i].name, qse_xli_geterrmsg(httpd_xtn->xli));
qse_xli_close (httpd_xtn->xli);
httpd_xtn->xli = QSE_NULL;
return -1;
@ -1505,12 +1507,12 @@ static int open_config_file (qse_httpd_t* httpd)
if (errloc->line > 0 || errloc->colm > 0)
{
qse_fprintf (QSE_STDERR, QSE_T("Cannot load %s at line %lu column %lu - %s\n"),
qse_sio_putstrf (QSE_STDERR, QSE_T("Cannot load %s at line %lu column %lu - %s\n"),
xli_in.u.file.path, (unsigned long int)errloc->line, (unsigned long int)errloc->colm, qse_xli_geterrmsg(httpd_xtn->xli));
}
else
{
qse_fprintf (QSE_STDERR, QSE_T("Cannot load %s - %s\n"), xli_in.u.file.path, qse_xli_geterrmsg(httpd_xtn->xli));
qse_sio_putstrf (QSE_STDERR, QSE_T("Cannot load %s - %s\n"), xli_in.u.file.path, qse_xli_geterrmsg(httpd_xtn->xli));
}
qse_xli_close (httpd_xtn->xli);
@ -1609,7 +1611,7 @@ static int load_config (qse_httpd_t* httpd)
if (pair->val->type != QSE_XLI_LIST)
{
qse_fprintf (QSE_STDERR, QSE_T("WARNING: non-list value for server\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("WARNING: non-list value for server\n"));
}
else
{
@ -1620,7 +1622,7 @@ static int load_config (qse_httpd_t* httpd)
{
if (load_server_config (httpd, server, (qse_xli_list_t*)pair->val) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("failed to load configuration from %s\n"), httpd_xtn->cfgfile);
qse_sio_putstrf (QSE_STDERR, QSE_T("failed to load configuration from %s\n"), httpd_xtn->cfgfile);
goto oops;
}
}
@ -1629,7 +1631,7 @@ static int load_config (qse_httpd_t* httpd)
if (i == 0)
{
qse_fprintf (QSE_STDERR, QSE_T("No valid server specified in %s\n"), httpd_xtn->cfgfile);
qse_sio_putstrf (QSE_STDERR, QSE_T("No valid server specified in %s\n"), httpd_xtn->cfgfile);
goto oops;
}
@ -1639,7 +1641,7 @@ static int load_config (qse_httpd_t* httpd)
{
if (load_loccfg (httpd, (qse_xli_list_t*)pair->val, &httpd_xtn->dflcfg) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("failed to load configuration from %s\n"), httpd_xtn->cfgfile);
qse_sio_putstrf (QSE_STDERR, QSE_T("failed to load configuration from %s\n"), httpd_xtn->cfgfile);
goto oops;
}
}
@ -1713,27 +1715,27 @@ static void logact_httpd (qse_httpd_t* httpd, const qse_httpd_act_t* act)
switch (act->code)
{
case QSE_HTTPD_CATCH_MERRMSG:
qse_printf (QSE_T("ERROR: %hs\n"), act->u.merrmsg);
qse_putstrf (QSE_T("ERROR: %hs\n"), act->u.merrmsg);
break;
case QSE_HTTPD_CATCH_MDBGMSG:
qse_printf (QSE_T("DEBUG: %hs\n"), act->u.mdbgmsg);
qse_putstrf (QSE_T("DEBUG: %hs\n"), act->u.mdbgmsg);
break;
case QSE_HTTPD_ACCEPT_CLIENT:
qse_nwadtostr (&act->u.client->local_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL);
qse_nwadtostr (&act->u.client->orgdst_addr, tmp2, QSE_COUNTOF(tmp2), QSE_NWADTOSTR_ALL);
qse_nwadtostr (&act->u.client->remote_addr, tmp3, QSE_COUNTOF(tmp3), QSE_NWADTOSTR_ALL);
qse_printf (QSE_T("accepted client %s(%s) from %s\n"), tmp, tmp2, tmp3);
qse_putstrf (QSE_T("accepted client %s(%s) from %s\n"), tmp, tmp2, tmp3);
case QSE_HTTPD_PURGE_CLIENT:
qse_nwadtostr (&act->u.client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL);
qse_printf (QSE_T("purged client - %s\n"), tmp);
qse_putstrf (QSE_T("purged client - %s\n"), tmp);
break;
case QSE_HTTPD_READERR_CLIENT:
qse_nwadtostr (&act->u.client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL);
qse_printf (QSE_T("failed to read client - %s\n"), tmp);
qse_putstrf (QSE_T("failed to read client - %s\n"), tmp);
break;
}
}
@ -1741,23 +1743,23 @@ static void logact_httpd (qse_httpd_t* httpd, const qse_httpd_act_t* act)
/* --------------------------------------------------------------------- */
static void print_version (void)
{
qse_printf (QSE_T("QSEHTTPD version %hs\n"), QSE_PACKAGE_VERSION);
qse_putstrf (QSE_T("QSEHTTPD version %hs\n"), QSE_PACKAGE_VERSION);
}
static void print_usage (QSE_FILE* out, int argc, qse_char_t* argv[])
static void print_usage (qse_sio_t* out, int argc, qse_char_t* argv[])
{
const qse_char_t* b = qse_basename (argv[0]);
qse_fprintf (out, QSE_T("USAGE: %s [options] -c file\n"), b);
qse_fprintf (out, QSE_T(" %s [options] --config-file file\n"), b);
qse_sio_putstrf (out, QSE_T("USAGE: %s [options] -c file\n"), b);
qse_sio_putstrf (out, QSE_T(" %s [options] --config-file file\n"), b);
qse_fprintf (out, QSE_T("options as follows:\n"));
qse_fprintf (out, QSE_T(" -h/--help show this message\n"));
qse_fprintf (out, QSE_T(" --version show version\n"));
qse_fprintf (out, QSE_T(" -c/--config-file file specify a configuration file\n"));
qse_fprintf (out, QSE_T(" -d/--daemon run in the background\n"));
qse_fprintf (out, QSE_T(" -n string specify a process name if supported\n"));
qse_fprintf (out, QSE_T(" -x output debugging messages\n"));
qse_sio_putstrf (out, QSE_T("options as follows:\n"));
qse_sio_putstrf (out, QSE_T(" -h/--help show this message\n"));
qse_sio_putstrf (out, QSE_T(" --version show version\n"));
qse_sio_putstrf (out, QSE_T(" -c/--config-file file specify a configuration file\n"));
qse_sio_putstrf (out, QSE_T(" -d/--daemon run in the background\n"));
qse_sio_putstrf (out, QSE_T(" -n string specify a process name if supported\n"));
qse_sio_putstrf (out, QSE_T(" -x output debugging messages\n"));
}
static int handle_args (int argc, qse_char_t* argv[])
@ -1785,14 +1787,14 @@ static int handle_args (int argc, qse_char_t* argv[])
goto wrongusage;
case QSE_T('?'):
qse_fprintf (QSE_STDERR,
qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: bad option - %c\n"),
opt.opt
);
goto wrongusage;
case QSE_T(':'):
qse_fprintf (QSE_STDERR,
qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: bad parameter for %c\n"),
opt.opt
);
@ -1880,7 +1882,7 @@ static int httpd_main (int argc, qse_char_t* argv[])
httpd = qse_httpd_openstd (QSE_SIZEOF(httpd_xtn_t));
if (httpd == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: Cannot open httpd\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: Cannot open httpd\n"));
goto oops;
}
@ -1896,7 +1898,7 @@ static int httpd_main (int argc, qse_char_t* argv[])
{
if (daemonize (1) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: Cannot daemonize\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: Cannot daemonize\n"));
goto oops;
}
}
@ -1927,7 +1929,7 @@ static int httpd_main (int argc, qse_char_t* argv[])
restore_signal_handlers ();
g_httpd = QSE_NULL;
if (ret <= -1) qse_fprintf (QSE_STDERR, QSE_T("Httpd error - %d\n"), qse_httpd_geterrnum (httpd));
if (ret <= -1) qse_sio_putstrf (QSE_STDERR, QSE_T("Httpd error - %d\n"), qse_httpd_geterrnum (httpd));
oops:
if (httpd) qse_httpd_close (httpd);
@ -1942,6 +1944,13 @@ int qse_main (int argc, qse_achar_t* argv[])
char locale[100];
UINT codepage;
WSADATA wsadata;
#else
/* nothing */
#endif
qse_openstdsios ();
#if defined(_WIN32)
codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
@ -1960,7 +1969,7 @@ int qse_main (int argc, qse_achar_t* argv[])
if (WSAStartup (MAKEWORD(2,0), &wsadata) != 0)
{
qse_fprintf (QSE_STDERR, QSE_T("Failed to start up winsock\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("Failed to start up winsock\n"));
return -1;
}
@ -1989,5 +1998,6 @@ int qse_main (int argc, qse_achar_t* argv[])
WSACleanup ();
#endif
qse_closestdsios ();
return ret;
}

View File

@ -27,7 +27,6 @@
#include <qse/cmn/xma.h>
#include <qse/cmn/path.h>
#include <qse/cmn/fs.h>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
#include <qse/cmn/glob.h>
@ -55,6 +54,9 @@
# include <signal.h>
#endif
#define QSE_STDOUT qse_getstdout()
#define QSE_STDERR qse_getstderr()
static struct
{
qse_sed_iostd_t* io;
@ -140,46 +142,46 @@ static qse_mmgr_t xma_mmgr =
static void print_version (void)
{
qse_printf (QSE_T("QSESED version %hs\n"), QSE_PACKAGE_VERSION);
qse_putstrf (QSE_T("QSESED version %hs\n"), QSE_PACKAGE_VERSION);
}
static void print_usage (QSE_FILE* out, int argc, qse_char_t* argv[])
static void print_usage (qse_sio_t* out, int argc, qse_char_t* argv[])
{
const qse_char_t* b = qse_basename (argv[0]);
qse_fprintf (out, QSE_T("USAGE: %s [options] script [file]\n"), b);
qse_fprintf (out, QSE_T(" %s [options] -f script-file [file]\n"), b);
qse_fprintf (out, QSE_T(" %s [options] -e script [file]\n"), b);
qse_sio_putstrf (out, QSE_T("USAGE: %s [options] script [file]\n"), b);
qse_sio_putstrf (out, QSE_T(" %s [options] -f script-file [file]\n"), b);
qse_sio_putstrf (out, QSE_T(" %s [options] -e script [file]\n"), b);
qse_fprintf (out, QSE_T("options as follows:\n"));
qse_fprintf (out, QSE_T(" -h/--help show this message\n"));
qse_fprintf (out, QSE_T(" --version show version\n"));
qse_fprintf (out, QSE_T(" -n disable auto-print\n"));
qse_fprintf (out, QSE_T(" -e script specify a script\n"));
qse_fprintf (out, QSE_T(" -f file specify a script file\n"));
qse_fprintf (out, QSE_T(" -o file specify an output file\n"));
qse_fprintf (out, QSE_T(" -r use the extended regular expression\n"));
qse_fprintf (out, QSE_T(" -R enable non-standard extensions to the regular\n"));
qse_fprintf (out, QSE_T(" expression\n"));
qse_fprintf (out, QSE_T(" -i perform in-place editing. imply -s\n"));
qse_fprintf (out, QSE_T(" -s process input files separately\n"));
qse_fprintf (out, QSE_T(" -a perform strict address and label check\n"));
qse_fprintf (out, QSE_T(" -b allow extended address formats\n"));
qse_fprintf (out, QSE_T(" <start~step>,<start,+line>,<start,~line>,<0,/regex/>\n"));
qse_fprintf (out, QSE_T(" -x allow text on the same line as c, a, i\n"));
qse_fprintf (out, QSE_T(" -y ensure a newline at text end\n"));
qse_fprintf (out, QSE_T(" -m number specify the maximum amount of memory to use in bytes\n"));
qse_fprintf (out, QSE_T(" -w expand file wildcards\n"));
qse_sio_putstrf (out, QSE_T("options as follows:\n"));
qse_sio_putstrf (out, QSE_T(" -h/--help show this message\n"));
qse_sio_putstrf (out, QSE_T(" --version show version\n"));
qse_sio_putstrf (out, QSE_T(" -n disable auto-print\n"));
qse_sio_putstrf (out, QSE_T(" -e script specify a script\n"));
qse_sio_putstrf (out, QSE_T(" -f file specify a script file\n"));
qse_sio_putstrf (out, QSE_T(" -o file specify an output file\n"));
qse_sio_putstrf (out, QSE_T(" -r use the extended regular expression\n"));
qse_sio_putstrf (out, QSE_T(" -R enable non-standard extensions to the regular\n"));
qse_sio_putstrf (out, QSE_T(" expression\n"));
qse_sio_putstrf (out, QSE_T(" -i perform in-place editing. imply -s\n"));
qse_sio_putstrf (out, QSE_T(" -s process input files separately\n"));
qse_sio_putstrf (out, QSE_T(" -a perform strict address and label check\n"));
qse_sio_putstrf (out, QSE_T(" -b allow extended address formats\n"));
qse_sio_putstrf (out, QSE_T(" <start~step>,<start,+line>,<start,~line>,<0,/regex/>\n"));
qse_sio_putstrf (out, QSE_T(" -x allow text on the same line as c, a, i\n"));
qse_sio_putstrf (out, QSE_T(" -y ensure a newline at text end\n"));
qse_sio_putstrf (out, QSE_T(" -m number specify the maximum amount of memory to use in bytes\n"));
qse_sio_putstrf (out, QSE_T(" -w expand file wildcards\n"));
#if defined(QSE_ENABLE_SEDTRACER)
qse_fprintf (out, QSE_T(" -t print command traces\n"));
qse_sio_putstrf (out, QSE_T(" -t print command traces\n"));
#endif
#if defined(QSE_BUILD_DEBUG)
qse_fprintf (out, QSE_T(" -X number fail the number'th memory allocation\n"));
qse_sio_putstrf (out, QSE_T(" -X number fail the number'th memory allocation\n"));
#endif
#if defined(QSE_CHAR_IS_WCHAR)
qse_fprintf (out, QSE_T(" --script-encoding string specify script file encoding name\n"));
qse_fprintf (out, QSE_T(" --infile-encoding string specify input file encoding name\n"));
qse_fprintf (out, QSE_T(" --outfile-encoding string specify output file encoding name\n"));
qse_sio_putstrf (out, QSE_T(" --script-encoding string specify script file encoding name\n"));
qse_sio_putstrf (out, QSE_T(" --infile-encoding string specify input file encoding name\n"));
qse_sio_putstrf (out, QSE_T(" --outfile-encoding string specify output file encoding name\n"));
#endif
}
@ -195,7 +197,7 @@ static int add_script (const qse_char_t* str, int mem)
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);
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory while processing %s\n"), str);
return -1;
}
@ -266,7 +268,7 @@ static int handle_args (int argc, qse_char_t* argv[])
goto oops;
case QSE_T('?'):
qse_fprintf (QSE_STDERR,
qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: bad option - %c\n"),
opt.opt
);
@ -274,7 +276,7 @@ static int handle_args (int argc, qse_char_t* argv[])
goto oops;
case QSE_T(':'):
qse_fprintf (QSE_STDERR,
qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: bad parameter for %c\n"),
opt.opt
);
@ -368,7 +370,7 @@ static int handle_args (int argc, qse_char_t* argv[])
g_script_cmgr = qse_findcmgr (opt.arg);
if (g_script_cmgr == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown script encoding - %s\n"), opt.arg);
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: unknown script encoding - %s\n"), opt.arg);
goto oops;
}
}
@ -377,7 +379,7 @@ static int handle_args (int argc, qse_char_t* argv[])
g_infile_cmgr = qse_findcmgr (opt.arg);
if (g_infile_cmgr == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown input file encoding - %s\n"), opt.arg);
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: unknown input file encoding - %s\n"), opt.arg);
goto oops;
}
}
@ -386,7 +388,7 @@ static int handle_args (int argc, qse_char_t* argv[])
g_outfile_cmgr = qse_findcmgr (opt.arg);
if (g_outfile_cmgr == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown output file encoding - %s\n"), opt.arg);
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: unknown output file encoding - %s\n"), opt.arg);
goto oops;
}
}
@ -425,7 +427,7 @@ void print_exec_error (qse_sed_t* sed)
const qse_sed_loc_t* errloc = qse_sed_geterrloc(sed);
if (errloc->line > 0 || errloc->colm > 0)
{
qse_fprintf (QSE_STDERR,
qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot execute - %s at line %lu column %lu\n"),
qse_sed_geterrmsg(sed),
(unsigned long)errloc->line,
@ -434,7 +436,7 @@ void print_exec_error (qse_sed_t* sed)
}
else
{
qse_fprintf (QSE_STDERR,
qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot execute - %s\n"),
qse_sed_geterrmsg(sed)
);
@ -567,16 +569,16 @@ static void trace_exec (qse_sed_t* sed, qse_sed_tracer_op_t op, const qse_sed_cm
switch (op)
{
case QSE_SED_TRACER_READ:
/*qse_fprintf (QSE_STDERR, QSE_T("reading...\n"));*/
/*qse_sio_putstrf (QSE_STDERR, QSE_T("reading...\n"));*/
break;
case QSE_SED_TRACER_WRITE:
/*qse_fprintf (QSE_STDERR, QSE_T("wrting...\n"));*/
/*qse_sio_putstrf (QSE_STDERR, QSE_T("wrting...\n"));*/
break;
/* TODO: use function to get hold space and pattern space and print them */
case QSE_SED_TRACER_MATCH:
qse_fprintf (QSE_STDERR, QSE_T("%s:%lu [%c] MA\n"),
qse_sio_putstrf (QSE_STDERR, QSE_T("%s:%lu [%c] MA\n"),
((cmd->lid && cmd->lid[0])? cmd->lid: QSE_T("<<UNKNOWN>>")),
(unsigned long)cmd->loc.line,
cmd->type
@ -584,7 +586,7 @@ static void trace_exec (qse_sed_t* sed, qse_sed_tracer_op_t op, const qse_sed_cm
break;
case QSE_SED_TRACER_EXEC:
qse_fprintf (QSE_STDERR, QSE_T("%s:%lu [%c] EC\n"),
qse_sio_putstrf (QSE_STDERR, QSE_T("%s:%lu [%c] EC\n"),
((cmd->lid && cmd->lid[0])? cmd->lid: QSE_T("<<UNKNOWN>>")),
(unsigned long)cmd->loc.line,
cmd->type
@ -709,7 +711,7 @@ static int sed_main (int argc, qse_char_t* argv[])
xma_mmgr.ctx = qse_xma_open (QSE_MMGR_GETDFL(), 0, g_memlimit);
if (xma_mmgr.ctx == QSE_NULL)
{
qse_printf (QSE_T("ERROR: cannot open memory heap\n"));
qse_putstrf (QSE_T("ERROR: cannot open memory heap\n"));
goto oops;
}
mmgr = &xma_mmgr;
@ -720,13 +722,13 @@ static int sed_main (int argc, qse_char_t* argv[])
fs = qse_fs_open (mmgr, 0);
if (fs == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open file system handler\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot open file system handler\n"));
goto oops;
}
if (qse_fs_chdir (fs, QSE_T(".")) <= -1)
{
qse_fprintf (QSE_STDERR,
qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot change direcotry in file system handler\n"));
goto oops;
}
@ -735,7 +737,7 @@ static int sed_main (int argc, qse_char_t* argv[])
sed = qse_sed_openstdwithmmgr (mmgr, 0);
if (sed == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open stream editor\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot open stream editor\n"));
goto oops;
}
@ -757,14 +759,14 @@ static int sed_main (int argc, qse_char_t* argv[])
{
/* i dont' use QSE_SED_IOSTD_SIO for input */
QSE_ASSERT (g_script.io[script_count].type == QSE_SED_IOSTD_STR);
qse_sprintf (exprbuf, QSE_COUNTOF(exprbuf),
QSE_T("expression #%lu"), (unsigned long)script_count);
qse_fmtuintmax (exprbuf, QSE_COUNTOF(exprbuf),
script_count, 10, -1, QSE_T('\0'), QSE_T("expression #"));
target = exprbuf;
}
if (errloc->line > 0 || errloc->colm > 0)
{
qse_fprintf (QSE_STDERR,
qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot compile %s - %s at line %lu column %lu\n"),
target,
qse_sed_geterrmsg(sed),
@ -774,7 +776,7 @@ static int sed_main (int argc, qse_char_t* argv[])
}
else
{
qse_fprintf (QSE_STDERR,
qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot compile %s - %s\n"),
target,
qse_sed_geterrmsg(sed)
@ -817,7 +819,7 @@ static int sed_main (int argc, qse_char_t* argv[])
);
if (out_file.u.sio == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), g_output_file);
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), g_output_file);
goto oops;
}
@ -828,7 +830,7 @@ static int sed_main (int argc, qse_char_t* argv[])
/* perform wild-card expansions for non-unix platforms */
if (expand_wildcards (argc - g_infile_pos, &argv[g_infile_pos], g_wildcard, &xarg) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops;
}
@ -850,7 +852,7 @@ static int sed_main (int argc, qse_char_t* argv[])
tmpl_tmpfile = qse_strdup2 (in[0].u.file.path, QSE_T(".XXXX"), qse_sed_getmmgr(sed));
if (tmpl_tmpfile == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops;
}
@ -870,7 +872,7 @@ static int sed_main (int argc, qse_char_t* argv[])
{
if (retried)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), tmpl_tmpfile);
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), tmpl_tmpfile);
QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile);
goto oops;
}
@ -881,7 +883,7 @@ static int sed_main (int argc, qse_char_t* argv[])
tmpl_tmpfile = qse_strdup (QSE_T("TMP-XXXX"), qse_sed_getmmgr(sed));
if (tmpl_tmpfile == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops;
}
retried = 1;
@ -913,7 +915,7 @@ static int sed_main (int argc, qse_char_t* argv[])
if (qse_fs_move (fs, tmpl_tmpfile, in[0].u.file.path) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot rename %s to %s. not deleting %s\n"),
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot rename %s to %s. not deleting %s\n"),
tmpl_tmpfile, in[0].u.file.path, tmpl_tmpfile);
QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile);
goto oops;
@ -943,14 +945,14 @@ static int sed_main (int argc, qse_char_t* argv[])
/* perform wild-card expansions for non-unix platforms */
if (expand_wildcards (argc - g_infile_pos, &argv[g_infile_pos], g_wildcard, &xarg) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops;
}
in = QSE_MMGR_ALLOC (qse_sed_getmmgr(sed), QSE_SIZEOF(*in) * (xarg.size + 1));
if (in == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops;
}
@ -1008,13 +1010,13 @@ oops:
#if defined(QSE_BUILD_DEBUG)
if (g_failmalloc > 0)
{
qse_fprintf (QSE_STDERR, QSE_T("\n"));
qse_fprintf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\n"));
qse_fprintf (QSE_STDERR, QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"),
qse_sio_putstrf (QSE_STDERR, QSE_T("\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"),
(unsigned long)debug_mmgr_alloc_count,
(unsigned long)debug_mmgr_free_count,
(unsigned long)debug_mmgr_realloc_count);
qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n"));
qse_sio_putstrf (QSE_STDERR, QSE_T("-------------------------------------------------------\n"));
}
#endif
return ret;
@ -1022,9 +1024,18 @@ oops:
int qse_main (int argc, qse_achar_t* argv[])
{
int ret;
#if defined(_WIN32)
char locale[100];
UINT codepage = GetConsoleOutputCP();
UINT codepage;
#else
/* nothing */
#endif
qse_openstdsios ();
#if defined(_WIN32)
codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
@ -1043,6 +1054,9 @@ int qse_main (int argc, qse_achar_t* argv[])
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
#endif
return qse_runmain (argc, argv, sed_main);
ret = qse_runmain (argc, argv, sed_main);
qse_closestdsios ();
return ret;
}