2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2024-01-01 14:15:26 -07:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Test program to test dynamic loading with the loadso subsystem.
|
2022-11-30 13:51:59 -07:00
|
|
|
*/
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-16 17:25:39 -06:00
|
|
|
#include <SDL3/SDL_test.h>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
typedef int (*fntype)(const char *);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
static void log_usage(char *progname, SDLTest_CommonState *state) {
|
|
|
|
static const char *options[] = { "library", "functionname|--hello", NULL };
|
|
|
|
SDLTest_CommonLogUsage(state, progname, options);
|
|
|
|
SDL_Log("USAGE: %s <library> <functionname>\n", progname);
|
|
|
|
SDL_Log(" %s <lib with puts()> --hello\n", progname);
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
2023-03-16 17:25:39 -06:00
|
|
|
int i;
|
2015-06-21 09:33:46 -06:00
|
|
|
int retval = 0;
|
|
|
|
int hello = 0;
|
|
|
|
const char *libname = NULL;
|
|
|
|
const char *symname = NULL;
|
|
|
|
void *lib = NULL;
|
|
|
|
fntype fn = NULL;
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonState *state;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
2023-11-09 14:29:15 -07:00
|
|
|
if (!state) {
|
2023-03-16 17:25:39 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable standard application logging */
|
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
|
|
|
/* Parse commandline */
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (!consumed) {
|
|
|
|
if (SDL_strcmp(argv[i], "--hello") == 0) {
|
|
|
|
if (!symname || SDL_strcmp(symname, "puts") == 0) {
|
|
|
|
symname = "puts";
|
|
|
|
consumed = 1;
|
|
|
|
hello = 1;
|
|
|
|
}
|
|
|
|
} else if (!libname) {
|
|
|
|
libname = argv[i];
|
|
|
|
consumed = 1;
|
|
|
|
} else if (!symname) {
|
|
|
|
symname = argv[i];
|
|
|
|
consumed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed <= 0) {
|
|
|
|
log_usage(argv[0], state);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += consumed;
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
if (!libname || !symname) {
|
|
|
|
log_usage(argv[0], state);
|
2015-06-21 09:33:46 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize SDL */
|
|
|
|
if (SDL_Init(0) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib = SDL_LoadObject(libname);
|
2023-11-09 14:29:15 -07:00
|
|
|
if (!lib) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadObject('%s') failed: %s\n",
|
2022-11-30 13:51:59 -07:00
|
|
|
libname, SDL_GetError());
|
2015-06-21 09:33:46 -06:00
|
|
|
retval = 3;
|
|
|
|
} else {
|
2022-11-30 13:51:59 -07:00
|
|
|
fn = (fntype)SDL_LoadFunction(lib, symname);
|
2023-11-09 14:29:15 -07:00
|
|
|
if (!fn) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadFunction('%s') failed: %s\n",
|
2022-11-30 13:51:59 -07:00
|
|
|
symname, SDL_GetError());
|
2015-06-21 09:33:46 -06:00
|
|
|
retval = 4;
|
|
|
|
} else {
|
|
|
|
SDL_Log("Found %s in %s at %p\n", symname, libname, fn);
|
|
|
|
if (hello) {
|
|
|
|
SDL_Log("Calling function...\n");
|
|
|
|
fn(" HELLO, WORLD!\n");
|
|
|
|
SDL_Log("...apparently, we survived. :)\n");
|
|
|
|
SDL_Log("Unloading library...\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SDL_UnloadObject(lib);
|
|
|
|
}
|
|
|
|
SDL_Quit();
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonDestroyState(state);
|
2015-06-21 09:33:46 -06:00
|
|
|
return retval;
|
|
|
|
}
|