LCOV - code coverage report
Current view: top level - src - mkernel.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 55 60 91.7 %
Date: 2024-08-21 13:23:45 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /**         @file  mkernel.c
       2             :  *         @brief  Micro-kernel core main source
       3             :  *          @date  10/11/2017
       4             :  *        @author  François Cerbelle (Fanfan), francois@cerbelle.net
       5             :  *     @copyright  Copyright (c) 2017, François Cerbelle
       6             :  *
       7             :  *   @internal
       8             :  *       Compiler  gcc
       9             :  *  Last modified  2024-07-31 18:23
      10             :  *   Organization  Cerbelle.net
      11             :  *        Company  Home
      12             :  *
      13             :  *   This source code is released for free distribution under the terms of the
      14             :  *   GNU General Public License as published by the Free Software Foundation.
      15             :  */
      16             : 
      17             : #ifdef HAVE_CONFIG_H
      18             : #include "config.h"
      19             : #endif
      20             : 
      21             : #include "revision.h"
      22             : #include "gettext.h"
      23             : /** GetText helper */
      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             : 
      46             : /** @todo Define in configure.ac with default value */
      47             : #ifndef MODULE_PATH_ENV
      48             : /** Default environment variable name to get module patch from */
      49             : #  define MODULE_PATH_ENV        "MODULE_PATH"
      50             : #endif
      51             : 
      52             : #ifndef MODULE_PATH_DEFAULT
      53             : /** Default path to search modules in, if not defined by autotools (should be) */
      54             : #  define MODULE_PATH_DEFAULT        "."
      55             : #endif
      56             : 
      57          17 : int 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          17 :         char* execCmdCopy = strdup(argv[0]);
      66          17 :         appDir = strdup(dirname(execCmdCopy));
      67             :     }
      68             : 
      69             :     /* LibIntl/GetText setup for Internationalization i18n */
      70          17 :     setlocale (LC_ALL, "");
      71          17 :     bindtextdomain (PACKAGE, LOCALEDIR);
      72          17 :     textdomain (PACKAGE);
      73             : 
      74             :     /* Application banner */
      75          17 :     printf(BCYN PACKAGE_NAME " " PACKAGE_VERSION RESET);
      76             : #ifdef REVISION
      77          17 :     printf("." BBLU REVISION RESET);
      78             : #endif
      79             : #ifdef BBID
      80          17 :     printf("." BBID);
      81             : #endif
      82          17 :     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          17 :     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          17 :     printf("Copyright 2024 François Cerbelle\n");
      90          17 :     printf(_("Report bugs to %s\n\n"), BYEL PACKAGE_BUGREPORT RESET);
      91             : 
      92             :     /* AutoGen option parsing and consumming */
      93             :     {
      94          17 :         int arg_ct = optionProcess( &mkernelOptions, argc, argv );
      95          11 :         argc -= arg_ct;
      96          11 :         argv += arg_ct;
      97             :         /* Avoid assignement without usage warnings from cppcheck */
      98             :         (void)argc;
      99             :     }
     100             : 
     101             :     /* Default (least significant) module search path */
     102          11 :     if (0!=modmgr_addpath(MODULE_PATH_DEFAULT))
     103           0 :         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          11 :         modulePath = (char*)malloc(strlen(appDir)+strlen("/plugins")+1);
     109          11 :         strcpy(modulePath,appDir);
     110          11 :         strcat(modulePath,"/plugins");
     111          11 :         if (0!=modmgr_addpath(appDir))
     112           0 :             fprintf(stderr,_("The module search path can not add the application folder (%s)\n"),appDir);
     113          11 :         if (0!=modmgr_addpath(modulePath))
     114           0 :             fprintf(stderr,_("The module search path can not add the application plugin folder (%s)\n"),modulePath);
     115          11 :         free(modulePath);
     116             :     }
     117             : 
     118             :     /* Override whole module path if environment defined */
     119          11 :     if ((getenv(MODULE_PATH_ENV)) && (strlen(getenv(MODULE_PATH_ENV))>0))
     120           3 :         if (0!=modmgr_setpath(getenv (MODULE_PATH_ENV)))
     121           0 :             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          11 :     if (HAVE_OPT(MODULE_PATH))
     125           3 :         if (0!=modmgr_setpath(OPT_ARG(MODULE_PATH)))
     126           0 :             fprintf(stderr,_("The module path can not be reset from CLI parameter (%s)\n"),OPT_ARG(MODULE_PATH));
     127             : 
     128             :     /* Main payload */
     129          11 :     printf (_("Hello from main\n"));
     130             : 
     131             :     /* AutoGen/AutoOpts shifted left argv */
     132             :     /* Try to load the provided module list */
     133             :     {
     134          11 :         modmgr_module_t* modhandle=NULL;
     135          11 :         mkmod_api_t* *modapi=NULL;
     136             :         int l_i;
     137             : 
     138          11 :         modhandle = malloc(argc*sizeof(modmgr_module_t*));
     139          11 :         memset(modhandle, 0, argc);
     140             : 
     141          11 :         modapi = malloc(argc*sizeof(mkmod_api_t*));
     142          11 :         memset(modapi, 0, argc);
     143             : 
     144          11 :         DBG_ITRACE(modmgr_list());
     145             : 
     146          11 :         l_i = 0;
     147          29 :         while (l_i < argc) {
     148          18 :             DBG_PRINTF("Loading module #%d: %s", l_i, argv[l_i]);
     149          18 :             MODMGR_LOAD(modhandle[l_i], modapi[l_i], argv[l_i]);
     150          18 :             if (NULL!=modhandle[l_i]) {
     151          13 :                 if ((NULL!=modapi[l_i])&&(NULL!=modapi[l_i]->mkmod_function))
     152           9 :                     modapi[l_i]->mkmod_function();
     153             :                 else
     154           4 :                     fprintf(stderr,_("mkmod_function not found\n"));
     155             :             }
     156          18 :             l_i++;
     157             :         }
     158             : 
     159          11 :         DBG_ITRACE(modmgr_list());
     160             : 
     161          11 :         l_i = 0;
     162          29 :         while (l_i < argc) {
     163          18 :             DBG_PRINTF("Unloading module #%d (%s)", l_i, argv[l_i]);
     164          18 :             if (NULL!=modhandle[l_i]) {
     165          13 :                 modmgr_unload(modhandle[l_i]);
     166             :             }
     167          18 :             l_i++;
     168             :         }
     169             : 
     170          11 :         DBG_ITRACE(modmgr_list());
     171             :     }
     172             : 
     173          11 :     free(appDir);
     174          11 :     memreport();
     175             : 
     176             : #ifdef _WIN32
     177             :     system("PAUSE");  /* Pour la console Windows. */
     178             : #endif
     179             : 
     180          11 :     return EXIT_SUCCESS;
     181             : }

Generated by: LCOV version 1.16