completed subtitute command
authorMazet Laurent <mazet@softndesign.org>
Sun, 15 Jan 2023 14:22:24 +0000 (15:22 +0100)
committerMazet Laurent <mazet@softndesign.org>
Sun, 15 Jan 2023 14:22:24 +0000 (15:22 +0100)
hexdump.c

index d7faa681edc25e61ebbfecf5fa85882b471297b2..9c90d21c921cdd45f4fc5e2ed563b3ba0f5aa8d9 100644 (file)
--- a/hexdump.c
+++ b/hexdump.c
@@ -149,7 +149,7 @@ int searchseq (sequence_t *seq) {
             offset = seq->length;
             addrfile += i;
             fseek (fin, i - nb, SEEK_CUR);
-            VERBOSE (DEBUG, printf ("found sequence (%d)\n", i - nb));
+            VERBOSE (DEBUG, printf ("found sequence at 0x%0*lx\n", getnbdigits (addrfile), addrfile));
             return 0;
         }
     }
@@ -614,6 +614,9 @@ int main (int argc, char *argv[])
                 rc = gethexaseq (&seq, commands, &commands);
                 if (rc == 0) {
                     rc = insertseq (&seq);
+                    if (rc == 1) {
+                        VERBOSE (ERROR, fprintf (stderr, "can't jump (%d)\n", offset));
+                    }
                 } else {
                     VERBOSE (ERROR, fprintf (stderr, "erroneous sequence '%s'\n", seq.sequence));
                 }
