LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   handle control characters (https://www.linuxquestions.org/questions/programming-9/handle-control-characters-4175459317/)

shdd 04-23-2013 12:51 PM

handle control characters
 
I need handle control characters in my program.
this code is a simple pipe:
Code:

#include <stdio.h>
void mypipe( FILE *fd)
{
        char *buf;
        size_t linesize = 0;
        while ( ! feof (fd) )
    {
                        getline (&buf , &linesize , fd);
                        fputs( buf , stdout );
                        fflush(stdout);
        }
        fclose(fd);
}
int main(int argc, char *argv[])
{
        mypipe(stdin);
        return 0;
}

for example if I run this command:
ls --color | ./mypipe
colored text will be display. how can I Ignore escape sequences?

NevemTeve 04-24-2013 05:08 AM

use ls --color=never
your program could filter the escape-sequences, but that wouldn't be easy, because there are many different espace-sequences. See man 5 terminfo for a start.

David the H. 04-25-2013 12:45 PM

"--color" is equivalent to "--color=always". If you use "--color=auto" instead it won't insert escape sequences when the output is a pipe or file, but will add them when it detects a raw terminal.


If you have to code it, however, a simple regular expression like "\033[[][0-9;]+m" can strip out all the color codes, at least. But as NevemTeve said, it would have to be much more complex to capture all of the possible ansi escape sequences.


All times are GMT -5. The time now is 11:28 PM.