static void copy(const string& from_name, const string& to_name, std::ostream& msg) { FILE *from = fopen(from_name.chars(), "r"); if (from == 0) return; // Don't care StreamAction action(msg, "Copying " + quote(from_name) + " to " + quote(to_name)); FILE *to = fopen(to_name.chars(), "w"); if (to == 0) { action.failed(strerror(errno)); return; } int c; while ((c = getc(from)) != EOF) putc(c, to); fclose(from); if (fclose(to) == EOF) { action.failed(strerror(errno)); unlink(to_name.chars()); } }
// Retrieve PID from PS output static int ps_pid(const string& line) { const char *s = line.chars() + ps_pid_index; while (s > line.chars() && isdigit(s[-1])) --s; return atoi(s); }
// Create a file dialog NAME with DO_SEARCH_FILES and DO_SEARCH_DIRS // as search procedures for files and directories, respectively, and // OK_CALLBACK as the procedure called when a file is selected. static Widget file_dialog(Widget w, const string& name, FileSearchProc do_search_files = 0, FileSearchProc do_search_dirs = 0, XtCallbackProc ok_callback = 0) { Delay delay(w); Arg args[10]; int arg = 0; string pwd; arg = 0; if (do_search_files) { XtSetArg(args[arg], XmNfileSearchProc, do_search_files); arg++; } if (do_search_dirs) { XtSetArg(args[arg], XmNdirSearchProc, do_search_dirs); arg++; } if (remote_gdb()) { static MString xmpwd; xmpwd = source_view->pwd(); XtSetArg(args[arg], XmNdirectory, xmpwd.xmstring()); arg++; } Widget dialog = verify(XmCreateFileSelectionDialog(w, XMST(name.chars()), args, arg)); Delay::register_shell(dialog); if (ok_callback != 0) XtAddCallback(dialog, XmNokCallback, ok_callback, 0); XtAddCallback(dialog, XmNcancelCallback, UnmanageThisCB, XtPointer(dialog)); XtAddCallback(dialog, XmNhelpCallback, ImmediateHelpCB, 0); Widget filter = XmFileSelectionBoxGetChild(dialog, XmDIALOG_FILTER_TEXT); file_filters += filter; if (!current_file_filter.empty()) XmTextSetString(filter, XMST(current_file_filter.chars())); XtAddCallback(filter, XmNvalueChangedCallback, SyncFiltersCB, 0); Widget filter_button = XmFileSelectionBoxGetChild(dialog, XmDIALOG_APPLY_BUTTON); XtAddCallback(filter_button, XmNactivateCallback, FilterAllCB, 0); XtAddCallback(dialog, XmNunmapCallback, ClearStatusCB, 0); file_dialogs += dialog; return dialog; }
void wm_set_name(Display *display, Window shell_window, string title, string icon) { strip_space(title); strip_space(icon); if (!title.empty()) XStoreName(display, shell_window, title.chars()); if (!icon.empty()) XSetIconName(display, shell_window, icon.chars()); }
// True if SESSION is temporary void set_temporary_session(const string& session, bool temporary) { if (temporary) { const string s1 = session_tmp_flag(session); std::ofstream os(s1.chars()); os << "This session will be deleted unless saved explicitly.\n"; } else { const string s1 = session_tmp_flag(session.chars()); unlink(s1.chars()); } }
void wm_set_name(Widget shell, string title, string icon) { strip_space(title); strip_space(icon); XtVaSetValues(shell, XmNiconName, icon.chars(), XmNtitle, title.chars(), XtPointer(0)); #if 0 // This should be done by the shell. wm_set_name(XtDisplay(shell), XtWindow(shell), title, icon); #endif }
// Add plot point void PlotAgent::add_point(const string& v, int dim) { values[values.size() - 1] = v; dims[dims.size() - 1] = dim; add_v(atof(v.chars())); }
// Return true iff S1 and S2 are identical bool same_file(const string& s1, const string& s2) { if (s1 == s2) return true; struct stat s1_stat; struct stat s2_stat; if (stat(s1.chars(), &s1_stat) == 0 && stat(s2.chars(), &s2_stat) == 0 && s1_stat.st_dev == s2_stat.st_dev && s1_stat.st_ino == s2_stat.st_ino) return true; return false; }
Boolean CvtStringToWidget(Display *display, XrmValue *args, Cardinal *num_args, XrmValue *fromVal, XrmValue *toVal, XtPointer *) { // Convert first arg into parent if (*num_args != 1) { XtAppErrorMsg(XtDisplayToApplicationContext(display), "wrongParameters", "CvtStringToWidget", "XtToolkitError", "String to Widget conversion needs parent arg", (String *)0, (Cardinal *)0); } Widget parent = *(Widget *) args[0].addr; // Get widget const string value = str(fromVal, false); Widget w = XtNameToWidget(parent, value.chars()); if (w == 0) { XtDisplayStringConversionWarning(display, fromVal->addr, XtRWidget); return False; } done(Widget, w); }
// Swallow new GNUPLOT window; search from root window (expensive). static void SwallowTimeOutCB(XtPointer client_data, XtIntervalId *id) { (void) id; PlotWindowInfo *plot = (PlotWindowInfo *)client_data; assert(*id == plot->swallow_timer); plot->swallow_timer = 0; Window root = RootWindowOfScreen(XtScreen(plot->swallower)); Window window = None; Display *display = XtDisplay(plot->swallower); // Try the exact name as given if (window == None) window = findWindow(display, root, plot->window_name.chars()); // Try the capitalized name. Gnuplot does this. if (window == None) { const string s1 = capitalize(plot->window_name); window = findWindow(display, root, s1.chars()); } if (window == None) { // Try again later plot->swallow_timer = XtAppAddTimeOut(XtWidgetToApplicationContext(plot->swallower), app_data.plot_window_delay, SwallowTimeOutCB, XtPointer(plot)); } if (window != None) swallow(plot, window); }
// Return home dir const char *gethome() { static string home = ""; // Try using $HOME if (home.empty()) { const char *h = getenv("HOME"); if (h != 0) home = h; } // Try using passwd entry if (home.empty()) { struct passwd *pw = 0; const char *user = getenv("USER"); if (user == 0) user = getenv("LOGNAME"); if (user != 0) pw = getpwnam(user); if (pw == 0) pw = getpwuid(getuid()); if (pw != 0) home = pw->pw_dir; } // Revert to current dir if (home.empty()) home = "."; return home.chars(); }
// Output TEXT on controlling TTY (unconditionally) void _tty_out(const string& text) { if (command_tty == 0) return; command_tty->write(text.chars(), text.length()); }
// Swallow new GNUPLOT window; search from window created on root. static void SwallowCB(Widget swallower, XtPointer client_data, XtPointer call_data) { PlotWindowInfo *plot = (PlotWindowInfo *)client_data; assert(plot->swallower == swallower); SwallowerInfo *info = (SwallowerInfo *)call_data; Window root = info->window; Window window = None; Display *display = XtDisplay(swallower); // Try the exact name as given if (window == None) window = findWindow(display, root, plot->window_name.chars()); // Try the capitalized name. Gnuplot does this. if (window == None) { const string s1 = capitalize(plot->window_name); window = findWindow(display, root, s1.chars()); } // Try any `Gnuplot' window just created if (window == None) window = findWindow(display, root, app_data.plot_window_class); if (window != None) swallow(plot, window); }
// Update list of arguments static void update_arguments(Widget dialog, Widget arguments_w, StringArray& arguments, const string& last, bool& updated) { if (updated || dialog == 0) return; bool *selected = new bool[arguments.size()]; int pos = -1; for (int i = 0; i < arguments.size(); i++) { if (arguments[i] == last) pos = i; selected[i] = false; } if (pos >= 0) selected[pos] = true; setLabelList(arguments_w, arguments.values(), selected, arguments.size(), false, false); if (pos >= 0) XmListSelectPos(arguments_w, pos + 1, False); delete[] selected; Widget text_w = XmSelectionBoxGetChild(dialog, XmDIALOG_TEXT); XmTextSetString(text_w, XMST(last.chars())); updated = true; }
// Install the given X bitmap as NAME static void InstallBitmapAsImage(unsigned char *bits, int width, int height, const string& name) { Boolean ok = InstallBitmap(bits, width, height, name.chars()); if (!ok) std::cerr << "Could not install " << quote(name) << " bitmap\n"; }
// Save history into history file void save_history(const string& file, Widget origin) { if (!file.empty()) { StatusDelay delay("Saving history in " + quote(file)); std::ofstream os(file.chars()); if (os.bad()) { post_error("Cannot save history in " + quote(file), "history_save_error", origin); delay.outcome = "failed"; return; } // Save the 10 most recently opened files int i; StringArray recent; get_recent(recent); for (i = recent.size() - 1; i >= 0 && i >= recent.size() - 10; i--) os << gdb->debug_command(recent[i]) << "\n"; // Now save the command history itself int start = gdb_history.size() - gdb_history_size; if (start < 0) start = 0; for (i = start; i < gdb_history.size(); i++) os << gdb_history[i] << "\n"; } }
// Make sure that every change in one filter is reflected in all others static void SyncFiltersCB(Widget dialog, XtPointer, XtPointer) { static bool entered = false; if (entered) return; entered = true; // std::clog << "widget = " << longName(text) << "\n"; while (dialog != 0 && !XmIsFileSelectionBox(dialog)) dialog = XtParent(dialog); // std::clog << "dialog = " << longName(dialog) << "\n"; Widget text = XmFileSelectionBoxGetChild(dialog, XmDIALOG_FILTER_TEXT); String _current_file_filter = XmTextGetString(text); current_file_filter = _current_file_filter; XtFree(_current_file_filter); for (int i = 0; i < file_filters.size(); i++) { if (file_dialogs[i] != dialog) { // std::clog << "other dialog = " << longName(file_dialogs[i]) << "\n"; XmTextSetString(file_filters[i], XMST(current_file_filter.chars())); } } entered = false; }
// locate path // this mimics XmGetPixmap's efforts to build a path static string bitmapPath() { static string path = ""; if (!path.empty()) return path; path = BASENAME; const char *xbmlangpath = getenv("XBMLANGPATH"); if (xbmlangpath == 0) { const char *xapplresdir = getenv("XAPPLRESDIR"); const string home = gethome(); if (xapplresdir != 0) addDefaultPaths(path, xapplresdir); else addDefaultPaths(path, home.chars()); path += DELIMITER + home + BASENAME; addDefaultPaths(path, "/usr/lib/X11"); path += DELIMITER + "/usr/include/X11/%T/" + BASENAME; } else path = xbmlangpath; return path; }
void ReadLineAgent::prompt(const string& prompt_string) { #if WITH_READLINE if (inputIsTerminal()) { // Prompt using PROMPT_STRING rl_callback_handler_remove(); rl_callback_handler_install(prompt_string.chars(), readline_handler); current_prompter = this; return; } #endif write(prompt_string.chars(), prompt_string.length()); current_prompter = 0; }
static bool is_file(const string& file) { FILE *fp = fopen(file.chars(), "r"); if (fp == 0) return false; fclose(fp); return true; }
void PlotAgent::add_point(double x, double y, const string& v) { assert(ndim == 3); plot_os << x << '\t' << y << '\t' << v << '\n'; add_x(x); add_y(y); add_v(atof(v.chars())); }
// Show whether plot is active void PlotAgent::set_state(const string& state) { string title; if (!state.empty()) title = " " + quote('(' + state + ')'); const string c = "set title" + title + "\nreplot\n"; write(c.chars(), c.length()); }
void PlotAgent::add_point(double x, const string& v) { if (ndim > 2) add_point(x, 0.0, v); else { plot_os << x << '\t' << v << '\n'; add_x(x); add_v(atof(v.chars())); } }
static void put(const string& s) { write(STDOUT_FILENO, s.chars(), s.length()); last_output += s; #if HAVE_TCDRAIN || defined(tcdrain) if (isatty(STDOUT_FILENO)) { tcdrain(STDOUT_FILENO); } #endif }
// Synchronize file dialogs with current directory void process_cd(const string& pwd) { current_file_filter = pwd + "/*"; for (int i = 0; i < file_filters.size(); i++) { if (file_filters[i] != 0) { XmTextSetString(file_filters[i], XMST(current_file_filter.chars())); break; } } }
// locate bitmap // this mimics XmGetPixmap's efforts to locate a path static String locateBitmap(Display *display, const _XtString basename) { SubstitutionRec subst; subst.match = 'B'; subst.substitution = CONST_CAST(char*,basename); return XtResolvePathname( display, // the display we use "bitmaps", // %T = bitmaps String(0), // %N = application class name "", // %S = "" (suffix) PATH.chars(), // path to use &subst, 1, // %B = basename XtFilePredicate(0)); // no checking for valid bitmap }
static void slurp_file(const string& filename, string& target) { std::ifstream is(filename.chars()); if (is.bad()) { target = ""; return; } std::ostringstream s; int c; while ((c = is.get()) != EOF) s << (unsigned char)c; target = s; }
// Called from readline library as soon as the line is complete void ReadLineAgent::readline_handler(char *line) { ReadLineAgent *prompter = current_prompter; assert (prompter != 0); make_sane(); if (line == 0) { prompter->inputEOF(); } else { const string input = string(line) + "\n"; prompter->dispatch(Input, input.chars(), input.length()); } }
static int makedir(const string& name, std::ostream& msg, bool user_only = false) { StreamAction action(msg, "Creating " + quote(name + "/")); mode_t mask = umask(0); umask(mask); mode_t mode; if (user_only) mode = S_IRWXU & ~mask; else mode = (S_IRWXU | S_IRWXG | S_IRWXO) & ~mask; int ret = mkdir(name.chars(), mode); if (ret != 0) action.failed(strerror(errno)); return ret; }
// Replace all '\t' by filling up spaces until multiple of TAB_WIDTH // is reached; ignore first INDENT characters. void untabify(string& str, int tab_width, int indent) { char *buffer = new char[str.length() + str.freq('\t') * (tab_width - 1) + 1]; char *b = buffer; int column = 0; int spaces; const char *s = str.chars(); while (*s != '\0') { switch (*s) { case '\t': if (column >= indent) spaces = tab_width - ((column - indent) % tab_width); else spaces = 1; while (spaces-- > 0) { *b++ = ' '; column++; } break; case '\n': case '\r': *b++ = *s; column = 0; break; default: *b++ = *s; column++; break; } s++; } *b++ = '\0'; str = buffer; delete[] buffer; }