Add a level of indirection to make it rather easier to work out which of a
[sgt/putty] / mac / mac.c
1 /* $Id: mac.c,v 1.45 2003/02/04 23:39:26 ben Exp $ */
2 /*
3 * Copyright (c) 1999 Ben Harris
4 * All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27 /*
28 * mac.c -- miscellaneous Mac-specific routines
29 */
30
31 #include <MacTypes.h>
32 #include <AEDataModel.h>
33 #include <AppleEvents.h>
34 #include <Quickdraw.h>
35 #include <Fonts.h>
36 #include <MacWindows.h>
37 #include <Menus.h>
38 #include <TextEdit.h>
39 #include <Appearance.h>
40 #include <CodeFragments.h>
41 #include <Dialogs.h>
42 #include <Devices.h>
43 #include <DiskInit.h>
44 #include <Gestalt.h>
45 #include <LowMem.h>
46 #include <Navigation.h>
47 #include <Resources.h>
48 #include <Script.h>
49 #include <TextCommon.h>
50 #include <ToolUtils.h>
51 #include <UnicodeConverter.h>
52
53 #include <assert.h>
54 #include <limits.h>
55 #include <stdarg.h>
56 #include <stdlib.h> /* putty.h needs size_t */
57 #include <stdio.h> /* for vsprintf */
58
59 #define PUTTY_DO_GLOBALS
60
61 #include "macresid.h"
62 #include "putty.h"
63 #include "ssh.h"
64 #include "mac.h"
65
66 #if !TARGET_API_MAC_CARBON
67 QDGlobals qd;
68 #endif
69
70 Session *sesslist;
71
72 static int cold = 1;
73 static int borednow = FALSE;
74 struct mac_gestalts mac_gestalts;
75
76 static void mac_startup(void);
77 static void mac_eventloop(void);
78 #pragma noreturn (mac_eventloop)
79 static void mac_event(EventRecord *);
80 static void mac_contentclick(WindowPtr, EventRecord *);
81 static void mac_growwindow(WindowPtr, EventRecord *);
82 static void mac_activatewindow(WindowPtr, EventRecord *);
83 static void mac_activateabout(WindowPtr, EventRecord *);
84 static void mac_updatewindow(WindowPtr);
85 static void mac_updatelicence(WindowPtr);
86 static void mac_keypress(EventRecord *);
87 static int mac_windowtype(WindowPtr);
88 static void mac_menucommand(long);
89 static void mac_openabout(void);
90 static void mac_openlicence(void);
91 static void mac_adjustcursor(RgnHandle);
92 static void mac_adjustmenus(void);
93 static void mac_closewindow(WindowPtr);
94 static void mac_zoomwindow(WindowPtr, short);
95 #pragma noreturn (cleanup_exit)
96
97 struct mac_windows {
98 WindowPtr about;
99 WindowPtr licence;
100 };
101
102 struct mac_windows windows;
103
104 int main (int argc, char **argv) {
105
106 mac_startup();
107 mac_eventloop();
108 }
109
110 #pragma noreturn (main)
111
112 static void mac_startup(void) {
113 Handle menuBar;
114 TECInfoHandle ti;
115
116 #if !TARGET_API_MAC_CARBON
117 /* Init Memory Manager */
118 MaxApplZone();
119 /* Init QuickDraw */
120 InitGraf(&qd.thePort);
121 /* Init Font Manager */
122 InitFonts();
123 /* Init Window Manager */
124 InitWindows();
125 /* Init Menu Manager */
126 InitMenus();
127 /* Init TextEdit */
128 TEInit();
129 /* Init Dialog Manager */
130 InitDialogs(NULL);
131 #endif
132 cold = 0;
133
134 /* Get base system version (only used if there's no better selector) */
135 if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
136 (mac_gestalts.sysvers &= 0xffff) < 0x700)
137 fatalbox("PuTTY requires System 7 or newer");
138 /* Find out if we've got Color Quickdraw */
139 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
140 mac_gestalts.qdvers = gestaltOriginalQD;
141 /* ... and the Appearance Manager? */
142 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
143 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
144 mac_gestalts.apprvers = 0x0100;
145 else
146 mac_gestalts.apprvers = 0;
147 #if TARGET_RT_MAC_CFM
148 /* Paranoia: Did we manage to pull in AppearanceLib? */
149 if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
150 mac_gestalts.apprvers = 0;
151 #endif
152 #if TARGET_CPU_68K
153 mac_gestalts.cntlattr = 0;
154 mac_gestalts.windattr = 0;
155 #else
156 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
157 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr ||
158 &SetControlViewSize == kUnresolvedCFragSymbolAddress)
159 mac_gestalts.cntlattr = 0;
160 /* Mac OS 8.5 Window Manager? */
161 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
162 &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
163 mac_gestalts.windattr = 0;
164 #endif
165 /* Text Encoding Conversion Manager? */
166 if (
167 #if TARGET_RT_MAC_CFM
168 &TECGetInfo == kUnresolvedCFragSymbolAddress ||
169 #else
170 InitializeUnicodeConverter(NULL) != noErr ||
171 #endif
172 TECGetInfo(&ti) != noErr)
173 mac_gestalts.encvvers = 0;
174 else {
175 mac_gestalts.encvvers = (*ti)->tecVersion;
176 mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
177 DisposeHandle((Handle)ti);
178 }
179 /* Navigation Services? */
180 if (NavServicesAvailable())
181 mac_gestalts.navsvers = NavLibraryVersion();
182 else
183 mac_gestalts.navsvers = 0;
184
185 sk_init();
186
187 /* We've been tested with the Appearance Manager */
188 if (mac_gestalts.apprvers != 0)
189 RegisterAppearanceClient();
190
191 menuBar = GetNewMBar(128);
192 if (menuBar == NULL)
193 fatalbox("Unable to create menu bar.");
194 SetMenuBar(menuBar);
195 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
196 mac_adjustmenus();
197 DrawMenuBar();
198 InitCursor();
199 windows.about = NULL;
200 windows.licence = NULL;
201
202 default_protocol = be_default_protocol;
203 /* Find the appropriate default port. */
204 {
205 int i;
206 default_port = 0; /* illegal */
207 for (i = 0; backends[i].backend != NULL; i++)
208 if (backends[i].protocol == default_protocol) {
209 default_port = backends[i].backend->default_port;
210 break;
211 }
212 }
213 flags = FLAG_INTERACTIVE;
214
215 #if !TARGET_API_MAC_CARBON
216 {
217 short vol;
218 long dirid;
219
220 /* Set the default directory for loading and saving settings. */
221 /* XXX Should we create it? */
222 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
223 LMSetSFSaveDisk(-vol);
224 LMSetCurDirStore(dirid);
225 }
226 }
227 #endif
228
229 /* Install Apple Event handlers. */
230 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
231 NewAEEventHandlerUPP(&mac_aevt_oapp), 0, FALSE);
232 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
233 NewAEEventHandlerUPP(&mac_aevt_odoc), 0, FALSE);
234 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
235 NewAEEventHandlerUPP(&mac_aevt_pdoc), 0, FALSE);
236 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
237 NewAEEventHandlerUPP(&mac_aevt_quit), 0, FALSE);
238 }
239
240 static void mac_eventloop(void) {
241 Boolean gotevent;
242 EventRecord event;
243 RgnHandle cursrgn;
244
245 cursrgn = NewRgn();
246 for (;;) {
247 mac_adjustcursor(cursrgn);
248 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
249 mac_adjustcursor(cursrgn);
250 if (gotevent)
251 mac_event(&event);
252 if (borednow)
253 cleanup_exit(0);
254 sk_poll();
255 mac_pollterm();
256 }
257 DisposeRgn(cursrgn);
258 }
259
260 static void mac_event(EventRecord *event) {
261 short part;
262 WindowPtr window;
263
264 switch (event->what) {
265 case mouseDown:
266 part = FindWindow(event->where, &window);
267 switch (part) {
268 case inMenuBar:
269 mac_adjustmenus();
270 mac_menucommand(MenuSelect(event->where));
271 break;
272 #if !TARGET_API_MAC_CARBON
273 case inSysWindow:
274 SystemClick(event, window);
275 break;
276 #endif
277 case inContent:
278 if (window != FrontWindow())
279 /* XXX: check for movable modal dboxes? */
280 SelectWindow(window);
281 else
282 mac_contentclick(window, event);
283 break;
284 case inGoAway:
285 if (TrackGoAway(window, event->where))
286 mac_closewindow(window);
287 break;
288 case inDrag:
289 /* XXX: moveable modal check? */
290 #if TARGET_API_MAC_CARBON
291 {
292 BitMap screenBits;
293
294 GetQDGlobalsScreenBits(&screenBits);
295 DragWindow(window, event->where, &screenBits.bounds);
296 }
297 #else
298 DragWindow(window, event->where, &qd.screenBits.bounds);
299 #endif
300 break;
301 case inGrow:
302 mac_growwindow(window, event);
303 break;
304 case inZoomIn:
305 case inZoomOut:
306 if (TrackBox(window, event->where, part))
307 mac_zoomwindow(window, part);
308 break;
309 }
310 break;
311 case keyDown:
312 case autoKey:
313 mac_keypress(event);
314 break;
315 case activateEvt:
316 mac_activatewindow((WindowPtr)event->message, event);
317 break;
318 case updateEvt:
319 mac_updatewindow((WindowPtr)event->message);
320 break;
321 #if !TARGET_API_MAC_CARBON
322 case diskEvt:
323 if (HiWord(event->message) != noErr) {
324 Point pt;
325
326 SetPt(&pt, 120, 120);
327 DIBadMount(pt, event->message);
328 }
329 break;
330 #endif
331 case kHighLevelEvent:
332 AEProcessAppleEvent(event); /* errors? */
333 break;
334 }
335 }
336
337 static void mac_contentclick(WindowPtr window, EventRecord *event) {
338 short item;
339 DialogRef dialog;
340
341 switch (mac_windowtype(window)) {
342 case wTerminal:
343 mac_clickterm(window, event);
344 break;
345 case wAbout:
346 dialog = GetDialogFromWindow(window);
347 if (DialogSelect(event, &dialog, &item))
348 switch (item) {
349 case wiAboutLicence:
350 mac_openlicence();
351 break;
352 }
353 break;
354 case wSettings:
355 mac_clickdlg(window, event);
356 break;
357 }
358 }
359
360 static void mac_growwindow(WindowPtr window, EventRecord *event) {
361
362 switch (mac_windowtype(window)) {
363 case wTerminal:
364 mac_growterm(window, event);
365 }
366 }
367
368 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
369 int active;
370
371 active = (event->modifiers & activeFlag) != 0;
372 mac_adjustmenus();
373 switch (mac_windowtype(window)) {
374 case wTerminal:
375 mac_activateterm(window, active);
376 break;
377 case wSettings:
378 mac_activatedlg(window, event);
379 break;
380 case wAbout:
381 mac_activateabout(window, event);
382 break;
383 }
384 }
385
386 static void mac_activateabout(WindowPtr window, EventRecord *event) {
387 DialogRef dialog;
388 DialogItemType itemtype;
389 Handle itemhandle;
390 short item;
391 Rect itemrect;
392 int active;
393
394 dialog = GetDialogFromWindow(window);
395 active = (event->modifiers & activeFlag) != 0;
396 GetDialogItem(dialog, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
397 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
398 DialogSelect(event, &dialog, &item);
399 }
400
401 static void mac_updatewindow(WindowPtr window)
402 {
403 #if TARGET_API_MAC_CARBON
404 RgnHandle rgn;
405 #endif
406
407 switch (mac_windowtype(window)) {
408 case wTerminal:
409 mac_updateterm(window);
410 break;
411 case wAbout:
412 case wSettings:
413 BeginUpdate(window);
414 #if TARGET_API_MAC_CARBON
415 rgn = NewRgn();
416 GetPortVisibleRegion(GetWindowPort(window), rgn);
417 UpdateDialog(GetDialogFromWindow(window), rgn);
418 DisposeRgn(rgn);
419 #else
420 UpdateDialog(window, window->visRgn);
421 #endif
422 EndUpdate(window);
423 break;
424 case wLicence:
425 mac_updatelicence(window);
426 break;
427 }
428 }
429
430 static void mac_updatelicence(WindowPtr window)
431 {
432 Handle h;
433 int len;
434 long fondsize;
435 Rect textrect;
436
437 SetPort((GrafPtr)GetWindowPort(window));
438 BeginUpdate(window);
439 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
440 TextFont(HiWord(fondsize));
441 TextSize(LoWord(fondsize));
442 h = Get1Resource('TEXT', wLicence);
443 len = GetResourceSizeOnDisk(h);
444 #if TARGET_API_MAC_CARBON
445 GetPortBounds(GetWindowPort(window), &textrect);
446 #else
447 textrect = window->portRect;
448 #endif
449 if (h != NULL) {
450 HLock(h);
451 TETextBox(*h, len, &textrect, teFlushDefault);
452 HUnlock(h);
453 }
454 EndUpdate(window);
455 }
456
457 /*
458 * Work out what kind of window we're dealing with.
459 */
460 static int mac_windowtype(WindowPtr window)
461 {
462
463 #if !TARGET_API_MAC_CARBON
464 if (GetWindowKind(window) < 0)
465 return wDA;
466 #endif
467 return ((WinInfo *)GetWRefCon(window))->wtype;
468 }
469
470 /*
471 * Handle a key press
472 */
473 static void mac_keypress(EventRecord *event) {
474 WindowPtr window;
475
476 window = FrontWindow();
477 /*
478 * Check for a command-key combination, but ignore it if it counts
479 * as a meta-key combination and we're in a terminal window.
480 */
481 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
482 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
483 mac_windowtype(window) == wTerminal)*/) {
484 mac_adjustmenus();
485 mac_menucommand(MenuKey(event->message & charCodeMask));
486 } else {
487 switch (mac_windowtype(window)) {
488 case wTerminal:
489 mac_keyterm(window, event);
490 break;
491 }
492 }
493 }
494
495 static void mac_menucommand(long result) {
496 short menu, item;
497 WindowPtr window;
498 #if !TARGET_API_MAC_CARBON
499 Str255 da;
500 #endif
501
502 menu = HiWord(result);
503 item = LoWord(result);
504 window = FrontWindow();
505 /* Things which do the same whatever window we're in. */
506 switch (menu) {
507 case mApple:
508 switch (item) {
509 case iAbout:
510 mac_openabout();
511 goto done;
512 #if !TARGET_API_MAC_CARBON
513 default:
514 GetMenuItemText(GetMenuHandle(mApple), item, da);
515 OpenDeskAcc(da);
516 goto done;
517 #endif
518 }
519 break;
520 case mFile:
521 switch (item) {
522 case iNew:
523 mac_newsession();
524 goto done;
525 case iOpen:
526 mac_opensession();
527 goto done;
528 case iClose:
529 mac_closewindow(window);
530 goto done;
531 case iSave:
532 mac_savesession();
533 goto done;
534 case iSaveAs:
535 mac_savesessionas();
536 goto done;
537 case iDuplicate:
538 mac_dupsession();
539 goto done;
540 case iQuit:
541 cleanup_exit(0);
542 goto done;
543 }
544 break;
545 }
546 /* If we get here, handling is up to window-specific code. */
547 switch (mac_windowtype(window)) {
548 case wTerminal:
549 mac_menuterm(window, menu, item);
550 break;
551 }
552 done:
553 HiliteMenu(0);
554 }
555
556 static void mac_openabout(void) {
557 DialogItemType itemtype;
558 Handle item;
559 VersRecHndl vers;
560 Rect box;
561 StringPtr longvers;
562 WinInfo *wi;
563
564 if (windows.about)
565 SelectWindow(windows.about);
566 else {
567 windows.about =
568 GetDialogWindow(GetNewDialog(wAbout, NULL, (WindowPtr)-1));
569 wi = smalloc(sizeof(*wi));
570 wi->s = NULL;
571 wi->wtype = wAbout;
572 SetWRefCon(windows.about, (long)wi);
573 vers = (VersRecHndl)Get1Resource('vers', 1);
574 if (vers != NULL && *vers != NULL) {
575 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
576 GetDialogItem(GetDialogFromWindow(windows.about), wiAboutVersion,
577 &itemtype, &item, &box);
578 assert(itemtype & kStaticTextDialogItem);
579 SetDialogItemText(item, longvers);
580 }
581 ShowWindow(windows.about);
582 }
583 }
584
585 static void mac_openlicence(void) {
586 WinInfo *wi;
587
588 if (windows.licence)
589 SelectWindow(windows.licence);
590 else {
591 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
592 wi = smalloc(sizeof(*wi));
593 wi->s = NULL;
594 wi->wtype = wLicence;
595 SetWRefCon(windows.licence, (long)wi);
596 ShowWindow(windows.licence);
597 }
598 }
599
600 static void mac_closewindow(WindowPtr window) {
601
602 switch (mac_windowtype(window)) {
603 #if !TARGET_API_MAC_CARBON
604 case wDA:
605 CloseDeskAcc(GetWindowKind(window));
606 break;
607 #endif
608 case wTerminal:
609 mac_closeterm(window);
610 break;
611 case wAbout:
612 windows.about = NULL;
613 DisposeDialog(GetDialogFromWindow(window));
614 break;
615 case wLicence:
616 windows.licence = NULL;
617 DisposeWindow(window);
618 break;
619 }
620 }
621
622 static void mac_zoomwindow(WindowPtr window, short part) {
623
624 /* FIXME: do something */
625 }
626
627 /*
628 * Make the menus look right before the user gets to see them.
629 */
630 #if TARGET_API_MAC_CARBON
631 #define EnableItem EnableMenuItem
632 #define DisableItem DisableMenuItem
633 #endif
634 static void mac_adjustmenus(void) {
635 WindowPtr window;
636 MenuHandle menu;
637
638 window = FrontWindow();
639 menu = GetMenuHandle(mApple);
640 EnableItem(menu, 0);
641 EnableItem(menu, iAbout);
642
643 menu = GetMenuHandle(mFile);
644 EnableItem(menu, 0);
645 EnableItem(menu, iNew);
646 if (window != NULL)
647 EnableItem(menu, iClose);
648 else
649 DisableItem(menu, iClose);
650 EnableItem(menu, iQuit);
651
652 switch (mac_windowtype(window)) {
653 case wSettings:
654 DisableItem(menu, iSave); /* XXX enable if modified */
655 EnableItem(menu, iSaveAs);
656 EnableItem(menu, iDuplicate);
657 menu = GetMenuHandle(mEdit);
658 DisableItem(menu, 0);
659 break;
660 case wTerminal:
661 mac_adjusttermmenus(window);
662 break;
663 default:
664 DisableItem(menu, iSave);
665 DisableItem(menu, iSaveAs);
666 DisableItem(menu, iDuplicate);
667 menu = GetMenuHandle(mEdit);
668 DisableItem(menu, 0);
669 break;
670 }
671 DrawMenuBar();
672 }
673
674 /*
675 * Make sure the right cursor's being displayed.
676 */
677 static void mac_adjustcursor(RgnHandle cursrgn) {
678 Point mouse;
679 WindowPtr window, front;
680 short part;
681 #if TARGET_API_MAC_CARBON
682 Cursor arrow;
683 RgnHandle visrgn;
684 #endif
685
686 GetMouse(&mouse);
687 LocalToGlobal(&mouse);
688 part = FindWindow(mouse, &window);
689 front = FrontWindow();
690 if (part != inContent || window == NULL || window != front) {
691 /* Cursor isn't in the front window, so switch to arrow */
692 #if TARGET_API_MAC_CARBON
693 GetQDGlobalsArrow(&arrow);
694 SetCursor(&arrow);
695 #else
696 SetCursor(&qd.arrow);
697 #endif
698 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
699 if (front != NULL) {
700 #if TARGET_API_MAC_CARBON
701 visrgn = NewRgn();
702 GetPortVisibleRegion(GetWindowPort(front), visrgn);
703 DiffRgn(cursrgn, visrgn, cursrgn);
704 DisposeRgn(visrgn);
705 #else
706 DiffRgn(cursrgn, front->visRgn, cursrgn);
707 #endif
708 }
709 } else {
710 switch (mac_windowtype(window)) {
711 case wTerminal:
712 mac_adjusttermcursor(window, mouse, cursrgn);
713 break;
714 default:
715 #if TARGET_API_MAC_CARBON
716 GetQDGlobalsArrow(&arrow);
717 SetCursor(&arrow);
718 GetPortVisibleRegion(GetWindowPort(window), cursrgn);
719 #else
720 SetCursor(&qd.arrow);
721 CopyRgn(window->visRgn, cursrgn);
722 #endif
723 break;
724 }
725 }
726 }
727
728 pascal OSErr mac_aevt_quit(const AppleEvent *req, AppleEvent *reply,
729 long refcon)
730 {
731 DescType type;
732 Size size;
733
734 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
735 &type, NULL, 0, &size) == noErr)
736 return errAEParamMissed;
737
738 borednow = 1;
739 return noErr;
740 }
741
742 void cleanup_exit(int status)
743 {
744
745 #if !TARGET_RT_MAC_CFM
746 if (mac_gestalts.encvvers != 0)
747 TerminateUnicodeConverter();
748 #endif
749 sk_cleanup();
750 exit(status);
751 }
752
753 void fatalbox(char *fmt, ...) {
754 va_list ap;
755 Str255 stuff;
756
757 va_start(ap, fmt);
758 /* We'd like stuff to be a Pascal string */
759 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
760 va_end(ap);
761 ParamText(stuff, NULL, NULL, NULL);
762 StopAlert(128, NULL);
763 cleanup_exit(1);
764 }
765
766 void modalfatalbox(char *fmt, ...) {
767 va_list ap;
768 Str255 stuff;
769
770 va_start(ap, fmt);
771 /* We'd like stuff to be a Pascal string */
772 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
773 va_end(ap);
774 ParamText(stuff, NULL, NULL, NULL);
775 StopAlert(128, NULL);
776 cleanup_exit(1);
777 }
778
779 /* This should only kill the current session, not the whole application. */
780 void connection_fatal(void *fontend, char *fmt, ...) {
781 va_list ap;
782 Str255 stuff;
783
784 va_start(ap, fmt);
785 /* We'd like stuff to be a Pascal string */
786 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
787 va_end(ap);
788 ParamText(stuff, NULL, NULL, NULL);
789 StopAlert(128, NULL);
790 cleanup_exit(1);
791 }
792
793 /* Null SSH agent client -- never finds an agent. */
794
795 int agent_exists(void)
796 {
797
798 return FALSE;
799 }
800
801 void agent_query(void *in, int inlen, void **out, int *outlen)
802 {
803
804 *out = NULL;
805 *outlen = 0;
806 }
807
808 /* Temporary null routines for testing. */
809
810 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
811 char *keystr, char *fingerprint)
812 {
813
814 }
815
816 void askcipher(void *frontend, char *ciphername, int cs)
817 {
818
819 }
820
821 void old_keyfile_warning(void)
822 {
823
824 }
825
826 FontSpec platform_default_fontspec(char const *name)
827 {
828 FontSpec ret;
829 long smfs;
830
831 if (!strcmp(name, "Font")) {
832 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
833 if (smfs == 0)
834 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
835 if (smfs != 0) {
836 GetFontName(HiWord(smfs), ret.name);
837 if (ret.name[0] == 0)
838 memcpy(ret.name, "\pMonaco", 7);
839 ret.size = LoWord(smfs);
840 } else {
841 memcpy(ret.name, "\pMonaco", 7);
842 ret.size = 9;
843 }
844 ret.face = 0;
845 } else {
846 ret.name[0] = 0;
847 }
848
849 return ret;
850 }
851
852 Filename platform_default_filename(const char *name)
853 {
854 Filename ret;
855 if (!strcmp(name, "LogFileName"))
856 FSMakeFSSpec(0, 0, "\pputty.log", &ret.fss);
857 else
858 memset(&ret, 0, sizeof(ret));
859 return ret;
860 }
861
862 char *platform_default_s(char const *name)
863 {
864 return NULL;
865 }
866
867 int platform_default_i(char const *name, int def)
868 {
869
870 /* Non-raw cut and paste of line-drawing chars works badly on the
871 * current Unix stub implementation of the Unicode functions.
872 * So I'm going to temporarily set the default to raw mode so
873 * that the failure mode isn't quite so drastically horrid.
874 * When Unicode comes in, this can all be put right. */
875 if (!strcmp(name, "RawCNP"))
876 return 1;
877 return def;
878 }
879
880 void platform_get_x11_auth(char *display, int *proto,
881 unsigned char *data, int *datalen)
882 {
883 /* SGT: I have no idea whether Mac X servers need anything here. */
884 }
885
886 Filename filename_from_str(const char *str)
887 {
888 Filename ret;
889 Str255 tmp;
890
891 /* XXX This fails for filenames over 255 characters long. */
892 c2pstrcpy(tmp, str);
893 FSMakeFSSpec(0, 0, tmp, &ret.fss);
894 return ret;
895 }
896
897 /*
898 * Convert a filename to a string for display purposes.
899 * See pp 2-44--2-46 of IM:Files
900 *
901 * XXX static storage considered harmful
902 */
903 const char *filename_to_str(const Filename *fn)
904 {
905 CInfoPBRec pb;
906 Str255 dirname;
907 OSErr err;
908 static char *path = NULL;
909 char *newpath;
910
911 if (path != NULL) sfree(path);
912 path = smalloc(fn->fss.name[0]);
913 p2cstrcpy(path, fn->fss.name);
914 pb.dirInfo.ioNamePtr = dirname;
915 pb.dirInfo.ioVRefNum = fn->fss.vRefNum;
916 pb.dirInfo.ioDrParID = fn->fss.parID;
917 pb.dirInfo.ioFDirIndex = -1;
918 do {
919 pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
920 err = PBGetCatInfoSync(&pb);
921
922 /* XXX Assume not A/UX */
923 newpath = smalloc(strlen(path) + dirname[0] + 2);
924 p2cstrcpy(newpath, dirname);
925 strcat(newpath, ":");
926 strcat(newpath, path);
927 sfree(path);
928 path = newpath;
929 } while (pb.dirInfo.ioDrDirID != fsRtDirID);
930 return path;
931 }
932
933 int filename_equal(Filename f1, Filename f2)
934 {
935
936 return f1.fss.vRefNum == f2.fss.vRefNum &&
937 f1.fss.parID == f2.fss.parID &&
938 f1.fss.name[0] == f2.fss.name[0] &&
939 memcmp(f1.fss.name + 1, f2.fss.name + 1, f1.fss.name[0]) == 0;
940 }
941
942 int filename_is_null(Filename fn)
943 {
944
945 return fn.fss.vRefNum == 0 && fn.fss.parID == 0 && fn.fss.name[0] == 0;
946 }
947
948 FILE *f_open(Filename fn, char const *mode)
949 {
950 short savevol;
951 long savedir;
952 char tmp[256];
953 FILE *ret;
954
955 HGetVol(NULL, &savevol, &savedir);
956 if (HSetVol(NULL, fn.fss.vRefNum, fn.fss.parID) == noErr) {
957 p2cstrcpy(tmp, fn.fss.name);
958 ret = fopen(tmp, mode);
959 } else
960 ret = NULL;
961 HSetVol(NULL, savevol, savedir);
962 return ret;
963 }
964
965 /*
966 * Local Variables:
967 * c-file-style: "simon"
968 * End:
969 */