[cig-commits] r15858 - doc/geodynamics.org/benchmarks/trunk

luis at geodynamics.org luis at geodynamics.org
Wed Oct 21 13:05:45 PDT 2009


Author: luis
Date: 2009-10-21 13:05:44 -0700 (Wed, 21 Oct 2009)
New Revision: 15858

Modified:
   doc/geodynamics.org/benchmarks/trunk/Makefile
   doc/geodynamics.org/benchmarks/trunk/diff.sh
   doc/geodynamics.org/benchmarks/trunk/watcher.c
Log:
Some quick refactorings in watcher.c

Also fixed missing echo in Makefile, and forwarded cli args
to colordiff in diff.sh script.

Modified: doc/geodynamics.org/benchmarks/trunk/Makefile
===================================================================
--- doc/geodynamics.org/benchmarks/trunk/Makefile	2009-10-21 20:05:38 UTC (rev 15857)
+++ doc/geodynamics.org/benchmarks/trunk/Makefile	2009-10-21 20:05:44 UTC (rev 15858)
@@ -17,7 +17,7 @@
 	@./download.py
 
 diff:
-	@Scanning for changes...
+	@echo Scanning for changes...
 	@./diff.sh
 
 clean:

Modified: doc/geodynamics.org/benchmarks/trunk/diff.sh
===================================================================
--- doc/geodynamics.org/benchmarks/trunk/diff.sh	2009-10-21 20:05:38 UTC (rev 15857)
+++ doc/geodynamics.org/benchmarks/trunk/diff.sh	2009-10-21 20:05:44 UTC (rev 15858)
@@ -4,5 +4,5 @@
 #
 diffcmd=colordiff
 for file in `cat sources.txt`; do
