bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sed - problem with function 'y' - transform


From: Stepan Kasal
Subject: Re: sed - problem with function 'y' - transform
Date: Tue, 21 May 2002 13:37:32 +0000 (UTC)
User-agent: slrn/0.9.6.2 (Linux)

Hallo,

On Mon, 20 May 2002 08:54:55 -0700, Ute Kavanaugh wrote:
> # cat /tmp/sed_test
> one:two:three:four: :
> blue:yellow:green:red: :
> # cat /tmp/sed_test | ./sed.gnu -e's/:$//' -e'y/:/\n/'
> onentwonthreenfourn
> bluenyellowngreennredn

BTW: with gnu sed, you can always escape the newline with backslash:
# cat /tmp/sed_test | ./sed.gnu -e's/:$//' -e'y/:/\
/'

Even though replacement string in the `s' command can contain "\n"
to denote newline, the `y' command has no such ability.

This ability can be added by a small patch I've just wrote.
The patch (relative to GNU sed 3.02.80) is attached.

Regards,
        Stepan Kasal

--- sed-3.02.80/sed/compile.c.orig      Tue Sep  7 02:37:29 1999
+++ sed-3.02.80/sed/compile.c   Tue May 21 15:28:35 2002
@@ -1003,36 +1003,30 @@
 
        case 'y':
          {
-           unsigned char *ustring;
-           size_t len;
-           int slash;
-
-           ustring = MALLOC(YMAP_LENGTH, unsigned char);
-           for (len = 0; len < YMAP_LENGTH; len++)
-             ustring[len] = len;
-           cur_cmd->x.translate = ustring;
+           char *buf[2], *trans;
+           size_t len[2], n;
+           int slash, i;
 
            slash = inchar();
-           if ( !(b = match_slash(slash, 0, 0)) )
-             bad_prog(_(UNTERM_Y_CMD));
-           ustring = CAST(unsigned char *)get_buffer(b);
-           for (len = size_buffer(b); len; --len)
-             {
-               ch = inchar();
-               if (ch == slash)
-                 bad_prog(_(Y_CMD_LEN));
-               if (ch == '\n')
-                 bad_prog(_(UNTERM_Y_CMD));
-               if (ch == '\\')
-                 ch = inchar();
-               if (ch == EOF)
-                 bad_prog(_(BAD_EOF));
-               cur_cmd->x.translate[*ustring++] = ch;
-             }
-           free_buffer(b);
+           for (i = 0; i < 2; i++) {
+             if ( !(b = match_slash(slash, 0, 0)) )
+               bad_prog(_(UNTERM_Y_CMD));
+             buf[i] = MEMDUP(get_buffer(b), len[i] = size_buffer(b), char);
+             len[i] = normalize_text(buf[i], len[i]);
+             free_buffer(b);
+           }
+           if (len[0] != len[1])
+             bad_prog(_(Y_CMD_LEN));
+
+           trans = MALLOC(YMAP_LENGTH, char);
+           for (n = 0; n < YMAP_LENGTH; n++)
+             trans[n] = n;
+           for (n = 0; n < len[0]; n++)
+             trans[CAST(unsigned char)buf[0][n]] = buf[1][n];
+           FREE(buf[0]);
+           FREE(buf[1]);
+           cur_cmd->x.translate = trans;
 
-           if (inchar() != slash)
-             bad_prog(_(EXCESS_JUNK));
            ch = in_nonblank();
            if (ch == CLOSE_BRACE || ch == '#')
              savchar(ch);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]