@@ -654,14 +657,19 @@ int main (int argc, char *argv[])
 
             case 's': /* substitute mode */
                 if (*commands == '/') {
-                    rc = getpattern (&seq, commands, &commands);
+                    rc = getpattern (&seq, ++commands, &commands);
                     if (rc == 0) {
                         rc = searchseq (&seq);
                         if (rc == 0) {
                             fseek (fin, offset, SEEK_CUR);
                             rc = gethexaseq (&seq, commands, &commands);
+                            commands++;
                             if (rc == 0) {
+                                offset = 0;
                                 rc = insertseq (&seq);
+                                if (rc == 1) {
+                                    VERBOSE (ERROR, fprintf (stderr, "can't jump (%d)\n", offset));
+                                }
                             } else {
                                 VERBOSE (ERROR, fprintf (stderr, "erroneous sequence '%s'\n", seq.sequence));
                             }
@@ -671,6 +679,9 @@ int main (int argc, char *argv[])
                     } else {
                         VERBOSE (ERROR, fprintf (stderr, "erroneous pattern '%s'\n", seq.sequence));
                     }
+                } else {
+                    VERBOSE (ERROR, fprintf (stderr, "erroneous sequence '%s'\n", seq.sequence));
+                    rc = 1;
                 }
                 offset = 0;
                 break;
@@ -710,9 +721,10 @@ int main (int argc, char *argv[])
 // test: hexdump.exe -i hexdump.c -o ko/test.c 2>&1 | grep -q "can't open file"
 // test: cat hexdump.c | hexdump.exe -n 3 | head -2 | tail -1 | grep -q '0x000003: 64 65 70  dep'
 // test: hexdump.exe -i hexdump.c -n 3 | head -2 | tail -1 | grep -q '0x0003: 64 65 70  dep'
+// test: hexdump.exe -i hexdump.c -v 3 -n 8 | grep -q 'no command'
 // test: hexdump.exe -i hexdump.c -o test.c -e 'p 200' | tail -1 | grep -q '0x00c0:'
 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
-// test: hexdump.exe -i hexdump.c -e ' /cflags/ p 17 /debug/ p 8' | grep -q '0x0019: 2a 2f 0a 2f 2a 20 6c 69  \*\/\./\* li'
+// test: hexdump.exe -i hexdump.c -n 8 -e ' /cflags/ p 17 /debug/ p 8' | grep -q '0x0019: 2a 2f 0a 2f 2a 20 6c 69  \*\/\./\* li'
 // test: hexdump.exe -i hexdump.c -o test.c -e ' /cfl\x61gs/ p 16 /d\145bug/ p 8' | grep -q '0x0027: 64 65 62 75 67 2e 6f 20  debug.o'
 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
 // test: hexdump.exe -i hexdump.c -e ' /\n/ p 8' | grep -q '0x000d: 0a 2f 2a 20 63 66 6c 61  \./\* cfla'
@@ -721,16 +733,27 @@ int main (int argc, char *argv[])
 // test: hexdump.exe -i hexdump.c -v 3 -e " /\'/" -e ' /\"/' -e ' /\\/' -e ' /\x2a/' -e ' /\x3A/' | grep l: | wc -l | xargs test 5 =
 // test: hexdump.exe -i hexdump.c -e ' /\n\/* vim:/ p -' | grep -q ': 74 3a 20 2a 2f 0a *t: \*\/\.'
 // test: hexdump.exe -i hexdump.c -e 'p go_to_end' 2>&1 | grep -q 'unknown length'
+// test: hexdump.exe -i hexdump.c -e ' /\x41BCD/' 2>&1 | grep -q "can't find pattern"
 // test: hexdump.exe -i hexdump.c -e ' //' 2>&1 | grep -q 'erroneous pattern'
 // test: hexdump.exe -i hexdump.c -e 'foo' 2>&1 | grep -q 'unknown command'
 // test: hexdump.exe -i hexdump.c -e '0x20 p 8 64 p 8 0200 p 16' | grep -q '0x0080:'
+// test: hexdump.exe -i hexdump.c -e '07777777' 2>&1 | grep -q "can't find address"
+// test: hexdump.exe -i hexdump.c -e '0xFFFFFF' 2>&1 | grep -q "can't find address"
+// test: hexdump.exe -i hexdump.c -e '99999999' 2>&1 | grep -q "can't find address"
+// test: hexdump.exe -i hexdump.c -e '+9999999' 2>&1 | grep -q "can't find address"
 // test: hexdump.exe -i hexdump.c -e '0xg' 2>&1 | grep -q 'erroneous address'
 // test: hexdump.exe -i hexdump.c -o test.c -e ' /cflags/ a 414e5a /link/ i 2F333B'
-// test: grep -q ANZcflags test.c && grep -q 'link/3;er' test.c; x=$?; rm test.c; test x$x = x0
-// test: hexdump.exe -i hexdump.c -e ' /cflags/ a 414e5' 2>&1 | grep 'erroneous sequence'
+// test: grep -q '[A]NZcflags' test.c && grep -q '[l]ink/3;er' test.c; x=$?; rm test.c; test x$x = x0
+// test: hexdump.exe -i hexdump.c -e ' /cflags/ a 414e5' 2>&1 | grep -q 'erroneous sequence'
 // test: hexdump.exe -i hexdump.c -o test.c -e ' /lags/ d 2'
-// test: grep -q cfgs test.c; x=$?; rm test.c; test x$x = x0
+// test: grep -q '[c]fgs' test.c; x=$?; rm test.c; test x$x = x0
 // test: hexdump.exe -i hexdump.c -o test.c -e ' /lags/ +2 i 2041'
-// test: grep -q 'cf Ags' test.c; x=$?; rm test.c; test x$x = x0
+// test: grep -q '[c]fla Ags:' test.c; x=$?; rm test.c; test x$x = x0
+// test: hexdump.exe -i hexdump.c -o test.c -e ' s/lags/2041/'
+// test: grep -q '[c]f A:' test.c; x=$?; rm test.c; test x$x = x0
+// test: hexdump.exe -i hexdump.c -e ' s' 2>&1 | grep -q 'erroneous sequence'
+// test: hexdump.exe -i hexdump.c -e ' s//' 2>&1 | grep -q 'erroneous pattern'
+// test: hexdump.exe -i hexdump.c -e ' s/\x41BCD/2041/' 2>&1 | grep -q "can't find pattern"
+// test: hexdump.exe -i hexdump.c -e ' s/cflags/414e5/' 2>&1 | grep -q 'erroneous sequence'
 
 /* vim: set ts=4 sw=4 et: */