[GRASS-SVN] r64410 - grass/trunk/display/d.mon

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Feb 3 02:26:58 PST 2015


Author: martinl
Date: 2015-02-03 02:26:58 -0800 (Tue, 03 Feb 2015)
New Revision: 64410

Modified:
   grass/trunk/display/d.mon/list.c
   grass/trunk/display/d.mon/proto.h
   grass/trunk/display/d.mon/start.c
   grass/trunk/display/d.mon/stop.c
Log:
d.mon: rewrite to use GRASS_RENDER_COMMAND mechanism


Modified: grass/trunk/display/d.mon/list.c
===================================================================
--- grass/trunk/display/d.mon/list.c	2015-02-03 10:20:28 UTC (rev 64409)
+++ grass/trunk/display/d.mon/list.c	2015-02-03 10:26:58 UTC (rev 64410)
@@ -1,39 +1,57 @@
 #include <string.h>
 #include <stdlib.h>
+#include <dirent.h>
+
 #include <grass/gis.h>
 #include <grass/glocale.h>
+
 #include "proto.h"
 
+/* get monitor path */
+char *get_path(const char *name, int fpath)
+{
+    char tmpdir[GPATH_MAX];
+    
+    G_temp_element(tmpdir);
+    strcat(tmpdir, "/");
+    strcat(tmpdir, "MONITORS");
+    if (name) {
+        strcat(tmpdir, "/");
+        strcat(tmpdir, name);
+    }
+
+    if (fpath) {
+        char ret[GPATH_MAX];
+        
+        G_file_name(ret, tmpdir, NULL, G_mapset());
+        return G_store(ret);
+    }
+    
+    return G_store(tmpdir);
+}
+
 /* get list of running monitors */
 void list_mon(char ***list, int *n)
 {
-    int i;
-    const char *name;
-    const char *env_prefix = "MONITOR_";
-    int env_prefix_len;
-    char **tokens;
-    
-    env_prefix_len = strlen(env_prefix);
-    
+    char *mon_path;
+    struct dirent *dp;
+    DIR *dirp;
+
+    mon_path = get_path(NULL, TRUE);
+    dirp = opendir(mon_path);
+
     *list = NULL;
     *n    = 0;
-    tokens = NULL;
-    for (i = 0; (name = G_get_env_name(i)); i++) {
-	if (strncmp(env_prefix, name, env_prefix_len) == 0) {
-	    tokens = G_tokenize(name, "_");
-	    if (G_number_of_tokens(tokens) != 3 ||
-		strcmp(tokens[2], "ENVFILE") != 0)
-		continue;
-	    *list = G_realloc(*list, (*n + 1) * sizeof(char *));
-	    /* GRASS variable names are upper case, but monitor names are lower
-	     * case. */
-	    (*list)[*n] = G_store_lower(tokens[1]);
-	    (*n)++;
-	    G_free_tokens(tokens);
-	    tokens = NULL;
-	}
+    while ((dp = readdir(dirp)) != NULL) {
+        *list = G_realloc(*list, (*n + 1) * sizeof(char *));
+        if (!dp->d_name || dp->d_name[0] == '.')
+            continue;
+        (*list)[*n] = dp->d_name;
+        (*n)++;
     }
+    closedir(dirp);
     
+    G_free(mon_path);
 }
 
 /* print list of running monitors */
@@ -57,39 +75,36 @@
 /* check if monitor is running */
 int check_mon(const char *name)
 {
-    char *env_name;
-    const char *str;
+    char **list;
+    int   i, n;
+
+    list_mon(&list, &n);
     
-    env_name = NULL;
-    G_asprintf(&env_name, "MONITOR_%s_ENVFILE", G_store_upper(name));
-    str = G_getenv_nofatal(env_name);
-    if (!str)
-	return FALSE;
+    for (i = 0; i < n; i++)
+        if (G_strcasecmp(list[i], name) == 0)
+            return TRUE;
     
-    return TRUE;
+    return FALSE;
 }
 
 /* list related commands for given monitor */
 void list_cmd(const char *name, FILE *fd_out)
 {
-    char buf[1024];
-    char *cmd_name;
-    const char *cmd_value;
+    char *mon_path;
+    char cmd_file[GPATH_MAX], buf[4096];
     FILE *fd;
-
-    cmd_name = NULL;
-    G_asprintf(&cmd_name, "MONITOR_%s_CMDFILE", G_store_upper(name));
-    cmd_value = G_getenv_nofatal(cmd_name);
-    if (!cmd_value)
-	G_fatal_error(_("Command file not found"));
     
-    fd = fopen(cmd_value, "r");
+    mon_path = get_path(name, FALSE);
+    G_file_name(cmd_file, mon_path, "cmd", G_mapset());
+    fd = fopen(cmd_file, "r");
     if (!fd)
-	G_fatal_error(_("Unable to read command file"));
+	G_fatal_error(_("Unable to open file '%s'"), cmd_file);
 
     while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
 	fprintf(fd_out, "%s\n", buf);
     }
     
     fclose(fd);
+
+    G_free(mon_path);
 }

