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

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

Re: awk -v BINMODE=rw # \r is getting removed


From: Aharon Robbins
Subject: Re: awk -v BINMODE=rw # \r is getting removed
Date: Mon, 25 Aug 2008 22:46:44 +0300

Hi again. Re this:

> Date: Fri, 22 Aug 2008 14:13:52 -0500
> From: address@hidden (Tom Rodman)
> Subject: awk -v BINMODE=rw # \r is getting removed
>
> Greetings:
>
> The cygwin list has not responded.
>
> Is the problem below cygwin specific?
>
> --
> thanks/regards,
> Tom Rodman
>
> ------- Forwarded Message
>
> Date:    Thu, 21 Aug 2008 18:34:44 -0500
> From:    address@hidden (Tom Rodman)
> To:      address@hidden
> Subject: awk -v BINMODE=rw
>
> Hello:
>
> awk is removing the \r below:
>
>   ~ $ date;uname -sr
>   Thu Aug 21 18:26:08 CDT 2008
>   CYGWIN_NT-5.0 1.5.25(0.156/4/2)
>   ~ $ printf "HI\r\n"| awk -v BINMODE=rw '{print}'|/bin/od -Ad -c -toC
>   0000000   H   I  \n
>           110 111 012
>   0000003
>   ~ $ cygcheck -f /bin/gawk
>   gawk-3.1.6-1
>
> - --
> thanks,
> Tom

You can ignore my previous mail about it not being a problem on Linux.
Duh: Linux is like Unix and BINMODE is irrelevant. That's what I get
for working on this stuff at the end of a long day.

Based on Andy's investigation, I decided to tighten up the code and only
allow reasonable values. Here is a patch. Please let me know if this
works for you. If so, I'll commit to CVS and update the documentation.

Thanks

Arnold
------------------------------------------------
Mon Aug 25 22:41:47 2008  Arnold D. Robbins  <address@hidden>

        * eval.c (set_BINMODE): Tighten up the code to only allow
        certain reasonable values when setting BINMODE.

diff -u -r1.6 eval.c
--- eval.c      25 Jan 2008 10:41:54 -0000      1.6
+++ eval.c      25 Aug 2008 19:41:35 -0000
@@ -2215,9 +2215,8 @@
 set_BINMODE()
 {
        static short warned = FALSE;
-       char *p, *cp, save;
+       char *p, save;
        NODE *v;
-       int digits = FALSE;
 
        if ((do_lint || do_traditional) && ! warned) {
                warned = TRUE;
@@ -2225,39 +2224,74 @@
        }
        if (do_traditional)
                BINMODE = 0;
+       else if ((BINMODE_node->var_value->flags & NUMBER) != 0) {
+               BINMODE = (int) force_number(BINMODE_node->var_value);
+               /* Make sure the value is rational. */
+               if (BINMODE < 0)
+                       BINMODE = 0;
+               else if (BINMODE > 3)
+                       BINMODE = 3;
+       }
        else if ((BINMODE_node->var_value->flags & STRING) != 0) {
                v = BINMODE_node->var_value;
                p = v->stptr;
                save = p[v->stlen];
                p[v->stlen] = '\0';
 
-               for (cp = p; *cp != '\0'; cp++) {
-                       if (ISDIGIT(*cp)) {
-                               digits = TRUE;
+               /*
+                * Allow only one of the following:
+                * "0", "1", "2", "3",
+                * "r", "w", "rw", "wr" (case independent).
+                * ANYTHING ELSE goes to 0. So there.
+                */
+               switch (v->stlen) {
+               case 1:
+                       switch (p[0]) {
+                       case '0':
+                       case '1':
+                       case '2':
+                       case '3':
+                               BINMODE = p[0] - '0';
                                break;
-                       }
-               }
-
-               if (! digits && (BINMODE_node->var_value->flags & MAYBE_NUM) == 
0) {
-                       BINMODE = 0;
-                       if (strcmp(p, "r") == 0)
+                       case 'r':
+                       case 'R':
                                BINMODE = 1;
-                       else if (strcmp(p, "w") == 0)
+                               break;
+                       case 'w':
+                       case 'W':
                                BINMODE = 2;
-                       else if (strcmp(p, "rw") == 0 || strcmp(p, "wr") == 0)
-                               BINMODE = 3;
-
-                       if (BINMODE == 0 && v->stlen != 0) {
-                               /* arbitrary string, assume both */
-                               BINMODE = 3;
-                               warning("BINMODE: arbitrary string value 
treated as \"rw\"");
+                               break;
+                       default:
+                               goto bad_value;
+                               break;
+                       }
+                       break;
+               case 2:
+                       switch (p[0]) {
+                       case 'r':
+                       case 'R':
+                               if (p[1] == 'w' || p[1] == 'W')
+                                       BINMODE = 3;
+                               else
+                                       goto bad_value;
+                               break;
+                       case 'w':
+                       case 'W':
+                               if (p[1] == 'r' || p[1] == 'R')
+                                       BINMODE = 3;
+                               else
+                                       goto bad_value;
+                               break;
+                       break;
+               default:
+       bad_value:      BINMODE = 0;
+                       lintwarn(_("BINMODE value `%s' is invalid, treated as 
0"), p);
+                       break;
                        }
-               } else
-                       BINMODE = (int) force_number(BINMODE_node->var_value);
+               }
 
                p[v->stlen] = save;
-       } else if ((BINMODE_node->var_value->flags & NUMBER) != 0)
-               BINMODE = (int) force_number(BINMODE_node->var_value);
+       }
        else
                BINMODE = 0;            /* shouldn't happen */
 }




reply via email to

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