Fix REPOSITION-FILE, HISTORY, locked file handle and other problems.
[pforth] / csrc / pf_save.c
index 6a9d195..f3f9ac0 100644 (file)
@@ -343,6 +343,33 @@ error:
        return -1;\r
 }\r
 \r
        return -1;\r
 }\r
 \r
+/* Convert dictionary info chunk between native and on-disk (big-endian). */\r
+static void\r
+convertDictionaryInfoWrite (DictionaryInfoChunk *sd)\r
+{\r
+/* Convert all fields in DictionaryInfoChunk from Native to BigEndian. \r
+ * This assumes they are all 32-bit integers.\r
+ */\r
+       int   i;\r
+       uint32_t *p = (uint32_t *) sd;\r
+       for (i=0; i<((int)(sizeof(*sd)/sizeof(uint32_t))); i++)\r
+       {\r
+               Write32BigEndian( (uint8_t *)&p[i], p[i] );\r
+       }\r
+}\r
+\r
+static void\r
+convertDictionaryInfoRead (DictionaryInfoChunk *sd)\r
+{\r
+/* Convert all fields in structure from BigEndian to Native. */\r
+       int   i;\r
+       uint32_t *p = (uint32_t *) sd;\r
+       for (i=0; i<((int)(sizeof(*sd)/sizeof(uint32_t))); i++)\r
+       {\r
+               p[i] = Read32BigEndian( (uint8_t *)&p[i] );\r
+       }\r
+}\r
+\r
 /****************************************************************\r
 ** Save Dictionary in File.\r
 ** If EntryPoint is NULL, save as development environment.\r
 /****************************************************************\r
 ** Save Dictionary in File.\r
 ** If EntryPoint is NULL, save as development environment.\r
@@ -356,7 +383,6 @@ cell_t ffSaveForth( const char *FileName, ExecToken EntryPoint, cell_t NameSize,
        uint32_t NameChunkSize = 0;\r
        uint32_t CodeChunkSize;\r
        uint32_t relativeCodePtr;\r
        uint32_t NameChunkSize = 0;\r
        uint32_t CodeChunkSize;\r
        uint32_t relativeCodePtr;\r
-       int   i;\r
 \r
        fid = sdOpenFile( FileName, "wb" );\r
        if( fid == NULL )\r
 \r
        fid = sdOpenFile( FileName, "wb" );\r
        if( fid == NULL )\r
@@ -423,7 +449,7 @@ cell_t ffSaveForth( const char *FileName, ExecToken EntryPoint, cell_t NameSize,
                uint32_t relativeHeaderPtr;\r
 /* Development mode. */\r
                SD.sd_RelContext = ABS_TO_NAMEREL(gVarContext);\r
                uint32_t relativeHeaderPtr;\r
 /* Development mode. */\r
                SD.sd_RelContext = ABS_TO_NAMEREL(gVarContext);\r
-               relativeHeaderPtr = ABS_TO_NAMEREL(gCurrentDictionary->dic_HeaderPtr.Byte);\r
+               relativeHeaderPtr = ABS_TO_NAMEREL(gCurrentDictionary->dic_HeaderPtr);\r
                SD.sd_RelHeaderPtr = relativeHeaderPtr;\r
 \r
 /* How much real name space is there? */\r
                SD.sd_RelHeaderPtr = relativeHeaderPtr;\r
 \r
 /* How much real name space is there? */\r
