diff options
| author | kx <kx@radix-linux.su> | 2026-10-02 08:45:51 +0300 |
|---|---|---|
| committer | kx <kx@radix-linux.su> | 2026-10-02 08:45:51 +0300 |
| commit | 9d5894a43e3b4dcc4c3b2756b289aba51eef6c63 (patch) | |
| tree | 5df3a0f2d7c387180da675aec606e276c0d76f11 /src | |
| parent | ba1b04d64bdfaf377915b22f77520216ebe47674 (diff) | |
| download | mcpu-cpp-9d5894a43e3b4dcc4c3b2756b289aba51eef6c63.tar.xz | |
Move per-user configuration to /home/kx/.mcpu/etc/mcpu-cpp.conf and add --sys-root=PATH
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 12 | ||||
| -rw-r--r-- | src/mcpp-lib.c | 4 | ||||
| -rw-r--r-- | src/mcpp-options.c | 18 | ||||
| -rw-r--r-- | src/mcpp-options.h | 1 | ||||
| -rw-r--r-- | src/mcpp-runtime.c | 63 | ||||
| -rw-r--r-- | src/mcpp-runtime.h | 2 |
6 files changed, 97 insertions, 3 deletions
@@ -46,6 +46,7 @@ usage( mcpp_options *opts ) printf( " -Wno-error keep emitted warnings as warnings\n" ); printf( " --config-file FILE use only FILE as configuration\n" ); printf( " --no-config do not read configuration files; keep runtime defaults\n" ); + printf( " --sys-root=PATH use PATH as MCPU root and do not read configuration files\n" ); printf( " -v, --verbose print configuration/include activity\n" ); printf( " --help display this help and exit\n" ); printf( " --version display version and exit\n\n" ); @@ -394,7 +395,16 @@ main( int argc, char **argv ) mpu_initialized = 1; mcpp_init( &cpp ); - if( mcpp_runtime_paths_discover(&cpp.runtime_paths, argv[0]) != 0 ) + if( options.sys_root != NULL ) + { + if( mcpp_runtime_paths_set_root(&cpp.runtime_paths, + options.sys_root) != 0 ) + fprintf( stderr, "%s: cannot use MCPU system root '%s': %s\n", + options.progname, options.sys_root, strerror(errno) ); + else if( run_preprocessor(&cpp, &options) == 0 ) + rc = 0; + } + else if( mcpp_runtime_paths_discover(&cpp.runtime_paths, argv[0]) != 0 ) { fprintf( stderr, "%s: cannot determine MCPU installation root: %s\n", options.progname, strerror(errno) ); diff --git a/src/mcpp-lib.c b/src/mcpp-lib.c index a0879f2..16ffd21 100644 --- a/src/mcpp-lib.c +++ b/src/mcpp-lib.c @@ -2866,11 +2866,11 @@ mcpp_load_config( mcpp *cpp, const char *explicit_filename, home = getenv( "HOME" ); if( home && *home ) { - size_t n = strlen(home) + sizeof("/.mcpu/mcpu-cpp.conf"); + size_t n = strlen(home) + sizeof("/.mcpu/etc/mcpu-cpp.conf"); user = (char *)malloc( n ); if( user == NULL ) return( -1 ); - snprintf( user, n, "%s/.mcpu/mcpu-cpp.conf", home ); + snprintf( user, n, "%s/.mcpu/etc/mcpu-cpp.conf", home ); if( mcpu_config_read( &cpp->config, user, 0 ) != 0 ) { free( user ); diff --git a/src/mcpp-options.c b/src/mcpp-options.c index 62fafc6..8d0d8cd 100644 --- a/src/mcpp-options.c +++ b/src/mcpp-options.c @@ -139,6 +139,24 @@ mcpp_handle_options( mcpp_options *opts, int argc, char **argv ) if( strcmp(arg, "--help") == 0 ) opts->help = 1; else if( strcmp(arg, "--version") == 0 ) opts->version = 1; else if( strcmp(arg, "--no-config") == 0 ) opts->no_config = 1; + else if( strncmp(arg, "--sys-root=", 11) == 0 ) + { + if( arg[11] == 0 ) + { + fprintf( stderr, "%s: empty path in '--sys-root='\n", + opts->progname ); + return( -1 ); + } + opts->sys_root = arg + 11; + opts->no_config = 1; + } + else if( strcmp(arg, "--sys-root") == 0 ) + { + value = option_argument(opts, argc, argv, &i, NULL, arg); + if( value == NULL || *value == 0 ) return( -1 ); + opts->sys_root = value; + opts->no_config = 1; + } else if( strcmp(arg, "--config-file") == 0 ) { value = option_argument(opts, argc, argv, &i, NULL, arg); diff --git a/src/mcpp-options.h b/src/mcpp-options.h index 910db92..a4af123 100644 --- a/src/mcpp-options.h +++ b/src/mcpp-options.h @@ -36,6 +36,7 @@ struct mcpp_options const char *in_fname; const char *out_fname; const char *config_fname; + const char *sys_root; const char *deps_file; const char *deps_target; const char *object_suffix; diff --git a/src/mcpp-runtime.c b/src/mcpp-runtime.c index f498b20..592a342 100644 --- a/src/mcpp-runtime.c +++ b/src/mcpp-runtime.c @@ -76,6 +76,29 @@ path_join( const char *directory, const char *name ) } +static char * +path_absolute( const char *path ) +{ + char cwd[PATH_MAX]; + char *result; + + if( path == NULL || *path == 0 ) + { + errno = EINVAL; + return( NULL ); + } + + if( path[0] == '/' ) + return( strdup(path) ); + + if( getcwd(cwd, sizeof(cwd)) == NULL ) + return( NULL ); + + result = path_join( cwd, path ); + return( result ); +} + + static int resolve_proc_self_exe( char path[PATH_MAX] ) { @@ -266,3 +289,43 @@ fail: free( system_include_path ); return( -1 ); } + + +int +mcpp_runtime_paths_set_root( mcpp_runtime_paths *paths, const char *root ) +{ + char *installation_root; + char *config_file; + char *system_include_path; + + if( paths == NULL || root == NULL || *root == 0 ) + { + errno = EINVAL; + return( -1 ); + } + + installation_root = path_absolute( root ); + if( installation_root == NULL ) + return( -1 ); + + config_file = path_join( installation_root, "etc/mcpu-cpp.conf" ); + if( config_file == NULL ) + goto fail; + + system_include_path = path_join( installation_root, "include" ); + if( system_include_path == NULL ) + goto fail; + + free( paths->installation_root ); + free( paths->config_file ); + free( paths->system_include_path ); + paths->installation_root = installation_root; + paths->config_file = config_file; + paths->system_include_path = system_include_path; + return( 0 ); + +fail: + free( installation_root ); + free( config_file ); + return( -1 ); +} diff --git a/src/mcpp-runtime.h b/src/mcpp-runtime.h index 75d1ddf..96a65a9 100644 --- a/src/mcpp-runtime.h +++ b/src/mcpp-runtime.h @@ -15,5 +15,7 @@ void mcpp_runtime_paths_init( mcpp_runtime_paths *paths ); void mcpp_runtime_paths_free( mcpp_runtime_paths *paths ); int mcpp_runtime_paths_discover( mcpp_runtime_paths *paths, const char *argv0 ); +int mcpp_runtime_paths_set_root( mcpp_runtime_paths *paths, + const char *root ); #endif /* __MCPU_CPP_RUNTIME_H__ */ |
