static int check_encoder(const char *encoder) { int status, prev; prev = G_suppress_warnings(1); status = G_spawn_ex( encoder, encoder, SF_REDIRECT_FILE, SF_STDERR, SF_MODE_OUT, G_DEV_NULL, NULL); G_suppress_warnings(prev); return status >= 0 && status != 127; }
/* start wxGUI display monitor */ void start_wx(const char *name, const char *tempfile, const char *env_value, const char *cmd_value, const char *width, const char *height) { char progname[GPATH_MAX]; char *env_name, *map_value; env_name = NULL; G_asprintf(&env_name, "MONITOR_%s_MAPFILE", name); G_asprintf(&map_value, "%s.ppm", tempfile); G_setenv(env_name, map_value); /* close(creat(map_value, 0666)); */ G_debug(3, " mapfile = %s", map_value); sprintf(progname, "%s/etc/gui/wxpython/gui_modules/mapdisp.py", G_gisbase()); G_spawn_ex(getenv("GRASS_PYTHON"), progname, progname, name, map_value, cmd_value, env_value, width ? width : "", height ? height : "", SF_BACKGROUND, NULL); }
static void mlist(const char *element, const char *wildarg, const char *outfile) { int n; const char *mapset; for (n = 0; (mapset = G_get_mapset_name(n)); n++) { char type_arg[GNAME_MAX]; char pattern_arg[GNAME_MAX]; char mapset_arg[GMAPSET_MAX]; if (strcmp(mapset, ".") == 0) mapset = G_mapset(); sprintf(type_arg, "type=%s", element); sprintf(pattern_arg, "pattern=%s", wildarg); sprintf(mapset_arg, "mapset=%s", mapset); G_spawn_ex("g.list", "g.list", type_arg, pattern_arg, mapset_arg, SF_REDIRECT_FILE, SF_STDOUT, SF_MODE_APPEND, outfile, NULL); } }
int main(int argc, char *argv[]) { struct Option *type, *rc_file; struct Flag *update, *nolaunch; struct GModule *module; const char *gui_type_env; char progname[GPATH_MAX]; char *desc; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("general")); G_add_keyword(_("gui")); G_add_keyword(_("user interface")); module->label = _("Launches a GRASS graphical user interface (GUI) session."); module->description = _("And updates default user interface settings."); type = G_define_option(); type->key = "ui"; type->type = TYPE_STRING; type->label = _("User interface"); type->description = _("Default value: GRASS_GUI if defined otherwise wxpython"); desc = NULL; G_asprintf(&desc, "wxpython;%s;text;%s", _("wxPython based GUI (wxGUI)"), _("command line interface only")); type->descriptions = desc; type->options = "wxpython,text"; type->guisection = _("Type"); rc_file = G_define_standard_option(G_OPT_F_INPUT); rc_file->key = "workspace"; rc_file->required = NO; rc_file->key_desc = "name.gxw"; rc_file->description = _("Name of workspace file to load on start-up (valid only for wxGUI)"); update = G_define_flag(); update->key = 'd'; update->description = _("Update default user interface settings"); update->guisection = _("Default"); nolaunch = G_define_flag(); nolaunch->key = 'n'; nolaunch->description = _("Do not launch GUI after updating the default user interface settings"); nolaunch->guisection = _("Default"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (type->answer && strcmp(type->answer, "text") == 0 && !nolaunch->answer) nolaunch->answer = TRUE; if (nolaunch->answer && !update->answer) update->answer = TRUE; gui_type_env = G__getenv("GUI"); if (!type->answer) { if (gui_type_env && strcmp(gui_type_env, "text")) { type->answer = G_store(gui_type_env); } else { type->answer = "wxpython"; } } if (((gui_type_env && update->answer) && strcmp(gui_type_env, type->answer) != 0) || !gui_type_env) { G_setenv("GUI", type->answer); G_message(_("<%s> is now the default GUI"), type->answer); } else { if(update->answer) if(gui_type_env) { G_debug(1, "No change: old gui_type_env=[%s], new type->ans=[%s]", gui_type_env, type->answer); } } if(nolaunch->answer) exit(EXIT_SUCCESS); G_message(_("Launching <%s> GUI in the background, please wait..."), type->answer); if (strcmp(type->answer, "wxpython") == 0) { sprintf(progname, "%s/gui/wxpython/wxgui.py", G_gisbase()); if (rc_file->answer) { G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname, "--workspace", rc_file->answer, SF_BACKGROUND, NULL); } else { G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname, SF_BACKGROUND, NULL); } } /* stop the impatient from starting it again before the splash screen comes up */ G_sleep(3); exit(EXIT_SUCCESS); }
/*! \brief Initialize a new dbDriver for db transaction. If <i>name</i> is NULL, the db name will be assigned connection.driverName. \param name driver name \return pointer to dbDriver structure \return NULL on error */ dbDriver *db_start_driver(const char *name) { dbDriver *driver; dbDbmscap *list, *cur; const char *startup; int p1[2], p2[2]; int pid; int stat; dbConnection connection; char ebuf[5]; /* Set some environment variables which are later read by driver. * This is necessary when application is running without GISRC file and all * gis variables are set by application. * Even if GISRC is set, application may change some variables during runtime, * if for example reads data from different gdatabase, location or mapset*/ /* setenv() is not portable, putenv() is POSIX, putenv() in glibc 2.0-2.1.1 doesn't conform to SUSv2, * G_putenv() as well, but that is what we want, makes a copy of string */ if (G_get_gisrc_mode() == G_GISRC_MODE_MEMORY) { G_debug(3, "G_GISRC_MODE_MEMORY\n"); sprintf(ebuf, "%d", G_GISRC_MODE_MEMORY); G_putenv("GRASS_DB_DRIVER_GISRC_MODE", ebuf); /* to tell driver that it must read variables */ if (G_getenv_nofatal("DEBUG")) { G_putenv("DEBUG", G_getenv_nofatal("DEBUG")); } else { G_putenv("DEBUG", "0"); } G_putenv("GISDBASE", G_getenv_nofatal("GISDBASE")); G_putenv("LOCATION_NAME", G_getenv_nofatal("LOCATION_NAME")); G_putenv("MAPSET", G_getenv_nofatal("MAPSET")); } else { /* Warning: GISRC_MODE_MEMORY _must_ be set to G_GISRC_MODE_FILE, because the module can be * run from an application which previously set environment variable to G_GISRC_MODE_MEMORY */ sprintf(ebuf, "%d", G_GISRC_MODE_FILE); G_putenv("GRASS_DB_DRIVER_GISRC_MODE", ebuf); } /* read the dbmscap file */ if (NULL == (list = db_read_dbmscap())) return (dbDriver *) NULL; /* if name is empty use connection.driverName, added by RB 4/2000 */ if (name == '\0') { db_get_connection(&connection); if (NULL == (name = connection.driverName)) return (dbDriver *) NULL; } /* find this system name */ for (cur = list; cur; cur = cur->next) if (strcmp(cur->driverName, name) == 0) break; if (cur == NULL) { char msg[256]; db_free_dbmscap(list); sprintf(msg, "%s: no such driver available", name); db_error(msg); return (dbDriver *) NULL; } /* allocate a driver structure */ driver = (dbDriver *) db_malloc(sizeof(dbDriver)); if (driver == NULL) { db_free_dbmscap(list); return (dbDriver *) NULL; } /* copy the relevant info from the dbmscap entry into the driver structure */ db_copy_dbmscap_entry(&driver->dbmscap, cur); startup = driver->dbmscap.startup; /* free the dbmscap list */ db_free_dbmscap(list); /* run the driver as a child process and create pipes to its stdin, stdout */ #ifdef __MINGW32__ #define pipe(fds) _pipe(fds, 250000, _O_BINARY | _O_NOINHERIT) #endif /* open the pipes */ if ((pipe(p1) < 0) || (pipe(p2) < 0)) { db_syserror("can't open any pipes"); return (dbDriver *) NULL; } close_on_exec(p1[READ]); close_on_exec(p1[WRITE]); close_on_exec(p2[READ]); close_on_exec(p2[WRITE]); pid = G_spawn_ex(startup, SF_BACKGROUND, SF_REDIRECT_DESCRIPTOR, 0, p1[READ], SF_CLOSE_DESCRIPTOR, p1[WRITE], SF_REDIRECT_DESCRIPTOR, 1, p2[WRITE], SF_CLOSE_DESCRIPTOR, p2[READ], startup, NULL); /* create a child */ if (pid < 0) { db_syserror("can't create fork"); return (dbDriver *) NULL; } close(p1[READ]); close(p2[WRITE]); /* record driver process id in driver struct */ driver->pid = pid; /* convert pipes to FILE* */ driver->send = fdopen(p1[WRITE], "wb"); driver->recv = fdopen(p2[READ], "rb"); /* most systems will have to use unbuffered io to get the send/recv to work */ #ifndef USE_BUFFERED_IO setbuf(driver->send, NULL); setbuf(driver->recv, NULL); #endif db__set_protocol_fds(driver->send, driver->recv); if (db__recv_return_code(&stat) != DB_OK || stat != DB_OK) driver = NULL; return driver; }
int main(int argc, char *argv[]) { struct Option *type, *rc_file; struct Flag *update_ui, *fglaunch, *nolaunch; struct GModule *module; const char *gui_type_env; char progname[GPATH_MAX]; char *desc; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("general")); G_add_keyword(_("GUI")); G_add_keyword(_("user interface")); module->label = _("Launches a GRASS graphical user interface (GUI) session."); module->description = _("Optionally updates default user interface settings."); type = G_define_option(); type->key = "ui"; type->type = TYPE_STRING; type->description = _("User interface"); desc = NULL; G_asprintf(&desc, "wxpython;%s;text;%s;gtext;%s;", _("wxPython based GUI (wxGUI)"), _("command line interface only"), _("command line interface with GUI startup screen")); type->descriptions = desc; type->options = "wxpython,text,gtext"; type->answer = "wxpython"; type->guisection = _("Type"); rc_file = G_define_standard_option(G_OPT_F_INPUT); rc_file->key = "workspace"; rc_file->required = NO; rc_file->key_desc = "name.gxw"; rc_file->label = _("Name of workspace file to load on start-up"); rc_file->description = _("This is valid only for wxGUI (wxpython)"); fglaunch = G_define_flag(); fglaunch->key = 'f'; fglaunch->label = _("Start GUI in the foreground"); fglaunch->description = _("By default the GUI starts in the background" " and control is immediately returned to the caller." " When GUI runs in foregreound, it blocks the command line"); update_ui = G_define_flag(); update_ui->key = 'd'; update_ui->description = _("Update default user interface settings"); update_ui->guisection = _("Default"); nolaunch = G_define_flag(); nolaunch->key = 'n'; nolaunch->description = _("Do not launch GUI after updating the default user interface settings"); nolaunch->guisection = _("Default"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); gui_type_env = G_getenv_nofatal("GUI"); G_debug(1, "GUI: %s", gui_type_env ? gui_type_env : "unset"); if (update_ui->answer) { if (!gui_type_env || strcmp(type->answer, gui_type_env)) { G_setenv("GUI", type->answer); G_message(_("<%s> is now the default GUI"), type->answer); } } if(strcmp(type->answer, "wxpython") != 0 || nolaunch->answer) { if (!update_ui->answer) G_warning(_("Nothing to do. For setting up <%s> as default UI use -%c flag."), type->answer, update_ui->key); exit(EXIT_SUCCESS); } sprintf(progname, "%s/gui/wxpython/wxgui.py", G_gisbase()); if (access(progname, F_OK) == -1) G_fatal_error(_("Your installation doesn't include GUI, exiting.")); if (fglaunch->answer) { G_message(_("Launching <%s> GUI, please wait..."), type->answer); if (rc_file->answer) { G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname, "--workspace", rc_file->answer, NULL); } else { G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname, NULL); } } else { G_message(_("Launching <%s> GUI in the background, please wait..."), type->answer); if (rc_file->answer) { G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname, "--workspace", rc_file->answer, SF_BACKGROUND, NULL); } else { G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname, SF_BACKGROUND, NULL); } /* stop the impatient from starting it again before the splash screen comes up */ G_sleep(3); } exit(EXIT_SUCCESS); }