C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
stripansi.c
Go to the documentation of this file.
1/** @file stripansi.c
2 @brief removes ansi escape sequences beginning with "\033["" and ending in
3 "m" or "K"
4 @author Bill Waller
5 Copyright (c) 2025
6 MIT License
7 billxwaller@gmail.com
8 @date 2026-02-09
9 */
10
11#include <cm.h>
12#include <stdio.h>
13
14/** C function - strip_ansi(out_str, in_str)
15 @details This function iterates through the input string, copying characters
16 to the output buffer while skipping over any ANSI escape sequences. It looks
17 for sequences that start with "\033[" and end with "m" or "K", and removes
18 them from the output.
19 @param argc Argument count (should be 2 for the program name and input file)
20 @param argv Argument vector (argv[1] should be the input file name)
21 @details ANSI escape sequences start with "\033[" and end with "m" or "K".
22 This function removes those sequences from the input string and writes the
23 cleaned string to stdout */
24int main(int argc, char *argv[]) {
25 char
26 in_buf[2048]; /**< Buffer to hold the input string read from the file */
27 char out_buf[2048]; /**< Buffer to hold the cleaned output string */
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}
int main(int argc, char **argv)
Capture the current terminal settings for later restoration.
Definition enterchr.c:38
size_t strip_ansi(char *, char *)
Strips ANSI SGR escape sequences (ending in 'm') from string s to d.
Definition futil.c:537