home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / uupc 3.1 / (uupc π) / uuhost.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-05  |  24.5 KB  |  1,009 lines  |  [TEXT/KAHL]

  1. /*        uuhost.c
  2.  
  3. */
  4. #define    MAIN    dcpmain
  5. #define CWDSPOOL
  6.  
  7. /*** #include    "host.c" ***/
  8. /*** host.c file included below:: ***/
  9. /*        mac.c
  10.  
  11.         macintosh host 
  12.  
  13.             Portions Copyright © Gary Morris, 1991.  All Rights Reserved Worldwide.
  14.  
  15.             Portions Copyright © David Platt, 1992, 1991.  All Rights Reserved
  16.             Worldwide.
  17.  
  18. */
  19.  
  20. #include <stdio.h>
  21. /* #include "host.h" */
  22. #include "dcp.h"
  23.  
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <setjmp.h>
  27. #include <errno.h>
  28.  
  29. #include <Sound.h>
  30. #include <ShutDown.h>
  31. #include <Connections.h>
  32. #include <CommResources.h>
  33. #include <GestaltEqu.h>
  34.  
  35. # ifdef THINK_C
  36. # include <console.h>
  37. # include <pascal.h>
  38. #endif THINK_C
  39.  
  40. /* about dialog id */
  41. #define Duplicate_CTB_Id (1014)
  42. #define Delete_CTB_Id    (1013)
  43. #define Name_CTB_Id        (1012)
  44. #define Debug_Level_Id  (1011)
  45. #define About_Dlog_Id    (1010)
  46. #define Cmd_Dlog_Id        (1000)
  47.  
  48. typedef enum {
  49.     OK_Button=1, Cancel_Button,
  50.     Slave_RButton, Master_RButton,
  51.     Call_Any_RButton, Call_All_RButton, Call_One_RButton, Call_Schedule,
  52.     Call_Need, Call_Autopilot,
  53. #ifdef DEF_FRAME
  54.     Site_Name_Text, Debug_Level_Text, Debug_Level_Title, D_Default
  55. #else DEF_FRAME
  56.     Site_Name_Text, Debug_Level_Text
  57. #endif DEF_FRAME
  58. } Cmd_Dlog_Items;
  59.  
  60. typedef enum {
  61.     OK_Button2=1, Cancel_Button2,
  62.     Debug_Level_Item, Button0, Button1, Button5, Debug_Default_Item } Debug_Level_Items;
  63.  
  64. /* menu id numbers */
  65. #define N_Menus 5
  66. enum Menu_Ids {Apple_Menu_Id=1, File_Menu_Id=1000, Edit_Menu_Id, Call_Menu_Id, CTB_Menu_Id};
  67.                 
  68. /* menu item list numbers */         
  69. enum Menu_Indexes {Apple_Menu_Index, File_Menu_Index, Edit_Menu_Index, Call_Menu_Index,
  70.                    CTB_Menu_Index};
  71. enum File_Menu_Items {Open_And_Call_Item=1, Call_System_Item, Cancel_Call_Item, Separator1, 
  72.                       Debug_Item, Separator2, Abort_Program_Item,
  73.                       Separator3, Quit_Item};
  74. enum Edit_Menu_Items {Undo_Item=1, Cut_Item=3, Copy_Item, Paste_Item, Clear_Item};
  75. enum Call_Menu_Items {All_Sites=1, Any_Site=2, Per_Schedule=3, Need=4, Slave_Mode=6, Autopilot=7, 
  76.                       Specific_Site=9};
  77. enum CTB_Menu_Items {New_Config=1};
  78.  
  79. static MenuHandle My_Menus[N_Menus];                /* indexed by Menu_Indexes */
  80.  
  81. #define N_Specific_Sites 256
  82.  
  83. static char **Sitenames[N_Specific_Sites];
  84. static int siteCount = 0;
  85.  
  86.  
  87.  
  88. char *curdir;
  89. char * getcwd();
  90. /*
  91. FILE * FOPEN();
  92.  
  93. int CHDIR();
  94. */
  95.  
  96. int    debuglevel;        /* debugging level */
  97.  
  98. int Main_State = Idle_System;
  99. int Background = FALSE;
  100.  
  101. long int connectionManagerGestalt = 0;
  102.  
  103. #ifdef Upgrade
  104. long int systimeout = 0;
  105. #endif Upgrade
  106.  
  107. char *n_argv[20];
  108. char line[128];
  109.  
  110. SysEnvRec SysEnv;
  111. int settingsFileVRefNum;
  112. long int settingsFileDirID;
  113.  
  114. #ifdef STANDALONE
  115. # define DMAILBOX    "mailbox"
  116. # define    DNAME        ""
  117. # define DHOME        "/usr/home/guest"
  118. # define    DDOMAIN        "mac.uucp"
  119. # define    DMAILDIR    "/usr/spool/mail"
  120. # define    DCONFDIR    "/usr/lib/uucp"
  121. # define    DSPOOLDIR    "/usr/spool/uucp"
  122. # define DPUBDIR        "/usr/spool/uucppublic"
  123. # define    DMAILSERVICE    "crash"
  124. # define    DNODENAME    "shappy"
  125. # define    DDEVICE        "modem"
  126. # define    DSPEED        "2400"
  127. # define    DTEMPDIR    "/usr/tmp"
  128.  
  129. #include "uuhost.proto.h"
  130.  
  131. FILE *fe;
  132.  
  133. void
  134. sgenv(char **thename, char *envname, char *dflt) {
  135.     char line[BUFSIZ];
  136.  
  137.     fprintf( stderr, "genv: %s %s\n", envname, dflt ); /**/
  138.     
  139.     if (fgets( line, BUFSIZ, fe ) == NULL) {
  140.         fprintf( stderr, "genv: %s not found, using %s\n", envname, dflt ); /* */
  141.         strcpy( *thename, dflt);
  142.     }
  143.     else {
  144.         strcpy( *thename, line);
  145.         fprintf( stderr, "genv: %s %s\n", envname, *thename ); /**/
  146.     }
  147. }
  148.  
  149. void sloadenv(void)
  150. {
  151.     fprintf( stderr, "sloadenv\n" );
  152.     if ( (fe = FOPEN( "/usr/lib/uucp/defaults", "r" )) == (FILE *) NULL ) {
  153.         fprintf( stderr, "can't open /usr/lib/uucp/defaults  %d\n", errno );
  154.         exit( -1 );
  155.     }
  156.     fprintf( stderr, "sloadenv - fopened\n" );
  157.     /* get environment var's */
  158.     sgenv( &name, NAME, DNAME );
  159.     sgenv( &mailbox, MAILBOX, DMAILBOX );
  160.     sgenv( &nodename, NODENAME, DNODENAME );
  161.     sgenv( &home, HOME, DHOME );
  162.     sgenv( &domain, DOMAIN, DDOMAIN );
  163.     sgenv( &maildir, MAILDIR, DMAILDIR );
  164.     sgenv( &confdir, CONFDIR, DCONFDIR );
  165.     sgenv( &spooldir, SPOOLDIR, DSPOOLDIR );
  166.     sgenv( &pubdir, PUBDIR, DPUBDIR );
  167.     sgenv( &mailserv, MAILSERVICE, DMAILSERVICE );
  168.     sgenv( &device, DEVICE, DDEVICE );
  169.     sgenv( &speed, SPEED, DSPEED );
  170.     sgenv( &tempdir, TEMPDIR, DTEMPDIR );
  171.     
  172.     fclose(fe);
  173. }
  174. #endif
  175.  
  176. #ifdef DEF_FRAME
  177. pascal void Draw_Ring(WindowPtr W, int Item);
  178.  
  179. pascal void Draw_Ring(WindowPtr W, int Item)
  180. {
  181.     int Item_Type;
  182.     Handle H;
  183.     Rect R;
  184.     
  185.     /* set the graphport */
  186.     SetPort((GrafPtr)W);
  187.  
  188.     GetDItem(W, Item, &Item_Type, &H, &R);
  189.  
  190.     PenNormal();
  191.     PenSize(3, 3);
  192.     FrameRoundRect(&R, 16, 16);
  193.     PenNormal();
  194. }
  195. #endif DEF_FRAME
  196.  
  197. char *gets();
  198.  
  199. void Set_Main_State(int New_State);
  200.  
  201. void Set_Main_State(int New_State)
  202. {
  203.     switch (Main_State) {
  204.       case Cancel_Call:
  205.           if (New_State == Call_Systems) return;
  206.           break;
  207.       case Quit_Program:
  208.           /* once in this state, don't change, wait for the program to end */
  209.           if (New_State != Idle_System && New_State != Abort_Program) 
  210.               return;
  211.           break;
  212.       case Abort_Program:
  213.           /* once in this state, don't change, wait for the program to end */
  214.           break;
  215.     }
  216.     
  217.     switch (New_State) {
  218.         case Startup_Program:
  219.         case Idle_System:
  220.             EnableItem(My_Menus[File_Menu_Index], Open_And_Call_Item);
  221.             EnableItem(My_Menus[File_Menu_Index], Call_System_Item);
  222.             DisableItem(My_Menus[File_Menu_Index], Cancel_Call_Item);
  223.             DisableItem(My_Menus[File_Menu_Index], Abort_Program_Item);
  224.             EnableItem(My_Menus[Call_Menu_Index], 0);
  225.             if ((connectionManagerGestalt & (1L << gestaltConnMgrPresent)) == 0) {
  226.                 DisableItem(My_Menus[CTB_Menu_Index], 0);
  227.             } else {
  228.                 EnableItem(My_Menus[CTB_Menu_Index], 0);
  229.             }
  230.             HiliteMenu(0);
  231.                DrawMenuBar();
  232.             printmsg(0, (New_State == Startup_Program) ? "Startup" : "Idle");
  233.             break;
  234.         
  235.         case Call_Systems:
  236.             DisableItem(My_Menus[File_Menu_Index], Open_And_Call_Item);
  237.             DisableItem(My_Menus[File_Menu_Index], Call_System_Item);
  238.             EnableItem(My_Menus[File_Menu_Index], Cancel_Call_Item);
  239.             EnableItem(My_Menus[File_Menu_Index], Abort_Program_Item);
  240.             DisableItem(My_Menus[Call_Menu_Index], 0);
  241.             DisableItem(My_Menus[CTB_Menu_Index], 0);
  242.             HiliteMenu(0);
  243.                DrawMenuBar();
  244.             break;
  245.         
  246.         case Cancel_Call:
  247.             DisableItem(My_Menus[File_Menu_Index], Cancel_Call_Item);
  248.             if (currentConnection) {
  249.                 (*currentConnection->Interrupt)();
  250.             }
  251.             printmsg(0, "Cancellation requested");
  252.             PlayNamed("\pCancel");
  253.             break;
  254.         
  255.         case Abort_Program:
  256.             PlayNamed("\pAbort");
  257.             break;
  258.  
  259.         case Quit_Program:
  260.             if (currentConnection) {
  261.                 (*currentConnection->Interrupt) ();
  262.             }
  263.             break;
  264.     }
  265.     
  266.     Main_State = New_State;
  267. }
  268.  
  269. #ifdef MULTIFINDER
  270. void Do_Command(long Menu_Command, int modifiers);
  271. void Do_Mouse_Action(EventRecord *Cur_Event);
  272.  
  273.  
  274. void Set_RButton(DialogPtr Dlog,int Item,int Min,int Max);
  275. void Hilite_Buttons(DialogPtr Dlog,int Min,int Max, int Value);
  276. int Get_Command_Options(void);
  277.  
  278. void Hilite_Buttons(DialogPtr Dlog, int Min, int Max, int Value)
  279. {
  280.     int i;
  281.     int Item_Type;
  282.     Handle H;
  283.     Rect R;
  284.     
  285.     for (i=Min; i<=Max; i++) {
  286.         GetDItem(Dlog, i, &Item_Type, &H, &R);
  287.         HiliteControl(H, (Value==1) ? 0 : 255);
  288.     }
  289. }
  290.  
  291. void Set_RButton(DialogPtr Dlog, int Item, int Min, int Max)
  292. {
  293.     int i;
  294.     int Item_Type;
  295.     Handle H;
  296.     Rect R;
  297.     
  298.     for (i=Min; i<=Max; i++) {
  299.         GetDItem(Dlog, i, &Item_Type, &H, &R);
  300.         SetCtlValue(H, (i==Item ? 1 : 0));
  301.     }
  302. }
  303.  
  304. int Get_Debug_Level(void);
  305.  
  306. int Get_Debug_Level(void)
  307. {
  308.     int Item_Hit = 0;
  309.     DialogPtr DL_Dlog;
  310.     int Debug_Level = debuglevel;
  311.     int Item_Type;
  312.     Handle H;
  313.     Rect R;
  314.     
  315.     /* open debug level dialog */
  316.     DL_Dlog = GetNewDialog(Debug_Level_Id, (Ptr) 0, (WindowPtr) -1);
  317.  
  318. #ifdef DEF_FRAME
  319.     /* hilite the default (D_Default) placeholder */
  320.     GetDItem(DL_Dlog, Debug_Default_Item,  &Item_Type, &H, &R);
  321.     InsetRect(&R, -4, -4);
  322.     SetDItem(DL_Dlog, Debug_Default_Item, userItem, Draw_Ring, &R);
  323. #endif DEF_FRAME
  324.     
  325.     GetDItem(DL_Dlog, Debug_Level_Item, &Item_Type, &H, &R);
  326.     sprintf(line, "%d", Debug_Level);
  327.     CtoPstr(line);
  328.     SetIText(H, line);
  329.     SelIText(DL_Dlog, Debug_Level_Item, 0, 32767);
  330.     
  331.     Item_Hit = 0;
  332.     while (Item_Hit != OK_Button && Item_Hit != Cancel_Button) {
  333.         ModalDialog((ProcPtr) 0, &Item_Hit);
  334.     
  335.         switch (Item_Hit) {
  336.             case Button0:
  337.                 Debug_Level = 0;
  338.                 break;
  339.             
  340.             case Button1:
  341.                 Debug_Level = 1;
  342.                 break;
  343.             
  344.             case Button5:
  345.                 Debug_Level = 5;
  346.                 break;
  347.             
  348.             case OK_Button:
  349.                 GetIText(H, line);
  350.                 PtoCstr(line);
  351.                 sscanf(line, "%d", &Debug_Level);
  352.                 debuglevel = Debug_Level;
  353.                 if (debuglevel < 0) debuglevel = 0;
  354.                 if (debuglevel >10) debuglevel = 10;
  355.                 break;
  356.         }
  357.         
  358.         if (Item_Hit >= Button0 && Item_Hit <= Button5) {
  359.             sprintf(line, "%d", Debug_Level);
  360.             CtoPstr(line);
  361.             SetIText(H, line);
  362.         }
  363.     }
  364.         
  365.     DisposDialog(DL_Dlog);
  366. }
  367.     
  368. int Get_Command_Options(void)
  369. {
  370.     int Item_Hit = 0;
  371.     DialogPtr Cmd_Dlog;
  372.     int Master = 1;
  373.     int Callee = Call_Schedule;
  374.     char Callee_Str[16];
  375.     int Debug_Level = debuglevel;
  376.     int Item_Type;
  377.     Handle H;
  378.     Rect R;
  379.     
  380.     /* open simulation dialog */
  381.     Cmd_Dlog = GetNewDialog(Cmd_Dlog_Id, (Ptr) 0, (WindowPtr) -1);
  382.     
  383.     Set_RButton(Cmd_Dlog, Master_RButton, Slave_RButton, Master_RButton);
  384.     Set_RButton(Cmd_Dlog, Call_Schedule,  Call_Any_RButton, Call_Autopilot);
  385.     
  386.     GetDItem(Cmd_Dlog, Debug_Level_Text, &Item_Type, &H, &R);
  387.     sprintf(line, "%d", Debug_Level);
  388.     CtoPstr(line);
  389.     SetIText(H, line);
  390.     
  391.     GetDItem(Cmd_Dlog, Site_Name_Text, &Item_Type, &H, &R);
  392.     strcpy(Callee_Str, mailserv);
  393.     CtoPstr(Callee_Str);
  394.     SetIText(H, Callee_Str);
  395.     SelIText(Cmd_Dlog, Site_Name_Text, 0, 32767);
  396.  
  397. #ifdef DEF_FRAME
  398.     /* hilite the default (D_Default) placeholder */
  399.     GetDItem(Cmd_Dlog, D_Default,  &Item_Type, &H, &R);
  400.     InsetRect(&R, -4, -4);
  401.     SetDItem(Cmd_Dlog, D_Default, userItem, Draw_Ring, &R);
  402. #endif DEF_FRAME
  403.     
  404.     Item_Hit = 0;
  405.     while (Item_Hit != OK_Button && Item_Hit != Cancel_Button) {
  406.         ModalDialog((ProcPtr) 0, &Item_Hit);
  407.     
  408.         switch (Item_Hit) {
  409.             case Master_RButton:
  410.             case Slave_RButton:
  411.                 Set_RButton(Cmd_Dlog, Item_Hit, Slave_RButton, Master_RButton);
  412.                 Master = (Item_Hit == Master_RButton) ? 1 : 0;
  413.                 Hilite_Buttons(Cmd_Dlog, Call_Any_RButton, Call_Autopilot, Master);
  414.                 break;
  415.                 
  416.             case Call_Any_RButton:
  417.             case Call_All_RButton:
  418.             case Call_One_RButton:
  419.             case Call_Schedule:
  420.             case Call_Need:
  421.             case Call_Autopilot:
  422.                 Callee = Item_Hit;
  423.                 Set_RButton(Cmd_Dlog, Callee, Call_Any_RButton, Call_Autopilot);
  424.                 break;
  425.             case OK_Button:
  426.                 Set_Main_State(Call_Systems);
  427.                 break;
  428.             case Cancel_Button:
  429.                 Set_Main_State(Idle_System);
  430.                 break;
  431.             case Site_Name_Text:
  432.                 Callee = Call_One_RButton;
  433.                 Set_RButton(Cmd_Dlog, Callee, Call_Any_RButton, Call_Autopilot);
  434.                 break;
  435.         }
  436.     }
  437.  
  438.     if (Main_State == Call_Systems) {
  439.         /* extract debug level from dialog box */
  440.         GetDItem(Cmd_Dlog, Debug_Level_Text, &Item_Type, &H, &R);
  441.         GetIText(H, line);
  442.         PtoCstr(line);
  443.         sscanf(line,"%d",&Debug_Level);
  444.         
  445.         switch (Callee) {
  446.             case Call_Any_RButton:
  447.                 strcpy(Callee_Str, "any");
  448.                 break;
  449.             case Call_All_RButton:
  450.                 strcpy(Callee_Str, "all");
  451.                 break;
  452.             case Call_One_RButton:
  453.                 GetDItem(Cmd_Dlog, Site_Name_Text, &Item_Type, &H, &R);
  454.                 GetIText(H, Callee_Str);
  455.                 PtoCstr(Callee_Str);
  456.                 break;
  457.             case Call_Schedule:
  458.                 strcpy(Callee_Str, "cron");
  459.                 break;
  460.             case Call_Need:
  461.                 strcpy(Callee_Str, "need");
  462.                 break;
  463.             case Call_Autopilot:
  464.                 strcpy(Callee_Str, "auto");
  465.                 break;
  466.         }
  467.         sprintf(line, "-r%d -x%d -s%s", Master, Debug_Level, Callee_Str); 
  468.     }
  469.         
  470.     DisposDialog(Cmd_Dlog);
  471.         
  472. }
  473.  
  474. void Do_Command(long Menu_Command, int modifiers)
  475. {
  476.     int The_Menu;    /* the menu selected */
  477.     int The_Item;    /* the item in the menu */
  478.     Str255 Name;    /* name of DA selected */
  479.     int Ref_Num;    /* reference number of the DA */
  480.     int siteIndex;    /* index number of site to call */
  481.     long dticks;
  482.     Point sfBox;
  483.     SFReply sfReply;
  484.     SFTypeList myType;
  485.         
  486.     The_Menu = HiWord(Menu_Command);
  487.     The_Item = LoWord(Menu_Command);
  488.     
  489.     switch (The_Menu) {
  490.         case Apple_Menu_Id:
  491.             /* open DA with item's name */
  492.             if (The_Item == 1) {
  493.                 /* show about box */
  494.                 int Item_Hit;
  495.                 DialogPtr About_Dlog;
  496.                 
  497.                 /* open about dialog */
  498.                 About_Dlog = GetNewDialog(About_Dlog_Id, (Ptr) 0, (WindowPtr) -1);
  499.                 ModalDialog((ProcPtr) 0, &Item_Hit);
  500.                 DisposDialog(About_Dlog);
  501.             } else {
  502.                 GetItem(My_Menus[Apple_Menu_Index],The_Item,Name);
  503.                 Ref_Num = OpenDeskAcc(Name);
  504.             }
  505.             break;
  506.             
  507.         case File_Menu_Id:
  508.             switch (The_Item) {
  509.                 case Open_And_Call_Item:
  510.                     sfBox.h = 100;
  511.                     sfBox.v = 50;
  512.                     myType[0] = (OSType) 'CALL';
  513.                     SFGetFile(sfBox, (unsigned char *) "\p", (ProcPtr) NULL, -1, &myType, (ProcPtr) NULL, &sfReply);
  514.                     if (sfReply.good) {
  515.                         PtoCstr((char *) sfReply.fName);
  516.                         if (strncmp((char *) sfReply.fName, "slave", 5) == 0) {
  517.                             sprintf(line, "-r0 -s%s", sfReply.fName);
  518.                         } else {
  519.                             sprintf(line, "-r1 -s%s", sfReply.fName);
  520.                         }
  521.                         Set_Main_State(Call_Systems);
  522.                     }
  523.                     break;
  524.                    case Call_System_Item:
  525.                     /* call which system?  put up dialog */
  526.                     Set_Main_State(Call_Systems);
  527.                     Get_Command_Options();
  528.                     break;
  529.                 
  530.                 case Cancel_Call_Item:
  531.                     Set_Main_State(Cancel_Call);
  532.                     Delay(15, &dticks);
  533.                     break;
  534.                     
  535.                 case Debug_Item:
  536.                     Get_Debug_Level();
  537.                     break;
  538.                     
  539.                 case Abort_Program_Item:
  540.                     Set_Main_State(Abort_Program);
  541.                     Delay(15, &dticks);
  542.                     break;
  543.                     
  544.                 case Quit_Item:
  545.                     Set_Main_State(Quit_Program);    /* exit at next opportunity */
  546.                     Delay(15, &dticks);
  547.                     break;
  548.             }
  549.             HiliteMenu(0);        /* unhilite menu bar */
  550.             break;
  551.             
  552.         case Edit_Menu_Id:
  553.             /* process edit command if not System's */
  554.             if (!SystemEdit(The_Item-1)) {
  555.                 Delay(15, &dticks);
  556.                 switch (The_Item) {
  557.                     case Undo_Item:
  558.                         /* UNDO */
  559.                         break;
  560.                     case Cut_Item: 
  561.                         /* CUT */
  562.                         break;
  563.                     case Copy_Item:
  564.                         /* COPY */
  565.                         break;
  566.                        case Paste_Item:
  567.                         /* PASTE */
  568.                         break;
  569.                     case Clear_Item:
  570.                         /* CLEAR */
  571.                         break;
  572.                 }
  573.             }
  574.             break;
  575.             
  576.         case Call_Menu_Id:
  577.             Set_Main_State(Call_Systems);
  578.             switch(The_Item) {
  579.                 case All_Sites:
  580.                     sprintf(line, "-r%d -x%d -s%s", 1, debuglevel, "all");
  581.                     break;
  582.                 case Any_Site:
  583.                     sprintf(line, "-r%d -x%d -s%s", 1, debuglevel, "any");
  584.                     break;
  585.                 case Per_Schedule:
  586.                     sprintf(line, "-r%d -x%d -s%s", 1, debuglevel, "cron");
  587.                     break;
  588.                 case Slave_Mode:
  589.                     sprintf(line, "-r%d -x%d -s%s", 0, debuglevel, "any");
  590.                     break;
  591.                 case Need:
  592.                     sprintf(line, "-r%d -x%d -s%s", 1, debuglevel, "need");
  593.                     break;
  594.                 case Autopilot:
  595.                     sprintf(line, "-r%d -x%d -s%s", 1, debuglevel, "auto");
  596.                     break;
  597.                 default:
  598.                     siteIndex = The_Item - (int) Specific_Site;
  599.                     if (siteIndex >= 0 && siteIndex < siteCount) {
  600.                         HLock(Sitenames[siteIndex]);
  601.                         sprintf(line, "-r%d -x%d -s%s", 1, debuglevel, *Sitenames[siteIndex]);
  602.                         HUnlock(Sitenames[siteIndex]);
  603.                     } else {
  604.                         Set_Main_State(Idle_System);
  605.                         SysBeep(10);
  606.                     }
  607.                     break;
  608.             }
  609.             break;
  610.         case CTB_Menu_Id:
  611.             CTB_Setup(The_Item, modifiers);
  612.             break;                
  613.     }
  614. }
  615.  
  616. void CTB_Setup(int item, int modifiers)
  617. {
  618.     ConnHandle connHandle;
  619.     Str255 menuItemName;
  620.     Point where;
  621.     Handle configHandle;
  622.     OSErr errCode;
  623.     Ptr configString;
  624.     int got, duplicate;
  625.     DialogPtr nameDialog;
  626.     Handle rItem;
  627.     int rItemType, itemHit;
  628.     Rect rRect;
  629.     const char nullString[] = "\p";
  630.     if (item == 1) {
  631.         connHandle = CTBLoadConfig("\pDefault");
  632.     } else {
  633.         GetItem(My_Menus[CTB_Menu_Index], item, menuItemName);
  634.         connHandle = CTBLoadConfig((char *) menuItemName);
  635.     }
  636.     if (!connHandle) {
  637.         SysBeep(10);
  638.         SysBeep(10);
  639.         SysBeep(10);
  640.         return;
  641.     }
  642.     if (item != 1 && (modifiers & optionKey)) {
  643.         ParamText(menuItemName, nullString, nullString, nullString);
  644.         itemHit = CautionAlert(Delete_CTB_Id, (ProcPtr) NULL);
  645.         if (itemHit == 1) {
  646.             CTBPurgeConfig((char *) menuItemName);
  647.             DisableItem(My_Menus[CTB_Menu_Index], item);
  648.         }
  649.         CMDispose(connHandle);
  650.         return;
  651.     }    
  652.     where.h = where.v = 40;
  653.     errCode = CMChoose( &connHandle, where, NULL);
  654.     if (    errCode == chooseDisaster || errCode == chooseFailed ||
  655.             errCode == chooseCancel)
  656.     {
  657.         if (errCode != chooseDisaster) {
  658.             CMDispose( connHandle);
  659.         }
  660.     } else {
  661.         if (item == 1 || (modifiers & shiftKey)) {
  662. ask:        strcpy((char *) menuItemName, "Untitled");
  663.             CtoPstr((char *) menuItemName);
  664.             nameDialog = GetNewDialog(Name_CTB_Id, (char *) NULL, (WindowPtr) -1);
  665.             ShowWindow(nameDialog);
  666.             GetDItem(nameDialog, 4, &rItemType, &rItem, &rRect);
  667.             SetIText(rItem, menuItemName);
  668.             itemHit = 0;
  669.             do {
  670.                 ModalDialog((ProcPtr) NULL, &itemHit);
  671.             } while (itemHit != 1 /* OK */ && itemHit != Cancel);
  672.             GetIText(rItem, menuItemName);
  673.             DisposDialog(nameDialog);
  674.             if (itemHit != Cancel) {
  675.                 if (CTBIsNamedConfig((char *) menuItemName)) {
  676.                     ParamText(menuItemName, nullString, nullString, nullString);
  677.                     NoteAlert(Duplicate_CTB_Id, (ProcPtr) NULL);
  678.                     ParamText(nullString, nullString, nullString, nullString);
  679.                     goto ask;
  680.                 }
  681.                    AppendMenu(My_Menus[CTB_Menu_Index], menuItemName);
  682.                 CTBSaveConfig((char *) menuItemName, connHandle);
  683.                }
  684.         } else {
  685.             CTBPurgeConfig((char *) menuItemName);
  686.             CTBSaveConfig((char *) menuItemName, connHandle);
  687.         }
  688.         CMDispose( connHandle );
  689.     }
  690. }
  691.  
  692. void Do_Mouse_Action(EventRecord *Cur_Event)
  693. {
  694.     WindowPeek Which_Window;
  695.     Rect dragRect;
  696.     int code = FindWindow(Cur_Event->where,&Which_Window);
  697.     
  698.     switch (code) {
  699.         case inDesk:
  700.             SysBeep(10);
  701.             break;
  702.         case inMenuBar:
  703.             Do_Command(MenuSelect(Cur_Event->where), Cur_Event->modifiers);
  704.             HiliteMenu(0);
  705.             break;
  706.         case inSysWindow:
  707.             SystemClick(Cur_Event,Which_Window);
  708.             break;
  709.         case inContent:
  710.             /* should already be handled by DialogSelect */
  711.             break;
  712.         case inDrag:
  713.             dragRect = screenBits.bounds;
  714.             DragWindow(Which_Window, Cur_Event->where, &dragRect);
  715.             break;
  716.         case inGrow:
  717.             /* no action, main window has no grow box */
  718.             break;
  719.         case inGoAway:
  720.             /* we don't have any window to make go away */
  721.             break;
  722.         }
  723. }
  724.  
  725. long Last_Check_Event = 0;
  726.  
  727. int Check_Events(long Sleep_Time)
  728. {
  729. /* #define osEvt app4Evt */
  730. #define suspend 1
  731. #define mouseMoved 0xFA
  732. #define resumeMask 1
  733.  
  734.     EventRecord Cur_Event;
  735.     long dticks;
  736.     int Event_Mask = keyDownMask | mDownMask | mUpMask | keyUpMask | autoKeyMask |
  737.                      updateMask | activMask | diskEvt;
  738.     
  739.     static int inited = 0;
  740.     static RgnHandle Mouse_Rgn;
  741.     static Point badMountPoint = {100, 100};
  742.  
  743.     
  744.     if (inited == 0) {
  745.         Mouse_Rgn = NewRgn();
  746.         SetRectRgn(Mouse_Rgn,
  747.                    screenBits.bounds.left,
  748.                    screenBits.bounds.top,
  749.                    screenBits.bounds.right,
  750.                    screenBits.bounds.bottom);
  751.         inited = 1;
  752.     }
  753.     
  754.     if (currentConnection) {
  755.         (*currentConnection->Idle)();
  756.     }
  757.  
  758.     (void) WaitNextEvent(Event_Mask, &Cur_Event, Sleep_Time, (Handle) NULL);
  759.     
  760.     if (currentConnection && (*currentConnection->Event)(&Cur_Event)) {
  761.         goto handled;
  762.     }
  763.  
  764.     /* any events for us? */
  765.     switch (Cur_Event.what) {
  766.         case keyDown:
  767.             if (Cur_Event.modifiers & cmdKey) {
  768.                 SetCursor(&arrow);
  769.                 Do_Command(MenuKey((char) Cur_Event.message & 0xFF), 0);
  770.                 Delay(15,&dticks);
  771.                 HiliteMenu(0);   /* unhilite after it's seen */
  772.             } 
  773.             break;        
  774.                     
  775.         case mouseDown:
  776.             SetCursor(&arrow);
  777.             Do_Mouse_Action(&Cur_Event);
  778.             break;
  779.         
  780.         case diskEvt:
  781.             if ((Cur_Event.message >> 16) != noErr) {
  782.                 (void) DIBadMount(badMountPoint, Cur_Event.message);
  783.             }
  784.             break;
  785.             
  786.         case activateEvt:
  787.             if (Cur_Event.modifiers & activeFlag) {
  788.                 /* set port to the window becoming activated */
  789.                 SetPort((WindowPtr) Cur_Event.message);
  790.             }
  791.             break;
  792.                     
  793.         case updateEvt:
  794.             break;
  795.                     
  796.         case osEvt:
  797.             if (((Cur_Event.message >> 24) & 0xFF) == suspend) {
  798.                 Background = (Cur_Event.message & resumeMask) ? FALSE : TRUE;
  799.                 /* switch to background or foreground */
  800.             }
  801.             break;
  802.     }
  803.  
  804. handled:
  805.     
  806.     Last_Check_Event = Ticks;
  807.     
  808.     return (Main_State == Cancel_Call) || (Main_State == Quit_Program) || 
  809.            (Main_State == Abort_Program) || (Main_State == Shutdown_Program);
  810. }
  811. #endif
  812.  
  813. int Count_Appl_Files(void);
  814. int Count_Appl_Files(void)
  815. {
  816.     int message, count;
  817.     
  818.     CountAppFiles(&message, &count);
  819.     
  820.     return count;
  821. }
  822.  
  823. void AppendToCallMenu(void);
  824.  
  825. void AppendToCallMenu(void)
  826. {
  827.     char state;
  828.     int i, j;
  829.     char **t;
  830.     mkfilename( line, confdir,  SYSTEMS );
  831.     if (strchr(confdir, SEPCHAR) == NULL && strchr(confdir, DIRCHAR) == NULL)
  832.         /* make it an absolute pathname */
  833.         mkfilename(s_systems, curdir, line);
  834.     else
  835.         strcpy(s_systems, line);
  836.     if (( fsys = FOPEN( s_systems, "r", 't' )) == (FILE *)NULL ) {
  837.         printmsg(0, "Can't open %s file!", s_systems);
  838.         return;
  839.     }
  840.     while (TRUE) {
  841.         if (siteCount >= N_Specific_Sites) break;
  842.         state = getsystem("list");
  843.         if (state == 'A') break;
  844.         if (strcmp(rmtname, "INCOMING") == SAME || strcmp(rmtname, "ANONYMOUS") == SAME) {
  845.             goto scanNext;
  846.         }
  847.         for (i = 0; i < siteCount; i++) {
  848.             if (strcmp(rmtname, *Sitenames[i]) == SAME) {
  849.                 goto scanNext;
  850.             }
  851.         }
  852.         Sitenames[siteCount] = NewHandle(strlen(rmtname) + 1);
  853.         MoveHHi(Sitenames[siteCount]);
  854.         HLock(Sitenames[siteCount]);
  855.         strcpy(*Sitenames[siteCount], rmtname);
  856.         siteCount++;
  857. scanNext: ;
  858.     }
  859.     fclose( fsys );
  860.     fsys = (FILE *)NULL;
  861.     for (i = 0; i < siteCount; i++) {
  862.         for (j = i+1; j < siteCount; j++) {
  863.             if (strcmp(*Sitenames[i], *Sitenames[j]) > 0) {
  864.                 t = Sitenames[i];
  865.                 Sitenames[i] = Sitenames[j];
  866.                 Sitenames[j] = t;
  867.             }
  868.         }
  869.         strcpy(rmtname, *Sitenames[i]);
  870.         HUnlock(Sitenames[i]);
  871.         CtoPstr(rmtname);
  872.         AppendMenu(My_Menus[Call_Menu_Index], rmtname);
  873.     }
  874. }
  875.         
  876.  
  877.  
  878.  
  879. main(int argc, char **argv)
  880. {
  881.     int Status;
  882.     int i;
  883.     int appFiles, appFileNo;
  884.     AppFile appFile;
  885.     Str255 apName;
  886.     Handle apParam;
  887.     
  888.     SysEnvirons(1, &SysEnv);
  889.     
  890.     if (noErr != Gestalt(gestaltConnMgrAttr, &connectionManagerGestalt)) {
  891.         connectionManagerGestalt = 0;
  892.     }
  893.  
  894.     console_options.title = (unsigned char *) "\pUUPC";
  895.     console_options.nrows = 24;        /* don't need that huge window, */
  896.     console_options.ncols = 64;        /* especially when it's running in background */
  897.     macinit();
  898.  
  899.     /***** setup menu bar *****/
  900.     /* get handles to menu resources */
  901.     My_Menus[Apple_Menu_Index]   = NewMenu(Apple_Menu_Id, "\p\024" );
  902.     My_Menus[File_Menu_Index]    = GetMenu(File_Menu_Id);
  903.     My_Menus[Edit_Menu_Index]    = GetMenu(Edit_Menu_Id);
  904.     My_Menus[Call_Menu_Index]    = GetMenu(Call_Menu_Id);
  905.     My_Menus[CTB_Menu_Index]    = GetMenu(CTB_Menu_Id);
  906.     
  907.     AppendMenu(My_Menus[Apple_Menu_Index],"\pAbout UUPC...");
  908.     AppendMenu(My_Menus[Apple_Menu_Index],"\p(-");
  909.     AddResMenu(My_Menus[Apple_Menu_Index],'DRVR');    /* get DA names */
  910.     
  911.     for (i=0; i<N_Menus; i++) {
  912.         InsertMenu(My_Menus[i],0);        /* Add menu to menu bar */
  913.     }
  914.  
  915.     DrawMenuBar();
  916.     Set_Main_State(Startup_Program);
  917.     
  918.     /* mac specific prolog */
  919.     
  920.     loadenv();
  921.     
  922.     AppendToCallMenu();
  923.     
  924.     CTBAddToMenu(My_Menus[CTB_Menu_Index]);
  925.  
  926.     appFiles = Count_Appl_Files();
  927.     appFileNo = 1;
  928.     
  929.     curdir = getcwd( NULL, 0 );
  930. #ifdef CWDSPOOL
  931.     CHDIR( spooldir );
  932. #endif
  933.  
  934.     while (Main_State != Quit_Program && Main_State != Abort_Program && 
  935.            Main_State != Shutdown_Program) {
  936.         
  937.         switch (Main_State) {
  938.  
  939.             case Idle_System:
  940.                 Check_Events(50);
  941.             case Startup_Program:
  942.                 if (appFileNo <= appFiles) {
  943.                     GetAppFiles(appFileNo, &appFile);
  944.                     appFileNo++;
  945.                     PtoCstr((char *) appFile.fName);
  946.                     if (strncmp((char *) appFile.fName, "slave", 5) == 0 &&
  947.                         !isalnum(appFile.fName[5])) {
  948.                         printmsg(0, "Launch request: %s", appFile.fName);
  949.                         sprintf(line, "-r0 -x0 -s%s", appFile.fName);
  950.                     } else {
  951.                         sprintf(line, "-r1 -x0 -s%s", appFile.fName);
  952.                         printmsg(0, "Launch request: call %s", appFile.fName);
  953.                     }
  954.                     Set_Main_State(Call_Systems);
  955.                 } else {
  956.                     if (Main_State == Startup_Program) {
  957.                         Set_Main_State(Idle_System);
  958.                     }
  959.                 }
  960.                         
  961.                 break;
  962.                 
  963.             case Call_Systems:
  964.                 Check_Events(MF_DELAY);
  965.                 argc = getargs(line, n_argv);
  966.                 argv = n_argv;
  967.  
  968.                 MAIN( argc, argv );
  969.                 
  970.                 if (Main_State == Call_Systems) Set_Main_State(Idle_System);
  971.                 break;
  972.             
  973.             case Cancel_Call:
  974.                 Check_Events(MF_DELAY);
  975.                 Set_Main_State(Idle_System);
  976.                 appFiles = 0;
  977.                 break;
  978.                 
  979.             default:
  980.                 Check_Events(MF_DELAY);
  981.                 break;
  982.         }
  983.     }
  984.     
  985.     /* mac specific epilog */
  986.     if (Main_State == Shutdown_Program) {
  987.         ShutDwnPower();
  988.     }
  989.     chdir( curdir );
  990.     exit(0);
  991. }
  992.  
  993. void PlayNamed(char *theName)
  994. {
  995.     Handle h;
  996.     long int flags;
  997.     OSErr osErr;
  998.     /*** this is the WRONG TEST but I can't figure out how to tell whether
  999.          SndPlay is present except by checking for the Apple Sound Chip!
  1000.     ***/
  1001.     osErr = Gestalt('hdwr', &flags);
  1002.     if (osErr == noErr &&
  1003.         (flags & 0x8) != 0 &&
  1004.         (h = GetNamedResource('snd ', theName)) != NULL) {
  1005.         SndPlay(NULL, h, FALSE);
  1006.     }
  1007. }
  1008.  
  1009.