diff --git a/IconSnap.code-workspace b/IconSnap.code-workspace index 0fedc66..a73d3e1 100644 --- a/IconSnap.code-workspace +++ b/IconSnap.code-workspace @@ -25,7 +25,8 @@ "dirent.h": "c", "unistd.h": "c", "intuition.h": "c", - "signal.h": "c" + "signal.h": "c", + "exec_sysbase_pragmas.h": "c" } } } \ No newline at end of file diff --git a/IconSnap.info b/IconSnap.info new file mode 100644 index 0000000..1210cd5 Binary files /dev/null and b/IconSnap.info differ diff --git a/b.bat b/b.bat index 8331a8f..431bbb8 100644 --- a/b.bat +++ b/b.bat @@ -1,4 +1,4 @@ @REM echo off @REM -v for verbose -vc src/main.c -v -stack-check -size -lamiga -lposix -IE:\Amiga\KrustWB3\Output\Dev\vbcc_posix\targets\m68k-amigaos\include -LE:\Amiga\KrustWB3\Output\Dev\vbcc_posix\targets\m68k-amigaos\lib -DBUILD_PLATFORM_WIN -o IconSnap +vc src/main.c src/sys.c -v -stack-check -size -lamiga -lposix -IE:\Amiga\KrustWB3\Output\Dev\vbcc_posix\targets\m68k-amigaos\include -LE:\Amiga\KrustWB3\Output\Dev\vbcc_posix\targets\m68k-amigaos\lib -DBUILD_PLATFORM_WIN -o IconSnap copy IconSnap r \ No newline at end of file diff --git a/f b/f new file mode 100644 index 0000000..f8a74d2 --- /dev/null +++ b/f @@ -0,0 +1,2 @@ +failat 15 +r diff --git a/src/main.c b/src/main.c index 13cab13..a3a70ce 100644 --- a/src/main.c +++ b/src/main.c @@ -17,35 +17,18 @@ #include #include +#include "sys.h" + // TODO: Scripted tests! // C helpers #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MIN(x, y) (((x) < (y)) ? (x) : (y)) -// VBCC -extern size_t __stack_usage; - // Version tag #define VERSTAG "\0$VER: IconSnap 0.3 (30.11.2018)" unsigned char versiontag[] = VERSTAG; -// Build Platform -#ifdef BUILD_PLATFORM_AMIGA -const char *BuildPlatform = "Amiga"; -#elif BUILD_PLATFORM_WIN -const char *BuildPlatform = "Win"; -#else -const char *BuildPlatform = "Unknown"; -#endif - -// Libraries -unsigned long DosVersion = 37L; -struct Library *DosBase = NULL; -unsigned long IconVersion = 37L; -struct Library *IconBase = NULL; -short iconLibraryV44Enabled = FALSE; - // Arguments unsigned char FILE_OPTION_POS = 0; unsigned char DIR_OPTION_POS = 1; @@ -74,11 +57,6 @@ struct RDArgs *rdargs = NULL; // clean exit handling void CleanExit(); -// Logging -short verbose = FALSE; -void Information(const char *fmt, ...); -void Verbose(const char *fmt, ...); - // String/Path handling size_t maxPathTreshold = PATH_MAX - 2; short StringEndsWith(const char *str, const char *suffix); @@ -104,35 +82,25 @@ long Align(long orig, long pad, long align, long alignoffset); int main(int argc, char **argv) { atexit(CleanExit); + + short sysInitResult = SysInit(argc, argv); + if (sysInitResult != RETURN_OK) + { + exit(sysInitResult); + } if (argc == 0) { //Opened from WB + Information("Started from Workbench\n"); + exit(RETURN_OK); } else { - // printf("fignal\n"); - // signal(SIGINT, intHandler); + Verbose("Started from CLI\n"); } - // Open libraries - DosBase = OpenLibrary("dos.library", DosVersion); - if (!DosBase) - { - Information("Failed to open dos.library %li\n", DosVersion); - return RETURN_ERROR; - } - // Verbose("DosBase: %p\n", (void *)DosBase); - - IconBase = OpenLibrary("icon.library", IconVersion); - if (!IconBase) - { - Information("Failed to open icon.library %li\n", IconVersion); - return RETURN_ERROR; - } - // Verbose("IconBase: %p\n", (void *)IconBase); // check arguments - rdargs = ReadArgs(argumentString, argArray, NULL); if (!rdargs) @@ -141,21 +109,10 @@ int main(int argc, char **argv) return RETURN_ERROR; } - verbose = argArray[VERBOSE_OPTION_POS] == DOSTRUE; - if (verbose) + short verbose2 = argArray[VERBOSE_OPTION_POS] == DOSTRUE; + if (verbose2) { - Verbose(" VERBOSE logging active\n"); - } - Verbose("Build platform: %s\n", BuildPlatform); - Verbose("dos.library version %li\n", DosBase->lib_Version); - Verbose("dos.library opencnt %li\n", DosBase->lib_OpenCnt); - Verbose("icon.library version %li\n", IconBase->lib_Version); - Verbose("icon.library opencnt %li\n", IconBase->lib_OpenCnt); - - if (IconBase->lib_Version >= 45) - { - Verbose("Icon library V44 features enabled\n"); - iconLibraryV44Enabled = TRUE; + SysSetVerboseEnabled(TRUE); } unsigned char *fileOption = (unsigned char *)argArray[FILE_OPTION_POS]; @@ -250,8 +207,6 @@ int main(int argc, char **argv) // return RETURN_ERROR; // } - Verbose("Stack used: %lu\n", (unsigned long)__stack_usage); - return RETURN_OK; } @@ -278,36 +233,7 @@ void CleanExit() FreeArgs(rdargs); rdargs = NULL; } - if (DosBase != NULL) - { - CloseLibrary(DosBase); - DosBase = NULL; - } - if (IconBase != NULL) - { - CloseLibrary(IconBase); - IconBase = NULL; - } -} - -// Logging -void Verbose(const char *fmt, ...) -{ - if (!verbose) - return; - - va_list args; - va_start(args, fmt); - vfprintf(stdout, fmt, args); - va_end(args); -} - -void Information(const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - vfprintf(stdout, fmt, args); - va_end(args); + SysCleanup(); } // String/Path handling @@ -409,7 +335,6 @@ unsigned int AlignDir(unsigned char *dirName) return fixCount; } - unsigned int AlignIcon(unsigned char *diskObjectName) { unsigned long diskObjectNameLen = strlen(diskObjectName); diff --git a/src/sys.c b/src/sys.c new file mode 100644 index 0000000..2bf0cf4 --- /dev/null +++ b/src/sys.c @@ -0,0 +1,145 @@ +#include +// #include +#include +// #include +// #include + +#include +#include +// #include +// #include + +#include +#include +#include + +// Build Platform +#ifdef BUILD_PLATFORM_AMIGA +const char *BuildPlatform = "Amiga"; +#elif BUILD_PLATFORM_WIN +const char *BuildPlatform = "Win"; +#else +const char *BuildPlatform = "Unknown"; +#endif + +// VBCC +extern size_t __stack_usage; + +// Library +unsigned long DosReqVersion = 37L; +struct Library *DosBase = NULL; +unsigned long IconReqVersion = 37L; +struct Library *IconBase = NULL; +short iconLibraryV44Enabled = FALSE; + +// Logging +#define LOG_MAX (256) +unsigned char *LogBuffer = NULL; +short verbose = FALSE; +BPTR wbcon = (BPTR)NULL; +// FILE *outputFile = NULL; +void Information(const char *fmt, ...); +void Verbose(const char *fmt, ...); + +short SysInit(int argc, char **argv) +{ + // Open libraries + DosBase = OpenLibrary(DOSNAME, DosReqVersion); + if (!DosBase) + { + // Information("Failed to open dos.library %li\n", DosReqVersion); + return RETURN_ERROR; + } + // Verbose("DosBase: %p\n", (void *)DosBase); + + IconBase = OpenLibrary("icon.library", IconReqVersion); + if (!IconBase) + { + // Information("Failed to open icon.library %li\n", IconReqVersion); + return RETURN_ERROR; + } + // Verbose("IconBase: %p\n", (void *)IconBase); + if (IconBase->lib_Version >= 45) + { + iconLibraryV44Enabled = TRUE; + } + + // Initialize logging + LogBuffer = AllocVec(LOG_MAX, MEMF_ANY); + + if (argc == 0) + { + //Opened from WB + wbcon = Open("CON:20/20/500/100/IconSnap", MODE_NEWFILE); + SelectOutput(wbcon); + } + + return RETURN_OK; +} + +void SysCleanup(void) +{ + Verbose("Stack used: %lu\n", (unsigned long)__stack_usage); + + if (LogBuffer != NULL) + { + FreeVec(LogBuffer); + } + if (wbcon != (BPTR)NULL) + { + Delay(100); + Close(wbcon); + } + if (DosBase != NULL) + { + CloseLibrary(DosBase); + DosBase = NULL; + } + if (IconBase != NULL) + { + CloseLibrary(IconBase); + IconBase = NULL; + } +} + +void SysSetVerboseEnabled(short enabled) +{ + verbose = enabled; + Verbose("Verbose logging active\n"); + Verbose("Build platform: %s\n", BuildPlatform); + Verbose("dos.library version %li\n", DosBase->lib_Version); + Verbose("dos.library opencnt %li\n", DosBase->lib_OpenCnt); + Verbose("icon.library version %li\n", IconBase->lib_Version); + Verbose("icon.library opencnt %li\n", IconBase->lib_OpenCnt); + if (iconLibraryV44Enabled) + { + Verbose("Icon library V44 features enabled\n"); + } +} + +// Logging +void Verbose(const char *fmt, ...) +{ + if (!verbose) + return; + + va_list args; + // va_start(args, fmt); + Information(fmt, args); + // va_end(args); +} + +void Information(const char *fmt, ...) +{ + if (LogBuffer == NULL) + { + Write(Output(), "Log error\n", 10); + return; + } + + va_list args; + va_start(args, fmt); + vsnprintf(LogBuffer, LOG_MAX-1, fmt, args); + Write(Output(), LogBuffer, strlen(LogBuffer)); + va_end(args); +} diff --git a/src/sys.h b/src/sys.h new file mode 100644 index 0000000..c1a2b36 --- /dev/null +++ b/src/sys.h @@ -0,0 +1,14 @@ +#ifndef SYS_H +#define SYS_H + +extern short iconLibraryV44Enabled; + +short SysInit(int argc, char **argv); +void SysCleanup(void); +void SysSetVerboseEnabled(short enabled); + +// Logging +void Information(const char *fmt, ...); +void Verbose(const char *fmt, ...); + +#endif /* SYS_H */