static int fnc_file_to_file (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { qse_sed_t* sed = QSE_NULL; qse_awk_val_t* retv; qse_awk_val_t* a[3]; qse_cstr_t xstr[3]; int i = 0, ret = 0; /* result = sed::file_to_file ("s/ABC/123/g", input_file, output_file [, option_string]) */ sed = qse_sed_openstdwithmmgr (qse_awk_rtx_getmmgr(rtx), 0); if (sed == QSE_NULL) { ret = -2; goto oops; } /* TODO qse_set_opt (TRAIT) using the optional parameter */ for (i = 0; i < 3; i++) { a[i] = qse_awk_rtx_getarg (rtx, i); xstr[i].ptr = qse_awk_rtx_getvalstr (rtx, a[i], &xstr[i].len); if (xstr[i].ptr == QSE_NULL) { ret = -2; goto oops; } } if (qse_sed_compstdxstr (sed, &xstr[0]) <= -1) { ret = -3; /* compile error */ goto oops; } if (qse_sed_execstdfile (sed, xstr[1].ptr, xstr[2].ptr, QSE_NULL) <= -1) { ret = -4; goto oops; } oops: retv = qse_awk_rtx_makeintval (rtx, ret); if (retv == QSE_NULL) retv = qse_awk_rtx_makeintval (rtx, -1); while (i > 0) { --i; qse_awk_rtx_freevalstr (rtx, a[i], xstr[i].ptr); } if (sed) qse_sed_close (sed); qse_awk_rtx_setretval (rtx, retv); return 0; }
static int sed_main (int argc, qse_char_t* argv[]) { qse_mmgr_t* mmgr = QSE_MMGR_GETDFL(); qse_sed_t* sed = QSE_NULL; qse_fs_t* fs = QSE_NULL; qse_size_t script_count; int ret = -1; xarg_t xarg; int xarg_inited = 0; ret = handle_args (argc, argv); if (ret <= -1) return -1; if (ret == 0) return 0; ret = -1; #if defined(QSE_BUILD_DEBUG) if (g_failmalloc > 0) { debug_mmgr.ctx = QSE_NULL; mmgr = &debug_mmgr; } else #endif if (g_memlimit > 0) { 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")); goto oops; } mmgr = &xma_mmgr; } if (g_separate && g_infile_pos > 0 && g_inplace) { fs = qse_fs_open (mmgr, 0); if (fs == QSE_NULL) { qse_fprintf (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_T("ERROR: cannot change direcotry in file system handler\n")); goto oops; } } sed = qse_sed_openstdwithmmgr (mmgr, 0, QSE_NULL); if (!sed) { qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open stream editor\n")); goto oops; } qse_sed_setopt (sed, QSE_SED_TRAIT, &g_option); if (qse_sed_compstd (sed, g_script.io, &script_count) <= -1) { const qse_sed_loc_t* errloc; const qse_char_t* target; qse_char_t exprbuf[128]; errloc = qse_sed_geterrloc(sed); if (g_script.io[script_count].type == QSE_SED_IOSTD_FILE) { target = g_script.io[script_count].u.file.path; } else { /* i dont' use QSE_SED_IOSTD_SIO for input */ QSE_ASSERT (g_script.io[script_count].type == QSE_SED_IOSTD_STR); 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_T("ERROR: cannot compile %s - %s at line %lu column %lu\n"), target, qse_sed_geterrmsg(sed), (unsigned long)errloc->line, (unsigned long)errloc->colm ); } else { qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot compile %s - %s\n"), target, qse_sed_geterrmsg(sed) ); } goto oops; } #if defined(QSE_ENABLE_SEDTRACER) if (g_trace) qse_sed_setopt (sed, QSE_SED_TRACER, trace_exec); #endif qse_memset (&xarg, 0, QSE_SIZEOF(xarg)); xarg.mmgr = qse_sed_getmmgr(sed); xarg_inited = 1; if (g_separate && g_infile_pos > 0) { /* 's' and input files are specified on the command line */ qse_sed_iostd_t out_file; qse_sed_iostd_t out_inplace; qse_sed_iostd_t* output_file = QSE_NULL; qse_sed_iostd_t* output = QSE_NULL; int inpos; /* a dash is treated specially for QSE_SED_IOSTD_FILE in * qse_sed_execstd(). so make an exception here */ if (g_output_file && qse_strcmp (g_output_file, QSE_T("-")) != 0) { out_file.type = QSE_SED_IOSTD_SIO; out_file.u.sio = qse_sio_open ( qse_sed_getmmgr(sed), 0, g_output_file, QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR ); if (out_file.u.sio == QSE_NULL) { qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), g_output_file); goto oops; } output_file = &out_file; output = output_file; } /* 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")); goto oops; } for (inpos = 0; inpos < xarg.size; inpos++) { qse_sed_iostd_t in[2]; qse_char_t* tmpl_tmpfile; in[0].type = QSE_SED_IOSTD_FILE; in[0].u.file.path = xarg.ptr[inpos]; in[0].u.file.cmgr = g_infile_cmgr; in[1].type = QSE_SED_IOSTD_NULL; tmpl_tmpfile = QSE_NULL; if (g_inplace && in[0].u.file.path) { int retried = 0; 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")); goto oops; } open_temp: out_inplace.type = QSE_SED_IOSTD_SIO; out_inplace.u.sio = qse_sio_open ( qse_sed_getmmgr(sed), 0, tmpl_tmpfile, QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_IGNOREMBWCERR | QSE_SIO_EXCLUSIVE | QSE_SIO_TEMPORARY ); if (out_inplace.u.sio == QSE_NULL) { if (retried) { qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), tmpl_tmpfile); QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile); goto oops; } else { /* retry to open the file with shorter names */ QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile); 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")); goto oops; } retried = 1; goto open_temp; } } output = &out_inplace; } if (qse_sed_execstd (sed, in, output) <= -1) { if (output) qse_sio_close (output->u.sio); if (tmpl_tmpfile) { qse_fs_delfile (fs, tmpl_tmpfile, 0); QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile); } print_exec_error (sed); goto oops; } if (tmpl_tmpfile) { QSE_ASSERT (output == &out_inplace); qse_sio_close (output->u.sio); output = output_file; 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"), tmpl_tmpfile, in[0].u.file.path, tmpl_tmpfile); QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile); goto oops; } QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile); } if (qse_sed_isstop (sed)) break; } if (output) qse_sio_close (output->u.sio); } else { int xx; qse_sed_iostd_t* in = QSE_NULL; qse_sed_iostd_t out; if (g_infile_pos > 0) { int i; const qse_char_t* tmp; /* input files are specified on the command line */ /* 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")); 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")); goto oops; } for (i = 0; i < xarg.size; i++) { in[i].type = QSE_SED_IOSTD_FILE; tmp = xarg.ptr[i]; in[i].u.file.path = tmp; in[i].u.file.cmgr = g_infile_cmgr; } in[i].type = QSE_SED_IOSTD_NULL; } if (g_output_file) { out.type = QSE_SED_IOSTD_FILE; out.u.file.path = g_output_file; out.u.file.cmgr = g_outfile_cmgr; } else { /* arrange to be able to specify cmgr. * if not for cmgr, i could simply pass QSE_NULL * to qse_sed_execstd() below like * xx = qse_sed_execstd (sed, in, QSE_NULL); */ out.type = QSE_SED_IOSTD_FILE; out.u.file.path = QSE_NULL; out.u.file.cmgr = g_outfile_cmgr; } g_sed = sed; set_intr_run (); xx = qse_sed_execstd (sed, in, &out); if (in) QSE_MMGR_FREE (qse_sed_getmmgr(sed), in); unset_intr_run (); g_sed = QSE_NULL; if (xx <= -1) { print_exec_error (sed); goto oops; } } ret = 0; oops: if (xarg_inited) purge_xarg (&xarg); if (sed) qse_sed_close (sed); if (fs) qse_fs_close (fs); if (xma_mmgr.ctx) qse_xma_close (xma_mmgr.ctx); free_scripts (); #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"), (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")); } #endif return ret; }
static int fnc_str_to_str (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { qse_sed_t* sed = QSE_NULL; qse_awk_val_t* retv; qse_awk_val_t* a[2], * tmp; qse_cstr_t xstr[2]; qse_cstr_t outstr; int i = 0, ret = 0, n; sed = qse_sed_openstdwithmmgr (qse_awk_rtx_getmmgr(rtx), 0); if (sed == QSE_NULL) { ret = -2; goto oops; } /* TODO qse_set_opt (TRAIT) using the optional parameter */ for (i = 0; i < 2; i++) { a[i] = qse_awk_rtx_getarg (rtx, i); xstr[i].ptr = qse_awk_rtx_getvalstr (rtx, a[i], &xstr[i].len); if (xstr[i].ptr == QSE_NULL) { ret = -2; goto oops; } } if (qse_sed_compstdxstr (sed, &xstr[0]) <= -1) { ret = -3; /* compile error */ goto oops; } if (qse_sed_execstdxstr (sed, &xstr[1], &outstr, QSE_NULL) <= -1) { ret = -4; goto oops; } tmp = qse_awk_rtx_makestrvalwithxstr (rtx, &outstr); qse_sed_freemem (sed, outstr.ptr); if (!tmp) { ret = -1; goto oops; } qse_awk_rtx_refupval (rtx, tmp); n = qse_awk_rtx_setrefval (rtx, (qse_awk_val_ref_t*)qse_awk_rtx_getarg (rtx, 2), tmp); qse_awk_rtx_refdownval (rtx, tmp); if (n <= -1) { ret = -5; goto oops; } oops: retv = qse_awk_rtx_makeintval (rtx, ret); if (retv == QSE_NULL) retv = qse_awk_rtx_makeintval (rtx, -1); while (i > 0) { --i; qse_awk_rtx_freevalstr (rtx, a[i], xstr[i].ptr); } if (sed) qse_sed_close (sed); qse_awk_rtx_setretval (rtx, retv); return 0; }