home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / applic / ncsa / Mac / Telnet2.6 / prerelease / d5 / Telnet 2.6.1d5.src.sit.hqx / Telnet 2.6.1d5 src / source / init / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-22  |  12.4 KB  |  447 lines

  1. /*
  2. *    init.c
  3. *    Top level of init code & general initialization routines
  4. *    Called only by main()
  5. *
  6. *****************************************************************
  7. *    NCSA Telnet for the Macintosh                                *
  8. *                                                                *
  9. *    National Center for Supercomputing Applications                *
  10. *    Software Development Group                                    *
  11. *    152 Computing Applications Building                            *
  12. *    605 E. Springfield Ave.                                        *
  13. *    Champaign, IL  61820                                        *
  14. *                                                                *
  15. *    Copyright (c) 1986-1992,                                    *
  16. *    Board of Trustees of the University of Illinois                *
  17. *****************************************************************
  18. *  Revisions:
  19. *  7/92        Telnet 2.6:    Initial version.  Jim Browne
  20. */
  21.  
  22. #ifdef MPW
  23. #pragma segment INIT
  24. #endif
  25.  
  26. #include "TelnetHeader.h"
  27. #include "general_resrcdefs.h"
  28. #include "string_resrcdefs.h"
  29. #include "dialog_resrcdefs.h"
  30. #include "menu_resrcdefs.h"
  31. #include <string.h>
  32. #include <Folders.h>
  33. #include <Script.h>
  34. #include <stdio.h>
  35. #include <Gestalt.h>
  36. #include <AppleEvents.h>
  37.  
  38. #include "init.proto.h"
  39. #include "debug.h"
  40. #include "mainseg.proto.h"        /* For forcequit() proto */
  41. #include "iNetwork.proto.h"        /* For initnet proto */
  42. #include "macros.proto.h"        /* For initmacro proto */
  43. #include "wind.h"                /* For WindRec definition */
  44. #include "rsinterf.proto.h"        /* For RSgetwindow & RSnewwindow protos */
  45. #include "DlogUtils.proto.h"    /* For VersionNumber, UItemAssign, GetNewMyDialog protos */
  46. #include "Sets.proto.h"            /* For readconfig proto */
  47. #include "prefs.proto.h"
  48. #include "vr.h"                    /* For VRinit proto */
  49. #include "vsdata.h"
  50. #include "vsinterf.proto.h"        /* For VSinit proto */
  51. #include "vgtek.proto.h"        /* For VGinit proto */
  52. #include "vrrgmac.proto.h"        /* For MacRGinit proto */
  53. #include "telneterrors.h"        /* for DoError proto */
  54. #include "menuseg.proto.h"
  55. #include "translate.proto.h"
  56. #include "tnae.h"
  57. #include "authencrypt.h"
  58. #include "authencrypt.proto.h"
  59. #include "ae.proto.h"            /* For AppleEvent handlers */
  60. #include "MacBinary.h"
  61. #include "binsubs.proto.h"        /* For init_mb_files proto */
  62. #include "drag.proto.h"            /* For MyDragInit */
  63.  
  64.  
  65. /*--------------------------------------------------------------------------------*/
  66. #define CurrentVersion 1        /* Last known SysEnvirons version */
  67. #define HFSPtr ((long *)1014)
  68.  
  69. //extern    char *tempspot;             /* for temp storage only */
  70.  
  71. extern    Cursor *theCursors[];
  72.  
  73. extern    WindRec
  74.     *screens,        /* Window Records (VS) for :    Current Sessions */
  75.     *ftplog;        /*                                FTP Log             */
  76. extern    MenuHandle    myMenus[];
  77.  
  78. OSErr        io;
  79. SysEnvRec    theWorld;                        /* System Environment record */
  80. short        TempItemsVRefNum;
  81. long        TempItemsDirID;
  82. Boolean        gKeyboardHasControlKey, gAEavail;
  83.  
  84. Boolean encryptOK;                // true if des encryption routines available
  85. Boolean authOK;                    // true if authentication driver available
  86.  
  87. /*    checkCONF - check if the user clicked on a configuration file */
  88. void checkCONF( void)
  89. {
  90.     short    i,mess,count;
  91.     AppFile    theFile;
  92.     FSSpec    FileSpec;
  93.     long    junk = 0;
  94.  
  95.     if (gAEavail) return;    // If AppleEvents are available, we'll get ODOCs
  96.  
  97. #ifndef __powerpc__
  98.     CountAppFiles(&mess,&count);
  99.     if (mess==1) return;
  100.     if (count<1) return;
  101.     for(i=1;i<=count;i++) {
  102.         GetAppFiles(i, &theFile);
  103.         if (theFile.fType=='CONF') {
  104.             ClrAppFiles(i);
  105.             BlockMove(&theFile.fName, FileSpec.name, (*theFile.fName)+1); // pstring copy theFile.fName -> FileSpec.name
  106.             GetWDInfo(theFile.vRefNum, &FileSpec.vRefNum, &FileSpec.parID, &junk);
  107.             readconfig(FileSpec);    /* BYU LSC */
  108.             }
  109.         }
  110. #endif
  111.     return;
  112. }
  113.  
  114. void initEvents( void)
  115. {
  116.     EventRecord myEvent;
  117.  
  118.     FlushEvents(everyEvent - diskEvt,0);    /* Don't Let 'em get off without seeing it. */
  119.  
  120.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);    /* WNE always avail */
  121.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);
  122. }
  123.  
  124. /* initftplog - initialize the FTP log window */
  125. void initftplog( void)
  126. {
  127.     Rect prect;
  128.  
  129.     SetRect(&prect, 300,256,512,384);
  130.  
  131.     ftplog = (WindRec *) NewPtrClear(sizeof(WindRec));
  132.     
  133.     ftplog->vs=RSnewwindow( &prect, 50, 80, 24,
  134.                     "\pFTP Log", 1, DefFONT, DefSIZE, gFTPServerPrefs->ShowFTPlog,
  135.                         1,0);    /* NCSA 2.5 */
  136.  
  137.     ftplog->wind = RSgetwindow( ftplog->vs);
  138.     ((WindowPeek)ftplog->wind)->windowKind = WIN_LOG;
  139.     
  140.     VSwrite(ftplog->vs,"\033[24;0H",7);        /* Move to bottom of screen */
  141.     ftplog->active=0;                         /* Meaningless */
  142.     ftplog->port=0;                            /* As is this */
  143.     ftplog->termstate=VTEKTYPE;
  144. }
  145.  
  146.   /* copies a specified cursor from a resource into a nonrelocatable block
  147.     and returns a pointer to it in *result. */
  148. void loadcursor( short CursorID, CursPtr *result)
  149. {
  150.     CursHandle tempcurs;
  151.     OSErr Err;
  152.  
  153.     *result = nil;
  154.     do /* once */
  155.       {
  156.         tempcurs = GetCursor(CursorID);
  157.         Err = ResError();
  158.         if (Err != noErr)
  159.             break;
  160.         *result = (CursPtr) NewPtr(sizeof(Cursor));        /* BYU LSC */
  161.         Err = MemError();
  162.         if (Err != noErr)
  163.             break;
  164.         LoadResource((Handle) tempcurs); /* in case it was purged */
  165.         Err = ResError();
  166.         if (Err != noErr)
  167.             break;
  168.         BlockMove((Ptr) *tempcurs, (Ptr) *result, sizeof(Cursor));
  169.       }
  170.     while (false);
  171.     if (Err != noErr)
  172.       {
  173.         if (*result != nil)
  174.             DisposPtr((Ptr) *result);
  175.         *result = &qd.arrow;
  176.       }
  177. } /* loadcursor */
  178.  
  179. /* initmcurs - Set up the mouse Cursors
  180.  *        NOTE: debug tells if the bug or the pointer is to be used as dflt.*/
  181. void initmcurs ( short debug) 
  182. {
  183.     loadcursor(rWatchCursor, &theCursors[watchcurs]);
  184.     loadcursor(rCrossCursor, &theCursors[graphcurs]);
  185.     loadcursor(rPOSCURS, &theCursors[poscurs]);
  186.     loadcursor(rBeamCursor, &theCursors[textcurs]);
  187.     loadcursor(rGINCURS, &theCursors[gincurs]);
  188.     loadcursor(rXferCURS, &theCursors[xfercurs]);
  189.     if (debug)
  190.         loadcursor(rDbugCURS, &theCursors[normcurs]);
  191.     else
  192.         theCursors[normcurs] = &qd.arrow;
  193.     SetCursor(theCursors[normcurs]);
  194. }
  195.  
  196. /*     If a file with the filetype "DBUG" was sent to us, set the debug mode. */
  197. short checkdebug( void)
  198. {
  199.     short i,mess,count;
  200.     AppFile theFile;
  201.     long junk = 0;
  202.  
  203.     if (gAEavail) return(FALSE);    //     Don't use CountAppFiles if AppleEvents
  204.                                     //    are available.
  205. #ifndef __powerpc__    
  206.     CountAppFiles(&mess,&count);
  207.     if (mess==1) return(FALSE);
  208.     if (count<1) return(FALSE);
  209.     for(i=1;i<=count;i++) {
  210.         GetAppFiles(i, &theFile);
  211.         if (theFile.fType=='DBUG') {
  212.             ClrAppFiles(i);
  213.             return(TRUE);
  214.             }
  215.         }
  216.     return(FALSE);
  217. #endif
  218. }
  219.  
  220. void InquireEnvironment( void)
  221. {
  222.     OSErr    err;
  223.     Boolean HFSflag;
  224.     long    response;
  225.     
  226.     long *HFSp=(long *)1014L;
  227.     
  228.     HFSflag= (*HFSp) > 0L;
  229.     if (!HFSflag) FatalAlert(NEED_HFS_ERR, 0, 0);
  230.  
  231.     err = SysEnvirons(CurrentVersion, &theWorld);
  232.     
  233.     if (err == envVersTooBig) FatalAlert(SYS_ENVIRON_ERR, 0, 0);
  234.     
  235.     if (theWorld.systemVersion < 0x0600) FatalAlert(SYSTEM_VERS_ERR, 0, 0);
  236.     
  237.     if (theWorld.machineType < 0) FatalAlert(ROM_VERS_ERR, 0, 0);
  238.     
  239.     // If there is a problem w/Gestalt, assume our keyboard has a Control key.
  240.     // Otherwise, we assume we have a control key unless a Mac or MacPlus keyboard is
  241.     // present.
  242.         
  243.     if ((err = Gestalt(gestaltKeyboardType, &response)) != noErr)
  244.         gKeyboardHasControlKey = TRUE;
  245.     else
  246.         gKeyboardHasControlKey = !(    (response == gestaltMacKbd) ||
  247.                                     (response == gestaltMacAndPad) ||
  248.                                     (response == gestaltMacPlusKbd) );
  249.                                     
  250.     /* System 6.0 and up ALWAYS has WNE trap! */
  251. }
  252.  
  253. #define    kURLEventClass    'GURL'
  254. #define    kGetURLEvent    'GURL'
  255.  
  256. void initmac( void)
  257. {
  258.     EventRecord myEvent;
  259.     short     i;
  260.     long    gestaltvalue;
  261.     OSErr    err;
  262.  
  263.     InitGraf(&qd.thePort);
  264.     InitFonts();
  265.     InitWindows();
  266.     InitMenus();
  267.     TEInit();
  268.     InitDialogs(0L);                /* No resume proc */
  269.     InitCursor();
  270.     
  271.     TelInfo = (TelInfoRec *)NewPtrClear(sizeof(TelInfoRec));
  272.     //tempspot = (char *)NewPtrClear(256);
  273.     gApplicationPrefs = (ApplicationPrefs *)NewPtrClear(sizeof(ApplicationPrefs));
  274.     gFTPServerPrefs = (FTPServerPrefs *)NewPtrClear(sizeof(FTPServerPrefs));
  275.     screens = (WindRec *)NewPtrClear(MaxSess*sizeof(WindRec));
  276.  
  277.     InquireEnvironment();
  278.  
  279.     for (i=1;i<12;i++)
  280.         EventAvail( 0xffff, &myEvent);
  281.     
  282.     err = Gestalt(gestaltAppleEventsAttr, &gestaltvalue);        // See if AppleEvents are available
  283.     gAEavail = (!err && ((gestaltvalue >> gestaltAppleEventsPresent) & 0x0001));
  284.     
  285.     if (gAEavail) {
  286.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  287.                                             MyHandleOAppUPP,0,FALSE)) != noErr)
  288.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  289.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  290.                                             MyHandleODocUPP,0,FALSE)) != noErr)
  291.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  292.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
  293.                                             MyHandlePDocUPP,0,FALSE)) != noErr)
  294.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  295.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  296.                                             MyHandleQuitUPP,0,FALSE)) != noErr)
  297.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  298.         if ((err = AEInstallEventHandler(kURLEventClass,kGetURLEvent,
  299.                                             MyHandleGURLUPP,0,FALSE)) != noErr)
  300.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  301.         }
  302.  
  303.     TelInfo->screenRect = qd.screenBits.bounds;            /* well, they have to be set */
  304.                                                 /* somewhere, where else ? */
  305.     SetRect(&TelInfo->dragRect, 4, 24, TelInfo->screenRect.right-4,
  306.                         TelInfo->screenRect.bottom-4);
  307. }
  308.  
  309. void DoTheGlobalInits(void)
  310. {
  311.     long junk = 0;
  312.     ParamBlockRec pb;
  313.     
  314.     TelInfo->ScrlLock=0;
  315.     TelInfo->ftplogon=0;
  316.     TelInfo->done=0;
  317.     TelInfo->MacBinary=0;
  318.     TelInfo->numwindows=0;
  319.     TelInfo->CONFstate=0;
  320.     TelInfo->CONFactive=0;
  321.     TelInfo->SettingsFile=-1;
  322.     TelInfo->ApplicationFile = CurResFile();
  323.     TelInfo->ginon   = 0;
  324.     TelInfo->xferon  = 0;
  325.     TelInfo->graphs = 0;
  326.     TelInfo->debug = 1;
  327.     TelInfo->myfronttype = NO_WINDOW;
  328.     TelInfo->myfrontvs = 0;
  329.     TelInfo->suspended = FALSE;
  330.     TelInfo->myfrontwindow = 0L;    
  331.     TelInfo->myfrontRgn = 0L;    
  332.     TelInfo->lastCursor = 0L;
  333.     TelInfo->DefaultDirVRefNum = -1;
  334.     TelInfo->FTPClientVRefNum = -1;
  335.     TelInfo->DefaultDirDirID = 2;
  336.     TelInfo->FTPClientDirID = 2;
  337.     
  338.     pb.ioParam.ioNamePtr = 0L;
  339.     PBGetVol(&pb, FALSE);            /* see Tech Note 140 for implications of this */
  340.  
  341.     GetWDInfo(pb.ioParam.ioVRefNum, &(TelInfo->ApFolder.vRefNum), &(TelInfo->ApFolder.parID), &junk);
  342.     TelInfo->ApFolder.name[0] = 0;
  343.  
  344.     FindFolder( kOnSystemDisk, kSystemFolderType, kCreateFolder,
  345.                     &(TelInfo->SysFolder.vRefNum), &(TelInfo->SysFolder.parID));
  346.     TelInfo->SysFolder.name[0]=0;
  347.  
  348.     FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  349.                     &(TelInfo->PrefFolder.vRefNum), &(TelInfo->PrefFolder.parID));
  350.     TelInfo->PrefFolder.name[0]=0;
  351.     
  352.     FindFolder( kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &TempItemsVRefNum, &TempItemsDirID);
  353. }
  354.  
  355. void init (void)
  356. {
  357.     DialogPtr     dtemp;        /* Used for dialog display */
  358.  
  359.     FlushEvents(everyEvent,0);
  360.     initmac();                /* initialize Macintosh stuff */
  361.     
  362.     DoTheGlobalInits();
  363.  
  364.     OpenPreferencesFile();
  365.     LoadPreferences();
  366.     UnloadSeg(&LoadPreferences);
  367.     
  368.     setupmenu(gApplicationPrefs->CommandKeys);
  369.  
  370.     dtemp = GetNewMyDialog(FirstDLOG, NULL, kInFront, (void *)ThirdCenterDialog);    /* opening dialog */
  371.     UItemAssign( dtemp, 2, VersionNumberUPP);
  372.     DrawDialog(dtemp);                                        /* while we init everything */
  373.  
  374.     trInit(myMenus[National]);    /* LU: Initialize translation routines; must be done */
  375.                                 /* LU: prior to checkdebug(), since even the debug      */
  376.                                 /* LU: output goes through the translation routines. */
  377.     UnloadSeg(&trInit);
  378.  
  379.     TelInfo->debug=checkdebug();    /* must return TRUE or FALSE */
  380.     
  381.     initmcurs(TelInfo->debug);        /* init the mouse cursor */
  382.     SetCursor(theCursors[watchcurs]);
  383.         
  384.     if (VSinit(MaxSess+2)) {        /* initialize the VS (which also inits RS stuff) */
  385.         DoError(104 | MEMORY_ERRORCLASS, LEVEL3, NULL);
  386.         forcequit();
  387.     }
  388.     UnloadSeg(&VSinit);
  389.     
  390.     InitDebug();
  391.     initftplog();                    /* allocate/initialize ftp log screen */
  392.  
  393.     VGinit();                        /* initialize Virtual Graphics after VS so that */
  394.                                     /* init messages can come on console screen */
  395.     UnloadSeg(&VGinit);
  396.     
  397.     VRinit();                        /* ICR init */
  398.     UnloadSeg(&VRinit);
  399.     
  400.     MacRGinit();                    /* ditto */
  401.     UnloadSeg(&MacRGinit);
  402.     
  403.     init_mb_files();                /* Initialize file id's to closed state */
  404.     UnloadSeg(init_mb_files);
  405.  
  406.     initnet();                        /* initialize network stuff */
  407.     switchMenus(gApplicationPrefs->CommandKeys);
  408.  
  409.     TelInfo->done = FALSE;            // We're not quitting yet.
  410.  
  411.     SetCursor(theCursors[normcurs]);
  412.  
  413.     initmacros();
  414.     UnloadSeg(initmacros);
  415.     initEvents();
  416.  
  417.     //initIC();
  418.     io = OpenResolver(NULL);
  419.  
  420.     if (io) {                                
  421.         DialogPtr theErrDialog;
  422.         short itemhit = 0;
  423.  
  424.         theErrDialog = GetNewDialog(RESOLVERERROR, NULL, kInFront);
  425.         DrawDialog(theErrDialog);
  426.         while (!itemhit)
  427.             ModalDialog(DLOGwOKUPP,&itemhit);
  428.         DisposDialog(theErrDialog);
  429.         }
  430.     
  431.     updateMenuChecks();
  432.     DoTheMenuChecks();
  433.  
  434.     MyDragInit();
  435.     UnloadSeg(MyDragInit);
  436.     
  437.     checkCONF();                     /* Did user click on a set? */
  438.  
  439.     DisposDialog(dtemp);            /* Remove the splashbox...  */
  440.  
  441.     encryptOK = authOK = true;
  442.     if (!auth_encrypt_init()) {
  443.         encryptOK = false;
  444.         authOK = false;
  445.     }
  446. }
  447.