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"); }
int stop_wx(const char *name) { char *env_name; const char *pid; env_name = NULL; G_asprintf(&env_name, "MONITOR_%s_PID", G_store_upper(name)); pid = G_getenv_nofatal(env_name); if (!pid) { clean_env(name); G_fatal_error(_("PID file not found")); } #ifdef __MINGW32__ /* TODO */ #else if (kill((pid_t) atoi(pid), SIGTERM) != 0) { /* G_fatal_error(_("Unable to stop monitor <%s>"), name); */ } #endif clean_env(name); return 0; }
/* check if monitor is running */ int check_mon(const char *name) { char *env_name; const char *str; env_name = NULL; G_asprintf(&env_name, "MONITOR_%s_ENVFILE", G_store_upper(name)); str = G__getenv(env_name); if (!str) return FALSE; return TRUE; }
int stop(const char *name) { char *env_name; const char *env_file; env_name = NULL; G_asprintf(&env_name, "MONITOR_%s_ENVFILE", G_store_upper(name)); env_file = G_getenv_nofatal(env_name); if (!env_file) G_warning(_("Env file not found")); clean_env(name); return 0; }
/*! \brief Append command to the cmd file Cmd file is created by d.mon by defining GRASS variable \c MONITOR_<name>_CMDFILE, where \c \<name\> is the upper case name of the monitor. Command string is usually generated by G_recreate_command(), NULL is used to clean up list of commands (see d.erase command). \param cmd string buffer with command or NULL \return 0 no monitor selected \return -1 on error \return 1 on success */ int D_save_command(const char *cmd) { const char *mon_name, *mon_cmd; char *env, *flag, *u_mon_name; FILE *fd; G_debug(1, "D_save_command(): %s", cmd); mon_name = G__getenv("MONITOR"); if (!mon_name || /* if no monitor selected */ /* or wx monitor selected and display commands called by the monitor */ (G_strncasecmp(mon_name, "wx", 2) == 0 && getenv("GRASS_RENDER_IMMEDIATE"))) return 0; /* GRASS variable names should be upper case. */ u_mon_name = G_store_upper(mon_name); env = NULL; G_asprintf(&env, "MONITOR_%s_CMDFILE", u_mon_name); mon_cmd = G__getenv(env); if (!mon_cmd) return 0; if (cmd) flag = "a"; else flag = "w"; fd = fopen(mon_cmd, flag); if (!fd) { G_warning(_("Unable to open file '%s'"), mon_cmd); return -1; } if (cmd) fprintf(fd, "%s\n", cmd); fclose(fd); return 1; }
/* 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; FILE *fd; cmd_name = NULL; G_asprintf(&cmd_name, "MONITOR_%s_CMDFILE", G_store_upper(name)); cmd_value = G__getenv(cmd_name); if (!cmd_value) G_fatal_error(_("Command file not found")); fd = fopen(cmd_value, "r"); if (!fd) G_fatal_error(_("Unable to read command file")); while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) { fprintf(fd_out, "%s\n", buf); } fclose(fd); }
/*! \brief Open display driver Default display driver is Cairo, if not available PNG is used. \return 0 on success \return 1 no monitor defined */ int D_open_driver(void) { const char *p, *m; G_debug(1, "D_open_driver():"); p = getenv("GRASS_RENDER_IMMEDIATE"); m = G__getenv("MONITOR"); if (m && G_strncasecmp(m, "wx", 2) == 0) { /* wx monitors always use GRASS_RENDER_IMMEDIATE. */ p = NULL; /* use default display driver */ } else if (m) { char *env; const char *v; char *u_m; if (p) G_warning(_("%s variable defined, %s ignored"), "MONITOR", "GRASS_RENDER_IMMEDIATE"); /* GRASS variable names should be upper case. */ u_m = G_store_upper(m); env = NULL; G_asprintf(&env, "MONITOR_%s_MAPFILE", u_m); v = G__getenv(env); p = m; if (v) { if (G_strcasecmp(p, "ps") == 0) G_putenv("GRASS_PSFILE", v); else G_putenv("GRASS_PNGFILE", v); } G_asprintf(&env, "MONITOR_%s_ENVFILE", u_m); v = G__getenv(env); if (v) read_env_file(v); } const struct driver *drv = (p && G_strcasecmp(p, "PNG") == 0) ? PNG_Driver() : (p && G_strcasecmp(p, "PS") == 0) ? PS_Driver() : (p && G_strcasecmp(p, "HTML") == 0) ? HTML_Driver() : #ifdef USE_CAIRO (p && G_strcasecmp(p, "cairo") == 0) ? Cairo_Driver() : Cairo_Driver(); #else PNG_Driver(); #endif if (p && G_strcasecmp(drv->name, p) != 0) G_warning(_("Unknown display driver <%s>"), p); G_verbose_message(_("Using display driver <%s>..."), drv->name); LIB_init(drv); init(); if (!getenv("GRASS_RENDER_IMMEDIATE") && !m) return 1; return 0; }