-    ${diffcmd} -u ${file} ./_latest/${file##./}
+    ${diffcmd} $* -u ${file} ./_latest/${file##./}
 done

Modified: doc/geodynamics.org/benchmarks/trunk/watcher.c
===================================================================
--- doc/geodynamics.org/benchmarks/trunk/watcher.c	2009-10-21 20:05:38 UTC (rev 15857)
+++ doc/geodynamics.org/benchmarks/trunk/watcher.c	2009-10-21 20:05:44 UTC (rev 15858)
@@ -80,7 +80,7 @@
 {
     if (!n)
     {
-        return n;
+        return NULL;
     }
 
     return n->next;
@@ -91,7 +91,7 @@
 {
     if (!n)
     {
-        return n;
+        return NULL;
     }
 
     n->next = next;
@@ -160,37 +160,10 @@
     l = NULL;
 }
 
-/* given an .html filename, set the file extension to .rst */
-void set_rst_extension(char *filename)
-{
-    /* 
-     * We assume filename is long enough (consists of at least ".html").
-     * Since we're passing in a buffer, we also make the modifications
-     * in-place.
-     */
-    int len = strlen(filename);
-    assert(len > 5);
-    filename[len-1] = '\0';
-    filename[len-2] = '\0';
-    filename[len-3] = 't';
-    filename[len-4] = 's';
-    filename[len-5] = 'r';
-}
 
-/* given an .rst filename, change file extension to .html */
-void set_html_extension(char *filename)
-{
-    /* We assume filename is buffer that's long enough */
-    int len = strlen(filename);
-    assert(len > 4);
-    filename[len+1] = '\0';
-    filename[len]   = 'l';
-    filename[len-1] = 'm';
-    filename[len-2] = 't';
-    filename[len-3] = 'h';
-}
+// ----------------------------------------------------------------------------
 
-/* read lines from a file, and store them in a linked list */
+/* Read lines from a file, and store them in a linked list */
 int readlines(char *filename, l_list *l)
 {
     FILE *fp;
@@ -205,7 +178,11 @@
 
     while (fgets(line, sizeof(line), fp) != NULL)
     {
-        set_rst_extension(line);
+        int len = strlen(line);
+        if (line[len-1] == '\n')
+        {
+            line[len-1] = '\0';
+        }
         l_push(l, line);
     }
 
@@ -214,7 +191,9 @@
     return 0;
 }
 
-/* return a linked list with the filenames we want to monitor */
+/* Return a linked list with the filenames we want to monitor.
+ * The caller takes responsibility for deallocating the linked list.
+ */
 l_list *readfiles()
 {
     l_list *l;
@@ -223,7 +202,7 @@
 
     l = l_alloc();
 
-    status = readlines("upload.txt", l);
+    status = readlines("sources.txt", l);
 
     if (status < 0)
     {
@@ -247,25 +226,70 @@
 
 // ----------------------------------------------------------------------------
 
-/* A simple routine to return a string for a set of flags. */
-char *flagstring(int flags)
+/* Given an .html filename, set the file extension to .rst
+ *
+ * The filename is assumed to be long enough (consists of at least ".html").
+ * Also, the string is modified in-place.
+ */
+void set_rst_extension(char *filename)
 {
-    static char ret[512];
-    char *or = "";
+    int len = strlen(filename);
+    assert(len > 5);
+    filename[len-1] = '\0';
+    filename[len-2] = '\0';
+    filename[len-3] = 't';
+    filename[len-4] = 's';
+    filename[len-5] = 'r';
+}
 
-    ret[0] = '\0'; // clear the string
 
-    if (flags & NOTE_DELETE) { strcat(ret, or); strcat(ret, "NOTE_DELETE"); or = "|"; }
-    if (flags & NOTE_WRITE)  { strcat(ret, or); strcat(ret, "NOTE_WRITE");  or = "|"; }
-    if (flags & NOTE_EXTEND) { strcat(ret, or); strcat(ret, "NOTE_EXTEND"); or = "|"; }
-    if (flags & NOTE_ATTRIB) { strcat(ret, or); strcat(ret, "NOTE_ATTRIB"); or = "|"; }
-    if (flags & NOTE_LINK)   { strcat(ret, or); strcat(ret, "NOTE_LINK");   or = "|"; }
-    if (flags & NOTE_RENAME) { strcat(ret, or); strcat(ret, "NOTE_RENAME"); or = "|"; }
-    if (flags & NOTE_REVOKE) { strcat(ret, or); strcat(ret, "NOTE_REVOKE"); or = "|"; }
+/* Given an .rst filename, change file extension to .html
+ *
+ * The filename is assumed to be long enough to support
+ * the longer .html extension.
+ */
+void set_html_extension(char *filename)
+{
+    /* We assume filename is buffer that's long enough */
+    int len = strlen(filename);
+    assert(len > 4);
+    filename[len+1] = '\0';
+    filename[len]   = 'l';
+    filename[len-1] = 'm';
+    filename[len-2] = 't';
+    filename[len-3] = 'h';
+}
 
-    return ret;
+
+void reload_browser(char *path)
+{
+    static char loaded[1024];
+    char html[1000];
+
+    /* get at path's own .html file */
+    strncpy(html, path, sizeof(html));
+    set_html_extension(html);
+
+    /* have we reloaded this .html file before? */
+    if (strncmp(loaded, html, sizeof(html)) == 0)
+    {
+        system("./reload-firefox.sh");
+    }
+    else
+    {
+        char cmd[1024];
+        snprintf(cmd, sizeof(cmd), "./reload-firefox.sh %s", html);
+        system(cmd);
+
+        // remember what we've reloaded last
+        strncpy(loaded, html, sizeof(html));
+    }
 }
 
+
+// ----------------------------------------------------------------------------
+
+/* Check flags in kevent list for an EV_ERROR */
 int errorflag(struct kevent *eventlist, int nevents)
 {
     int i;
@@ -279,7 +303,9 @@
     return 0;
 }
 
+
 // ----------------------------------------------------------------------------
+
 int main(int argc, char *argv[])
 {
     /* some loop variables */
@@ -299,16 +325,10 @@
     struct timespec timeout;
     unsigned int vnode_events;
 
-    /* for publishing */
+    /* extra options */
     int publishing = 0;
-
-    /* for reloading browser */
     int reloading = 0;
-    char loaded[1024];
 
-    /* initialize last reloaded filename to the empty string */
-    loaded[0] = '\0';
-
     /* figure out if we want to reload browser (argv[1] needs to be "-r"). */
     if (argc > 1)
     {
@@ -339,7 +359,7 @@
         n->fd = open(n->path, O_EVTONLY);
         if (n->fd <= 0)
         {
-            fprintf(stderr, "The file %s could not be opened for monitoring. ", n->path);
+            fprintf(stderr, "The file %s could not be opened for monitoring.\n", n->path);
             fprintf(stderr, "Error was: %s.\n", strerror(errno));
             exit(-1);
         }
@@ -358,18 +378,30 @@
     vnode_events = NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_RENAME;
     for (i = 0, n = f->first; n != NULL; i++, n = n->next)
     {
+        // have each node remember its own index
         n->idx = i;
-        EV_SET(&changelist[i], n->fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, vnode_events, 0, n);
+
+        // initialize kevent struct
+        EV_SET(
+            &changelist[i]      // &kev
+            , n->fd             // ident
+            , EVFILT_VNODE      // filter
+            , EV_ADD | EV_CLEAR // flags
+            , vnode_events      // fflags
+            , 0                 // data
+            , n                 // udata
+        );
     }
 
     /* set the timeout to wake us every half second */
     timeout.tv_sec = 0;             // 0 seconds
     timeout.tv_nsec = 500000000;    // 500 milliseconds
 
-    /* handle events */
+    /* start handling events */
     printf("Watching %d files...\n", num_files);
     while (1)
     {
+        /* check for any pending events */
         int event_count = kevent(kq, changelist, num_event_fds, eventlist, num_event_slots, &timeout);
 
         if ((event_count < 0) || errorflag(eventlist, num_event_slots))
@@ -389,9 +421,9 @@
              *
              * We do this because when vim saves a file, it replaces it
              * by the temporary copy it used for editing. This means our
-             * original file descriptor is stale, since original file gets
-             * renamed and then deleted. However, the file was only replaced,
-             * so we can open it again using the same path.
+             * original file descriptor is now stale, since the original
+             * file gets renamed and then deleted. However, the file was
+             * only replaced, so we can open it again using the same path.
              */
             if (fflags & NOTE_DELETE)
             {
@@ -415,40 +447,17 @@
                 close(fd_old);
             }
 
-            /* run "./generate.py" whenever any changes are detected */
-            if ((fflags & NOTE_EXTEND) ||
-                (fflags & NOTE_WRITE) ||
-                (fflags & NOTE_DELETE))
+            /* If any other changes (except NOTE_RENAME) to our source files happen,
+             * then we want to regenerate the corresponding .html file, and refresh
+             * our browser view of it.
+             */
+            if ((fflags & NOTE_EXTEND) || (fflags & NOTE_WRITE) || (fflags & NOTE_DELETE))
             {
                 /* Regenerate local .html file */
                 system("./generate.py");
 
-                /* Publish changes */
-                if (reloading && publishing)
-                {
-                    // TODO: Add command-line option (don't enable this by default).
-                    system("./publish.py");
-                }
-
-                if (reloading)
-                {
-                    char buffer[1000];
-
-                    strncpy(buffer, n->path, sizeof(buffer));
-                    set_html_extension(buffer);
-
-                    if (strncmp(loaded, buffer, sizeof(buffer)) == 0)
-                    {
-                        system("./reload-firefox.sh");
-                    }
-                    else
-                    {
-                        char cmd[1024];
-                        strncpy(loaded, buffer, sizeof(buffer));
-                        snprintf(cmd, sizeof(cmd), "./reload-firefox.sh %s", buffer);
-                        system(cmd);
-                    }
-                }
+                /* Reload browser */
+                if (reloading) { reload_browser(n->path); }
             }
         }
 



More information about the CIG-COMMITS mailing list