static void clean_temp_dirs () { bool err= false; array<string> a= read_directory (main_tmp_dir, err); #ifndef __MINGW32__ for (int i=0; i<N(a); i++) if (is_int (a[i])) if (!process_running (as_int (a[i]))) if (a[i] != as_string ((int) getpid ())) system ("rm -rf", url (main_tmp_dir) * url (a[i])); #else /* delete the directories after 7 days */ time_t ts = as_int (basename (url_temp_dir_sub ())) - (3600 * 24 * 7 ); for (int i=0; i<N(a); i++) if (is_int (a[i])) { time_t td= as_int (a[i]); if (td < ts) { url cur = url (main_tmp_dir) * url (a[i]); array<string> f= read_directory (cur, err); for (int j=0; j<N(f); j++) remove (cur * url (f[j])); _rmdir (as_charp (as_string (cur))); } } #endif }
int unix_system (string s) { char* _s = as_charp (s * " > /dev/null 2>&1"); int ret = system (_s); tm_delete_array (_s); return ret; }
int unix_system (string cmd, string& result) { url temp= url_temp (); string temp_s= escape_sh (concretize (temp)); char* _cmd = as_charp (cmd * " > " * temp_s * " 2>&1"); int ret = system (_cmd); tm_delete_array (_cmd); bool flag= load_string (temp, result, false); remove (temp); if (flag) result= ""; return ret; }
void set_env (string var, string with) { #if defined(STD_SETENV) && !defined(__MINGW32__) c_string _var (var); c_string _with (with); setenv (_var, _with, 1); #else char* _varw= as_charp (var * "=" * with); (void) putenv (_varw); // do not delete _varw !!! // -> known memory leak, but solution more complex than it is worth #endif }
void x_window_rep::set_full_screen (bool flag) { if (full_screen_flag == flag) return; string old_name= get_name (); if (old_name == "") old_name= as_string (name); if (flag) { save_win= win; name= NULL; save_x= win_x; save_y= win_y; save_w= win_w; save_h= win_h; initialize (); XMoveResizeWindow (dpy, win, 0, 0, gui->screen_width, gui->screen_height); move_event (0, 0); resize_event (gui->screen_width, gui->screen_height); set_visibility (true); XSetInputFocus (dpy, win, PointerRoot, CurrentTime); } else { set_visibility (false); Window_to_window->reset (win); nr_windows--; XDestroyWindow (dpy, win); win= save_win; set_visibility (false); Window_to_window->reset (win); nr_windows--; XDestroyWindow (dpy, win); //FIXME: is this 'as_charp' a possible memory leak? name= as_charp (old_name); win_x= save_x; win_y= save_y; win_w= save_w; win_h= save_h; initialize (); set_visibility (true); XMoveResizeWindow (dpy, win, save_x, save_y, save_w, save_h); resize_event (save_w, save_h); move_event (save_x, save_y); } set_name (old_name); full_screen_flag= flag; }
tree bibtex_run (string bib, string style, url bib_file, tree bib_t) { int i; string bib_s= "\\bibstyle{" * style * "}\n"; for (i=0; i<arity(bib_t); i++) bib_s << "\\citation{" << as_string (bib_t[i]) << "}\n"; string dir= concretize (head (bib_file)); string bib_name= as_string (tail (bib_file)); if ((N(bib_name) >= 4) && (bib_name (N(bib_name)-4, N(bib_name)) == ".bib")) bib_name= bib_name (0, N(bib_name)- 4); bib_s << "\\bibdata{" << bib_name << "}\n"; save_string ("$TEXMACS_HOME_PATH/system/bib/temp.aux", bib_s); #ifdef OS_WIN32 char *directory = as_charp (dir); RunBibtex(directory, "$TEXMACS_HOME_PATH/system/bib", "temp"); tm_delete_array (directory); #else string cmdln= "cd $TEXMACS_HOME_PATH/system/bib; "; cmdln << "BIBINPUTS=" << dir << ":$BIBINPUTS " << "BSTINPUTS=" << dir << ":$BSTINPUTS " << bibtex_command << " temp"; if (DEBUG_AUTO) cout << "TeXmacs] BibTeX command: " << cmdln << "\n"; system (cmdln); #endif return bibtex_load_bbl (bib, "$TEXMACS_HOME_PATH/system/bib/temp.bbl"); /* string result; if (load_string ("$TEXMACS_HOME_PATH/system/bib/temp.bbl", result, false)) return "Error: bibtex failed to create bibliography"; int count=1; tree t= generic_to_tree (result, "latex-snippet"); t= search_bib (t); if (t == "") return ""; tree largest= t[0]; t= t[1]; tree u (DOCUMENT); for (i=0; i<arity(t); i++) { if (is_concat (t[i]) && (is_compound (t[i][0], "bibitem") || is_compound (t[i][0], "bibitem*"))) { tree item= t[i][0]; if (is_compound (item, "bibitem")) item= compound ("bibitem*", as_string (count++), item[0]); t[i][0]= item; tree v (CONCAT, compound ("bibitem*", item[0])); if (is_atomic (item[1])) v << tree (LABEL, bib * "-" * item[1]->label); if (N(t[i])>1) { v << remove_start_space (t[i][1]); v << A (t[i] (2, N(t[i]))); } u << v; } } if (N(u) == 0) u= tree (DOCUMENT, ""); return compound ("bib-list", largest, u); */ }
int unix_system (array<string> arg, array<int> fd_in, array<string> str_in, array<int> fd_out, array<string*> str_out) { // Run command arg[0] with arguments arg[i], i >= 1. // str_in[i] is sent to the file descriptor fd_in[i]. // str_out[i] is filled from the file descriptor fd_out[i]. // If str_in[i] is -1 then $$i automatically replaced by a valid // file descriptor in arg. if (N(arg) == 0) return 0; string which= recompose (arg, " "); int n_in= N(fd_in), n_out= N(fd_out); ASSERT(N(str_in) == n_in, "size mismatch"); ASSERT(N(str_out) == n_out, "size mismatch"); array<_pipe_t> pp_in (n_in), pp_out (n_out); _file_actions_t file_actions; for (int i= 0; i < n_in; i++) { if (posix_spawn_file_actions_addclose (&file_actions.rep, pp_in[i].out ()) != 0) return -1; if (fd_in[i] >= 0) { if (posix_spawn_file_actions_adddup2 (&file_actions.rep, pp_in[i].in (), fd_in[i]) != 0) return -1; if (posix_spawn_file_actions_addclose (&file_actions.rep, pp_in[i].in ()) != 0) return -1; } } for (int i= 0; i < n_out; i++) { if (posix_spawn_file_actions_addclose (&file_actions.rep, pp_out[i].in ()) != 0) return -1; if (posix_spawn_file_actions_adddup2 (&file_actions.rep, pp_out[i].out (), fd_out[i]) != 0) return -1; if (posix_spawn_file_actions_addclose (&file_actions.rep, pp_out[i].out ()) != 0) return -1; } array<string> arg_= arg; for (int j= 0; j < N(arg); j++) for (int i= 0; i < n_in; i++) if (fd_in[i] < 0) arg_[j]= replace (arg_[j], "$$" * as_string (i), as_string (pp_in[i].in ())); if (DEBUG_IO) debug_io << "unix_system, launching: " << arg_ << "\n"; array<char*> _arg; for (int j= 0; j < N(arg_); j++) _arg << as_charp (arg_[j]); _arg << (char*) NULL; pid_t pid; int status= posix_spawnp (&pid, _arg[0], &file_actions.rep, NULL, A(_arg), environ); for (int j= 0; j < N(arg_); j++) tm_delete_array (_arg[j]); if (status != 0) { if (DEBUG_IO) debug_io << "unix_system, failed" << "\n"; return -1; } if (DEBUG_IO) debug_io << "unix_system, succeeded to create pid " << pid << "\n"; // close useless ports for (int i= 0; i < n_in ; i++) close (pp_in[i].in ()); for (int i= 0; i < n_out; i++) close (pp_out[i].out ()); // write to spawn process array<_channel> channels_in (n_in); array<pthread_t> threads_write (n_in); for (int i= 0; i < n_in; i++) { channels_in[i]._init_in (pp_in[i].out (), str_in[i], 1 << 12); if (pthread_create (&threads_write[i], NULL /* &attr */, _background_write_task, (void *) &(channels_in[i]))) return -1; } // read from spawn process array<_channel> channels_out (n_out); array<pthread_t> threads_read (n_out); for (int i= 0; i < n_out; i++) { channels_out[i]._init_out (pp_out[i].in (), 1 << 12); if (pthread_create (&threads_read[i], NULL /* &attr */, _background_read_task, (void *) &(channels_out[i]))) return -1; } int wret; time_t last_wait_time= texmacs_time (); while ((wret= waitpid (pid, &status, WNOHANG)) == 0) { usleep (100); if (texmacs_time () - last_wait_time > 5000) { last_wait_time= texmacs_time (); _unix_system_warn (pid, which, "waiting spawn process"); } } if (DEBUG_IO) debug_io << "unix_system, pid " << pid << " terminated" << "\n"; // wait for terminating threads void* exit_status; int thread_status= 0; for (int i= 0; i < n_in; i++) { pthread_join (threads_write[i], &exit_status); if (channels_in[i].status < 0) thread_status= -1; } for (int i= 0; i < n_out; i++) { pthread_join (threads_read[i], &exit_status); *(str_out[i])= string (channels_out[i].data.data (), channels_out[i].data.length ()); if (channels_out[i].status < 0) thread_status= -1; } if (thread_status < 0) return thread_status; if (wret < 0 || WIFEXITED(status) == 0) return -1; return WEXITSTATUS(status); }