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