mkernel 0.0.1
Micro-kernel framework, everything as a module
mkernel.c
Go to the documentation of this file.
1
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include "revision.h"
22#include "gettext.h"
24#define _(String) gettext (String)
25#include <locale.h>
26
27/* Exclude inclusion for static code analysis with cppcheck */
28#ifndef NOCPPCHECK
29#include "mkernel-opt.h"
30#endif
31
32#include "modmgr.h"
33#include "mkmod.h"
34#include "ansi-color-codes.h"
35
36#include <stdlib.h> /* EXIT_SUCCESS / EXIT_FAILURE */
37#include <libgen.h> /* dirname */
38
39#ifndef PATH_MAX
40# define PATH_MAX 255
41#endif
42
43#include "debug/assert.h"
44#include "debug/memory.h"
45
47#ifndef MODULE_PATH_ENV
49# define MODULE_PATH_ENV "MODULE_PATH"
50#endif
51
52#ifndef MODULE_PATH_DEFAULT
54# define MODULE_PATH_DEFAULT "."
55#endif
56
57int main(int argc, char** argv, char**env)
58{
59 char *appDir;
60
61 (void)env;
62
63 /* Get executable relative invocation path from argv0 BEFORE AutoOpts */
64 {
65 char* execCmdCopy = strdup(argv[0]);
66 appDir = strdup(dirname(execCmdCopy));
67 }
68
69 /* LibIntl/GetText setup for Internationalization i18n */
70 setlocale (LC_ALL, "");
71 bindtextdomain (PACKAGE, LOCALEDIR);
72 textdomain (PACKAGE);
73
74 /* Application banner */
75 printf(BCYN PACKAGE_NAME " " PACKAGE_VERSION RESET);
76#ifdef REVISION
77 printf("." BBLU REVISION RESET);
78#endif
79#ifdef BBID
80 printf("." BBID);
81#endif
82 printf("\n");
83
84#pragma GCC diagnostic push /* save the actual diag context */
85#pragma GCC diagnostic ignored "-Wdate-time" /* locally disable warnings because of non reproductible build triggered by pbuild */
86 printf(_("Compiled %s at %s\n"),__DATE__, __TIME__);
87#pragma GCC diagnostic pop /* restore previous diag context */
88 /* TRANSLATORS: This is a French proper name. "fraa-swa sEr\-bEl" "François Cerbelle" */
89 printf("Copyright 2024 François Cerbelle\n");
90 printf(_("Report bugs to %s\n\n"), BYEL PACKAGE_BUGREPORT RESET);
91
92 /* AutoGen option parsing and consumming */
93 {
94 int arg_ct = optionProcess( &mkernelOptions, argc, argv );
95 argc -= arg_ct;
96 argv += arg_ct;
97 /* Avoid assignement without usage warnings from cppcheck */
98 (void)argc;
99 }
100
101 /* Default (least significant) module search path */
103 fprintf(stderr,_("The module search path can not add MODULE_PATH_DEFAULT (%s)\n"),MODULE_PATH_DEFAULT);
104
105 /* Add Application root and plugins subdir to module path */
106 {
107 char *modulePath;
108 modulePath = (char*)malloc(strlen(appDir)+strlen("/plugins")+1);
109 strcpy(modulePath,appDir);
110 strcat(modulePath,"/plugins");
111 if (0!=modmgr_addpath(appDir))
112 fprintf(stderr,_("The module search path can not add the application folder (%s)\n"),appDir);
113 if (0!=modmgr_addpath(modulePath))
114 fprintf(stderr,_("The module search path can not add the application plugin folder (%s)\n"),modulePath);
115 free(modulePath);
116 }
117
118 /* Override whole module path if environment defined */
119 if ((getenv(MODULE_PATH_ENV)) && (strlen(getenv(MODULE_PATH_ENV))>0))
120 if (0!=modmgr_setpath(getenv (MODULE_PATH_ENV)))
121 fprintf(stderr,_("The module path can not be reset from MODULE_PATH_ENV (%s)\n"),getenv (MODULE_PATH_ENV));
122
123 /* Override whole module path if options passed in CLI */
124 if (HAVE_OPT(MODULE_PATH))
125 if (0!=modmgr_setpath(OPT_ARG(MODULE_PATH)))
126 fprintf(stderr,_("The module path can not be reset from CLI parameter (%s)\n"),OPT_ARG(MODULE_PATH));
127
128 /* Main payload */
129 printf (_("Hello from main\n"));
130
131 /* AutoGen/AutoOpts shifted left argv */
132 /* Try to load the module provided as first invocation argument */
133 {
137 mkmod_api_t* api1=NULL;
138 mkmod_api_t* api2=NULL;
139 mkmod_api_t* api3=NULL;
140
142
143 if (argc>=1) {
144 DBG_PRINTF("Loading %s",argv[0]);
145 MODMGR_LOAD(m1,api1,argv[0]);
147 if (NULL!=m1) {
148 if ((NULL!=api1)&&(NULL!=api1->mkmod_function))
149 api1->mkmod_function();
150 else
151 fprintf(stderr,_("mkmod_function not found\n"));
152 } else
153 fprintf(stderr,_("Module not loaded\n"));
154 }
155
156 if (argc>=2) {
157 DBG_PRINTF("Loading %s",argv[1]);
158 MODMGR_LOAD(m2,api2,argv[1]);
160 if (NULL!=m2) {
161 if ((NULL!=api2)&&(NULL!=api2->mkmod_function))
162 api2->mkmod_function();
163 else
164 fprintf(stderr,_("mkmod_function not found\n"));
165 } else
166 fprintf(stderr,_("Module not loaded\n"));
167 }
168
169 if (argc>=3) {
170 DBG_PRINTF("Loading %s",argv[2]);
171 MODMGR_LOAD(m3,api3,argv[2]);
173 if (NULL!=m3) {
174 if ((NULL!=api3)&&(NULL!=api3->mkmod_function))
175 api3->mkmod_function();
176 else
177 fprintf(stderr,_("mkmod_function not found\n"));
178 } else
179 fprintf(stderr,_("Module not loaded\n"));
180 }
181
182 if (NULL!=m1) {
183 modmgr_unload(m1);
184 } else
185 fprintf(stderr,_("Module not loaded\n"));
187
188 if (NULL!=m2) {
189 modmgr_unload(m2);
190 } else
191 fprintf(stderr,_("Module not loaded\n"));
193
194 if (NULL!=m3) {
195 modmgr_unload(m3);
196 } else
197 fprintf(stderr,_("Module not loaded\n"));
199 }
200
201 free(appDir);
202 memreport();
203
204#ifdef _WIN32
205 system("PAUSE"); /* Pour la console Windows. */
206#endif
207
208 return EXIT_SUCCESS;
209}
#define BYEL
#define BBLU
#define RESET
#define BCYN
Debugging macros.
#define DBG_ITRACE(inst)
Instruction checkpint.
Definition: assert.h:142
#define DBG_PRINTF(p_Format,...)
Log a timestamped debugging message on stderr.
Definition: assert.h:155
#define textdomain(Domainname)
Definition: gettext.h:85
#define bindtextdomain(Domainname, Dirname)
Definition: gettext.h:87
Tracks memory allocation and leaks when compiled without NDEBUG.
#define malloc(size)
Same syntaxt and same behavior than regular malloc function, with memory leaks tracking.
Definition: memory.h:32
#define memreport()
Prints a list of currently allocated blocks on stderr.
Definition: memory.h:50
#define strdup(chaine)
Same syntaxt and same behavior than regular strdup function, with memory leaks tracking.
Definition: memory.h:44
#define free(ptr)
Same syntaxt and same behavior than regular free function, with memory leaks tracking.
Definition: memory.h:41
#define NULL
Definition: mkernel-opt.c:64
tOptions mkernelOptions
The option definitions for mkernel.
Definition: mkernel-opt.c:343
#define MODULE_PATH_DEFAULT
Default path to search modules in, if not defined by autotools (should be)
Definition: mkernel.c:54
#define _(String)
GetText helper.
Definition: mkernel.c:24
int main(int argc, char **argv, char **env)
Definition: mkernel.c:57
#define MODULE_PATH_ENV
Default environment variable name to get module patch from.
Definition: mkernel.c:49
ABI interface shared between module class and application.
int modmgr_setpath(const char *path)
Reset and initialize the modules search path.
Definition: modmgr.c:125
void modmgr_list()
Output the list of modules.
Definition: modmgr.c:379
int modmgr_addpath(const char *path)
Add a path at the end (lowest prio) of the search path.
Definition: modmgr.c:141
void modmgr_unload(modmgr_module_t module)
Decrement the usage counter, if last usage, remove from the module list and call onUnload.
Definition: modmgr.c:306
Module manager headers.
#define MODMGR_LOAD(module, api, filename)
Definition: modmgr.h:33
#define BBID
Definition: revision.h:11
#define REVISION
Definition: revision.h:7
void(* mkmod_function)()
Definition: mkmod.h:24
Module list item structure.
Definition: modmgr.c:42