@@ -445,16 +471,7 @@ cell_t ffSaveForth( const char *FileName, ExecToken EntryPoint, cell_t NameSize,
        SD.sd_CodeSize = CodeSize;\r
 \r
        \r
        SD.sd_CodeSize = CodeSize;\r
 \r
        \r
-/* Convert all fields in DictionaryInfoChunk from Native to BigEndian. \r
- * This assumes they are all 32-bit integers.\r
- */\r
-       {\r
-               uint32_t *p = (uint32_t *) &SD;\r
-               for( i=0; i<((int)(sizeof(SD)/sizeof(uint32_t))); i++ )\r
-               {\r
-                       Write32BigEndian( (uint8_t *)&p[i], p[i] );\r
-               }\r
-       }\r
+       convertDictionaryInfoWrite (&SD);\r
 \r
        if( WriteChunkToFile( fid, ID_P4DI, (char *) &SD, sizeof(DictionaryInfoChunk) ) < 0 ) goto error;\r
 \r
 \r
        if( WriteChunkToFile( fid, ID_P4DI, (char *) &SD, sizeof(DictionaryInfoChunk) ) < 0 ) goto error;\r
 \r
@@ -517,7 +534,6 @@ PForthDictionary pfLoadDictionary( const char *FileName, ExecToken *EntryPointPt
        uint32_t FormSize;\r
        uint32_t BytesLeft;\r
        uint32_t numr;\r
        uint32_t FormSize;\r
        uint32_t BytesLeft;\r
        uint32_t numr;\r
-       int   i;\r
        int   isDicBigEndian;\r
 \r
 DBUG(("pfLoadDictionary( %s )\n", FileName ));\r
        int   isDicBigEndian;\r
 \r
 DBUG(("pfLoadDictionary( %s )\n", FileName ));\r
@@ -568,15 +584,8 @@ DBUG(("pfLoadDictionary( %s )\n", FileName ));
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
                        \r
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
                        \r
-/* Convert all fields in structure from BigEndian to Native. */\r
-                       {\r
-                               uint32_t *p = (uint32_t *) sd;\r
-                               for( i=0; i<((int)(sizeof(*sd)/sizeof(uint32_t))); i++ )\r
-                               {\r
-                                       p[i] = Read32BigEndian( (uint8_t *)&p[i] );\r
-                               }\r
-                       }\r
-                               \r
+                       convertDictionaryInfoRead (sd);\r
+\r
                        isDicBigEndian = sd->sd_Flags & SD_F_BIG_ENDIAN_DIC;\r
 \r
                        if( !gVarQuiet )\r
                        isDicBigEndian = sd->sd_Flags & SD_F_BIG_ENDIAN_DIC;\r
 \r
                        if( !gVarQuiet )\r
@@ -645,13 +654,13 @@ DBUG(("pfLoadDictionary( %s )\n", FileName ));
                        if( sd->sd_NameSize > 0 )\r
                        {\r
                                gVarContext = (char *) NAMEREL_TO_ABS(sd->sd_RelContext); /* Restore context. */\r
                        if( sd->sd_NameSize > 0 )\r
                        {\r
                                gVarContext = (char *) NAMEREL_TO_ABS(sd->sd_RelContext); /* Restore context. */\r
-                               gCurrentDictionary->dic_HeaderPtr.Byte = (uint8_t *)\r
+                               gCurrentDictionary->dic_HeaderPtr = (ucell_t)(uint8_t *)\r
                                        NAMEREL_TO_ABS(sd->sd_RelHeaderPtr);\r
                        }\r
                        else\r
                        {\r
                                gVarContext = 0;\r
                                        NAMEREL_TO_ABS(sd->sd_RelHeaderPtr);\r
                        }\r
                        else\r
                        {\r
                                gVarContext = 0;\r
-                               gCurrentDictionary->dic_HeaderPtr.Byte = NULL;\r
+                               gCurrentDictionary->dic_HeaderPtr = (ucell_t)NULL;\r
                        }\r
                        gCurrentDictionary->dic_CodePtr.Byte = (uint8_t *) CODEREL_TO_ABS(sd->sd_RelCodePtr);\r
                        gNumPrimitives = sd->sd_NumPrimitives;  /* Must match compiled dictionary. */\r
                        }\r
                        gCurrentDictionary->dic_CodePtr.Byte = (uint8_t *) CODEREL_TO_ABS(sd->sd_RelCodePtr);\r
                        gNumPrimitives = sd->sd_NumPrimitives;  /* Must match compiled dictionary. */\r
@@ -810,7 +819,7 @@ PForthDictionary pfLoadStaticDictionary( void )
        if( NAME_BASE != NULL)\r
        {\r
 /* Setup name space. */\r
        if( NAME_BASE != NULL)\r
        {\r
 /* Setup name space. */\r
-               dic->dic_HeaderPtr.Byte = (uint8_t *) NAMEREL_TO_ABS(HEADERPTR);\r
+               dic->dic_HeaderPtr = (ucell_t)(uint8_t *) NAMEREL_TO_ABS(HEADERPTR);\r
                gVarContext = (char *) NAMEREL_TO_ABS(RELCONTEXT); /* Restore context. */\r
 \r
 /* Find special words in dictionary for global XTs. */\r
                gVarContext = (char *) NAMEREL_TO_ABS(RELCONTEXT); /* Restore context. */\r
 \r
 /* Find special words in dictionary for global XTs. */\r