From 2b76e490f1ce4ada3a28e80e637d0bc14c19d69e Mon Sep 17 00:00:00 2001 From: krustur Date: Tue, 11 Dec 2018 23:56:00 +0100 Subject: [PATCH] refactored system code into seperate c-file, basic support to run from wb added --- IconSnap.code-workspace | 3 +- IconSnap.info | Bin 0 -> 2665 bytes b.bat | 2 +- f | 2 + src/main.c | 105 +++++------------------------ src/sys.c | 145 ++++++++++++++++++++++++++++++++++++++++ src/sys.h | 14 ++++ 7 files changed, 179 insertions(+), 92 deletions(-) create mode 100644 IconSnap.info create mode 100644 f create mode 100644 src/sys.c create mode 100644 src/sys.h 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 0000000000000000000000000000000000000000..1210cd5f97407690d6f87c0661dc4e2b4f53f69c GIT binary patch literal 2665 zcmbW3e^66b7RT@7Aqg1r5*ll8h2*8(QO2r(bwMm7Ap|gz;9^7scSI$)h2_VBEnU|! z5fCx8D{QIdN2{=FbXbO#c8CRrkM$l>Vd*;o z>t48A;l0=q?fuv_#DzC3gd zxP=~*%Uel906_o*fttqx1&`fgzQY2+EOra+Q)9PYvEn^p8VJ1u6x_q4Kf^>cUV|t? zgjEQ=LHK__;ld&{aiHW1j=y^eKs_7=(zlNR1z$2YILB#GAG|h6OMv3qp8*+3K8__{ zVM=>55fUvd0W#tTZNT?Bp`7?z^Ic%k#Ou&OmbcbJ&7b(ZZ$s4YXj31e+QZIdBC7U{ zFF|nrz^fqGrG6O%p-bvP5a@3N0b}12WJ7|1>tfhvPycD)TH7!%P_iE=j*J8f?n-S? z`y2&CH0cJa0z&jd{s?tZ#)8-K_u?fI_$<`w%o|d*W_@%R9`a2}-m2S{6dj6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s5doS&^2&s&jEjdGlc1>-2`KW&O>+yDRo literal 0 HcmV?d00001 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 */