home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / clintsrc.sit / unixDA.c < prev    next >
Text File  |  1990-05-05  |  11KB  |  508 lines

  1. /*========================================================================
  2. ===  unixDA.c
  3. ===
  4. ===  Greg Anderson
  5. ===  29 Kerr Hall
  6. ===  Social Sciences Computing
  7. ===  University of California at Santa Cruz
  8. ===  Santa Cruz CA  95062
  9. ===
  10. ===  (408) 459-2658
  11. ===  sirkm@ssyx.ucsc.edu
  12. ===
  13. ===  Client DA is a desk accessory client to Steven Grimm's Unix Server.
  14. ===
  15. ===  Client DA requires MacTCP to run.  It was written with MPW and
  16. ===  is based on the example "memory" desk accessory provided with
  17. ===  that package.
  18. ===
  19. ========================================================================*/
  20. #include    <types.h>
  21. #include    <osutils.h>
  22. #include    <memory.h>
  23. #include    <devices.h>
  24. #include    <events.h>
  25. #include    <quickdraw.h>
  26. #include    <fonts.h>
  27. #include    <windows.h>
  28. #include    <files.h>
  29. #include    <errors.h>
  30. #include    <toolutils.h>
  31. #include    <packages.h>
  32.  
  33. #include    <Limits.h>
  34. #include    <Controls.h>
  35. #include    <TextEdit.h>
  36. #include    <Dialogs.h>
  37. #include    <Desk.h>
  38. #include    <Scrap.h>
  39. #include    <Traps.h>
  40. #include    <Lists.h>
  41.  
  42.  
  43. #include    "unixDA.h"
  44. #include    "DA.h"
  45.  
  46. extern    void    doCtlEvent();
  47. extern    void    doPeriodic();
  48.  
  49. #define toupper(c) ((c>='a')&&(c<='z') ? (c-('a'-'A')) : c)
  50.  
  51. /*----------------------------------------------------------------------
  52. |  Open the DA.
  53. ----------------------------------------------------------------------*/
  54. pascal short DRVROpen(CntrlParam *ctlPB, DCtlPtr dCtl)
  55. {
  56.     #pragma    unused (ctlPB)
  57.     GrafPtr        savePort;
  58.     
  59.     GetPort(&savePort);
  60.  
  61.     /*
  62.      * If the windowPtr is non-nil, we already have a window open.
  63.      * Make it frontmost.
  64.      */
  65.     if( DA_active )
  66.     {
  67.         SelectWindow(DA_window);
  68.         return noErr;
  69.     }
  70.     
  71.     /*
  72.      * Get a handle to some storage that will hold our pseudo-global
  73.      * variables.  Save the handle in a location accessible by
  74.      * all the driver routines.
  75.      *
  76.      * dCtl->dCtlStorage is equivalent to 'DA_global' (by #define)
  77.      */
  78.     dCtl->dCtlStorage = NewHandle(sizeof(Globals));
  79.     /*
  80.      * Compute the resource id of the DA's resources.  The id is saved
  81.      * in one place that can be accessed by all the driver routines.
  82.      */
  83.     DA_global->rsrcID = OWNEDRSRCID(dCtl->dCtlRefNum);
  84.     DA_te = 0L;
  85.     DA_window = 0L;
  86.     DA_global->flag = 0;
  87.     DA_global->lastService = 0;
  88.     
  89.     startDA( dCtl );
  90.  
  91.     SetPort(savePort);
  92.     ctlPB->ioResult = noErr;
  93.     return noErr;
  94. }
  95.  
  96. /*----------------------------------------------------------------------
  97. |  Driver Prime unused.
  98. ----------------------------------------------------------------------*/
  99. pascal short DRVRPrime(CntrlParam *ctlPB, DCtlPtr dCtl)
  100. {
  101.     #pragma    unused (ctlPB, dCtl)
  102.     return noErr;
  103. }
  104.  
  105. /*----------------------------------------------------------------------
  106. |  Driver Status unused.
  107. ----------------------------------------------------------------------*/
  108. pascal short DRVRStatus(CntrlParam *ctlPB, DCtlPtr dCtl)
  109. {
  110.     #pragma    unused (ctlPB, dCtl)
  111.     return noErr;
  112. }
  113.  
  114. /*----------------------------------------------------------------------
  115. |  Driver Control:  main entry point for DA actions.
  116. ----------------------------------------------------------------------*/
  117. pascal short DRVRControl(CntrlParam *ctlPB, DCtlPtr dCtl)
  118. {
  119.     GrafPtr        savePort;
  120.     
  121.     GetPort(&savePort);
  122.  
  123.     if( DA_inactive ) return noErr;
  124.     
  125.     /*
  126.      * The current grafPort is saved & restored by the Desk Manager.
  127.      * Lock the handle since it will be dereferenced.
  128.      */
  129.     HLock(dCtl->dCtlStorage);
  130.     doIdle( dCtl );
  131.     /*
  132.      *  Main action switch:
  133.      */
  134.     switch (ctlPB->csCode) {
  135.         case accEvent:
  136.             doCtlEvent( *((EventRecord **) &ctlPB->csParam[0]), dCtl );
  137.             break;
  138.  
  139.         case accRun:
  140.             doPeriodic( dCtl );
  141.             break;
  142.         
  143.         case accCursor:
  144.             adjustCursor( dCtl );
  145.             break;
  146.         
  147.         case accUndo:
  148.             doUndo( dCtl );
  149.             break;
  150.             
  151.         case accCut:
  152.             doCut( dCtl );
  153.             break;
  154.         
  155.         case accCopy:
  156.             doCopy( dCtl );
  157.             break;
  158.         
  159.         case accPaste:
  160.             doPaste( dCtl );
  161.             break;
  162.         
  163.         case accClear:
  164.             doClear( dCtl );
  165.             break;
  166.  
  167.         default:
  168.             break;
  169.     }
  170.     HUnlock(dCtl->dCtlStorage);
  171.     SetPort(savePort);
  172.     return 0;
  173. }
  174.  
  175. /*----------------------------------------------------------------------
  176. |  Close the DA.
  177. ----------------------------------------------------------------------*/
  178. pascal short DRVRClose(char *ctlPB, DCtlPtr dCtl)
  179. {
  180.     GrafPtr        savePort;
  181.     
  182.     GetPort(&savePort);
  183.  
  184.     #pragma    unused (ctlPB)
  185.  
  186.     if( DA_window != nil)
  187.         doCloseWindow( dCtl );
  188.         
  189.     DisposHandle(dCtl->dCtlStorage);
  190.  
  191.     SetPort(savePort);
  192.     return 0;
  193. }
  194.  
  195. /*----------------------------------------------------------------------
  196. |  Startup code:  puts up the 'select service' window, then opens the
  197. |  connection and makes a TE window.
  198. ----------------------------------------------------------------------*/
  199. void startDA( DCtlPtr dCtl )
  200. {
  201.     long        inet_addr;
  202.     char        selection[400];
  203.  
  204.     /*
  205.      *  Select a service to run
  206.      */
  207.     if( selectService( dCtl, &inet_addr, selection ) == 1 )
  208.     {
  209.         /*
  210.          *  Open a window for the DA & send the command to the superserver
  211.          */
  212.         doNew( dCtl );
  213.         connectToServer( dCtl, inet_addr, selection );
  214.     }
  215. }
  216.  
  217. /*----------------------------------------------------------------------
  218. |  DA event handling.
  219. ----------------------------------------------------------------------*/
  220. static void doCtlEvent(register EventRecord *event, DCtlPtr dCtl)
  221. {
  222.     extern        void        drawWindow();
  223.  
  224.     switch ( event->what )
  225.     {
  226.         case nullEvent:
  227.             doIdle( dCtl );
  228.             break;
  229.         case mouseDown:
  230.             doContentClick( dCtl, event);
  231.             break;
  232.         case keyDown:
  233.             if( event->modifiers & cmdKey )
  234.                 doCommandKey(event, dCtl);
  235.         case autoKey:
  236.             if( !(event->modifiers & cmdKey) )
  237.                 doKeyDown(event, dCtl);
  238.             break;
  239.         case activateEvt:
  240.             doActivate( dCtl , (event->modifiers & activeFlag) != 0);
  241.             break;
  242.         case updateEvt:
  243.             doUpdate( dCtl );
  244.             break;
  245.     }
  246. }
  247.  
  248. /*----------------------------------------------------------------------
  249. |  This routine is called every now and again.  All it does at the
  250. |  moment is insure that the TextEdit cursor flashes.
  251. ----------------------------------------------------------------------*/
  252. void doIdle( DCtlPtr dCtl )
  253. {
  254.     if( DA_te ) TEIdle( DA_te );
  255. }
  256.  
  257. /*----------------------------------------------------------------------
  258. |  Periodic action.
  259. ----------------------------------------------------------------------*/
  260. static void doPeriodic(DCtlPtr dCtl)
  261. {
  262.     SetPort( DA_window );
  263.     /*
  264.      *  Do we have memory for our control record?
  265.      */
  266.     if( DA_tcp != nil )
  267.         checkTCP( dCtl );
  268. }
  269.  
  270. /*----------------------------------------------------------------------
  271. |  Abort the process in progress.
  272. ----------------------------------------------------------------------*/
  273. void doAbort( DCtlPtr dCtl )
  274. {
  275.     if( connectionState( dCtl ) >= 8 )
  276.         CloseTCP( dCtl );
  277.     
  278.     DA_global->windButton    = eReconnectButton;
  279.     drawWInfo( dCtl );
  280. }
  281.  
  282. /*----------------------------------------------------------------------
  283. |  Reconnect (start a new session) -- but only if the current one
  284. |  has already closed.
  285. ----------------------------------------------------------------------*/
  286. void doReconnect( DCtlPtr dCtl )
  287. {
  288.     if( connectionState( dCtl ) < 8 )
  289.     {
  290.         doCloseWindow( dCtl );
  291.         startDA( dCtl );
  292.     }
  293. }
  294.  
  295. /*----------------------------------------------------------------------
  296. |  Print a number.
  297. ----------------------------------------------------------------------*/
  298. static void printNum(unsigned long num)
  299. {
  300.     unsigned    char    numStr[32];
  301.  
  302.     TextFace(normal);
  303.     NumToString(num, numStr);
  304.     DrawString(numStr);
  305.     TextFace(bold);
  306. }
  307.  
  308. /*----------------------------------------------------------------------
  309. |  Takes string number 'index' from the STR# resource and copies it to
  310. |  the global string buffer.
  311. ----------------------------------------------------------------------*/
  312. StringPtr text(int index, Globals *globals)
  313. {
  314.     GetIndString(globals->strBuf, globals->rsrcID, index);
  315.     return(globals->strBuf);
  316. }
  317.  
  318. /*----------------------------------------------------------------------
  319. |  Takes string number 'index' from the STR# resource and copies it to
  320. |  the C string passed in.
  321. ----------------------------------------------------------------------*/
  322. void getCstr(int index, char *str, Globals *globals)
  323. {
  324.     Str255    pstring;
  325.     
  326.     GetIndString(pstring,globals->rsrcID, index);
  327.     P2Cstr(str,pstring);
  328. }
  329.  
  330. /*----------------------------------------------------------------------
  331. |  Replace LF with CR.  Note that \r and \n are backwards because
  332. |  MPW defined them that way.
  333. |
  334. |  This routine also translates any CR LF or LF CR pair into a single
  335. |  CR.  CR LF LF will be translated to two CR's.
  336. ----------------------------------------------------------------------*/
  337. int fix_cr(char *str)
  338. {
  339.     char    cr = 0,
  340.             *new;
  341.     int        len = 0;
  342.     
  343.     new = str;
  344.     while( *str )
  345.     {
  346.         if( *str == cr )
  347.         {
  348.             cr = 0;
  349.             ++str;
  350.             continue;
  351.         }
  352.         cr = 0;
  353.         if( *str == '\n' )
  354.             cr = '\r';
  355.         /*
  356.          *  Is the next character a LF?
  357.          */
  358.         if( *str == '\r' )
  359.         {
  360.             cr = '\n';
  361.             *new = '\n';
  362.         }
  363.         else
  364.         {
  365.             *new = *str;
  366.         }
  367.         ++str;
  368.         ++new;
  369.         ++len;
  370.     }
  371.     *new = 0;
  372.     
  373.     return(len);
  374. }
  375.  
  376. /*----------------------------------------------------------------------
  377. |  Avoid linking with the string library:
  378. ----------------------------------------------------------------------*/
  379. void strcpy(char *a, char *b)
  380. {
  381.     while( *b )
  382.         *a++ = *b++;
  383.     *a = 0;
  384. }
  385.  
  386. void pstrcpy(char *a, char *b)
  387. {
  388.     int    i = pstrlen(b) + 1;
  389.     
  390.     while( i-- )
  391.         *a++ = *b++;
  392. }
  393.  
  394. int strlen(char *str)
  395. {
  396.     int        i = 0;
  397.     
  398.     while( *str )
  399.     {
  400.         ++i;
  401.         ++str;
  402.     }
  403.     
  404.     return(i);
  405. }
  406.  
  407. long lstrlen(char *str)
  408. {
  409.     long    i = 0;
  410.     
  411.     while( *str )
  412.     {
  413.         ++i;
  414.         ++str;
  415.     }
  416.     
  417.     return(i);
  418. }
  419.  
  420. void strcat(char *a,char *b)
  421. {
  422.     strcpy(a+strlen(a),b);
  423. }
  424.  
  425. void straddc(char *a,char c)
  426. {
  427.     while( *a ) ++a;
  428.     *a++ = c;
  429.     *a++ = 0;
  430. }
  431.  
  432. int strcmp(char *a,char *b)
  433. {
  434.     while( *a && *b )
  435.     {
  436.         if( *a != *b )
  437.             return( (*a > *b) - (*a < *b) );
  438.         ++a;
  439.         ++b;
  440.     }
  441.     
  442.     if( *a ) return(1);
  443.     if( *b ) return(-1);
  444.     return( 0 );
  445. }
  446.  
  447. int cistrcmp(char *a,char *b)
  448. {
  449.     int    test;
  450.     
  451.     while( *a && *b )
  452.     {
  453.         test = (toupper(*a) > toupper(*b)) - ( toupper(*a) < toupper(*b) );
  454.         if( test )
  455.             return( test );
  456.         ++a;
  457.         ++b;
  458.     }
  459.     
  460.     if( *a ) return(1);
  461.     if( *b ) return(-1);
  462.     return( 0 );
  463. }
  464.  
  465. int pstrcmp( char *a,char *b )
  466. {
  467.     int        minL = min( pstrlen(a), pstrlen(b) ),
  468.             eq_len = ( (pstrlen(a) > pstrlen(b)) - (pstrlen(a) < pstrlen(b)) );
  469.     
  470.     ++a;
  471.     ++b;
  472.     
  473.     while( minL-- )
  474.     {
  475.         if( *a != *b )
  476.             return( (*a > *b) - (*a < *b) );
  477.         ++a;
  478.         ++b;
  479.     }
  480.     return( eq_len );
  481. }
  482.  
  483. /*-----------------------------------------------------------------
  484. |  Convert a pascal string to a C string.  Note that it is legal to
  485. |  call with two pointers to the same string if desired.
  486. -----------------------------------------------------------------*/
  487. char *P2Cstr(char *cstr,char *pstr)
  488. {
  489.     int len,i;
  490.     
  491.     len = pstrlen(pstr);
  492.     for(i=0;i<len;++i)
  493.         cstr[i] = pstr[i+1];
  494.     cstr[len] = 0;
  495.     return(cstr);
  496. }
  497.  
  498. /*-----------------------------------------------------------------
  499. |  Convert a C string to a pascal string.
  500. -----------------------------------------------------------------*/
  501. char *C2Pstr(char *pstr,char *cstr)
  502. {
  503.     pstr[0] = strlen(cstr);
  504.     while(*cstr)
  505.         *(++pstr) = *cstr++;
  506.     return(pstr);
  507. }
  508.