473,569 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

want a "here doc" equivalent in "c"

I want to embed a bash script inside the c program.

How could you do the below bash snippet in "c"?:

cat > /tmp/foo <<\__EOD_
all kinds of nasty stuff in here including single and double quotes
and bashslashes ..
(note that the here doc delimiter is preceded by a "\"
__EOD_

--
regards,
Tom Rodman
pls run for my address:
perl -e 'print unpack("u", "1\:6UP\,\$\!T\ <F\]D\;6\%N\+F\-O\;0H\`");'
Nov 13 '05 #1
27 8014
On Wed, 03 Sep 2003 15:14:28 -0500
Tom Rodman <Use-Author-Address-Header@[127.1]> wrote:
I want to embed a bash script inside the c program.
Standard C knows nothing of 'embed', 'bash' or 'script'.
How could you do the below bash snippet in "c"?:

cat > /tmp/foo <<\__EOD_
all kinds of nasty stuff in here including single and double quotes
and bashslashes ..
(note that the here doc delimiter is preceded by a "\"
__EOD_


Ask this in comp.unix.progr ammer and prepare to be helped, or rephrase the
question to something not involving anything platform-specific.

--
char*x(c,k,s)ch ar*k,*s;{if(!k) return*s-36?x(0,0,s+1):s ;if(s)if(*s)c=1 0+(c?(x(
c,k,0),x(c,k+=* s-c,s+1),*k):(x(* s,k,s+1),0));el se c=10;printf(&x( ~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0 ,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+, )/'///*");}
Nov 13 '05 #2
Tom Rodman <Use-Author-Address-Header@[127.1]> writes:
I want to embed a bash script inside the c program.

How could you do the below bash snippet in "c"?:

cat > /tmp/foo <<\__EOD_
all kinds of nasty stuff in here including single and double quotes
and bashslashes ..
(note that the here doc delimiter is preceded by a "\"
__EOD_

#include <stdlib.h>
#include <stdio.h>

int main (void)
{
FILE *f;

f = fopen ("/tmp/foo", "w");
if (f == NULL)
{
fputs ("Cannot create /tmp/foo.\n", stderr);
return EXIT_FAILURE;
}

if (fputs ("\
all kinds of nasty stuff in here including single and double quotes\n\
and bashslashes ..\n\
(note that the here doc delimiter is preceded by a \"\\\"\n", f) == EOF
|| fclose (f) == EOF)
{
fputs ("Error writing to /tmp/foo.\n", stderr);
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
Martin
Nov 13 '05 #3
In article <20************ *************@t igris.pounder.s ol.net>,
Tom Rodman <Use-Author-Address-Header@[127.1]> wrote:
I want to embed a bash script inside the c program.

How could you do the below bash snippet in "c"?:

cat > /tmp/foo <<\__EOD_
all kinds of nasty stuff in here including single and double quotes
and bashslashes ..
(note that the here doc delimiter is preceded by a "\"
__EOD_


Treating a chunk of text as a bash script once you've got it embedded
in your program is beyond the scope of this newsgroup (but why don't
you just create it as a separate file and use system() to ask bash to
run it?), but:

You can pack a big chunk of text nicely into a string literal by doing
something like this:
--------
const char *my_long_string =
"This is a nice long string.\n"
"Note that every line ends with a '\\n\"', and we're taking advantage\n"
"of the string-splicing feature.\n"
"Note that single quotes inside string literals don't need to be escaped.\n";
--------
You're not guaranteed to be able to embed arbitrarily long strings this
way (the Standard places a minimum on the maximum length compilers can
restrict you to, but a compiler is allowed to complain about or reject
anything beyond that minimum[1]), but in practice I'd be surprised to see
GCC (which I assume you're using) choke on any length of string literal.

Something not entirely unlike this snippet (typed directly into the editor
for my posting software, never seen a compiler, usual disclaimers apply)
might help you with escaping the double quotes and backslashes if you'd
rather not do it by hand:

--------
/*This code as posted is a demonstration-of-concept and has never seen a
compiler, let alone been tested. The usual disclaimers apply.
Output will need fixing up by hand at beginning (adding '"' at the
beginning of the first line) and end (removing '"' at the beginning of
the nonexistent just-past-last line), but should be appropriately
escaped for inclusion into C source as a big string literal otherwise.
Caller is responsible for checking ferror(in) and acting appropriately
on error.
*/
void convert_to_stri ng_literal(FILE *in,FILE *out)
{
int c;
while(c=getc(in ) != EOF)
{
switch(c)
{
case '\n':
fputs("\\n\"\n\ "",out);
break;
case '"':
fputs("\\\"",ou t);
break;
case '\\':
fputs("\\\\",ou t);
break;
default:
putc(c,out);
break;
}
}
}
--------
dave

[1] Actually, it doesn't quite even guarantee this, but the intention
is clear enough that even the DS9k implementors had to try pretty
hard to get around it.

--
Dave Vandervies dj******@csclub .uwaterloo.ca
Maybe I should add a line that says, "No matter what you do, it is sure
to annoy at least one of the regulars".
--Billy Chambless in comp.lang.c
Nov 13 '05 #4
In article <20************ *************@b inky.homeunix.o rg>,
Pieter Droogendijk <gi*@binky.home unix.org> wrote:
or rephrase the
question to something not involving anything platform-specific.


Since you seem to realize that it was in fact a C question, is there any
particular reason you couldn't answer the C question instead of telling
the OP "This isn't off topic, but go away anyways"?
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
I have neither the need, the time, or the inclination to put words into your
mouth. You are perfectly capable of damaging your reputation without any help
from me. --Richard Heathfield roasts a troll in comp.lang.c
Nov 13 '05 #5
dj******@csclub .uwaterloo.ca (Dave Vandervies) wrote in
news:bj******** **@tabloid.uwat erloo.ca on Wed 03 Sep 2003 05:34:12p:
In article <20************ *************@b inky.homeunix.o rg>,
Pieter Droogendijk <gi*@binky.home unix.org> wrote:
or rephrase the
question to something not involving anything platform-specific.


Since you seem to realize that it was in fact a C question, is there any
particular reason you couldn't answer the C question instead of telling
the OP "This isn't off topic, but go away anyways"?


Ah, but it /is/ off-topic for comp.lang.c. This group's charter is for
Standard C (C89 and C99, mainly, but K&R pre-Standard C is also acceptable
for historic reasons), and Standard C is not as platform-specific as OS
shells are. Therefore, Standard C has nothing to say about here documents,
bash, or any behaviors thereof.

Asking us to translate a snippet of platform-specific code is not only
unfair to us (who will probably give a bad answer because we don't know
the behavior of the code), but is markedly stupid behavior. It wastes your
time, our time, and the bandwidth of this newsgroup, making it less likely
that we'll /ever/ help you, even if you should stumble upon a good
question. After all, we're none of us paid for this.

Nov 13 '05 #6
August Derleth wrote:
Ah, but it /is/ off-topic for comp.lang.c. This group's charter is for
Standard C


This group no Charter.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #7
On Thu, 04 Sep 2003 01:58:28 GMT, August Derleth
<li************ *****@onewest.n et> wrote in comp.lang.c:
dj******@csclub .uwaterloo.ca (Dave Vandervies) wrote in
news:bj******** **@tabloid.uwat erloo.ca on Wed 03 Sep 2003 05:34:12p:
In article <20************ *************@b inky.homeunix.o rg>,
Pieter Droogendijk <gi*@binky.home unix.org> wrote:
or rephrase the
question to something not involving anything platform-specific.


Since you seem to realize that it was in fact a C question, is there any
particular reason you couldn't answer the C question instead of telling
the OP "This isn't off topic, but go away anyways"?


Ah, but it /is/ off-topic for comp.lang.c. This group's charter is for

^^^^^^^

While I agree with everything you said, you are wrong where
highlighted above. This group has no charter, it started long before
there were newsgroup charters.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #8

"Dave Vandervies" <dj******@csclu b.uwaterloo.ca> wrote in message
news:bj******** **@tabloid.uwat erloo.ca...
In article <20************ *************@b inky.homeunix.o rg>,
Pieter Droogendijk <gi*@binky.home unix.org> wrote:
or rephrase the
question to something not involving anything platform-specific.
Since you seem to realize that it was in fact a C question, is there any
particular reason you couldn't answer the C question instead of telling
the OP "This isn't off topic, but go away anyways"?
dave

--
Dave Vandervies

dj******@csclub .uwaterloo.ca I have neither the need, the time, or the inclination to put words into your mouth. You are perfectly capable of damaging your reputation without any help from me. --Richard Heathfield roasts a troll in

comp.lang.c
You are wrong. The OP question is clearly off-topic. Although the OP
mentioned C language in his post, he is asking a platform-specific question,
it is off-topic. We should kindly redirect him to the relevant group, it is
good for him.
--
Jeff
Nov 13 '05 #9
On Wed, 3 Sep 2003 23:34:12 +0000 (UTC)
dj******@csclub .uwaterloo.ca (Dave Vandervies) wrote:
In article <20************ *************@b inky.homeunix.o rg>,
Pieter Droogendijk <gi*@binky.home unix.org> wrote:
or rephrase the
question to something not involving anything platform-specific.


Since you seem to realize that it was in fact a C question, is there any
particular reason you couldn't answer the C question instead of telling
the OP "This isn't off topic, but go away anyways"?


Questions to this newsgroup are topical if they are questions about Standard C
(C89/C99). This question was about translating a bash script. While he mentioned
C, it was in fact A. a homework question and B. off-topic. The first deserves
plonking and the second deserves redirection.
Giving him the option to rephrase the question to something not containing any
off-topic material, means 'translate your question to one that is topical here'
(like: how do I write text to a file).

--
char*x(c,k,s)ch ar*k,*s;{if(!k) return*s-36?x(0,0,s+1):s ;if(s)if(*s)c=1 0+(c?(x(
c,k,0),x(c,k+=* s-c,s+1),*k):(x(* s,k,s+1),0));el se c=10;printf(&x( ~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0 ,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+, )/'///*");}
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
9132
by: Jim Hill | last post by:
I've done some Googling around on this and it seems like creating a here document is a bit tricky with Python. Trivial via triple-quoted strings if there's no need for variable interpolation but requiring a long, long formatted arglist via (%s,%s,%s,ad infinitum) if there is. So my question is: Is there a way to produce a very long...
5
3468
by: karteikarte | last post by:
hi, i just wondered how to retrieve the 4digit key of this script? anybody who can help? best regards Karteikarte <script type="text/javascript"> <!-- Key-code script by Bart Jellema var code=0 var mul=1
3
18252
by: Summit | last post by:
Does anyone know what the C# equivalent for VB6 End is? I'm starting up a form with a boolean test. If I fail, I just want to end the app. Even though I close the form, it picks up on the line after the bracket and continues loading the form. Here's my snippet: DalMan._drCurrentUser = DalMan._dtUsers.FindByUserName("Init"); if...
1
9163
by: gslim | last post by:
I am trying to implement the busybox sample from // From Mark Wagner // http://blogs.crsw.com/mark/articles/642.aspx When I get to this line I get an access denied error. Could someone give me an idea why this might be? Here is the entire function
35
29199
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. import shutil, os, sys
206
8222
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of...
13
7942
by: xzzy | last post by:
None of the following properly do the VB.net double quote conversion because all of the following in csharp convert to \" instead of just a double quote: " I have tried: char myDoubleQuote = (char)34; string myDoubleQuote = "" + (char)34;
1
3551
by: murthychvrm | last post by:
why we declare "ReportDocument doc;" in this coding?what it is doing here? using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI;
0
1430
by: vhrao | last post by:
I want to Encrypt and Decrypt different elements of xml file with different RSACryptoServiceProvider keys RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams); string publicKey = rsaKey.ToXmlString(false); RSACryptoServiceProvider rsaKey2 = new RSACryptoServiceProvider(cspParams2); string publicKey2 =...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.