add option execute and read data from stdout
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Wed, 20 Dec 2023 12:57:19 +0000 (13:57 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Wed, 20 Dec 2023 12:57:19 +0000 (13:57 +0100)
bf.c

diff --git a/bf.c b/bf.c
index 0e331d9c4d594c38371698cbf92100b5cf9b81c9..be9c11f2e219bb8a213c9775241533edd2671daf 100644 (file)
--- a/bf.c
+++ b/bf.c
@@ -80,7 +80,8 @@ int process (char *buffer, int nb, FILE *out) {
                 break;
             case ',': /* read a byte and store it in memory */
                 if ((p >= 0) && (p < MEMSIZE)) {
-                    mem[p] = buffer[++i];
+                    int c = getchar ();
+                    mem[p] = (c > 0) ? c : 0;
                 } else {
                     VERBOSE (ERROR, fprintf (stderr, "%s: invalid address (%d)\n", progname, p));
                     return 1;
@@ -105,6 +106,8 @@ int process (char *buffer, int nb, FILE *out) {
                     i = -1;
                 }
                 break;
+            case ' ':
+            case '\t':
             case '\n':
             case '\r':
                 break;
@@ -141,8 +144,19 @@ int main (int argc, char *argv[])
     }
 
     int c;
-    while ((c = getopt(argc, argv, "i:m:o:hv:")) != EOF) {
+    while ((c = getopt(argc, argv, "e:i:m:o:hv:")) != EOF) {
         switch (c) {
+        case 'e':
+            if (buffer) {
+                free (buffer);
+            }
+            buffer = strdup (optarg);
+            if (buffer == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: can't allocate memory\n", progname));
+                return 1;
+            }
+            size = strlen (buffer) + 1;
+            break;
         case 'i':
             input = optarg;
             break;
@@ -181,29 +195,33 @@ int main (int argc, char *argv[])
             VERBOSE (ERROR, fprintf (stderr, "%s: can't open file '%s' for reading\n", progname, input));
             return 1;
         }
-    } else {
+    } else if (buffer == NULL) {
         fid = stdin;
     }
 
     /* read input file */
-    while (!feof (fid)) {
-        buffer = (char *) realloc (buffer, size + BUFSIZE);
-        if (buffer == NULL) {
-            VERBOSE (ERROR, fprintf (stderr, "%s: can't allocate memory\n", progname));
-            return 1;
-        }
-        memset (buffer + size, 0, BUFSIZE);
-        n = fread (buffer + size, 1, BUFSIZE, fid);
-        if (errno != 0) {
-            VERBOSE (ERROR, fprintf (stderr, "%s: can't read data from file '%s'\n", progname, input));
-            return 1;
+    if (fid) {
+        while (!feof (fid)) {
+            buffer = (char *) realloc (buffer, size + BUFSIZE);
+            if (buffer == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: can't allocate memory\n", progname));
+                return 1;
+            }
+            memset (buffer + size, 0, BUFSIZE);
+            n = fread (buffer + size, 1, BUFSIZE, fid);
+            if (errno != 0) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: can't read data from file '%s'\n", progname, input));
+                return 1;
+            }
+            size += BUFSIZE;
         }
-        size += BUFSIZE;
-    }
 
-    /* close input file */
-    fclose (fid);
-    VERBOSE (DEBUG, fprintf (stderr, "%s: read %d bytes\n", progname, size + n - BUFSIZE));
+        /* close input file */
+        fclose (fid);
+        VERBOSE (DEBUG, fprintf (stderr, "%s: read %d bytes\n", progname, size + n - BUFSIZE));
+    } else {
+        VERBOSE (DEBUG, fprintf (stderr, "%s: prog %d bytes\n", progname, size -1));
+    }
 
     /* check output file */
     if (output) {
@@ -230,11 +248,16 @@ int main (int argc, char *argv[])
 // test: bf.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
 // test: bf.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: bf.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: bf.exe -i error.b 2>&1 | grep "can't open" | grep -q "reading"
+// test: echo ">>." | bf.exe -o error/error.b 2>&1 | grep "can't open" | grep -q "writing"
+// test: echo '+++>++>>-<--' | bf.exe -v2 | grep -q "memory: 3 2 -2 -1 0"
+// test: bf.exe -e '+++>++>>-<--' -v2 | grep -q "memory: 3 2 -2 -1 0"
 // test: echo '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.' | bf.exe -v1 | grep -q "Hello World!"
 // test: echo '++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++++++++++++++.>+++++++++++++++++.<<++.>+++++++++++++.>--.<<.>+++.+.--.>----.++++++.+.<++.>----.++.<<.>>+.-------.<<.>>++.<.>+++++.<<.>-.+.<.>---.>---.<-.++++++++.>----.<---.>+++++++.<---.++++++++.' | bf.exe -v1 | grep -q "Tu as decouvert un peu de brainfuck"
-// test: echo ',4>++++++[<-------->-],3[<+>-]<.' | bf.exe -v1 | grep -q 7
+// test: echo -e "123\0" | bf.exe -e ',[>,]' -v2 | grep -q "memory: 49 50 51 0"
+// test: echo -e "43\0" | bf.exe -e ',>++++++[<-------->-],[<+>-]<.' -v1 | grep -q 7
 
-// test: echo ',3>,2>>++++++++++++++++[-<+++<---<--->>>]<<[<[>>+>+<<<-]>>>[<<<+>>>-]<<-]>.' | bf.exe -v1 | grep -q 6
-// test: echo ',3>,2[->>+<<]+<[->>>[>>>>>+<<<<<->+<<]<[>]>>>>>[-<<->>>>>>>]<[>]<<<<<<]>[>>>>->]<<<<<<[-]>[-]>>[-]>.' | bf.exe -v1 | grep -q 3
+// test: echo -e "32\0" | bf.exe -e ',>,>>++++++++++++++++[-<+++<---<--->>>]<<[<[>>+>+<<<-]>>>[<<<+>>>-]<<-]>.' -v1 | grep -q 6
+// test: echo -e "32\0" | bf.exe -e ',>,[->>+<<]+<[->>>[>>>>>+<<<<<->+<<]<[>]>>>>>[-<<->>>>>>>]<[>]<<<<<<]>[>>>>->]<<<<<<[-]>[-]>>[-]>.' -v1 | grep -q 3
 
 /* vim: set ts=4 sw=4 et: */