A tale in 3 pictures. In which our hero wonders if he can and doesn't stop to ask if he should.

#c #programming

C preprocessor abuse

C preprocessor abuse. GitHub Gist: instantly share code, notes, and snippets.

Gist
@citizen428 some slight modifications, and you can use cpp -E + that to make a static site generator using the preprocessor.
@necrophcodr Funny you'd say that, because this (`clang -I. -P -E`) but in another context is how this little side quest started.

@citizen428 I mean it has simple uses too, like adding simple include statements in any file, or, as you've shown, macros.

I've used it to make a lockfile for my Containerfile, and I'd just build using cpp -E <file> | docker build -f- <...>

@citizen428

So, it's some kind of chtml...

(you'll definitively want to put the ```#define HTML``` in a .h.tml file included by abuse.c)

@citizen428 I highly recommend combining this with a MAP macro.
@ryanc Please don’t give me ideas πŸ˜‚
@citizen428 Fine. I will not inflict further ideas upon you, but why do you hate fun?
@ryanc I'm just trying to avoid getting nerd sniped into spending any more time on this. I have a much bigger preprocessor abuse planned :D
@citizen428 omg, you even serve it right from C code?! God, I want to unsee this...

@siguza Maximum cursed is what I was going for, so yes. If you prefer CGI and another web server you could use kcgi:

https://kristaps.bsd.lv/kcgi/

kcgi: minimal CGI and FastCGI library for C/C++

A C/C++ library for security-oriented web applications.

@citizen428 Thanks for sharing the code.

In a weird way, this is exactly what the C preprocessor is designed to do.

I still don't want to debug a bigger website though. 

@[email protected] $DEITY no, I'd never want to actually work with this outside of a little late-night hack session.
@citizen428 seems perfectly sensible to me. C pre processor is fast and well-understood.
@citizen428 I mean, this is actually not *that* far off from what lispers do when generating sites in lisp
@reiddragon Sure, but compared to operating on an AST simple text substitution only gets you this far.
@citizen428 Looks more sensible than generating programs with it.
@citizen428 reminds me of the horrors me and @friz64 were up to, also serving an entire web page from an ESP32 as one big string iirc, base64 encoded images and all.
@tea @citizen428 @friz64 others do the same using ConfigMaps ... 🫒
@tea @citizen428 @friz64 reminded of tue gbs-control building a webpage into a byte array and spitting it out at runtime, as well as saving and loading files in a flash filesystem
and the gazillion associated race conditions
@tea @friz64 I'd unironically like to learn more about this.
@citizen428 That's called GNU M4.
@jwo Technically it's called an ill-advised use of my time that should never have happened, but yeah.
@citizen428 Well, something like this is provided as a quasi-standard Kotlin/Ktor library IIRC.
@jwo Yup, kotlinx.html. Rust has https://maud.lambda.xyz. There's plenty of precedent, but that doesn't make this specific late night digression a good idea.
Maud, a macro for writing HTML

@citizen428
Macros are a really bad idea that should never be used in C. They are unfortunately needed in Assembler, but assembler, other than to boot strap a compiler is not needed for the last 30 years. Not even on microcontrollers (see Forth and JAL).
@raymaccarthy I would have thought the "extremely bad idea" cartoon in the first image made it clear that I agree with this. But this is chaos.social and "Spaß am GerÀt" (loosely "Fun with the device) is basically the CCC motto.
@citizen428 Oh great, doxygen patch coming in 3 .. 2 .. 1 ..
@citizen428 I did a somewhat similar thing in AtomVM on a microcontroller. Saves lots of memory!
@citizen428 Ein klarer Fall fΓΌr die kinky geeks
@citizen428 I did this with rust before!
@sashin Rust having proper macros makes this a lot easier, see e.g. https://maud.lambda.xyz
Maud, a macro for writing HTML

@citizen428 old and busted: php as a c preprocessorΒΉ

hot and new: c preprocessor as a php

ΒΉ https://stackoverflow.com/questions/396644/replacements-for-the-c-preprocessor/16256052#16256052

Edit: Originalquelle verlinkt

Replacements for the C preprocessor

I'm interested in using something other than the C preprocessor to preprocess my C and Objective-C source code. Are there good alternatives? An example would be something that allowed one to esca...

Stack Overflow
@schaf That's exactly what the lolsob emoji is for.
@citizen428 if it works don't fix it i guess. Somehow it feels like react but without the reactivity :D
@m3tti Depends on your definition of "works" :D I'd not be inclined to actually use this.
@citizen428 Your scientists were so preoccupied with whether or not they could, they didn't stop to think if they should
@citizen428 the C preprocessor is just m4 with marginally worse syntax. it was *designed* for this. (or more accurately, m4 was designed because someone else did this)
@citizen428 @eniko Impressive! 😳😍

@ics @eniko

Not the first word I’d use πŸ˜‚

@citizen428 @eniko The first words were "WTF????" - that's why the first emoji πŸ˜‚πŸ˜‚

@citizen428 @joe Hey, guess what syntax the C preprocessor strips out for you?

Guess what syntax is really common in web pages? (and also property lists, where I first encountered this clash)

@jrose @citizen428 to their credit, they did say it was a bad idea
@joe @jrose I thought it was rather obvious that this is not a serious suggestion, then I added the cartoon just in case.
@citizen428 @joe Yeah, sorry, I didn’t mean it as a mark against you either! It just rang a bell, having run into people attempting to do even a less silly thing.
@citizen428 @roskakori probably did this about 30 years ago πŸ˜‰
@citizen428 this is based actually
@citizen428 for GCC, add #define true 1
@RueNahcMohr Works fine as-is. The Makefile targets `-std=c23` where `true` is an actual keyword. Unless I misunderstood your intention here.
@citizen428 ah! didn't work on my GCC!
@citizen428 oh! I bet I know why, I noticed clang in the makefile and command line-ran gcc without options!
nabijaczleweli.github.io/src/blogue/018-mean-shift-clusterer.html.pp at dev Β· nabijaczleweli/nabijaczleweli.github.io

A repo for http://nabijaczleweli.github.io. Contribute to nabijaczleweli/nabijaczleweli.github.io development by creating an account on GitHub.

GitHub