C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
stripansi.c File Reference

removes ansi escape sequences beginning with "\033["" and ending in "m" or "K" More...

#include <cm.h>
#include <stdio.h>
Include dependency graph for stripansi.c:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Detailed Description

removes ansi escape sequences beginning with "\033["" and ending in "m" or "K"

Author
Bill Waller Copyright (c) 2025 MIT License billx.nosp@m.wall.nosp@m.er@gm.nosp@m.ail..nosp@m.com
Date
2026-02-09

Definition in file stripansi.c.

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

C function - strip_ansi(out_str, in_str)

This function iterates through the input string, copying characters to the output buffer while skipping over any ANSI escape sequences. It looks for sequences that start with "\033[" and end with "m" or "K", and removes them from the output.

Parameters
argcArgument count (should be 2 for the program name and input file)
argvArgument vector (argv[1] should be the input file name)

ANSI escape sequences start with "\033[" and end with "m" or "K". This function removes those sequences from the input string and writes the cleaned string to stdout

< Buffer to hold the input string read from the file

< Buffer to hold the cleaned output string

Definition at line 24 of file stripansi.c.

24 {
25 char
26 in_buf[2048];
27 char out_buf[2048];
28 if (argc != 2) {
29 fprintf(stderr, "Usage: %s [file_with_ansi_codes]\n", argv[0]);
30 return 1;
31 }
32 FILE *in_fp = fopen(argv[1], "r");
33 if (!in_fp) {
34 perror("Error opening file");
35 return 1;
36 }
37 while (fgets(in_buf, sizeof(in_buf), in_fp)) {
38 strip_ansi(out_buf, in_buf);
39 fputs(out_buf, stdout);
40 }
41 fclose(in_fp);
42 return 0;
43}
size_t strip_ansi(char *, char *)
Strips ANSI SGR escape sequences (ending in 'm') from string s to d.
Definition futil.c:537

References strip_ansi().

Here is the call graph for this function: