autoescape

Discuss Applications

autoescape

Postby Tux » Sun Apr 25, 2004 8:45 am

Is there a program that will automatically escape characters in a text stream that would otherwise choke bash (like the function in PHP)
Tux
guru
guru
 
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Postby Void Main » Sun Apr 25, 2004 7:34 pm

I'm not sure I follow. Can you give an example?
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Tux » Tue Apr 27, 2004 9:24 am

A program that would take input from stdin and escape all the special characters and spit it out again, I should probably knock up a script for it.

eg.
Say I have a text file called crap with rg46r3yj^ *hdfd3435( in it.
I could do
Code: Select all
cat crap | autoescape
to give me, rg46r3yj\^\ \*hdfd3435\(
Tux
guru
guru
 
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Postby Void Main » Tue Apr 27, 2004 9:33 am

Well, you can strip the non-printable chars with "strings" but you just want them escaped, not sure why, but I'll go along with you on this. I assume you mean sort of like "od -c" but without the formatting? I don't know of one off the top of my head but it would be *extremely* easy to write one in C. Let's see what we can whip up in the next 5 minutes.

EDIT: I'm not sure what you mean by "special characters". I thought you meant "non-printable" chars but I see you have "^" and "*" which are printable. "special chars" in what context? Different chars are considered special depending on what you mean. I guess I need to see the bigger picture of what you are getting at. I hate to get bits and pieces only to work out a very complex solution for something that was very simple in the first place. :)
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Tux » Tue Apr 27, 2004 9:54 am

Void Main wrote:EDIT: I'm not sure what you mean by "special characters". I thought you meant "non-printable" chars but I see you have "^" and "*" which are printable. "special chars" in what context? Different chars are considered special depending on what you mean. I guess I need to see the bigger picture of what you are getting at.


I meant the bash special characters.

I hate to get bits and pieces only to work out a very complex solution for something that was very simple in the first place. :)

The funny thing is, I have no idea why I wan't do this - you just made me consider it.
We can forget about it if you like because I have no actual requirement for it, it just popped into my head sometime. Maybe i'm crazy ;)
Tux
guru
guru
 
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Postby Void Main » Tue Apr 27, 2004 10:03 am

Again, it would only be a few lines of C (litereally). It's no big deal. I guess I could whip something up while I eat my lunch. :)
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Tux » Tue Apr 27, 2004 10:14 am

Void Main wrote:Again, it would only be a few lines of C (litereally). It's no big deal. I guess I could whip something up while I eat my lunch. :)


If you're really that bored, go for it. Otherwise, enjoy your lunch my friend :D
Tux
guru
guru
 
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Postby Void Main » Tue Apr 27, 2004 12:50 pm

This only reads from stdin (has to have stuff piped to it or type in it directly):

escape.c
Code: Select all
#include <stdio.h>
int main() {

  char c;

  while ((c = getchar ()) != EOF) {
    switch (c) {
      case '!' : case '@' : case '#' : case '$' : case '%' : case '^' : case '&' :
      case '*' : case '(' : case ')' : case '-' : case '_' : case '=' : case '+' :
      case '\\': case '|' : case '`' : case '~' : case '\'': case '\"': case '/' :
      case '?' : case '.' : case '>' : case ',' : case '<' :
               printf("\\%c",c);
               break;
      default: printf("%c",c);
    }
  }
}


or shorter:

Code: Select all
#include <stdio.h>
int main() {
  char c;
  char str[] = "!@#$%^&*()-_=+|`~""/?.>,<";
  while ((c = getchar ()) != EOF) {
    if (strchr(str,c)) printf("\\%c",c);
    else printf("%c",c);
  }
}


$ make escape
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Tux » Wed Apr 28, 2004 11:43 am

Neato.
Tux
guru
guru
 
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Postby Void Main » Wed Apr 28, 2004 12:51 pm

Of course you could also use "sed" or probably a few other standard UNIX utils:

Pipe your text through the following command:
Code: Select all
sed 's/\([!@#$%^&*()_=+|`~"/?.>,<]\)/\\\1/g'


e.g.
Code: Select all
$ echo "Hmm... &This might work! Maybe?" | sed 's/\([!@#$%^&*()_=+|\`~"/?.>,<]\)/\\\1/g'
Hmm\.\.\. \&This might work\! Maybe\?
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA


Return to Applications

Who is online

Users browsing this forum: No registered users and 1 guest