aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/devdraw/macargv.c
blob: a5ea1ade7725b8d6230984c7b9c5f5a5dac78de5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <u.h>
#include <stdio.h>
#include <Carbon/Carbon.h>

AUTOFRAMEWORK(Carbon)

static OSErr Handler(const AppleEvent *event, AppleEvent *reply, long handlerRefcon);

int
main(void)
{
	AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, Handler, 0, false);
	RunApplicationEventLoop();
	return 0;
}

static OSErr
GetFullPathname(FSSpec *fss, char *path, int len)
{
        FSRef fsr;
        OSErr err;

        *path = '\0';
        err = FSpMakeFSRef(fss, &fsr);
        if (err == fnfErr) {
                /* FSSpecs can point to non-existing files, fsrefs can't. */
                FSSpec fss2;
                int tocopy;

                err = FSMakeFSSpec(fss->vRefNum, fss->parID,
                                   (unsigned char*)"", &fss2);
                if (err)
                        return err;
                err = FSpMakeFSRef(&fss2, &fsr);
                if (err)
                        return err;
                err = (OSErr)FSRefMakePath(&fsr, (unsigned char*)path, len-1);
                if (err)
                        return err;
                /* This part is not 100% safe: we append the filename part, but
                ** I'm not sure that we don't run afoul of the various 8bit
                ** encodings here. Will have to look this up at some point...
                */
                strcat(path, "/");
                tocopy = fss->name[0];
                if ((strlen(path) + tocopy) >= len)
                        tocopy = len - strlen(path) - 1;
                if (tocopy > 0)
                        strncat(path, (char*)fss->name+1, tocopy);
        }
        else {
                if (err)
                        return err;
                err = (OSErr)FSRefMakePath(&fsr, (unsigned char*)path, len);
                if (err)
                        return err;
        }
        return 0;
}

static void
chk(int err)
{
	if(err != 0) {
		printf("err %d\n", err);
		exit(1);
	}
}

static OSErr
Handler(const AppleEvent *event, AppleEvent *reply, long handlerRefcon)
{
	AEDesc list;
	DescType type;
	FSSpec f;
	AEKeyword keyword;
	Size actual;
	long len;
	char s[1000];

	chk(AEGetParamDesc(event, keyDirectObject, typeAEList, &list));
	chk(AECountItems(&list, &len));
	chk(AEGetNthPtr(&list, 1, typeFSS, &keyword, &type, (Ptr*)&f, sizeof(FSSpec), &actual));
	chk(GetFullPathname(&f, s, sizeof s));
	printf("%s\n", s);
	fflush(stdout);

	// uncomment to keep handling more open events
	exit(0);
}