mirror of
https://github.com/ksherlock/mpw.git
synced 2025-02-19 17:30:32 +00:00
move platform-specific code into it's own file. Eventually, we want to support Solaris and Windows NT.
This commit is contained in:
parent
3ef7de4b8a
commit
8c50095b2d
@ -12,3 +12,4 @@ add_subdirectory(toolbox)
|
||||
add_subdirectory(mplite)
|
||||
add_subdirectory(mpw)
|
||||
add_subdirectory(macos)
|
||||
add_subdirectory(native)
|
||||
|
@ -79,6 +79,7 @@ target_link_libraries(mpw CPU_LIB)
|
||||
target_link_libraries(mpw TOOLBOX_LIB)
|
||||
target_link_libraries(mpw MPW_LIB)
|
||||
target_link_libraries(mpw MPLITE_LIB)
|
||||
target_link_libraries(mpw NATIVE_LIB)
|
||||
target_link_libraries(mpw MACOS_LIB)
|
||||
set_target_properties(mpw PROPERTIES LINK_FLAGS "-ledit")
|
||||
|
||||
|
8
native/CMakeLists.txt
Normal file
8
native/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
add_definitions(-I ${CMAKE_SOURCE_DIR}/)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wall -g")
|
||||
|
||||
set(NATIVE_SRC native.cpp ${CMAKE_SYSTEM_NAME}.cpp)
|
||||
|
||||
add_library(NATIVE_LIB ${NATIVE_SRC})
|
251
native/Darwin.cpp
Normal file
251
native/Darwin.cpp
Normal file
@ -0,0 +1,251 @@
|
||||
|
||||
#include "native_internal.h"
|
||||
#include <sys/attr.h>
|
||||
#include <sys/xattr.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
//using MacOS::macos_error_from_errno;
|
||||
//using MacOS::macos_error;
|
||||
using namespace MacOS;
|
||||
|
||||
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1050
|
||||
#define st_birthtime st_mtime
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
uint32_t rforksize(const std::string &path_name)
|
||||
{
|
||||
ssize_t rv;
|
||||
|
||||
rv = getxattr(path_name.c_str(), XATTR_RESOURCEFORK_NAME, nullptr, 0, 0, 0);
|
||||
if (rv < 0) return 0;
|
||||
return rv;
|
||||
}
|
||||
|
||||
uint32_t rforksize(int fd)
|
||||
{
|
||||
ssize_t rv;
|
||||
|
||||
rv = fgetxattr(fd, XATTR_RESOURCEFORK_NAME, nullptr, 0, 0, 0);
|
||||
if (rv < 0) return 0;
|
||||
return rv;
|
||||
|
||||
}
|
||||
|
||||
void fixup_prodos(uint8_t *buffer) {
|
||||
if (memcmp(buffer + 4, "pdos", 4) == 0) {
|
||||
// mpw expects 'xx ' where
|
||||
// xx are the ascii-encode hex value of the file type.
|
||||
// the hfs fst uses 'p' ftype8 auxtype16
|
||||
|
||||
// todo -- but only if auxtype is $0000 ??
|
||||
if (buffer[0] == 'p' && buffer[2] == 0 && buffer[3] == 0)
|
||||
{
|
||||
static char Hex[] = "0123456789ABCDEF";
|
||||
|
||||
uint8_t ftype = buffer[1];
|
||||
buffer[0] = Hex[ftype >> 4];
|
||||
buffer[1] = Hex[ftype & 0x0f];
|
||||
buffer[2] = ' ';
|
||||
buffer[3] = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
namespace native {
|
||||
|
||||
/*
|
||||
|
||||
tech note PT515
|
||||
ProDOS -> Macintosh conversion
|
||||
|
||||
ProDOS Macintosh
|
||||
Filetype Auxtype Creator Filetype
|
||||
$00 $0000 'pdos' 'BINA'
|
||||
$B0 (SRC) (any) 'pdos' 'TEXT'
|
||||
$04 (TXT) $0000 'pdos' 'TEXT'
|
||||
$FF (SYS) (any) 'pdos' 'PSYS'
|
||||
$B3 (S16) (any) 'pdos' 'PS16'
|
||||
$uv $wxyz 'pdos' 'p' $uv $wx $yz
|
||||
|
||||
Programmer's Reference for System 6.0:
|
||||
|
||||
ProDOS Macintosh
|
||||
File Type Auxiliary Type Creator Type File Type
|
||||
$00 $0000 “pdos” “BINA”
|
||||
$04 (TXT) $0000 “pdos” “TEXT”
|
||||
$FF (SYS) (any) “pdos” “PSYS”
|
||||
$B3 (S16) $DByz “pdos” “p” $B3 $DB $yz
|
||||
$B3 (S16) (any) “pdos” “PS16”
|
||||
$D7 $0000 “pdos” “MIDI”
|
||||
$D8 $0000 “pdos” “AIFF”
|
||||
$D8 $0001 “pdos” “AIFC”
|
||||
$E0 $0005 “dCpy” “dImg”
|
||||
$FF (SYS) (any) “pdos” “PSYS”
|
||||
$uv $wxyz “pdos” “p” $uv $wx $yz
|
||||
|
||||
|
||||
mpw standard:
|
||||
$uv (any) "pdos" printf("%02x ",$uv)
|
||||
|
||||
*/
|
||||
|
||||
|
||||
macos_error get_finder_info(const std::string &path_name, void *info, bool extended) {
|
||||
|
||||
uint8_t buffer[32];
|
||||
int rv;
|
||||
|
||||
std::memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
rv = getxattr(path_name.c_str(), XATTR_FINDERINFO_NAME, buffer, 32, 0, 0);
|
||||
if (rv == 16 || rv == 32) {
|
||||
fixup_prodos(buffer);
|
||||
memcpy(info, buffer, extended ? 32 : 16);
|
||||
return noErr;
|
||||
}
|
||||
|
||||
if (rv < 0) {
|
||||
switch (errno) {
|
||||
case ENOATTR:
|
||||
case ENOTSUP:
|
||||
break;
|
||||
default:
|
||||
return macos_error_from_errno();
|
||||
}
|
||||
}
|
||||
|
||||
/* finder info does not exist */
|
||||
/* 1. check prodos ftype/aux type */
|
||||
uint8_t ftype;
|
||||
uint16_t atype;
|
||||
|
||||
int rv1 = ::getxattr(path_name.c_str(), "prodos.FileType", &ftype, 1, 0, 0);
|
||||
int rv2 = ::getxattr(path_name.c_str(), "prodos.AuxType", &atype, 2, 0, 0);
|
||||
|
||||
if (rv1 == 1 && rv2 == 2) {
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
ftype = (ftype >> 8) | (ftype << 8);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
memcpy(buffer, "pxxxpdos", 8);
|
||||
buffer[1] = (char)ftype;
|
||||
buffer[2] = (char)((atype >> 8) & 0xff);
|
||||
buffer[3] = (char)(atype & 0xff);
|
||||
|
||||
switch (atype)
|
||||
{
|
||||
case 0x00:
|
||||
std::memcpy(buffer, "BINA", 4);
|
||||
break;
|
||||
|
||||
case 0x04:
|
||||
case 0xb0:
|
||||
std::memcpy(buffer, "TEXT", 4);
|
||||
break;
|
||||
|
||||
case 0xff:
|
||||
std::memcpy(buffer, "PSYS", 4);
|
||||
break;
|
||||
|
||||
case 0xd7:
|
||||
std::memcpy(buffer, "MIDI", 4);
|
||||
break;
|
||||
|
||||
case 0xd8:
|
||||
switch (atype)
|
||||
{
|
||||
case 0x0000:
|
||||
std::memcpy(buffer, "AIFF", 4);
|
||||
break;
|
||||
case 0x0001:
|
||||
std::memcpy(buffer, "AIFC", 4);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0xe0:
|
||||
switch (atype)
|
||||
{
|
||||
case 0x0005:
|
||||
std::memcpy(buffer, "dImgdCpy", 8);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
case 0xb3:
|
||||
if (atype == 0)
|
||||
{
|
||||
std::memcpy(buffer, "PS16", 4); // verify dumpobj.
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
fixup_prodos(buffer);
|
||||
memcpy(info, buffer, extended ? 32 : 16);
|
||||
return noErr;
|
||||
}
|
||||
|
||||
/* if it's a text file, call it a text file */
|
||||
if (is_text_file_internal(path_name)) {
|
||||
memcpy(buffer, "TEXTMPS ", 8);
|
||||
memcpy(info, buffer, extended ? 32 : 16);
|
||||
}
|
||||
|
||||
memcpy(info, buffer, extended ? 32 : 16);
|
||||
return noErr;
|
||||
|
||||
}
|
||||
|
||||
macos_error get_file_info(const std::string &path_name, file_info &fi)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if (stat(path_name.c_str(), &st) < 0)
|
||||
return macos_error_from_errno();
|
||||
|
||||
fi.create_date = unix_to_mac(st.st_birthtime);
|
||||
fi.modify_date = unix_to_mac(st.st_mtime);
|
||||
fi.backup_date = unix_to_mac(st.st_mtime);
|
||||
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
fi.type = file_info::directory;
|
||||
|
||||
|
||||
int links = st.st_nlink - 2;
|
||||
if (links < 0) links = 0;
|
||||
if (links > 65535) links = 65535;
|
||||
|
||||
fi.entry_count = links;
|
||||
return noErr;
|
||||
}
|
||||
|
||||
// todo -- get actual block size instead of assuming 512. oh well!
|
||||
|
||||
fi.type = file_info::file;
|
||||
fi.data_logical_size = st.st_size;
|
||||
fi.data_physical_size = (st.st_size + 511) & ~511;
|
||||
fi.resource_physical_size = 0;
|
||||
fi.resource_logical_size = 0;
|
||||
|
||||
get_finder_info(path_name, fi.finder_info);
|
||||
|
||||
ssize_t rsize = rforksize(path_name);
|
||||
if (rsize > 0) {
|
||||
fi.resource_physical_size = rsize;
|
||||
fi.resource_logical_size = (rsize + 511) & ~511;
|
||||
}
|
||||
|
||||
return noErr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
234
native/native.cpp
Normal file
234
native/native.cpp
Normal file
@ -0,0 +1,234 @@
|
||||
|
||||
|
||||
#include "native_internal.h"
|
||||
|
||||
|
||||
using namespace MacOS;
|
||||
|
||||
namespace {
|
||||
const long epoch_adjust = 86400 * (365 * (1970 - 1904) + 17); // 17 leap years.
|
||||
|
||||
|
||||
std::string extension(const std::string &s)
|
||||
{
|
||||
std::string tmp;
|
||||
int pos;
|
||||
|
||||
pos = s.find_last_of("./:");
|
||||
|
||||
if (pos == s.npos) return tmp;
|
||||
if (s[pos++] != '.') return tmp;
|
||||
if (pos >= s.length()) return tmp;
|
||||
|
||||
tmp = s.substr(pos);
|
||||
|
||||
std::transform(tmp.begin(), tmp.end(), tmp.begin(),
|
||||
[](char c) { return tolower(c); }
|
||||
);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
std::string basename(const std::string &s)
|
||||
{
|
||||
int pos = s.find_last_of("/:");
|
||||
if (pos == s.npos) return s;
|
||||
|
||||
return s.substr(pos + 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace native {
|
||||
|
||||
|
||||
time_t unix_to_mac(time_t t) {
|
||||
if (!t) return 0;
|
||||
return t + epoch_adjust;
|
||||
|
||||
}
|
||||
|
||||
|
||||
macos_error get_finder_info(const std::string &path_name, uint32_t &ftype, uint32_t &ctype) {
|
||||
|
||||
uint8_t buffer[16];
|
||||
|
||||
auto err = get_finder_info(path_name, buffer, false);
|
||||
if (err) return err;
|
||||
|
||||
return noErr;
|
||||
ftype = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3] << 0);
|
||||
ctype = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | (buffer[7] << 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool is_text_file_internal(const std::string &path_name) {
|
||||
|
||||
std::string ext = extension(path_name);
|
||||
if (ext.empty()) return false;
|
||||
|
||||
char c = ext[0];
|
||||
switch(c)
|
||||
{
|
||||
case 'a':
|
||||
if (ext == "aii") // assembler
|
||||
return true;
|
||||
if (ext == "asm")
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
if (ext == "c")
|
||||
return true;
|
||||
if (ext == "cpp")
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
if (ext == "equ") // asm iigs include file.
|
||||
return true;
|
||||
if (ext == "equates") // asm iigs include file.
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
if (ext == "i") // asmiigs include file
|
||||
return true;
|
||||
if (ext == "inc")
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
if (ext == "h") // c header
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'l':
|
||||
if (ext == "lst") // asm iigs listing
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'm':
|
||||
if (ext == "macros")
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
if (ext == "p") // pascal
|
||||
return true;
|
||||
if (ext == "pas") // pascal
|
||||
return true;
|
||||
if (ext == "pii") // pascal
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
if (ext == "r")
|
||||
return true;
|
||||
if (ext == "rez")
|
||||
return true;
|
||||
if (ext == "rii") // rez
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 's':
|
||||
if (ext == "src") // asm equates
|
||||
return true;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
// check for e16.xxxx or m16.xxxx
|
||||
ext = basename(path_name);
|
||||
if (ext.length() > 4)
|
||||
{
|
||||
switch (ext[0])
|
||||
{
|
||||
case 'm':
|
||||
case 'M':
|
||||
case 'e':
|
||||
case 'E':
|
||||
if (!strncmp("16.", ext.c_str() + 1, 3))
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_binary_file_internal(const std::string &path_name) {
|
||||
|
||||
std::string ext = extension(path_name);
|
||||
if (ext.empty()) return false;
|
||||
|
||||
char c = ext[0];
|
||||
switch(c)
|
||||
{
|
||||
case 'l':
|
||||
if (ext == "lib")
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
// MrC / MrCpp temp file.
|
||||
if (ext == "n")
|
||||
return true;
|
||||
// Newton C++ Tools output
|
||||
if (ext == "ntkc")
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
if (ext == "o")
|
||||
return true;
|
||||
if (ext == "obj")
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 's':
|
||||
// Newton C++ Intermediate file
|
||||
if (ext == "sym")
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool is_text_file(const std::string &path_name) {
|
||||
|
||||
uint32_t ftype, ctype;
|
||||
|
||||
auto err = get_finder_info(path_name, ftype, ctype);
|
||||
if (!err) return ftype == 'TEXT';
|
||||
|
||||
return is_text_file_internal(path_name);
|
||||
}
|
||||
|
||||
bool is_binary_file(const std::string &path_name) {
|
||||
|
||||
uint32_t ftype, ctype;
|
||||
|
||||
auto err = get_finder_info(path_name, ftype, ctype);
|
||||
if (!err) {
|
||||
if (ctype == 'pdos') {
|
||||
// 'Bx '
|
||||
if ((ftype & 0xf0ff) == 'B\x00 ') return true;
|
||||
/* really, anything not TEXT is binary... */
|
||||
if ((ftype & 0xf000) == 'p') return true;
|
||||
//if (ftype >= 'p\xb1\x00\x00' && ftype <= 'p\xbf\xff\xff') return true;
|
||||
//if (ftype == 'PSYS' || ftype == 'PS16') return true;
|
||||
}
|
||||
if (ftype == 'BINA') return true;
|
||||
}
|
||||
|
||||
return is_text_file_internal(path_name);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
48
native/native.h
Normal file
48
native/native.h
Normal file
@ -0,0 +1,48 @@
|
||||
#ifndef __native_h__
|
||||
#define __native_h__
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
|
||||
#include <macos/errors.h>
|
||||
|
||||
namespace native {
|
||||
|
||||
using MacOS::macos_error;
|
||||
|
||||
struct file_info {
|
||||
|
||||
enum { none, file, directory } type = none;
|
||||
|
||||
uint32_t create_date = 0;
|
||||
uint32_t modify_date = 0;
|
||||
uint32_t backup_date = 0;
|
||||
|
||||
/* for files */
|
||||
uint32_t data_logical_size = 0;
|
||||
uint32_t data_physical_size = 0;
|
||||
uint32_t resource_logical_size = 0;
|
||||
uint32_t resource_physical_size = 0;
|
||||
unsigned char finder_info[32] = {};
|
||||
|
||||
/* for directories */
|
||||
uint32_t entry_count = 0; //
|
||||
};
|
||||
|
||||
macos_error get_file_info(const std::string &path_name, file_info &fi);
|
||||
macos_error get_finder_info(const std::string &path_name, void *buffer, bool extended = true);
|
||||
macos_error get_finder_info(const std::string &path_name, uint32_t &ftype, uint32_t &ctype);
|
||||
|
||||
|
||||
time_t unix_to_mac(time_t t);
|
||||
|
||||
|
||||
bool is_text_file(const std::string &path_name);
|
||||
bool is_binary_file(const std::string &path_name);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
15
native/native_internal.h
Normal file
15
native/native_internal.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef __native_internal_h__
|
||||
#define __native_internal_h__
|
||||
|
||||
#include "native.h"
|
||||
|
||||
namespace native {
|
||||
|
||||
/* use filename only */
|
||||
bool is_text_file_internal(const std::string &path_name);
|
||||
bool is_binary_file_internal(const std::string &path_name);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user