autoescape

Discuss Applications
Post Reply
Tux
guru
guru
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

autoescape

Post by Tux »

Is there a program that will automatically escape characters in a text stream that would otherwise choke bash (like the function in PHP)

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

I'm not sure I follow. Can you give an example?

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

Post by Tux »

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\(

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

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. :)

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

Post by Tux »

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 ;)

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

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. :)

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

Post by Tux »

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

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

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

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

Post by Tux »

Neato.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

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\?

Post Reply