LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2013, 12:51 PM   #1
shdd
LQ Newbie
 
Registered: Apr 2013
Posts: 1

Rep: Reputation: Disabled
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?
 
Old 04-24-2013, 05:08 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
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.
 
Old 04-25-2013, 12:45 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
"--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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to handle file names with space characters in a shell script? Sammy_G Programming 14 04-27-2009 10:40 AM
backup2l doesn't seem to handle special characters in filenames Excalibre Linux - Software 1 10-21-2008 09:14 AM
How to handle odd characters in sed maker10 Programming 6 07-02-2008 03:15 PM
oggenc does not handle special characters correctly hansalfredche Linux - Software 2 08-24-2007 03:42 AM
Handle escape characters in a string Helene Programming 7 05-01-2004 11:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration