Web programming in C lang with TCC

K

Keith Thompson

Seebs said:
I think the only complaint here is that this is not a "higher level version of
C" but "a totally different language", which compiles into C. C does not have
a := assignment operator. A language which does is not a version of C, but
some other kind of language.

A language that has a := assignment operator but is otherwise
identical to C might reasonably be called a "version of C".
(But (x)Harbour is not such a language.)
 
K

Keith Thompson

Seebs said:
Seebs said:
Um, things like Apache expect both characters and most platforms do
not turn \n into two characters. Indeed sizeof("\n") is 2 ...
always. Where things differ is what \n DOES.
Pretty sure it'll be sizeof(char *), not sizeof(char[2]). :)
sizeof "\n" == 2 from 6.3.2.1 p3 (and 6.4.5 p5).

Woah.

That I could be wrong about this for twenty years without noticing sort
of suggests that it's esoteric, but it's still pretty basic stuff. *sigh*

It's one of the three exceptions to the rule that an expression of array
type is converted to a pointer to the array's first element:

The argument of unary "&";
The argument of unary "sizeof";
A string literal in an initializer used to initialize an array object.
 
S

Seebs

... and I emphasixe that Harbour is *more C* than anything else except C
itself.

Uh.

C++, Objective-C, and C With Classes, at the very least, wish to dispute
the point.

-s
 
S

Seebs

Keith muttered:
"> The language used to implement (x)Harbour is irrelevant.
If it wasn't mostly all C and Harbour itself, I wouldn't have come back
here to post. Because my life is now mostly web development with xHarbour, I
replied to this particular post. But it *is* relevant because C is the
underpinning of all of Harbour.

Uh.

The primary implementations of perl, PHP, and Ruby are all in C, too.
"I build C functions all day. I manipulate them and massage them to
create hi-level functions for the app/language called xHarbour. But xHarbour
is basically a set of rebuilt C functions to implement and ease features
that others find difficult in C or non-existent in C. "

If someone then claimed that this was a "high level version of C", that would
be convincing evidence that the person in question was not a C expert, or
indeed, qualified to say much of anything about programming languages.
To correct you, I didn't say Harbour is a version of C, but simply a
much higher level version of C, and is totally implemented by C.

It's not a "version of C" any more than perl, PHP, or Ruby are. That I
can use C code to implement new functions which are then available to Python
programs doesn't make Python a "high level version of C".

Heck, you can use C code to implement functions for most FORTRAN compilers,
that doesn't make Fortran a kind of C.

-s
 
S

Seebs

What good is in other script languages string handling which can't be
made in C? C has regex, strcmp, strncat, strncpy, strstr. In case
there's no one, just write one. It's C way. Can you tell more about
this?

Okay.

It's not a question of whether you CAN do them. It's how easy or expressive
it is.

While people have written regex tools (some of which perhaps support
substitution) in C, that doesn't mean that they're comparable.

Lemme drift off-topic a little. Say I've got Spencer's regex library
to hand, or something similar.

Let's compare two pieces of code. Both are going to take the string
"hello, world", and return the longest subset of it which consists of
a punctuation character (one of ,.! for my purposes) followed by some
string of letters, ending with a vowel.

That regular expression is:
[,.!].*[aeiou]

We'll ignore the fiddly bits of the spec; this is just a sort of example
of how things differ.

int
main(void) {
char *pattern = "[,.!].*[aeiou]";
char *haystack = "hello, world";
char *s;
size_t len;
regex_t preg;
regmatch_t pmatch[10];

if (regcomp(&preg, pattern, 0)) {
fprintf(stderr, "Error compiling regex.\n");
exit(1);
}
if (regexec(&preg, haystack, 1, pmatch, 0) != 0) {
fprintf(stderr, "No match.\n");
exit(1);
}
s = haystack + pmatch[0].rm_so;
len = pmatch[0].rm_eo - pmatch[0].rm_so;

printf("%.*s\n", (int) len, s);
regfree(&preg);

return 0;
}

I'm a moderately experienced programmer who's used the regexec library
a few times before. I had to look several things up in the man page to
do this, and my first try had a bug, but this now does in fact print
", wo".

Now let's look at this in a few other languages.

Perl:

"hello, world" =~ /[,.!].*[aeiou]/;
print "$&\n";

Ruby:
puts "hello, world".match(/[,.!].*[aeiou]/)

Want a better example?

Let's do something fancier. Let's search for the first word after
punctuation, and replace it with "kitty".

Ruby:
x = "hello, world!"
x[/, ([a-z]*)/, 1] = "kitty"
puts x

C:

(Omitting context, just body-of-main here.)

char *pattern = ", \\([a-z]*\\)";
char *haystack = "hello, world!";
char *new;
char *s;
size_t len;
regex_t preg;
regmatch_t pmatch[10];

if (regcomp(&preg, pattern, 0)) {
fprintf(stderr, "Error compiling regex.\n");
exit(1);
}
if (regexec(&preg, haystack, 2, pmatch, 0) != 0) {
fprintf(stderr, "No match.\n");
exit(1);
}
s = haystack + pmatch[1].rm_so;
len = pmatch[1].rm_eo - pmatch[1].rm_so;

new = malloc(strlen(haystack) + strlen("kitty") - len + 1);
sprintf(new, "%.*s%s%s", pmatch[1].rm_so, haystack, "kitty",
haystack + pmatch[1].rm_eo);

printf("%s\n", new);
regfree(&preg);
free(new);

Notice a pattern?

It is frankly STUPID to try to do stuff like this in C just because you like
C. Doing it in C because you are writing the implementation of a higher-level
language? Sure. Doing it in C because you need to do a small amount of
string manipulation in a large program which needs to be C for compelling
reasons (say, an operating system kernel)? Sure.

For extra credit: Did I calculate the allocated space for "new" correctly?
Why or why not?

And most importantly: Why the HELL would you ever choose to use a language
where that question is even coherent for string processing? I know reasons
to do this, but they're... rare.

-s
 
S

Seebs

All the developers of (x)Harbour are C experts (and there are dozens of
these Open Source guys), and usually code *only* in C, using primarily
MinGW's gcc or Borland's 5.5.n versions of C.
Okay.

So, if these people are asked if the Harbour language Hi-C, they'd
probably say: "Yes"

Hmm.

Consider the line of code:
foo := bar

note the lack of semicolon, and the := assignment operator.

If you tell me that a given person X considers that to be a variant of C,
and then tell me that person X is a "C expert", I will believe at most one
of those claims.

Yes, I consider myself a "C expert", although I would say I'm on the very
low end of what I'd consider "expert" level knowledge. There are plenty of
people who know C better than I do. Hundreds, at the very least. :p

-s
 
S

Seebs

Not here. Study a scripting language if you want to learn about it.

While I basically approve of the sentiment... Part of learning a language
really well involves comprehending what it's bad at.

C is bad at some things. Being able to discuss this and work with it makes
us much better C programmers. :)

-s
 
L

lovecreatesbeauty

Perl:
"hello, world" =~ /[,.!].*[aeiou]/;
print "$&\n";

Ruby:
puts "hello, world".match(/[,.!].*[aeiou]/)
....

It is frankly STUPID to try to do stuff like this in C just because you like
C. Doing it in C because you are writing the implementation of a higher-level
language?

Thank you.

What if we had wrapped a C function to implement this interface /
prototype. C doesn't do this just because C along with its library is
general purpose. I think you are an C expert. What if some 3rd party
(it could be you:) library already supports this wrapper? We'll have
the similar one line C code too:

char *my_match(const char *haystack, const char *pattern);
printf(my_match("hello, world!", ", \\([a-z]*\\)"));

I think the other script languages are stupid. Some of them maybe
implemented in C, although they don't make their syntax: operators,
expressions, control flow as same as C. But they still call them C-
background languages[*]. This is stupid and misleading.

Actually, I had a chance to write an auto-ssh script in Perl. It was
just a quick solution. At that time, I did't find suitable C libraries
(libssh2 ?) and Bourne shell tools (expect ?). http://www.cyberspace.org/~jhl/script-ssh-telnet
.. The only languages I want to learn: C, bsh, make, javascript (jQuery
for Ajax, the web is so hot and great nowdays).

