From 9e3562dd5fe6e5b9be2b3ed59abca8e0525178fe Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Thu, 29 Apr 2021 00:24:27 -0400 Subject: [PATCH] Implement a workaround for writing resources. Write the resources I want to a .rez file and use a script in the build itself to turn that into resources and attach it to the output file. This seems to result in a styled teach file for my test input that loads and looks pretty close to right. --- md2teach.xcodeproj/project.pbxproj | 2 + md2teach/io.c | 160 +++++++++++++++++++++++------ md2teach/io.h | 2 +- md2teach/main.c | 13 ++- md2teach/make/tail.mk | 1 + md2teach/make/teachRez | 23 +++++ md2teach/style.c | 27 ++--- md2teach/style.h | 3 + 8 files changed, 180 insertions(+), 51 deletions(-) create mode 100755 md2teach/make/teachRez diff --git a/md2teach.xcodeproj/project.pbxproj b/md2teach.xcodeproj/project.pbxproj index 7f3fb1a..c16f3d1 100644 --- a/md2teach.xcodeproj/project.pbxproj +++ b/md2teach.xcodeproj/project.pbxproj @@ -68,6 +68,7 @@ 9DDFC7C4262FD50C006D6E71 /* createDiskImage */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = createDiskImage; sourceTree = ""; }; 9DDFC7C5262FD50D006D6E71 /* config.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = config.txt; sourceTree = ""; }; 9DDFC7CA262FD7DD006D6E71 /* tar */ = {isa = PBXFileReference; lastKnownFileType = file; path = tar; sourceTree = ""; }; + 9DEEBB90263A673200DAA063 /* teachRez */ = {isa = PBXFileReference; lastKnownFileType = text; path = teachRez; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -129,6 +130,7 @@ 9D6532F52626240800105D50 /* orca-cc */, 9D6532F72626240800105D50 /* orca-rez */, 9D6532F92626240800105D50 /* tail.mk */, + 9DEEBB90263A673200DAA063 /* teachRez */, 9DDFC7C5262FD50D006D6E71 /* config.txt */, 9DDFC7C4262FD50C006D6E71 /* createDiskImage */, 9DDFC7C3262FD50C006D6E71 /* empty.2mg */, diff --git a/md2teach/io.c b/md2teach/io.c index 001d1a8..2a24bb7 100644 --- a/md2teach/io.c +++ b/md2teach/io.c @@ -17,6 +17,7 @@ #include "io.h" #include "main.h" +#include "style.h" // Defines @@ -26,16 +27,44 @@ // but this will let me test it without that capability. #define RESOURCE_WORKAROUND +#define TEACH_FILE_TYPE 0x50 +#define TEACH_AUX_TYPE 0x5445 + +#define R_WINDOW_POSITION 0x7001 +#define WINDOW_POSITION_NUM 1 + +#define STYLE_BLOCK_NUM 1 + + + +// Typedefs + +typedef struct tWindowPos +{ + int16_t height; + int16_t width; + int16_t top; + int16_t left; + int32_t version; +} tWindowPos; + // Globals static GSString255 outputFileName; -static Word writeResId; static IORecGS writeRec; static char writeBuffer[4096]; static int32_t writeBufferOffset = 0; static MD_SIZE writePos = 0; +static tWindowPos windowPos = { + 0xad, // height + 0x27c, // width + 0x1a, // top + 0x02, // left + 0x0 // version +}; + // Implementation @@ -71,8 +100,8 @@ int openOutputFile(const char * filename) createRec.pCount = 5; createRec.pathname = &outputFileName; createRec.access = destroyEnable | renameEnable | readWriteEnable; - createRec.fileType = 0x50; // Type for Teach file - createRec.auxType = 0x5445; // Aux type for Teach file + createRec.fileType = TEACH_FILE_TYPE; + createRec.auxType = TEACH_AUX_TYPE; createRec.storageType = extendedFile; CreateGS(&createRec); if (toolerror()) { @@ -126,10 +155,106 @@ MD_SIZE outputPos(void) } -void closeOutputFile(void) +static int writeResources(void) +{ +#ifndef RESOURCE_WORKAROUND + int shutdownResources = 0; + Word writeResId; + + if (!ResourceStatus()) { + ResourceStartUp(userid()); + shutdownResources = 1; + } + + CreateResourceFile(TEACH_AUX_TYPE, TEACH_FILE_TYPE, destroyEnable | renameEnable | readWriteEnable, (Pointer)outputFileName); + if (toolerror()) { + fprintf(stderr, "%s: Unable to create resources of file %s, toolerror=0x%x\n", commandName, outputFileName.text, toolerror()); + return 1; + } + writeResId = OpenResourceFile(0x8000 | readWriteEnable, NULL, (Pointer)outputFileName); + if (toolerror()) { + fprintf(stderr, "%s: Unable to open resources of file %s, toolerror=0x%x\n", commandName, outputFileName.text, toolerror()); + return 1; + } + + CloseResourceFile(writeResId); + + // Implement more of this... + + if (shutdownResources) + ResourceShutDown(); +#else + FILE * rezFile; + uint8_t * ptr; + uint32_t size; + uint32_t i; + + strcat(outputFileName.text, ".rez"); + rezFile = fopen(outputFileName.text, "w"); + if (rezFile == NULL) { + fprintf(stderr, "%s: Unable to open resource file %s, %s\n", commandName, outputFileName.text, strerror(errno)); + return 1; + } + + fprintf(rezFile, +"#define rStyleBlock 0x%x\n" +"#define rWindowPosition 0x%x\n" +"\n" +"type rStyleBlock {\n" +" hex string;\n" +"};\n" +"\n" +"type rWindowPosition {\n" +" hex string;\n" +"};\n" +"\n" +"resource rStyleBlock (%u) {\n" +" $\"", + rStyleBlock, R_WINDOW_POSITION, STYLE_BLOCK_NUM + ); + + ptr = stylePtr(); + size = styleSize(); + for (i = 0; i < size; i++) { + if ((i > 0) && + ((i % 32) == 0)) { + fprintf(rezFile, "\"\n $\""); + } + fprintf(rezFile, "%02x", (uint16_t)*ptr); + ptr++; + } + + fprintf(rezFile, "\"\n" +"};\n" +"\n" +"resource rWindowPosition (%u) {\n" +" $\"", + WINDOW_POSITION_NUM); + + ptr = (uint8_t *)(&windowPos); + size = sizeof(windowPos); + for (i = 0; i < size; i++) { + if ((i > 0) && + ((i % 32) == 0)) { + fprintf(rezFile, "\"\n $\""); + } + fprintf(rezFile, "%02x", (uint16_t)*ptr); + ptr++; + } + fprintf(rezFile, "\"\n" +"};\n" +"\n"); + + fclose(rezFile); +#endif + + return 0; +} + + +int closeOutputFile(void) { RefNumRecGS closeRec; - int shutdownResources = 0; if (writeBufferOffset > 0) flushBuffer(); @@ -137,30 +262,7 @@ void closeOutputFile(void) closeRec.refNum = writeRec.refNum; CloseGS(&closeRec); - if (!ResourceStatus()) { - ResourceStartUp(userid()); - shutdownResources = 1; - } - -#ifdef RESOURCE_WORKAROUND - CreateResourceFile(0x5445, 0x50, destroyEnable | renameEnable | readWriteEnable, (Pointer)outputFileName); - if (toolerror()) { - fprintf(stderr, "%s: Unable to create resources of file %s, toolerror=0x%x\n", commandName, outputFileName.text, toolerror()); - } - writeResId = OpenResourceFile(0x8000 | readWriteEnable, NULL, (Pointer)outputFileName); - if (toolerror()) { - fprintf(stderr, "%s: Unable to open resources of file %s, toolerror=0x%x\n", commandName, outputFileName.text, toolerror()); - } else { - CloseResourceFile(writeResId); - } - - // Implement more of this... - - if (shutdownResources) - ResourceShutDown(); -#else - // Implement the workaround here... -#endif + return writeResources(); } diff --git a/md2teach/io.h b/md2teach/io.h index 3d7f87d..bf8d426 100644 --- a/md2teach/io.h +++ b/md2teach/io.h @@ -17,7 +17,7 @@ extern int openOutputFile(const char * filename); extern void writeChar(MD_CHAR ch); extern void writeString(const MD_CHAR * str, MD_SIZE size); extern MD_SIZE outputPos(void); -extern void closeOutputFile(void); +extern int closeOutputFile(void); extern const MD_CHAR * readInputFile(const char * filename, MD_SIZE * bufferSize); extern void releaseInputBuffer(const MD_CHAR * inputBuffer); diff --git a/md2teach/main.c b/md2teach/main.c index c3a6cf7..c79663d 100644 --- a/md2teach/main.c +++ b/md2teach/main.c @@ -102,12 +102,9 @@ int main(int argc, char * argv[]) } result = parse(inputBuffer, inputFileLen); - - closeOutputFile(); + releaseInputBuffer(inputBuffer); - putchar('\n'); - if (debugEnabled) { fprintf(stderr, "Parser result: %d\n", result); } @@ -115,5 +112,13 @@ int main(int argc, char * argv[]) if (result != 0) fprintf(stderr, "%s: Parser failed (%d)\n", commandName, result); + if (closeOutputFile() != 0) + result = 1; + + if (result != 0) + remove(argv[index + 1]); + + putchar('\n'); + return result; } diff --git a/md2teach/make/tail.mk b/md2teach/make/tail.mk index 57e81fd..80ea03a 100644 --- a/md2teach/make/tail.mk +++ b/md2teach/make/tail.mk @@ -192,6 +192,7 @@ executeGUI: all executeShell: all $(ORCA) --mem $(TARGETDIR)/$(PGM) -d test.md $(TARGETDIR)/outfile.txt + make/teachRez $(TARGETDIR)/outfile.txt make/createDiskImage "$(DISKIMAGE)" $(DESTBOOTIMAGE) "$(TARGETDIR)/outfile.txt" make/launchEmulator "$(DISKIMAGE)" "$(DESTBOOTIMAGE)" diff --git a/md2teach/make/teachRez b/md2teach/make/teachRez new file mode 100755 index 0000000..f058362 --- /dev/null +++ b/md2teach/make/teachRez @@ -0,0 +1,23 @@ +#!/bin/bash + +PROJECT_DIR=`pwd` +cd `dirname $1` + +TEACH_FILE=`basename $1` +BASEFILE=`echo $TEACH_FILE | sed 's/\.[^\/]*$//'` +REZ_FILE="${BASEFILE}.rez" +R_FILE="${BASEFILE}.r" + +mv "${TEACH_FILE}.rez" "$REZ_FILE" + +$ORCA chtyp -l rez "$REZ_FILE" +$PROJECT_DIR/make/orca-rez "$REZ_FILE" "$BASEFILE" +if [ $? -ne 0 ] +then + echo Error compiling resource file + exit 1 +fi + +cp "${R_FILE}"/..namedfork/rsrc "$TEACH_FILE"/..namedfork/rsrc + +exit 0 diff --git a/md2teach/style.c b/md2teach/style.c index 41da5e7..631092b 100644 --- a/md2teach/style.c +++ b/md2teach/style.c @@ -36,15 +36,6 @@ // Typedefs -typedef struct tWindowPos -{ - int16_t height; - int16_t width; - int16_t top; - int16_t left; - int32_t version; -} tWindowPos; - // I wish I could use the structure definition from textedit.h but TERuler contains optional // fields in the definition and Teach isn't expecting them it seems (array of theTabs). So, // I need my own struct which omits them. @@ -80,14 +71,6 @@ typedef struct tFormat // Globals -static tWindowPos windowPos = { - 0xad, // height - 0x27c, // width - 0x1a, // top - 0x02, // left - 0x0 // version -}; - // For the 6 header sizes, we are going with: // 1 -> Helvetica 36 // 2 -> Helvetica 30 @@ -268,3 +251,13 @@ void closeStyle(void) formatPtr->styleItems[lastStyleIndex]. dataLength = currentPos - styleChangedAt; } } + +uint8_t * stylePtr(void) +{ + return (uint8_t *)formatPtr; +} + +uint32_t styleSize(void) +{ + return sizeof(formatPtr->header) + (sizeof(formatPtr->styleItems) * formatPtr->header.numberOfStyles); +} diff --git a/md2teach/style.h b/md2teach/style.h index 2655eaf..ffe6f9c 100644 --- a/md2teach/style.h +++ b/md2teach/style.h @@ -35,5 +35,8 @@ extern int styleInit(void); extern void setStyle(tStyleType styleType, uint16_t textMask, uint16_t headerSize); extern void closeStyle(void); +uint8_t * stylePtr(void); +uint32_t styleSize(void); + #endif /* define _GUARD_PROJECTmd2teach_FILEstyle_ */