Modified: grass/trunk/display/d.mon/proto.h
===================================================================
--- grass/trunk/display/d.mon/proto.h	2015-02-03 10:20:28 UTC (rev 64409)
+++ grass/trunk/display/d.mon/proto.h	2015-02-03 10:26:58 UTC (rev 64410)
@@ -13,3 +13,4 @@
 void print_list(FILE *);
 int check_mon(const char *);
 void list_cmd(const char *, FILE *);
+char *get_path(const char *, int);

Modified: grass/trunk/display/d.mon/start.c
===================================================================
--- grass/trunk/display/d.mon/start.c	2015-02-03 10:20:28 UTC (rev 64409)
+++ grass/trunk/display/d.mon/start.c	2015-02-03 10:26:58 UTC (rev 64410)
@@ -7,26 +7,30 @@
 
 #include "proto.h"
 
-static void start(const char *, const char *, int);
-static void start_wx(const char *, const char *, const char *,
-		     const char *, int, int, int);
+static char *start(const char *, const char *, int);
+static char *start_wx(const char *, const char *, int, int, int);
 static void error_handler(void *);
 
 /* start file-based monitor */
-void start(const char *name, const char *output, int update)
+char *start(const char *name, const char *output, int update)
 {
-    char *env_name, output_path[GPATH_MAX];
+    char *output_path;
     const char *output_name;
     
     /* stop monitor on failure */
     G_add_error_handler(error_handler, (char *)name);
     
+    /* full path for output file */
+    output_path = (char *) G_malloc(GPATH_MAX);
+    output_path[0] = '\0';
+    
     if (!output) {
+        setenv("GRASS_RENDER_IMMEDIATE", name, 1);
         D_open_driver();
         
         output_name = D_get_file();
         if (!output_name) 
-            return;
+            return NULL;
         if (!update && access(output_name, F_OK) == 0) {
             if (G_get_overwrite()) {
                 G_warning(_("File '%s' already exists and will be overwritten"), output_name);
@@ -41,6 +45,7 @@
         }
         D_close_driver(); /* must be called after check because this
                            * function produces default map file */
+        unsetenv("GRASS_RENDER_IMMEDIATE");
     }
     else {
         output_name = output;
@@ -49,7 +54,7 @@
         
     if (!strchr(output_name, HOST_DIRSEP)) { /* relative path */
         char *ptr;
-        
+    
         if (!getcwd(output_path, GPATH_MAX))
             G_fatal_error(_("Unable to get current working directory"));
         ptr = output_path + strlen(output_path) - 1;
@@ -64,26 +69,20 @@
         strcpy(output_path, output_name); /* already full path */
     }
 
-    env_name = NULL;
-    G_asprintf(&env_name, "MONITOR_%s_MAPFILE", G_store_upper(name));
-    G_setenv(env_name, output_path);
+    return output_path;
 }
 
 /* start wxGUI display monitor */
-void start_wx(const char *name, const char *tempfile,
-	      const char *env_value, const char *cmd_value,
-	      int width, int height, int x_only)
+char *start_wx(const char *name, const char *element,
+               int width, int height, int x_only)
 {
     char progname[GPATH_MAX];
-    char *env_name, *map_value, str_width[1024], str_height[1024], *str_x_only;
-
-    env_name = NULL;
-    G_asprintf(&env_name, "MONITOR_%s_MAPFILE", G_store_upper(name));
-    G_asprintf(&map_value, "%s.ppm", tempfile);
-    G_setenv(env_name, map_value);
-    /* close(creat(map_value, 0666)); */
+    char str_width[1024], str_height[1024], *str_x_only;
+    char *mapfile;
     
-    G_debug(3, "       mapfile = %s", map_value);
+    /* full path */
+    mapfile = (char *) G_malloc(GPATH_MAX);
+    mapfile[0] = '\0';
 
     sprintf(progname, "%s/gui/wxpython/mapdisp/main.py", G_gisbase());
     if (width > 0)
@@ -101,18 +100,23 @@
         str_x_only = "0";
 
     G_spawn_ex(getenv("GRASS_PYTHON"), progname, progname,
-	       name, map_value, cmd_value, env_value,
-               str_width, str_height, str_x_only, SF_BACKGROUND, NULL);
+	       name, element, str_width, str_height, str_x_only, SF_BACKGROUND, NULL);
+
+    G_file_name(mapfile, element, "ppm", G_mapset());
+    
+    return mapfile;
 }
 
 int start_mon(const char *name, const char *output, int select,
 	      int width, int height, const char *bgcolor,
 	      int truecolor, int x_only, int update)
 {
-    char *u_name;
-    char *env_name, *env_value, *cmd_value;
-    char *tempfile, buf[1024];
-    int env_fd;
+    char *mon_path;
+    char *out_file, *env_file, *cmd_file;
+    char  buf[1024];
+    char file_path[GPATH_MAX];
+    char *pycode;
+    int  fd;
 
     if (check_mon(name)) {
         const char *curr_mon;
@@ -124,71 +128,107 @@
         G_fatal_error(_("Monitor <%s> already running"), name);
     }
 
-    tempfile = G_tempfile();
+    G_verbose_message(_("Starting monitor <%s>..."), name);
+    
+    /* create .tmp/HOSTNAME/u_name directory */
+    mon_path = get_path(name, FALSE);
+    G_make_mapset_element(mon_path);
+    
+    G_file_name(file_path, mon_path, "env", G_mapset());
+    env_file = G_store(file_path);
+    G_file_name(file_path, mon_path, "cmd", G_mapset());
+    cmd_file = G_store(file_path);
 
-    u_name = G_store_upper(name);
+    /* create py file (renderer) */
+    G_file_name(file_path, mon_path, "render.py", G_mapset());
+    G_debug(1, "Monitor name=%s, pyfile = %s", name, file_path);
+    fd = creat(file_path, 0666);
+    G_asprintf(&pycode,
+               "#!/usr/bin/env python\n\n"
+               "import os\n"
+               "import sys\n\n"
+               "from grass.script import core as grass\n"
+               "from grass.script import task as gtask\n\n"
+               "cmd, dcmd = gtask.cmdstring_to_tuple(sys.argv[1])\n"
+               "if not cmd or cmd == 'd.mon':\n"
+               "    sys.exit(0)\n\n"
+               "mode = 'w' if cmd == 'd.erase' else 'a'\n"
+               "fd = open('%s', mode)\n"
+               "if fd is None:\n"
+               "    grass.fatal(\"Unable to open file '%s'\")\n"
+               "if mode == 'a':\n"
+               "    fd.write(sys.argv[1])\n"
+               "    fd.write('\\n')\n"
+               "else:\n"
+               "    fd.write('')\n"
+               "fd.close()\n\n"
+               "fd = open('%s', 'r')\n"
+               "if fd is None:\n"
+               "    grass.fatal(\"Unable to open file '%s'\")\n"
+               "lines = fd.readlines()\n"
+               "for l in lines:\n"
+               "    k, v = l.rstrip('\\n').split('=')\n"
+               "    os.environ[k] = v\n"
+               "fd.close()\n\n"
+               "grass.run_command(cmd, **dcmd)\n"
+               "sys.exit(0)\n",
+               cmd_file, cmd_file, env_file, env_file);
+    write(fd, pycode, strlen(pycode));
+    G_free(pycode);
+    close(fd);
 
-    env_name = env_value = NULL;
-    G_asprintf(&env_name, "MONITOR_%s_ENVFILE", u_name);
-    G_asprintf(&env_value, "%s.env", tempfile);
-    G_setenv(env_name, env_value);
-    env_fd = creat(env_value, 0666);
-    if (env_fd < 0)
-	G_fatal_error(_("Unable to create file '%s'"), env_value);
-
+    /* start monitor */
+    if (strncmp(name, "wx", 2) == 0) 
+        out_file = start_wx(name, mon_path, width, height, x_only);
+    else
+        out_file = start(name, output, update);
+    
+    /* create env file (environmental variables used for rendering) */
+    G_debug(1, "Monitor name=%s, envfile=%s", name, env_file);
+    fd = creat(env_file, 0666);
+    if (fd < 0)
+	G_fatal_error(_("Unable to create file '%s'"), env_file);
+    
+    sprintf(buf, "GRASS_RENDER_IMMEDIATE=%s\n", name);
+    write(fd, buf, strlen(buf));
+    sprintf(buf, "GRASS_RENDER_FILE=%s\n", out_file);
+    write(fd, buf, strlen(buf));
     sprintf(buf, "GRASS_RENDER_FILE_READ=TRUE\n");
-    write(env_fd, buf, strlen(buf));
+    write(fd, buf, strlen(buf));
     if (width) {
 	sprintf(buf, "GRASS_RENDER_WIDTH=%d\n", width);
-	write(env_fd, buf, strlen(buf));
+	write(fd, buf, strlen(buf));
     }
     if (height) {
 	sprintf(buf, "GRASS_RENDER_HEIGHT=%d\n", height);
-	write(env_fd, buf, strlen(buf));
+	write(fd, buf, strlen(buf));
     }
     if (bgcolor) {
 	if (strcmp(bgcolor, "none") == 0)
 	    sprintf(buf, "GRASS_RENDER_TRANSPARENT=TRUE\n");
 	else
 	    sprintf(buf, "GRASS_RENDER_BACKGROUNDCOLOR=%s\n", bgcolor);
-	write(env_fd, buf, strlen(buf));
+	write(fd, buf, strlen(buf));
     }
     if (truecolor) {
 	sprintf(buf, "GRASS_RENDER_TRUECOLOR=TRUE\n");
-	write(env_fd, buf, strlen(buf));
+	write(fd, buf, strlen(buf));
     }
-    close(env_fd);
+    close(fd);
+   
+    /* create cmd file (list of GRASS display commands to render) */
+    G_debug(1, "Monitor name=%s, cmdfile = %s", name, cmd_file);
+    if (0 > creat(cmd_file, 0666))
+        G_fatal_error(_("Unable to create file '%s'"), cmd_file);
 
-    cmd_value = NULL;
-    G_asprintf(&env_name, "MONITOR_%s_CMDFILE", u_name);
-    G_asprintf(&cmd_value, "%s.cmd", tempfile);
-    G_setenv(env_name, cmd_value);
-    close(creat(cmd_value, 0666));
-
-    G_verbose_message(_("Starting monitor <%s> with env file '%s'"), name, env_value);
-    if (G_verbose() > G_verbose_std()) {
-        FILE *fd;
-        
-        fd = fopen(env_value, "r");
-        while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
-            fprintf(stderr, " %s\n", buf);
-        }
-        fclose(fd);
-    }
-
-    G_debug(1, "start: name=%s ", name);
-    G_debug(3, "       envfile = %s", env_value);
-    G_debug(3, "       cmdfile = %s", cmd_value);
-    
+    /* select monitor if requested */
     if (select)
 	G_setenv("MONITOR", name);
-    
-    if (strncmp(name, "wx", 2) == 0) 
-	start_wx(name, tempfile, env_value, cmd_value, 
-		 width, height, x_only);
-    else
-      start(name, output, update);
-    
+   
+    G_free(mon_path);
+    G_free(out_file);
+    G_free(env_file);
+
     return 0;
 }
 

