Author: julianalbo
Date: Mon Aug 18 05:42:36 2008
New Revision: 30300

Modified:
   trunk/include/parrot/debugger.h
   trunk/src/debug.c

Log:
refactor PDB_script_file

Modified: trunk/include/parrot/debugger.h
==============================================================================
--- trunk/include/parrot/debugger.h     (original)
+++ trunk/include/parrot/debugger.h     Mon Aug 18 05:42:36 2008
@@ -170,6 +170,7 @@
     Interp                  *debugee;
     Interp                  *debugger;
     unsigned long           tracing;
+    FILE                    *script_file;
 } PDB_t;
 
 

Modified: trunk/src/debug.c
==============================================================================
--- trunk/src/debug.c   (original)
+++ trunk/src/debug.c   Mon Aug 18 05:42:36 2008
@@ -99,6 +99,9 @@
 /* HEADERIZER BEGIN: static */
 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will 
be lost. */
 
+static void close_script_file(PARROT_INTERP)
+        __attribute__nonnull__(1);
+
 static void debugger_cmdline(PARROT_INTERP)
         __attribute__nonnull__(1);
 
@@ -404,6 +407,16 @@
     }
 }
 
+static void
+close_script_file(PARROT_INTERP)
+{
+    TRACEDEB_MSG("Closing debugger script file");
+    if (interp->pdb->script_file) {
+        fclose(interp->pdb->script_file);
+        interp->pdb->script_file = NULL;
+    }
+}
+
 /*
 
 =item C<void Parrot_debugger_init>
@@ -619,50 +632,70 @@
 
     PARROT_ASSERT(pdb->last_command);
     PARROT_ASSERT(pdb->cur_command);
-    /* update the last command */
-    if (pdb->cur_command[0] != '\0')
-        strcpy(pdb->last_command, pdb->cur_command);
 
-    #if 0
-    /* if the program is stopped and running show the next line to run */
-    if ((pdb->state & PDB_STOPPED) && (pdb->state & PDB_RUNNING)) {
-        PDB_line_t *line = pdb->file->line;
+    if (interp->pdb->script_file) {
+        FILE *fd = interp->pdb->script_file;
+        char buf[DEBUG_CMD_BUFFER_LENGTH+1];
+        char *ptr = buf;
+        buf[0]='\0';
 
-        while (line && pdb->cur_opcode != line->opcode) {
-                line = line->next;
+        if (feof(fd)) {
+            close_script_file(interp);
+            return;
         }
+        do {
+            fgets(buf, DEBUG_CMD_BUFFER_LENGTH, fd);
+
+            /* skip spaces */
+            for (ptr = (char *)&buf; *ptr && isspace((unsigned char)*ptr); 
ptr++);
 
-        if (line) {
-            PIO_eprintf(interp, "%li  ", line->number);
-            c = pdb->file->source + line->source_offset;
+            /* avoid null blank and commented lines */
+            if (*buf == '\0' || *buf == '#')
+                continue;
+        } while (0);
 
-            while (c && (*c != '\n'))
-                PIO_eprintf(interp, "%c", *(c++));
+        buf[strlen(buf)-1]='\0';
+        /* RT #46117: handle command error and print out script line
+         *       PDB_run_command should return non-void value?
+         *       stop execution of script if fails
+         * RT #46115: avoid this verbose output? add -v flag? */
+        if (PDB_run_command(interp, buf)) {
+            IMCC_warning(interp, "script_file: "
+                "Error interpreting command (%s).\n",
+                buf);
+            close_script_file(interp);
+            return;
         }
+        strcpy(pdb->cur_command, buf);
     }
-    #endif
+    else {
 
-    i = 0;
+        /* update the last command */
+        if (pdb->cur_command[0] != '\0')
+            strcpy(pdb->last_command, pdb->cur_command);
 
-    c = pdb->cur_command;
+        i = 0;
 
-    PIO_eprintf(interp, "\n(pdb) ");
+        c = pdb->cur_command;
 
-    /* skip leading whitespace */
-    do {
-        ch = fgetc(stdin);
-    } while (isspace((unsigned char)ch) && ch != '\n');
+        PIO_eprintf(interp, "\n(pdb) ");
 
-    /* generate string (no more than buffer length) */
-    while (ch != EOF && ch != '\n' && (i < DEBUG_CMD_BUFFER_LENGTH)) {
-        c[i++] = (char)ch;
-        ch     = fgetc(stdin);
-    }
+        /* skip leading whitespace */
+        do {
+            ch = fgetc(stdin);
+        } while (isspace((unsigned char)ch) && ch != '\n');
+
+        /* generate string (no more than buffer length) */
+        while (ch != EOF && ch != '\n' && (i < DEBUG_CMD_BUFFER_LENGTH)) {
+            c[i++] = (char)ch;
+            ch     = fgetc(stdin);
+        }
 
-    c[i] = '\0';
+        c[i] = '\0';
 
-    if (ch == -1)
-        strcpy(c, "quit");
+        if (ch == -1)
+            strcpy(c, "quit");
+    }
 }
 
 /*
@@ -685,6 +718,11 @@
 
     command = nextarg(command);
 
+    /* If already executing a script, close it */
+    close_script_file(interp);
+
+    TRACEDEB_MSG("Opening debugger script file");
+
     fd = fopen(command, "r");
     if (!fd) {
         IMCC_warning(interp, "script_file: "
@@ -692,32 +730,7 @@
             command);
         return;
     }
-
-    while (!feof(fd)) {
-        line++;
-        buf[0]='\0';
-        fgets(buf, 1024, fd);
-
-        /* skip spaces */
-        for (ptr = (char *)&buf; *ptr && isspace((unsigned char)*ptr); ptr++);
-
-        /* avoid null blank and commented lines */
-        if (*buf == '\0' || *buf == '#')
-            continue;
-
-        buf[strlen(buf)-1]='\0';
-        /* RT #46117: handle command error and print out script line
-         *       PDB_run_command should return non-void value?
-         *       stop execution of script if fails
-         * RT #46115: avoid this verbose output? add -v flag? */
-        if (PDB_run_command(interp, buf)) {
-            IMCC_warning(interp, "script_file: "
-                "Error interpreting command at line %d (%s).\n",
-                line, command);
-                break;
-        }
-    }
-    fclose(fd);
+    interp->pdb->script_file = fd;
 }
 
 /*

Reply via email to