Scroll to navigation

R_CONS(3) Library Functions Manual R_CONS(3)

NAME

r_consRadare2 Console Input/Output Library API

SYNOPSIS

#include <r_cons.h>

DESCRIPTION

The r_cons library provides console input/output functionality for radare2, including text output, input handling, color management, canvas drawing, and line editing. It abstracts terminal operations and provides a unified interface for console interactions.

Key concepts:

  • RCons is the main console structure managing output, input, and state.
  • RConsCanvas allows drawing text-based graphics and layouts.
  • RLine handles line editing and command history.
  • Color palettes support theming and syntax highlighting.
  • Grep functionality filters and processes output.

INITIALIZATION

Initialization functions create, clone and free console contexts and ancillary state used throughout radare2; these are intended to establish a working `RCons` instance (or duplicate its execution context) before any I/O or visual operations are performed.

RCons * (void)

Creates a new console context.

void (RCons *cons)

Frees a console context.

RCons * (void)

Returns the global console singleton instance.

OUTPUT

Output helpers format, buffer and emit text to the active console. They are the primary API used by radare2 command handlers to present results, so they are designed to be safe for repeated, high-frequency use and to cooperate with grep/pager/visual layers.

int (RCons *cons, const char *format, ...)

Prints formatted text to the console.

void (RCons *cons, const char *str)

Prints a string to the console.

void (RCons *cons, const char *str)

Prints a string followed by a newline.

void (RCons *cons)

Flushes the console output buffer.

void (RCons *cons)

Prints a newline character.

INPUT

Input helpers provide both simple blocking reads and richer prompts with optional editing; these are used by interactive commands and the visual layer to collect user keystrokes or whole lines, and support timeouts and pushed input for remote/automated workflows.

int (RCons *cons)

Reads a single character from input.

int (RCons *cons, char *buf, int len, int argc, const char **argv)

Reads a line from input into the buffer.

char * (RCons *cons, const char *msg)

Prompts for user input with a message.

bool (RCons *cons, int def, const char *fmt, ...)

Prompts for a yes/no answer with default value.

CONTROL

Control routines manipulate the terminal state (cursor, screen, raw mode, mouse and similar settings). They are used to prepare the terminal for visual rendering or to temporarily change behaviour while a command runs.

void (RCons *cons)

Clears the console screen.

void (RCons *cons, int x, int y)

Moves the cursor to the specified position.

void (RCons *cons, int cursor)

Shows or hides the cursor.

void (RCons *cons, bool is_raw)

Sets raw mode for direct input handling.

COLORS

Color management APIs maintain palettes and convert palette entries into escape sequences; these functions are used by output and canvas codepaths to apply consistent theming and highlighting across the UI.

RColor (RCons *cons, const char *key)

Gets a color from the palette by key.

bool (RCons *cons, const char *key, const char *val)

Sets a color in the palette.

void (RCons *cons)

Initializes the color palette.

void (RCons *cons)

Sets random colors in the palette.

char * (RCons *cons, char *outstr, size_t sz, RColor *rcolor)

Converts a color to ANSI escape sequence string.

CANVAS

Canvas APIs implement an off-screen textual drawing surface that can be composed and flushed to the console; the visual subsystem and custom UI widgets use canvases to build complex layouts without scattering escape sequences through business logic.

RConsCanvas * (RCons *cons, int w, int h, int flags)

Creates a new canvas with specified dimensions.

void (RConsCanvas *c)

Frees a canvas.

void (RConsCanvas *c, const char *_s)

Writes text to the canvas.

void (RConsCanvas *c, int x, int y)

Moves the canvas cursor.

void (RConsCanvas *c, int x, int y, int w, int h, const char *color)

Draws a box on the canvas.

void (RConsCanvas *c, int x, int y, int x2, int y2, RCanvasLineStyle *style)

Draws a line on the canvas.

GREP

Grep helpers provide filtering and tokenization of textual output so command code can set filters that limit what is displayed; these are typically applied before printing long command outputs to the console.

void (RCons *cons, const char *grep)

Sets grep filtering for output.

void (RCons *cons, const char *str)

Sets a grep expression.

char * (char *cmd, const char *quotestr)

Strips grep commands from a string.

LINE EDITING

Line-editing utilities expose an editable input line with history and basic editing shortcuts; they are used by interactive modes that accept commands from the user and by higher-level shells embedded in radare2.

RLine * (RCons *cons)

Creates a new line editor.

void (RLine *line)

Frees a line editor.

const char * (RCons *cons)

Reads a line with editing capabilities.

void (RLine *line, const char *prompt)

Sets the prompt for line input.

bool (RLine *line, const char *text)

Adds a line to history.

PIXEL

Pixel buffer APIs offer a small raster-like buffer for representing monochrome or low-resolution images that may be converted to character graphics and flushed to the console by the pixel renderer.

RConsPixel * (int w, int h)

Creates a new pixel buffer.

void (RConsPixel *p)

Frees a pixel buffer.

void (RConsPixel *p, int x, int y, ut8 v)

Sets a pixel value.

void (RCons *cons, RConsPixel *p, int sx, int sy)

Flushes the pixel buffer to console.

EXAMPLES

Examples below show common usage patterns observed across `libr/core/` and the `libr/cons` implementation, including context cloning for background tasks, switching to raw mode for visual rendering, and basic printing/reading flows. Basic output:

RCons *cons = r_cons_new();
r_cons_printf(cons, "Hello, %s!\n", "world");
r_cons_flush(cons);
r_cons_free(cons);

Reading input:

RCons *cons = r_cons_new();
char buf[256];
r_cons_fgets(cons, buf, sizeof(buf), 0, NULL);
r_cons_free(cons);

Using colors:

RCons *cons = r_cons_new();
RColor red = r_cons_pal_get(cons, "red");
char *color_str = r_cons_rgb_str(cons, NULL, 0, &red);
r_cons_printf(cons, "%sError!%s\n", color_str, Color_RESET);
free(color_str);
r_cons_flush(cons);
r_cons_free(cons);

Canvas drawing:

RCons *cons = r_cons_new();
RConsCanvas *canvas = r_cons_canvas_new(cons, 80, 24, 0);
r_cons_canvas_box(canvas, 10, 5, 20, 10, Color_BLUE);
r_cons_canvas_write_at(canvas, "Hello", 15, 10);
r_cons_canvas_print(canvas);
r_cons_canvas_free(canvas);
r_cons_free(cons);

Line editing:

RCons *cons = r_cons_new();
RLine *line = r_line_new(cons);
r_line_set_prompt(line, "> ");
const char *input = r_line_readline(cons);
if (input) {
    r_cons_printf(cons, "You entered: %s\n", input);
}
r_line_free(line);
r_cons_free(cons);

SEE ALSO

r_core(3), r_util(3)

AUTHORS

The radare2 project team.

September 20, 2025 Debian