When handling the y command, or the case-changing replacement, stay a bit more in the wchar_t world, to simplify allocation and help make future changes simpler. * basicdefs.h (STREQ_LEN): Remove; no longer used. * cfg.mk (sc_prohibit_strncmp): Don’t suggest STREQ_LEN. * sed/compile.c (compile_program): * sed/execute.c (str_append_modified, translate_mb): Instead of preserving the bytes of a multibyte character, just save its wchar_t counterpart, or negative of its single-byte encoding error. * sed/debug.c (debug_print_mb): New static function. (debug_print_translation): Use it to adjust to new data structure. * sed/execute.c (translate_mb): Args are now NPAIRS and PAIR, not TRANS. Caller changed. * sed/sed.h (struct sed_cmd.translate.mb): Now a struct with an npairs and pair members, instead of a single char **. All uses changed.
474 lines
9.2 KiB
C
474 lines
9.2 KiB
C
/* GNU SED, a batch stream editor.
|
|
Copyright (C) 2018-2026 Free Software Foundation, Inc.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3, or (at your option)
|
|
any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; If not, see <https://www.gnu.org/licenses/>. */
|
|
|
|
/* Written by Assaf Gordon. */
|
|
|
|
/* debug.c: debugging functions */
|
|
|
|
#include "sed.h"
|
|
#include "basicdefs.h"
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
|
|
/* indentation level when printing the program */
|
|
static off_t block_level = 0;
|
|
|
|
|
|
void
|
|
debug_print_char (char c)
|
|
{
|
|
if (ISPRINT (c) && c != '\\')
|
|
{
|
|
putchar (c);
|
|
return;
|
|
}
|
|
|
|
putchar ('\\');
|
|
switch (c)
|
|
{
|
|
case '\a':
|
|
putchar ('a');
|
|
break;
|
|
case '\f':
|
|
putchar ('f');
|
|
break;
|
|
case '\r':
|
|
putchar ('r');
|
|
break;
|
|
case '\t':
|
|
putchar ('t');
|
|
break;
|
|
case '\v':
|
|
putchar ('v');
|
|
break;
|
|
case '\n':
|
|
putchar ('n');
|
|
break;
|
|
case '\\':
|
|
putchar ('\\');
|
|
break;
|
|
|
|
default:
|
|
printf ("o%03o", (unsigned int) c);
|
|
}
|
|
}
|
|
|
|
static void
|
|
debug_print_regex_pattern (const char *pat, idx_t len)
|
|
{
|
|
const char *p = pat;
|
|
while (len--)
|
|
{
|
|
if (*p == '/')
|
|
fputs ("\\/", stdout);
|
|
else
|
|
debug_print_char (*p);
|
|
++p;
|
|
}
|
|
}
|
|
|
|
static void
|
|
debug_print_regex_flags (const struct regex *r, bool addr)
|
|
{
|
|
if (!r)
|
|
return;
|
|
|
|
#ifdef REG_PERL
|
|
if (r->flags & REG_DOTALL) /* REG_PERL */
|
|
putchar ('s');
|
|
if (r->flags & REG_EXTENDED) /* REG_PERL */
|
|
putchar ('x');
|
|
#endif
|
|
|
|
if (r->flags & REG_ICASE)
|
|
putchar (addr ? 'I' : 'i');
|
|
if (r->flags & REG_NEWLINE)
|
|
putchar (addr ? 'M' : 'm');
|
|
}
|
|
|
|
static void
|
|
debug_print_regex (const struct regex *r)
|
|
{
|
|
if (!r)
|
|
{
|
|
/* Previous Regex */
|
|
fputs ("//", stdout);
|
|
return;
|
|
}
|
|
|
|
putchar ('/');
|
|
debug_print_regex_pattern (r->re, r->sz);
|
|
putchar ('/');
|
|
}
|
|
|
|
static void
|
|
debug_print_addr (const struct addr *a)
|
|
{
|
|
if (!a)
|
|
return;
|
|
switch (a->addr_type)
|
|
{
|
|
case ADDR_IS_NULL:
|
|
fputs ("[ADDR-NULL]", stdout);
|
|
break;
|
|
case ADDR_IS_REGEX:
|
|
debug_print_regex (a->addr_regex);
|
|
debug_print_regex_flags (a->addr_regex, true);
|
|
break;
|
|
case ADDR_IS_NUM:
|
|
printf ("%jd", a->addr_number);
|
|
break;
|
|
case ADDR_IS_NUM_MOD:
|
|
printf ("%jd~%jd", a->addr_number, a->addr_step);
|
|
break;
|
|
case ADDR_IS_STEP:
|
|
printf ("+%jd", a->addr_step);
|
|
break;
|
|
case ADDR_IS_STEP_MOD:
|
|
printf ("~%jd", a->addr_step);
|
|
break;
|
|
case ADDR_IS_LAST:
|
|
putchar ('$');
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void
|
|
debug_print_subst_replacement (const struct replacement *r)
|
|
{
|
|
enum replacement_types last_repl_type = REPL_ASIS;
|
|
|
|
if (!r)
|
|
return;
|
|
|
|
const struct replacement *p = r;
|
|
while (p)
|
|
{
|
|
if (p->repl_type != last_repl_type)
|
|
{
|
|
/* Special GNU replacements \E\U\u\L\l should be printed
|
|
BEFORE the 'prefix' .... the 'prefix' refers to being
|
|
before the backreference. */
|
|
putchar ('\\');
|
|
if (p->repl_type == 0)
|
|
putchar ('E');
|
|
else if (p->repl_type == REPL_UPPERCASE)
|
|
putchar ('U');
|
|
else if (p->repl_type == REPL_LOWERCASE)
|
|
putchar ('L');
|
|
else if ((p->repl_type & REPL_MODIFIERS) == REPL_UPPERCASE_FIRST)
|
|
putchar ('u');
|
|
else if ((p->repl_type & REPL_MODIFIERS) == REPL_LOWERCASE_FIRST)
|
|
putchar ('l');
|
|
|
|
last_repl_type = p->repl_type;
|
|
}
|
|
|
|
if (p->prefix_length)
|
|
fwrite (p->prefix, 1, p->prefix_length, stdout);
|
|
|
|
if (p->subst_id != -1)
|
|
{
|
|
if (p->subst_id == 0)
|
|
putchar ('&');
|
|
else
|
|
printf ("\\%d", p->subst_id);
|
|
}
|
|
|
|
p = p->next;
|
|
}
|
|
}
|
|
|
|
static void
|
|
debug_print_output_file (const struct output *o)
|
|
{
|
|
if (!o)
|
|
return;
|
|
|
|
fputs (o->name, stdout);
|
|
}
|
|
|
|
static void
|
|
debug_print_subst (const struct subst *s)
|
|
{
|
|
if (!s)
|
|
return;
|
|
|
|
debug_print_regex (s->regx);
|
|
debug_print_subst_replacement (s->replacement);
|
|
putchar ('/');
|
|
|
|
debug_print_regex_flags (s->regx, false);
|
|
|
|
if (s->global)
|
|
putchar ('g');
|
|
if (s->eval)
|
|
putchar ('e');
|
|
if (s->print)
|
|
putchar ('p');
|
|
if (s->numb)
|
|
printf ("%jd", s->numb);
|
|
if (s->outf)
|
|
{
|
|
putchar ('w');
|
|
debug_print_output_file (s->outf);
|
|
}
|
|
}
|
|
|
|
static void
|
|
debug_print_mb (int ch, mbstate_t *ps)
|
|
{
|
|
if (ch < 0)
|
|
{
|
|
fputc (-ch, stdout);
|
|
mbszero (ps);
|
|
}
|
|
else
|
|
{
|
|
char buf[MB_LEN_MAX];
|
|
size_t s = wcrtomb (buf, ch, ps);
|
|
fwrite (buf, 1, s, stdout);
|
|
}
|
|
}
|
|
|
|
static void
|
|
debug_print_translation (const struct sed_cmd *sc)
|
|
{
|
|
if (mb_cur_max > 1)
|
|
{
|
|
/* multibyte translation */
|
|
putchar ('/');
|
|
mbstate_t mbs; mbszero (&mbs);
|
|
for (idx_t i = 0; i < sc->x.translate.mb.npairs; i++)
|
|
debug_print_mb (sc->x.translate.mb.pair[2 * i], &mbs);
|
|
putchar ('/');
|
|
mbszero (&mbs);
|
|
for (idx_t i = 0; i < sc->x.translate.mb.npairs; i++)
|
|
debug_print_mb (sc->x.translate.mb.pair[2 * i + 1], &mbs);
|
|
putchar ('/');
|
|
}
|
|
else
|
|
{
|
|
/* unibyte translation */
|
|
putchar ('/');
|
|
for (idx_t i = 0; i < UCHAR_MAX + 1; ++i)
|
|
if (sc->x.translate.sb[i] != i)
|
|
putchar (i);
|
|
putchar ('/');
|
|
for (idx_t i = 0; i < UCHAR_MAX + 1; ++i)
|
|
if (sc->x.translate.sb[i] != i)
|
|
putchar (sc->x.translate.sb[i]);
|
|
putchar ('/');
|
|
}
|
|
}
|
|
|
|
static void
|
|
debug_print_function (const struct vector *program, const struct sed_cmd *sc)
|
|
{
|
|
if (!sc)
|
|
return;
|
|
|
|
putchar (sc->cmd);
|
|
|
|
switch (sc->cmd) /* LCOV_EXCL_BR */
|
|
{
|
|
case '=':
|
|
break;
|
|
|
|
case ':':
|
|
if (sc->x.label_name)
|
|
printf ("%s", sc->x.label_name);
|
|
break;
|
|
|
|
case '{':
|
|
break;
|
|
|
|
case '}':
|
|
break;
|
|
|
|
case '#':
|
|
/* should not happen - discarded during compilation. */
|
|
abort ();
|
|
|
|
case 'a':
|
|
case 'c':
|
|
case 'i':
|
|
fputs ("\\", stdout);
|
|
if (sc->x.cmd_txt.text_length)
|
|
fwrite (sc->x.cmd_txt.text, 1, sc->x.cmd_txt.text_length, stdout);
|
|
break;
|
|
|
|
case 'b':
|
|
case 't':
|
|
case 'T':
|
|
{
|
|
if (sc->x.jump_index < program->v_length)
|
|
{
|
|
const char *label_name = program->v[sc->x.jump_index].x.label_name;
|
|
if (label_name)
|
|
printf (" %s", label_name);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'D':
|
|
break;
|
|
|
|
case 'd':
|
|
break;
|
|
|
|
case 'e':
|
|
putchar (' ');
|
|
if (sc->x.cmd_txt.text_length)
|
|
fwrite (sc->x.cmd_txt.text, 1, sc->x.cmd_txt.text_length, stdout);
|
|
break;
|
|
|
|
case 'F':
|
|
break;
|
|
|
|
case 'g':
|
|
break;
|
|
|
|
case 'G':
|
|
break;
|
|
|
|
case 'h':
|
|
break;
|
|
|
|
case 'H':
|
|
break;
|
|
|
|
/* 'i' is lumped above with 'a' and 'c' */
|
|
|
|
case 'L':
|
|
case 'l':
|
|
case 'q':
|
|
case 'Q':
|
|
if (sc->x.int_arg != -1)
|
|
printf (" %jd", sc->x.int_arg);
|
|
break;
|
|
|
|
case 'n':
|
|
break;
|
|
|
|
case 'N':
|
|
break;
|
|
|
|
case 'P':
|
|
break;
|
|
|
|
case 'p':
|
|
break;
|
|
|
|
/* 'q','Q' are lumped above with 'L' and 'l' */
|
|
|
|
case 'r':
|
|
putchar (' ');
|
|
fputs (sc->x.readcmd.fname, stdout);
|
|
break;
|
|
|
|
case 'R':
|
|
putchar (' ');
|
|
fputs (sc->x.inf->name, stdout);
|
|
break;
|
|
|
|
case 's':
|
|
debug_print_subst (sc->x.cmd_subst);
|
|
break;
|
|
|
|
/* 't','T' are lumped above with 'b' */
|
|
|
|
case 'v':
|
|
/* should not happen - handled during compilation then discarded. */
|
|
abort ();
|
|
|
|
case 'W':
|
|
debug_print_output_file (sc->x.outf);
|
|
break;
|
|
|
|
case 'w':
|
|
debug_print_output_file (sc->x.outf);
|
|
break;
|
|
|
|
case 'x':
|
|
break;
|
|
|
|
case 'y':
|
|
debug_print_translation (sc);
|
|
break;
|
|
|
|
case 'z':
|
|
break;
|
|
|
|
default:
|
|
/* should not happen - unless missed a sed command. */
|
|
abort ();
|
|
}
|
|
}
|
|
|
|
void
|
|
debug_print_command (const struct vector *program, const struct sed_cmd *sc)
|
|
{
|
|
bool addr_bang;
|
|
if (!program)
|
|
return;
|
|
|
|
if (sc->cmd == '}')
|
|
--block_level;
|
|
|
|
for (off_t j = 0; j < block_level; j++)
|
|
fputs (" ", stdout);
|
|
|
|
debug_print_addr (sc->a1);
|
|
if (sc->a2)
|
|
putchar (',');
|
|
debug_print_addr (sc->a2);
|
|
|
|
addr_bang = sc->addr_bang;
|
|
/* Implementation detail: GNU Sed implements beginning of block
|
|
by negating the matched address and jumping if there's no match. */
|
|
if (sc->cmd == '{')
|
|
addr_bang = !addr_bang;
|
|
if (addr_bang)
|
|
putchar ('!');
|
|
|
|
if (sc->a1 || sc->a2)
|
|
putchar (' ');
|
|
|
|
debug_print_function (program, sc);
|
|
|
|
putchar ('\n');
|
|
|
|
if (sc->cmd == '{')
|
|
++block_level;
|
|
}
|
|
|
|
void
|
|
debug_print_program (const struct vector *program)
|
|
{
|
|
if (!program)
|
|
return;
|
|
|
|
block_level = 1;
|
|
puts ("SED PROGRAM:");
|
|
for (idx_t i = 0; i < program->v_length; i++)
|
|
debug_print_command (program, &program->v[i]);
|
|
block_level = 0;
|
|
}
|