template<> inline tree as_tree (long int x) { return as_string (x); }
template<> inline tree as_tree (double x) { return as_string (x); }
string tm_frame_rep::get_string_window_property (string what) { return as_string (concrete_window () -> get_property (what)); }
inline string get_label (tree t) { return is_atomic (t)? t->label: copy (as_string (L(t))); }
x_character::operator tree () { tree t (TUPLE, as_string (rep->c), rep->fng->res_name); t << as_string (rep->sf) << as_string (rep->fg) << as_string (rep->bg); return t; }
static string get_delimiter (string s, font fn, SI height) { int ns= N(s); ASSERT (ns >= 2 && s[0] == '<' && s[ns-1] == '>', "invalid rubber character"); if (s[ns-2] >= '0' && s[ns-2] <= '9') { int pos; int plus= get_number (s, pos); if (pos > 0) { string s2= s (0, pos) * ">"; string r2= get_delimiter (s2, fn, height); int pos2; int nr2= get_number (r2, pos2); if (pos2 > 0) { int nr= max (nr2 + plus, 0); return r2 (0, pos2) * "-" * as_string (nr) * ">"; } } } height -= PIXEL; string radical= s (0, N(s)-1) * "-"; string best= radical * "0>"; SI best_h= 0; int n= 0; SI last= 0; int credit= 20; while (credit > 0) { metric ex; string test= radical * as_string (n) * ">"; fn->get_extents (test, ex); SI h= ex->y2 - ex->y1; if (h >= (height - (n==1? PIXEL: 0))) return test; if (h > best_h) { best_h= h; best= test; } int d= h - last; if (last > 0 && d > 0) { int plus= (height - h - 1) / d; if (plus <= 1 || n <= 4) { n++; last= h; } else { int n2= n + plus; metric ex2; string test2= radical * as_string (n2) * ">"; fn->get_extents (test2, ex2); SI h2= ex2->y2 - ex2->y1; if (h2 >= height || h2 < h) { n++; last= h; } else { n= n2; last= 0; } } } else if (last <= 0 || n < 10) { n++; last= h; } else return best; credit--; } return best; }
static int munge_line_in_editor(int count, int key) { int line_number = 0, column_number = 0, ret, tmpfile_fd, bytes_read; size_t tmpfilesize; char *p, *tmpfilename, *text_to_edit; char *editor_command1, *editor_command2, *editor_command3, *editor_command4, *line_number_as_string, *column_number_as_string; char *input, *rewritten_input, *rewritten_input2, **possible_editor_commands; if (!multiline_separator) return 0; tmpfile_fd = open_unique_tempfile(multi_line_tmpfile_ext, &tmpfilename); text_to_edit = search_and_replace(multiline_separator, "\n", rl_line_buffer, rl_point, &line_number, &column_number); write_patiently(tmpfile_fd, text_to_edit, strlen(text_to_edit), "to temporary file"); if (close(tmpfile_fd) != 0) /* improbable */ myerror(FATAL|USE_ERRNO, "couldn't close temporary file %s", tmpfilename); /* find out which editor command we have to use */ possible_editor_commands = list4(getenv("RLWRAP_EDITOR"), getenv("EDITOR"), getenv("VISUAL"), "vi +%L"); editor_command1 = first_of(possible_editor_commands); line_number_as_string = as_string(line_number); column_number_as_string = as_string(column_number); editor_command2 = search_and_replace("%L", line_number_as_string, editor_command1, 0, NULL, NULL); editor_command3 = search_and_replace("%C", column_number_as_string, editor_command2, 0, NULL, NULL); editor_command4 = add3strings(editor_command3, " ", tmpfilename); /* call editor, temporarily restoring terminal settings */ if (terminal_settings_saved && (tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_terminal_settings) < 0)) /* reset terminal */ myerror(FATAL|USE_ERRNO, "tcsetattr error on stdin"); DPRINTF1(DEBUG_READLINE, "calling %s", editor_command4); if ((ret = system(editor_command4))) { if (WIFSIGNALED(ret)) { fprintf(stderr, "\n"); myerror(FATAL|NOERRNO, "editor killed by signal"); } else { myerror(FATAL|USE_ERRNO, "failed to invoke editor with '%s'", editor_command4); } } completely_mirror_slaves_terminal_settings(); ignore_queued_input = TRUE; /* read back edited input, replacing real newline with substitute */ tmpfile_fd = open(tmpfilename, O_RDONLY); if (tmpfile_fd < 0) myerror(FATAL|USE_ERRNO, "could not read temp file %s", tmpfilename); tmpfilesize = filesize(tmpfilename); input = mymalloc(tmpfilesize + 1); bytes_read = read(tmpfile_fd, input, tmpfilesize); if (bytes_read < 0) myerror(FATAL|USE_ERRNO, "unreadable temp file %s", tmpfilename); input[bytes_read] = '\0'; rewritten_input = search_and_replace("\t", " ", input, 0, NULL, NULL); /* rlwrap cannot handle tabs in input lines */ rewritten_input2 = search_and_replace("\n", multiline_separator, rewritten_input, 0, NULL, NULL); for(p = rewritten_input2; *p ;p++) if(*p >= 0 && *p < ' ') /* @@@FIXME: works for UTF8, but not UTF16 or UTF32 (Mention this in manpage?)*/ *p = ' '; /* replace all control characters (like \r) by spaces */ rl_delete_text(0, strlen(rl_line_buffer)); rl_point = 0; clear_line(); cr(); my_putstr(saved_rl_state.cooked_prompt); rl_insert_text(rewritten_input2); rl_point = 0; /* leave cursor on predictable place */ rl_done = 1; /* accept line immediately */ /* wash those dishes */ if (unlink(tmpfilename)) myerror(FATAL|USE_ERRNO, "could not delete temporary file %s", tmpfilename); free(editor_command2); free(editor_command3); free(editor_command4); free(line_number_as_string); free(column_number_as_string); free(tmpfilename); free(text_to_edit); free(input); free(rewritten_input); free(rewritten_input2); return_key = (char)'\n'; return 0; }
string pretty_time (int t) { return var_eval_system ("date -r " * as_string (t)); }
[[noreturn]] void error(osmium::io::file_compression compression) { std::string error_message {"Support for compression '"}; error_message += as_string(compression); error_message += "' not compiled into this binary."; throw std::runtime_error(error_message); }
static void math_print (string& s, tree t, int level) { if (level <= 0 && is_func (t, PLUS, 2)) { math_print (s, t[0], 0); s << " + "; math_print (s, t[1], 0); } else if (level <= 0 && is_func (t, MINUS, 2)) { math_print (s, t[0], 0); s << " - "; math_print (s, t[1], 1); } else if (level <= 1 && is_func (t, TIMES, 2)) { math_print (s, t[0], 1); s << " * "; math_print (s, t[1], 1); } else if (level <= 1 && is_func (t, OVER, 2)) { math_print (s, t[0], 1); s << " / "; math_print (s, t[1], 2); } else if (level <= 2 && is_func (t, POW, 2)) { math_print (s, t[0], 3); s << "^"; math_print (s, t[1], 2); } else if (level <= 3 && is_func (t, MINUS, 1)) { s << "-"; math_print (s, t[0], 3); } else if (level <= 4 && is_func (t, RSUB, 2)) { math_print (s, t[0], 4); s << "["; math_print (s, t[1], 0); s << "]"; } else if (is_func (t, PLUS, 2) || is_func (t, MINUS, 2) || is_func (t, TIMES, 2) || is_func (t, OVER, 2) || is_func (t, POW, 2) || is_func (t, MINUS, 1) || is_func (t, RSUB, 2)) { s << "("; math_print (s, t, 0); s << ")"; } else if (is_atomic (t)) s << t->label; else if (is_tuple (t)) { int i, n= N(t); s << "[ "; for (i=0; i<n; i++) { if (i != 0) s << ", "; math_print (s, t[i], 0); } if (n != 0) s << " "; s << "]"; } else { int i, n= N(t); if (L(t) == MATH_SQRT) s << "sqrt ("; else s << as_string (L(t)) << " ("; for (i=0; i<n; i++) { if (i != 0) s << ", "; math_print (s, t[i], 0); } s << ")"; } }
void spawn_filter(const char *filter_command) { int input_pipe_fds[2]; int output_pipe_fds[2]; mypipe(input_pipe_fds); filter_input_fd = input_pipe_fds[1]; /* rlwrap writes filter input to this */ mypipe(output_pipe_fds); filter_output_fd = output_pipe_fds[0]; /* rlwrap reads filter output from here */ DPRINTF1(DEBUG_FILTERING, "preparing to spawn filter <%s>", filter_command); assert(!command_pid || signal_handlers_were_installed); /* if there is a command, then signal handlers are installed */ if ((filter_pid = fork()) < 0) myerror(FATAL|USE_ERRNO, "Cannot spawn filter '%s'", filter_command); else if (filter_pid == 0) { /* child */ int signals_to_allow[] = {SIGPIPE, SIGCHLD, SIGALRM, SIGUSR1, SIGUSR2}; char **argv; unblock_signals(signals_to_allow); /* when we run a pager from a filter we want to catch these */ DEBUG_RANDOM_SLEEP; i_am_child = TRUE; /* set environment for filter (it needs to know at least the file descriptors for its input and output) */ if (!getenv("RLWRAP_FILTERDIR")) mysetenv("RLWRAP_FILTERDIR", add2strings(DATADIR,"/rlwrap/filters")); mysetenv("PATH", add3strings(getenv("RLWRAP_FILTERDIR"),":",getenv("PATH"))); mysetenv("RLWRAP_VERSION", VERSION); mysetenv("RLWRAP_COMMAND_PID", as_string(command_pid)); mysetenv("RLWRAP_COMMAND_LINE", command_line); if (impatient_prompt) mysetenv("RLWRAP_IMPATIENT", "1"); mysetenv("RLWRAP_INPUT_PIPE_FD", as_string(input_pipe_fds[0])); mysetenv("RLWRAP_OUTPUT_PIPE_FD", as_string(output_pipe_fds[1])); mysetenv("RLWRAP_MASTER_PTY_FD", as_string(master_pty_fd)); close(filter_input_fd); close(filter_output_fd); if (scan_metacharacters(filter_command, "'|\"><")) { /* if filter_command contains shell metacharacters, let the shell unglue them */ char *exec_command = add3strings("exec", " ", filter_command); argv = list4("sh", "-c", exec_command, NULL); } else { /* if not, split and feed to execvp directly (cheaper, better error message) */ argv = split_with(filter_command, " "); } assert(argv[0]); if(execvp(argv[0], argv) < 0) { char *sorry = add3strings("Cannot exec filter '", argv[0], add2strings("': ", strerror(errno))); write_message(output_pipe_fds[1], TAG_ERROR, sorry, "to stdout"); /* this will kill rlwrap */ mymicrosleep(100 * 1000); /* 100 sec for rlwrap to go away should be enough */ exit (-1); } assert(!"not reached"); } else { DEBUG_RANDOM_SLEEP; signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE - we have othere ways to deal with filter death */ DPRINTF1(DEBUG_FILTERING, "spawned filter with pid %d", filter_pid); close (input_pipe_fds[0]); close (output_pipe_fds[1]); } }
/// set_color builtin. int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) { // By the time this is called we should have initialized the curses subsystem. assert(curses_initialized); // Hack in missing italics and dim capabilities omitted from MacOS xterm-256color terminfo // Helps Terminal.app/iTerm #if __APPLE__ const auto term_prog = parser.vars().get(L"TERM_PROGRAM"); if (!term_prog.missing_or_empty() && (term_prog->as_string() == L"Apple_Terminal" || term_prog->as_string() == L"iTerm.app")) { const auto term = parser.vars().get(L"TERM"); if (!term.missing_or_empty() && (term->as_string() == L"xterm-256color")) { enter_italics_mode = sitm_esc; exit_italics_mode = ritm_esc; enter_dim_mode = dim_esc; } } #endif // Variables used for parsing the argument list. wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); // Some code passes variables to set_color that don't exist, like $fish_user_whatever. As a // hack, quietly return failure. if (argc <= 1) { return EXIT_FAILURE; } const wchar_t *bgcolor = NULL; bool bold = false, underline = false, italics = false, dim = false, reverse = false; // Parse options to obtain the requested operation and the modifiers. int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) { switch (opt) { case 'b': { bgcolor = w.woptarg; break; } case 'h': { builtin_print_help(parser, streams, argv[0], streams.out); return STATUS_CMD_OK; } case 'o': { bold = true; break; } case 'i': { italics = true; break; } case 'd': { dim = true; break; } case 'r': { reverse = true; break; } case 'u': { underline = true; break; } case 'c': { print_colors(streams); return STATUS_CMD_OK; } case ':': { builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]); return STATUS_INVALID_ARGS; } case '?': { return STATUS_INVALID_ARGS; } default: { DIE("unexpected retval from wgetopt_long"); break; } } } // Remaining arguments are foreground color. std::vector<rgb_color_t> fgcolors; for (; w.woptind < argc; w.woptind++) { rgb_color_t fg = rgb_color_t(argv[w.woptind]); if (fg.is_none()) { streams.err.append_format(_(L"%ls: Unknown color '%ls'\n"), argv[0], argv[w.woptind]); return STATUS_INVALID_ARGS; } fgcolors.push_back(fg); } if (fgcolors.empty() && bgcolor == NULL && !bold && !underline && !italics && !dim && !reverse) { streams.err.append_format(_(L"%ls: Expected an argument\n"), argv[0]); return STATUS_INVALID_ARGS; } // #1323: We may have multiple foreground colors. Choose the best one. If we had no foreground // color, we'll get none(); if we have at least one we expect not-none. const rgb_color_t fg = best_color(fgcolors, output_get_color_support()); assert(fgcolors.empty() || !fg.is_none()); const rgb_color_t bg = rgb_color_t(bgcolor ? bgcolor : L""); if (bgcolor && bg.is_none()) { streams.err.append_format(_(L"%ls: Unknown color '%ls'\n"), argv[0], bgcolor); return STATUS_INVALID_ARGS; } // Test if we have at least basic support for setting fonts, colors and related bits - otherwise // just give up... if (cur_term == NULL || !exit_attribute_mode) { return STATUS_CMD_ERROR; } outputter_t outp; if (bold && enter_bold_mode) { writembs_nofail(outp, tparm((char *)enter_bold_mode)); } if (underline && enter_underline_mode) { writembs_nofail(outp, enter_underline_mode); } if (italics && enter_italics_mode) { writembs_nofail(outp, enter_italics_mode); } if (dim && enter_dim_mode) { writembs_nofail(outp, enter_dim_mode); } if (reverse && enter_reverse_mode) { writembs_nofail(outp, enter_reverse_mode); } else if (reverse && enter_standout_mode) { writembs_nofail(outp, enter_standout_mode); } if (bgcolor != NULL && bg.is_normal()) { writembs_nofail(outp, tparm((char *)exit_attribute_mode)); } if (!fg.is_none()) { if (fg.is_normal() || fg.is_reset()) { writembs_nofail(outp, tparm((char *)exit_attribute_mode)); } else { if (!outp.write_color(fg, true /* is_fg */)) { // We need to do *something* or the lack of any output messes up // when the cartesian product here would make "foo" disappear: // $ echo (set_color foo)bar outp.set_color(rgb_color_t::reset(), rgb_color_t::none()); } } } if (bgcolor != NULL && !bg.is_normal() && !bg.is_reset()) { outp.write_color(bg, false /* not is_fg */); } // Output the collected string. streams.out.append(str2wcstring(outp.contents())); return STATUS_CMD_OK; }
string edit_typeset_rep::get_init_string (string var) { return as_string (get_init_value (var)); }