Modified: grass/trunk/display/d.mon/stop.c
===================================================================
--- grass/trunk/display/d.mon/stop.c	2015-02-03 10:20:28 UTC (rev 64409)
+++ grass/trunk/display/d.mon/stop.c	2015-02-03 10:26:58 UTC (rev 64410)
@@ -1,20 +1,20 @@
 #include <stdlib.h>
 #include <signal.h>
 #include <string.h>
+#include <unistd.h>
+#include <dirent.h>
 
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
 #include "proto.h"
 
-static void clean_env(const char *);
 static int stop_wx(const char *);
 static int stop(const char *);
 
 int stop_mon(const char *name)
 {
     if (!check_mon(name)) {
-	clean_env(name);
 	G_fatal_error(_("Monitor <%s> is not running"), name);
     }
     
@@ -26,18 +26,29 @@
 
 int stop(const char *name)
 {
-    char *env_name;
-    const char *env_file;
+    char *mon_path, file_path[GPATH_MAX];
+    struct dirent *dp;
+    DIR *dirp;
 
-    env_name = NULL;
-    G_asprintf(&env_name, "MONITOR_%s_ENVFILE", G_store_upper(name));
+    mon_path = get_path(name, TRUE);
+    dirp = opendir(mon_path);
+
+    while ((dp = readdir(dirp)) != NULL) {
+        if (!dp->d_name || dp->d_name[0] == '.')
+            continue;
+        sprintf(file_path, "%s/%s", mon_path, dp->d_name);
+        if (unlink(file_path) == -1)
+            G_warning(_("Unable to delete file '%s'"), file_path);
+    }
+    closedir(dirp);
     
-    env_file = G_getenv_nofatal(env_name);
-    if (!env_file)
-	G_warning(_("Env file not found"));
-    
-    clean_env(name);
+    if (rmdir(mon_path) == -1)
+        G_warning(_("Unable to delete directory '%s'"), mon_path);
 
+    G_free(mon_path);
+
+    G_unsetenv("MONITOR");
+
     return 0;
 }
 
@@ -51,7 +62,6 @@
     
     pid = G_getenv_nofatal(env_name);
     if (!pid) {
-	clean_env(name);
 	G_fatal_error(_("PID file not found"));
     }
     
@@ -63,37 +73,5 @@
     }
 #endif
     
-    clean_env(name);
-
     return 0;
 }
-
-void clean_env(const char *name)
-{
-    int i;
-    char *u_name;
-    const char *env_prefix = "MONITOR_";
-    const char *env;
-    int env_prefix_len;
-    char **tokens;
-
-    u_name = G_store_upper(name);
-    env_prefix_len = strlen(env_prefix);
-    
-    tokens = NULL;
-    for (i = 0; (env = G_get_env_name(i)); i++) {
-	if (strncmp(env_prefix, env, env_prefix_len) != 0)
-	    continue;
-	
-	tokens = G_tokenize(env, "_");
-	if (G_number_of_tokens(tokens) != 3 ||
-	    strcmp(tokens[1], u_name) != 0)
-	    continue;
-	G_unsetenv(env);
-	i--; /* env has been removed for the list */
-	G_free_tokens(tokens);
-	tokens = NULL;
-    }
-
-    G_unsetenv("MONITOR");
-}



More information about the grass-commit mailing list