Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ReferenceTests/Retro68/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ add_application(UtilitiesCapture UtilitiesCapture.c)
add_application(TrackingCapture TrackingCapture.c)
add_application(AEDescLayoutCapture AEDescLayoutCapture.c)
add_application(ControlRecordCapture ControlRecordCapture.c)
add_application(ObjectSpecifierCapture ObjectSpecifierCapture.c)
target_link_libraries(ObjectSpecifierCapture ObjectSupportLib)
1 change: 1 addition & 0 deletions ReferenceTests/Retro68/ObjectSpecifier-macos-9.2.2-ppc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CreateDesc abso all -> 0CreateDesc abso firs -> 0CreateDesc TEXT -> 0null container: type null handle nilthe ordinal: type abso size 4616c6c20 CreateObjSpecifier TxtW indx all -> 0every text window: type obj size 6800000004 00000000 77616e74 74797065 00000004 54787457 66726f6d 6e756c6c 00000000 666f726d 656e756d 00000004 696e6478 73656c64 6162736f 00000004 616c6c20 container afterwards: type null handle nilkey data afterwards: type abso size 4616c6c20 CreateObjSpecifier cwin name -> 0window by name: type obj size 7000000004 00000000 77616e74 74797065 00000004 6377696e 66726f6d 6e756c6c 00000000 666f726d 656e756d 00000004 6e616d65 73656c64 54455854 00000005 4e6f7465 7300CreateObjSpecifier cha firs in window -> 0first character of it: type obj size 13800000004 00000000 77616e74 74797065 00000004 63686120 66726f6d 6f626a20 00000046 00000004 00000000 77616e74 74797065 00000004 6377696e 66726f6d 6e756c6c 00000000 666f726d 656e756d 00000004 6e616d65 73656c64 54455854 00000005 4e6f7465 7300666f 726d656e 756d0000 0004696e 64787365 6c646162 736f0000 00046669 7273CoerceDesc to reco -> 0as a record: type reco size 8400000000 ab800010 00000018 6f626a20 00000004 00000000 77616e74 74797065 00000004 54787457 66726f6d 6e756c6c 00000000 666f726d 656e756d 00000004 696e6478 73656c64 6162736f 00000004 616c6c20 CountItems on the specifier -> -1704CountItems on the record -> 0the record's count -> 4CreateObjSpecifier disposing -> 0disposing: result: type obj size 7200000004 00000000 77616e74 74797065 00000004 6377696e 66726f6d 6c6f6e67 00000004 00000001 666f726d 656e756d 00000004 696e6478 73656c64 6162736f 00000004 616c6c20 disposing: container left: type long size 400000001 disposing: key data left: type null handle nildone
Expand Down
122 changes: 122 additions & 0 deletions ReferenceTests/Retro68/ObjectSpecifierCapture.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Captures what CreateObjSpecifier makes: the descriptor's type and the bytes
* of its data handle, for the specifier BBEdit Lite 6.1 builds ("every text
* window") and a few others, and what disposing the inputs leaves behind.
*
* It sends nothing. It writes its results as text lines to
* "ObjectSpecifier.txt" in its own folder, and refuses to run if that is
* already there. Each dump is the descriptor's type, its handle's size and the
* handle's bytes.
*/
#include <AppleEvents.h>
#include <Files.h>
#include <MacMemory.h>
#include <stdio.h>
#include <string.h>

/* AEObjects.h, which Multiversal doesn't have. */
pascal OSErr CreateObjSpecifier(DescType desiredClass, AEDesc *theContainer, DescType keyForm,
AEDesc *keyData, Boolean disposeInputs, AEDesc *objSpecifier);

static short out;

static void line(const char *text)
{
long count = strlen(text);
FSWrite(out, &count, text);
count = 1;
FSWrite(out, &count, "\r");
FlushVol(0, 0);
}

static void result(const char *label, OSErr error)
{
char text[200];
sprintf(text, "%s -> %d", label, error);
line(text);
}

static void dump(const char *label, const AEDesc *d)
{
char text[200];
DescType type = d->descriptorType;
Size size, i;
int used;

if (d->dataHandle == NULL) {
sprintf(text, "%s: type %.4s handle nil", label, (const char *)&type);
line(text);
return;
}
size = GetHandleSize((Handle)d->dataHandle);
sprintf(text, "%s: type %.4s size %ld", label, (const char *)&type, (long)size);
line(text);
used = 0;
for (i = 0; i < size && i < 1024; ++i) {
used += sprintf(text + used, "%02x", (unsigned char)(*(Handle)d->dataHandle)[i]);
if (i % 4 == 3) text[used++] = ' ';
if (i % 32 == 31 || i == size - 1) {
text[used] = 0;
line(text);
used = 0;
}
}
}

int main(void)
{
static const unsigned char name[] = { 19, 'O','b','j','e','c','t','S','p','e','c','i','f','i','e','r','.','t','x','t' };
AEDesc nothing, all, first, text, spec, nested, record;
DescType ordinal;
short ref;
long index = 1;
char title[] = "Notes";

if (FSOpen(name, 0, &ref) == noErr) { FSClose(ref); return 1; }
if (Create(name, 0, 'Grph', 'TEXT') != noErr) return 2;
if (FSOpen(name, 0, &out) != noErr) return 3;

nothing.descriptorType = typeNull;
nothing.dataHandle = NULL;
ordinal = 'all ';
result("CreateDesc abso all", AECreateDesc('abso', &ordinal, 4, &all));
ordinal = 'firs';
result("CreateDesc abso firs", AECreateDesc('abso', &ordinal, 4, &first));
result("CreateDesc TEXT", AECreateDesc(typeChar, title, 5, &text));
dump("null container", &nothing);
dump("the ordinal", &all);

/* BBEdit's: every text window of the application. */
result("CreateObjSpecifier TxtW indx all", CreateObjSpecifier('TxtW', &nothing, 'indx', &all, false, &spec));
dump("every text window", &spec);
dump("container afterwards", &nothing);
dump("key data afterwards", &all);

result("CreateObjSpecifier cwin name", CreateObjSpecifier('cwin', &nothing, 'name', &text, false, &nested));
dump("window by name", &nested);
result("CreateObjSpecifier cha firs in window", CreateObjSpecifier('cha ', &nested, 'indx', &first, false, &record));
dump("first character of it", &record);

result("CoerceDesc to reco", AECoerceDesc(&spec, typeAERecord, &record));
dump("as a record", &record);
{
long count = -1;
result("CountItems on the specifier", AECountItems(&spec, &count));
result("CountItems on the record", AECountItems(&record, &count));
result("the record's count", (OSErr)count);
}

/* Disposing the inputs: what is left in the caller's descriptors. */
ordinal = 'all ';
AECreateDesc('abso', &ordinal, 4, &all);
AECreateDesc('long', &index, 4, &nested);
result("CreateObjSpecifier disposing", CreateObjSpecifier('cwin', &nested, 'indx', &all, true, &spec));
dump("disposing: result", &spec);
dump("disposing: container left", &nested);
dump("disposing: key data left", &all);

line("done");
FSClose(out);
FlushVol(0, 0);
return 0;
}
20 changes: 20 additions & 0 deletions ReferenceTests/Retro68/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,26 @@ An event (type `aevt`):
leftover long and padding byte. The null-target event is not compared: its
target type is not meaningful.

## Object specifiers

`ObjectSpecifierCapture.c` builds object specifiers with `CreateObjSpecifier`
and dumps their data handles; BBEdit Lite 6.1 builds the first one, every text
window. The output is `ObjectSpecifier-macos-9.2.2-ppc.txt` (CR line endings).

- A specifier is typed `'obj '`. Its data is the number of items, a zero and
the items `want`, `from`, `form` and `seld`, in that order, laid out as a
record's items: a record without its header. A specifier inside another is
the same bytes.
- `form` is typed `'enum'`, and a null container is a `'null'` item of no
length.
- Coercing a specifier to a record gives a record's header that still names
`'obj '`. `AECountItems` refuses the specifier itself with -1704.
- With `disposeInputs` false both inputs are left as they were. With it true,
the key data was disposed of; a container of type `'long'` was left.

`Descriptor.objectSpecifier(desiredClass:container:keyForm:keyData:)` builds
one, and `ObjectSpecifierTests` compares its bytes with the capture.

## Control records

`ControlRecordCapture.c` makes two scroll bars in a window of its own, the
Expand Down
25 changes: 20 additions & 5 deletions Sources/GraphiteAutomation/DescriptorClassicData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
// event (ReferenceTests/Retro68/AEDescLayout-macos-9.2.2-ppc.txt). Programs
// that write that data themselves depend on it. The layout's own numbers are
// big-endian, and a descriptor's bytes are copied as they are, so one made
// with host numbers goes through toBigEndian() first.
// with host numbers goes through toBigEndian() first. An object specifier's
// handle is a record's without the header: its count, a zero and its items
// (ObjectSpecifier-macos-9.2.2-ppc.txt).

import GraphiteCore

public extension Descriptor {

/// The data handle's bytes for a list, record or Apple event;
/// The data handle's bytes for a list, record, object specifier or Apple event;
/// errAENotAEDesc for another descriptor, and errAEWrongDataType for an
/// event inside one, whose layout isn't known.
func classicData() throws(MacError) -> [UInt8] {
if isAppleEvent { return try classicEventData() }
guard isList || isKeyed else { throw .notDescriptor }
var data = ClassicData()
if type == DescriptorType.objectSpecifier {
data.long(UInt32(items.count))
data.long(0)
try data.append(items, keyed: true, depth: 0)
return data.bytes
}
data.long(0)
data.long(0) // Left over from earlier use on Mac OS.
data.long(UInt32(ClassicData.listHeader))
Expand All @@ -25,14 +33,20 @@ public extension Descriptor {
return data.bytes
}

/// A list, record or Apple event from its data handle's bytes;
/// A list, record, object specifier or Apple event from its data handle's bytes;
/// errAECorruptData for bytes that don't hold what they say they do.
init(type: FourCharCode, classicData bytes: [UInt8]) throws(MacError) {
var data = ClassicData(bytes: bytes)
if type == DescriptorType.appleEvent {
self = try data.event()
return
}
if type == DescriptorType.objectSpecifier {
let count = try data.long()
_ = try data.long()
self.init(type: type, items: try data.items(count, keyed: true, depth: 0), isKeyed: true)
return
}
let isList = type == DescriptorType.list
data.position = 8
let start = try data.long()
Expand Down Expand Up @@ -154,14 +168,15 @@ private struct ClassicData {
mutating func descriptor(depth: Int) throws(MacError) -> Descriptor {
let type = try code()
let data = try value()
guard type == DescriptorType.list || type == DescriptorType.record else {
guard type == DescriptorType.list || type == DescriptorType.record
|| type == DescriptorType.objectSpecifier else {
return Descriptor(type: type, bytes: data)
}
guard depth < Self.maximumDepth else { throw MacError(-108) }
var inner = ClassicData(bytes: data)
let count = try inner.long()
_ = try inner.long()
let keyed = type == DescriptorType.record
let keyed = type != DescriptorType.list
return Descriptor(type: type, items: try inner.items(count, keyed: keyed, depth: depth + 1), isKeyed: keyed)
}

Expand Down
22 changes: 22 additions & 0 deletions Sources/GraphiteAutomation/ObjectSpecifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// What CreateObjSpecifier makes (ReferenceTests/Retro68/
// ObjectSpecifier-macos-9.2.2-ppc.txt): a record of the desired class, the
// container, the key form and the key data, in that order, retyped 'obj '.

import GraphiteCore

public extension Descriptor {

/// An object specifier, with its codes big-endian as Mac OS keeps them.
static func objectSpecifier(desiredClass: FourCharCode, container: Descriptor,
keyForm: FourCharCode, keyData: Descriptor) -> Descriptor {
func code(_ type: FourCharCode, _ value: FourCharCode) -> Descriptor {
Descriptor(type: type, bytes: withUnsafeBytes(of: value.rawValue.bigEndian) { Array($0) })
}
return Descriptor(type: DescriptorType.objectSpecifier, items: [
Item(keyword: "want", descriptor: code(DescriptorType.type, desiredClass)),
Item(keyword: "from", descriptor: container),
Item(keyword: "form", descriptor: code(DescriptorType.enumerated, keyForm)),
Item(keyword: "seld", descriptor: keyData),
], isKeyed: true)
}
}
61 changes: 61 additions & 0 deletions Tests/GraphiteAutomationTests/ObjectSpecifierTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Testing
import Foundation
import GraphiteCore
import GraphiteAutomation

/// Object specifiers' data handles, compared byte for byte with what Mac OS
/// 9.2.2 held (ReferenceTests/Retro68, ObjectSpecifierCapture.c).
@Suite struct ObjectSpecifierTests {

private static let capture: [String] = {
let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent()
.deletingLastPathComponent().deletingLastPathComponent()
let data = try! Data(contentsOf: root.appendingPathComponent("ReferenceTests/Retro68/ObjectSpecifier-macos-9.2.2-ppc.txt"))
return String(decoding: data, as: UTF8.self).split(separator: "\r").map(String.init)
}()

private func captured(_ label: String, sourceLocation: SourceLocation = #_sourceLocation) -> [UInt8] {
guard let start = Self.capture.firstIndex(where: { $0.hasPrefix(label + ": type obj ") }) else {
Issue.record("no capture for \(label)", sourceLocation: sourceLocation)
return []
}
var hex = ""
for line in Self.capture[(start + 1)...] {
guard line.allSatisfy({ $0 == " " || $0.isHexDigit }) else { break }
hex += line.filter { $0 != " " }
}
return stride(from: 0, to: hex.count, by: 2).map { UInt8(hex.dropFirst($0).prefix(2), radix: 16)! }
}

private func check(_ label: String, _ descriptor: Descriptor, sourceLocation: SourceLocation = #_sourceLocation) throws {
let expected = captured(label, sourceLocation: sourceLocation)
#expect(try descriptor.classicData() == expected, "\(label)", sourceLocation: sourceLocation)
let read = try Descriptor(type: DescriptorType.objectSpecifier, classicData: expected)
#expect(read == descriptor, "\(label) read back", sourceLocation: sourceLocation)
}

private func ordinal(_ code: String) -> Descriptor { Descriptor(type: "abso", bytes: Array(code.utf8)) }

@Test func specifiersAsMacOSKeepsThem() throws {
let every = Descriptor.objectSpecifier(desiredClass: "TxtW", container: .null,
keyForm: "indx", keyData: ordinal("all "))
try check("every text window", every)

let named = Descriptor.objectSpecifier(desiredClass: "cwin", container: .null, keyForm: "name",
keyData: Descriptor(type: "TEXT", bytes: Array("Notes".utf8)))
try check("window by name", named)

try check("first character of it", .objectSpecifier(desiredClass: "cha ", container: named,
keyForm: "indx", keyData: ordinal("firs")))
try check("disposing: result", .objectSpecifier(desiredClass: "cwin",
container: Descriptor(type: "long", bytes: [0, 0, 0, 1]),
keyForm: "indx", keyData: ordinal("all ")))
}

@Test func truncatedDataIsRefused() {
let bytes = captured("every text window")
#expect(throws: MacError.self) {
_ = try Descriptor(type: DescriptorType.objectSpecifier, classicData: Array(bytes.dropLast(6)))
}
}
}
Loading