I know now how to do this better in Perl and bsh, but I won't try Perl
anymore.
if ($prot =~ /[Tt][Ee][Ll][Nn][Ee][Tt]/){


[*] Perl combines (in the author's opinion, anyway) some of the best
features of C,
 
B

BGB

Mel Smith said:
Keith muttered:
[...]
You've had multiple C experts tell you that Harbour is not a version
of C. Why do you not believe us?

To correct you, I didn't say Harbour is a version of C, but simply a
much higher level version of C, and is totally implemented by C.

Isn't a higher level version of C a version of C?

....


oh wow, maybe my own custom scripting language is a higher-level version
of C, in addition to being related to ActionScript?... now this
justifies tormenting everyone here with every little trivial detail...

yep... so totally C with a scoping model involving dynamic delegation
between class instances, and 128 bit numeric constants in the form
0x01234567_89ABCDEF_DECAFBAD_DEADF00DLX...

C, now with lexical closures...


now, we can get Thomas Mertes in here to start flogging off Seed7 and we
can all get into some philosophical debate over defining syntax in terms
of itself or similar.

yep, so totally C as well...


let the spamming begin...
 
K

Keith Thompson

lovecreatesbeauty said:
I know now how to do this better in Perl and bsh, but I won't try Perl
anymore.
if ($prot =~ /[Tt][Ee][Ll][Nn][Ee][Tt]/){


[*] Perl combines (in the author's opinion, anyway) some of the best
features of C,

<OT>
if ($prot =~ /telnet/i) {
</OT>
 
B

BGB

I don't.


Frankly... I think it's stupid, and won't work.

Look, it's not that I don't like C. I love C. But this is not what C
is good at. C is not really a great language for expressive string
manpulation. C is good for back-end stuff, but it's simply not a good
choice for the kinds of things we use scripting languages for.

yeah...

sadly, I found this one out through personal experience in my own
projects. through this and several other events, I am back mostly
using/working on my own (long-time semi-dead/zombified) scripting
language...

granted, "maybe" TinyC might have turned out better than my own
compiler, but who knows?...

It's like watching someone who's working on replacing screws with nails
on the grounds that he really likes using a hammer, and screws don't
hammer very well.

"some people ask why, I ask, why not?!"

The fact is, even a very good C programmer will not be able to compete
effectively with someone using a more appropriate language for this kind
of thing. It's a waste of energy.

I don't want to give the impression that it's an unheard of waste of
energy; I wrote a wrapper program which "interpreted" C about twenty years
ago. It was an interesting curiousity; it was not useful.

yeah... and I have piles of arguably not-very-useful stuff...
 
B

BGB

Not necessarily. Typically both the function and the caller have
to deal with memory management.

not necessarily...
one can refrain from bothering with it, and let the OS manage freeing
the memory, when the process exits (both Windows and Linux will do this
as a nifty feature of destroying the process's address space).

this generally works plenty well for short-lived processes, since all is
good provided they exit before running out of address space (~3GB worth
on 32-bit systems... not enough real RAM?.. this is where swap comes in...).


yes, yes, this is not, technically, good practice...

but, a good part of engineering is also knowing when to cut corners...

I kind of think it's silly to compare C-style
strings with the strings that belong to another language that has much
better "native" string handing resources.

By saying that one language has much better "native" string handing
resources than another, you've just compared them. I don't find that
silly at all.

[...]
 
L

lovecreatesbeauty

granted, "maybe" TinyC might have turned out better than my own
compiler, but who knows?...

Where's yours?

I use Fedora 15 on my home pc, and deploy development environment on
KVM inside the same pc.

I know the French genius author from his QEMU, so it's nature for me
to get in touch with his another great, the TCC.
 
B

BGB

Where's yours?

in my projects' code...
I don't have an up-to-date version online.

not that my C compiler is liable to be all that useful anyways, as its
performance and reliability were a bit, lacking (it was kind of slow to
compile modules, and the produced code often did not work correctly due
to bugs).

since then, it has been largely re-purposed as a Metadata/FFI generation
tool for my own scripting VM, since "parse all these headers and spit
out a database" is a bit less demanding than compile these C fragments
at run-time and hot-patch them into the running program image.

although my (ActionScript-like) scripting language is not perfect, it
generally works a lot better, and is less prone to blowing up (partly
because it is less complex and, thus, easier to debug, and partly that
being dynamically-typed and interpreter based, most errors mostly just
lead to wonky behavior, rather than crashing the program...).

I use Fedora 15 on my home pc, and deploy development environment on
KVM inside the same pc.

I know the French genius author from his QEMU, so it's nature for me
to get in touch with his another great, the TCC.

ok.


note that I am dealing with application scripting, and not so much CGI
scripting, so it is a different type of scripting language for a
different sort of program (mostly, I am using it for
application-scripts, in particular, in the context of a 3D engine, where
the idea is to use it for things like high-level game logic and
controlling the action in cutscenes and similar...).
 
M

Malcolm McLean

Now let's look at this in a few other languages.

Perl:

        "hello, world" =~ /[,.!].*[aeiou]/;
        print "$&\n";
This is gibberish, unless you know Perl reasonably well. What exactly
is "$&\n" supposed to mean? A string literal is a literal, surely? If
$& is a variable, how is it assigned by line one?

The C version, on the other hand, anyone can understand who knows one
or two high-level programming languages.

I do take your point, however, C is harder to use because a) for
efficiency, the regular expression is compiled, b) errors are passed
up instead of crashing out, c) there's no garbage collection.
 
S

Seebs

What if we had wrapped a C function to implement this interface /
prototype.

Wouldn't help, largely just because of memory management.
C doesn't do this just because C along with its library is
general purpose. I think you are an C expert. What if some 3rd party
(it could be you:) library already supports this wrapper? We'll have
the similar one line C code too:
char *my_match(const char *haystack, const char *pattern);
printf(my_match("hello, world!", ", \\([a-z]*\\)"));

Nice memory leak. Note that the only alternatives to a memory leak here
are worse. If you have a static buffer you're not thread-safe, for instance.
I think the other script languages are stupid.

Then you are wrong. They're awesome tools for some things. (Well.
Ruby is. Perl is useful. Python annoys me a bit but is quite nice.
Lua is quite nice. Let's just not talk about PHP.)
Some of them maybe
implemented in C, although they don't make their syntax: operators,
expressions, control flow as same as C. But they still call them C-
background languages[*]. This is stupid and misleading.

Not really.
[*] Perl combines (in the author's opinion, anyway) some of the best
features of C,

He's entitled to that opinion; after all, it's a matter of his opinions
of what is best about C.

Anyway, I don't think it's particularly misleading.

-s
 
S

Seebs

This is gibberish, unless you know Perl reasonably well.
Maybe.

What exactly
is "$&\n" supposed to mean? A string literal is a literal, surely? If
$& is a variable, how is it assigned by line one?

This all makes sense if you've used sed. :)
The C version, on the other hand, anyone can understand who knows one
or two high-level programming languages.

And in particular, knows C's string handling idioms.
I do take your point, however, C is harder to use because a) for
efficiency, the regular expression is compiled, b) errors are passed
up instead of crashing out, c) there's no garbage collection.

Really, it's all about the string handling. C sucks at it.

-s
 
L

lovecreatesbeauty

char *my_match(const char *haystack, const char *pattern);
printf(my_match("hello, world!", ", \\([a-z]*\\)"));

Nice memory leak. ?Note that the only alternatives to a memory leak here
are worse. ?If you have a static buffer you're not thread-safe, for instance.

Thank you.

I guess you were pointing out the possible memory leak in that
proposed design of my_match(), right?

There's no necessary to relate Memory Leak with The C Language, right?

How about the functions specified in REGEX(3). They have regfree(). Is
it possible to have memory leak when I use these functions?

For example, is there risk of memory leak in this kind of new design?

char *my_match(const char *haystack,
const char *pattern,
char *result,
size_t pos);

If it still has memory leak anyway, is it possible to improve it
further to avoid the memory leak? Can an expert like you implement it
without memory leak? Can you improve it with you expertise and
experience?
 
M

Malcolm McLean

And in particular, knows C's string handling idioms.
That's true. There's no "string" type in C, unlike most programming
languages. Probably there should be.
 
J

James

That's true. There's no "string" type in C, unlike most programming
languages. Probably there should be.

You can build what you want from scratch; that is beauty of C in essence
right?

:^)
 

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

No members online now.

Forum statistics

Threads
474,091
Messages
2,570,604
Members
47,223
Latest member
smithjens316

Latest Threads

Top