want a "here doc" equivalent in "c"

T

Tom Rodman

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_
 
P

Pieter Droogendijk

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.programmer and prepare to be helped, or rephrase the
question to something not involving anything platform-specific.
 
D

Dave Vandervies

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_string_literal(FILE *in,FILE *out)
{
int c;
while(c=getc(in) != EOF)
{
switch(c)
{
case '\n':
fputs("\\n\"\n\"",out);
break;
case '"':
fputs("\\\"",out);
break;
case '\\':
fputs("\\\\",out);
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.
 
M

Martin Dickopp

Tom Rodman said:
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
 
D

Dave Vandervies

Pieter Droogendijk said:
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
 
A

August Derleth

(e-mail address removed) (Dave Vandervies) wrote in
on Wed 03 Sep 2003 05:34:12p:
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.
 
J

Jack Klein

(e-mail address removed) (Dave Vandervies) wrote in
on Wed 03 Sep 2003 05:34:12p:


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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

Jeff

Dave Vandervies said:
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 (e-mail address removed)
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.
 
P

Pieter Droogendijk

On Wed, 3 Sep 2003 23:34:12 +0000 (UTC)
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).
 
N

Nils Petter Vaskinn

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.

No the question is clarly on-topic, to rephrase the OP's question (with
apologies to the OP if I misunderstand) "What would I do in standard C to
achieve a result similar to that of bash-s here-documents". The question
is NOT platform specific, while the example he used to describe what he
wanted to achieve is.

An appropriate response could have been something like "Your example is
platform specific, please explain in plain english what you want to
achieve"

OP: Look at the response from Derk Gwen, and ignore the people that are so
used to complaining about off-topic posts that they can't recognize an
on-topic.

NPV
 
P

pete

Nils said:
No the question is clarly on-topic,

I think that translating text into a C string, is on topic,
regardless of what the text says.
Translating text into a C string was the question, wasn't it ?

I thought that this part:
"all kinds of nasty stuff in here including single and double quotes
and bashslashes .."
had to do with how you quote quotes and backslashes,
which is in fact, something that gets asked here from time to time.
 
A

Alan Balmer

This group no Charter.

Can have a charter without having a Charter :)

Someone should write one, just to have an answer to the question of
"where's the charter?" and for ready reference to an "authority" at
those time when it might be useful. (No, I'm not volunteering.)
 
R

Richard Heathfield

Alan said:
Someone should write one, just to have an answer to the question of
"where's the charter?" and for ready reference to an "authority" at
those time when it might be useful. (No, I'm not volunteering.)

Some of us prefer the group /not/ to have a charter, not least because of
the Schadenfreude that inevitably results from someone thoughtlessly
suggesting that we refer to it.
 
A

August Derleth

Richard Heathfield said:
This group no Charter.

We don't have a Charter, but we do have a FAQ which is fairly complete
and, IMHO, authoritative. In other words, the authority that would be
granted a Charter has de facto devolved onto the FAQ, making what the
FAQ says the law around here.

If people read the FAQ before posting (as I did, all that time ago),
they'd know exactly what was up in here and might not post that
VisualAda++ question about how to access a specific model of video
card under MSWinXPR6.765443 Patchlevel 23.998.

Maybe.
 
R

Richard Heathfield

August said:
We don't have a Charter, but we do have a FAQ which is fairly complete
and, IMHO, authoritative. In other words, the authority that would be
granted a Charter has de facto devolved onto the FAQ, making what the
FAQ says the law around here.

Please cite the relevant FAQ, the one that deals with topicality in
comp.lang.c.
If people read the FAQ before posting (as I did, all that time ago),
they'd know exactly what was up in here and might not post that
VisualAda++ question about how to access a specific model of video
card under MSWinXPR6.765443 Patchlevel 23.998.

Which FAQ tells them that VisualAda++ is off-topic here? I don't think the
word "topic" is even mentioned in the FAQ. It certainly doesn't appear in
the index of the book version.
 
A

August Derleth

Please cite the relevant FAQ, the one that deals with topicality in
comp.lang.c.

OK. I was wrong about the FAQ being about comp.lang.c in that way.

Jeez, the one way I can get people to notice me is to screw up... :)
Which FAQ tells them that VisualAda++ is off-topic here? I don't think
the word "topic" is even mentioned in the FAQ. It certainly doesn't
appear in the index of the book version.

http://www.eskimo.com/~scs/C-faq/q19.1.html -- First topic in the section
System Dependencies. It says:

"Since comp.lang.c is oriented towards topics that C does deal with, you
will usually get better answers to these questions by referring to a
system-specific newsgroup such as comp.unix.questions or
comp.os.msdos.programmer, and to the FAQ lists for these groups."

It isn't as hardassed as most clc regs are, but it does gently state that
we deal with Standard C, not Ada, not Pascal, not system-specific
extensions to C.

And if we see a post about VisualAda++, we will storm Redmond and shove
the One Boot up the Gates of Hell.

So was I wrong? Yes, I was. Will I admit that? No, not really. I can still
quote mostly-relevant portions of the FAQ and appeal to the common sense
of Usenetters.
 
A

August Derleth

Alan Balmer wrote:
Some of us prefer the group /not/ to have a charter, not least because
of the Schadenfreude that inevitably results from someone thoughtlessly
suggesting that we refer to it.

And I might write one, just for the Schadenfreude that will result when
someone smugly says that we don't have one.

.... grumble grumble grumble ...
 
R

Richard Heathfield

August said:
on Thu 04 Sep 2003 06:34:49p:


http://www.eskimo.com/~scs/C-faq/q19.1.html -- First topic in the section
System Dependencies. It says:

"Since comp.lang.c is oriented towards topics that C does deal with, you
will usually get better answers to these questions by referring to a
system-specific newsgroup such as comp.unix.questions or
comp.os.msdos.programmer, and to the FAQ lists for these groups."

I don't suppose I should be too surprised that this sentence doesn't appear
in my paper edition. Clearly, it's been edited out as being irrelevant to
casual purchasers.
It isn't as hardassed as most clc regs are, but it does gently state that
we deal with Standard C, not Ada, not Pascal, not system-specific
extensions to C.

Yes, that's a fair point.
And if we see a post about VisualAda++, we will storm Redmond and shove
the One Boot up the Gates of Hell.

Why wait for a post about VisualAda++? If it feels good, do it.
So was I wrong? Yes, I was. Will I admit that? No, not really. I can still
quote mostly-relevant portions of the FAQ and appeal to the common sense
of Usenetters.

The common sense of Usenetters? You're new here, right? ;-)
 
R

Richard Heathfield

August said:
And I might write one, just for the Schadenfreude that will result when
someone smugly says that we don't have one.

But since any charter you write would be yours, not ours, the Schadenfreude
would be ours, not yours.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top