mkernel 0.0.2
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 provided module list */
133 {
134 modmgr_module_t* modhandle=NULL;
135 mkmod_api_t* *modapi=NULL;
136 int l_i;
137
138 modhandle = malloc(argc*sizeof(modmgr_module_t*));
139 memset(modhandle, 0, argc);
140
141 modapi = malloc(argc*sizeof(mkmod_api_t*));
142 memset(modapi, 0, argc);
143
145
146 l_i = 0;
147 while (l_i < argc) {
148 DBG_PRINTF("Loading module #%d: %s", l_i, argv[l_i]);
149 MODMGR_LOAD(modhandle[l_i], modapi[l_i], argv[l_i]);
150 if (NULL!=modhandle[l_i]) {
151 if ((NULL!=modapi[l_i])&&(NULL!=modapi[l_i]->mkmod_function))
152 modapi[l_i]->mkmod_function();
153 else
154 fprintf(stderr,_("mkmod_function not found\n"));
155 }
156 l_i++;
157 }
158
160
161 l_i = 0;
162 while (l_i < argc) {
163 DBG_PRINTF("Unloading module #%d (%s)", l_i, argv[l_i]);
164 if (NULL!=modhandle[l_i]) {
165 modmgr_unload(modhandle[l_i]);
166 }
167 l_i++;
168 }
169
171 }
172
173 free(appDir);
174 memreport();
175
176#ifdef _WIN32
177 system("PAUSE"); /* Pour la console Windows. */
178#endif
179
180 return EXIT_SUCCESS;
181}
#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:25
Module list item structure.
Definition: modmgr.c:42