P99

◆ P99_MAIN_INTERCEPT

#define P99_MAIN_INTERCEPT (   NAME)
Value:
int NAME(int, char*[]); \
P99_WEAK(P99_PASTE2(p00_init_func_, NAME)) \
void P99_PASTE2(p00_init_func_, NAME)(int*, char***); \
P99_WEAK(main) \
int main(int p00_argc, char**p00_argv) { \
fprintf(stderr, "%s: intercepting " P99_STRINGIFY(NAME) "\n", __func__); \
P99_PASTE2(p00_init_func_, NAME)(&p00_argc, &p00_argv); \
return NAME(p00_argc, p00_argv); \
} \
P99_WEAK(P99_PASTE2(p00_init_func_, NAME)) \
void P99_PASTE2(p00_init_func_, NAME)(int * p00_argc, char***p00_argv)

Intercept the main function before it is called and run some startup code.

This is a fragile method to intercept main, e.g to act on argc and argv before the program run enters user code. This is only fully active when the program is compiled with the macro P99_INTERCEPT_MAIN enabled. If it is not enabled you can still call your defined function through the use of the macro P99_INIT_TRIGGER.

A complete example would like the one defined here in this file:

// here do something reasonable
}
# if defined(P99_INTERCEPT_MAIN)
# undef main
# define main p99_init_main
# endif
int main(int argc, char *argv[]) {
...
P99_INIT_TRIGGER(NAME, &argc, &argv);
...
return EXIT_SUCCESS;
}

This has three different parts. First comes the declaration of the function body that is to be executed when main is intercepted. Then comes a re-definition of main. As you can guess, this will rename the user function to p99_init_main in this example.

Remarks
The call to the P99_INIT_TRIGGER function is optional if compiled with P99_INTERCEPT_MAIN.
Warning
If the program is compiled P99_INTERCEPT_MAIN the return is mandatory: for the compiler the user main is not a main in the sense of the C standard anymore.

Definition at line 238 of file p99_init.h.

P99_STRINGIFY
#define P99_STRINGIFY(...)
Transform the argument list into one string.
Definition: p99_compiler.h:109
P99_INIT_TRIGGER
#define P99_INIT_TRIGGER(NAME, ARGC, ARGV)
Definition: p99_init.h:251
p99_init_main
int p99_init_main(int, char *[])
P99_PASTE2
#define P99_PASTE2(_1, _2)
Paste two token sequences at their junction.
Definition: p99_paste.h:82
main
#define main
P99_MAIN_INTERCEPT
P99_MAIN_INTERCEPT(p99_threads_main)
Definition: p99_threads_posix.h:625