home *** CD-ROM | disk | FTP | other *** search
- /*========================================================================
- === unixDA.c
- ===
- === Greg Anderson
- === 29 Kerr Hall
- === Social Sciences Computing
- === University of California at Santa Cruz
- === Santa Cruz CA 95062
- ===
- === (408) 459-2658
- === sirkm@ssyx.ucsc.edu
- ===
- === Client DA is a desk accessory client to Steven Grimm's Unix Server.
- ===
- === Client DA requires MacTCP to run. It was written with MPW and
- === is based on the example "memory" desk accessory provided with
- === that package.
- ===
- ========================================================================*/
- #include <types.h>
- #include <osutils.h>
- #include <memory.h>
- #include <devices.h>
- #include <events.h>
- #include <quickdraw.h>
- #include <fonts.h>
- #include <windows.h>
- #include <files.h>
- #include <errors.h>
- #include <toolutils.h>
- #include <packages.h>
-
- #include <Limits.h>
- #include <Controls.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <Scrap.h>
- #include <Traps.h>
- #include <Lists.h>
-
-
- #include "unixDA.h"
- #include "DA.h"
-
- extern void doCtlEvent();
- extern void doPeriodic();
-
- #define toupper(c) ((c>='a')&&(c<='z') ? (c-('a'-'A')) : c)
-
- /*----------------------------------------------------------------------
- | Open the DA.
- ----------------------------------------------------------------------*/
- pascal short DRVROpen(CntrlParam *ctlPB, DCtlPtr dCtl)
- {
- #pragma unused (ctlPB)
- GrafPtr savePort;
-
- GetPort(&savePort);
-
- /*
- * If the windowPtr is non-nil, we already have a window open.
- * Make it frontmost.
- */
- if( DA_active )
- {
- SelectWindow(DA_window);
- return noErr;
- }
-
- /*
- * Get a handle to some storage that will hold our pseudo-global
- * variables. Save the handle in a location accessible by
- * all the driver routines.
- *
- * dCtl->dCtlStorage is equivalent to 'DA_global' (by #define)
- */
- dCtl->dCtlStorage = NewHandle(sizeof(Globals));
- /*
- * Compute the resource id of the DA's resources. The id is saved
- * in one place that can be accessed by all the driver routines.
- */
- DA_global->rsrcID = OWNEDRSRCID(dCtl->dCtlRefNum);
- DA_te = 0L;
- DA_window = 0L;
- DA_global->flag = 0;
- DA_global->lastService = 0;
-
- startDA( dCtl );
-
- SetPort(savePort);
- ctlPB->ioResult = noErr;
- return noErr;
- }
-
- /*----------------------------------------------------------------------
- | Driver Prime unused.
- ----------------------------------------------------------------------*/
- pascal short DRVRPrime(CntrlParam *ctlPB, DCtlPtr dCtl)
- {
- #pragma unused (ctlPB, dCtl)
- return noErr;
- }
-
- /*----------------------------------------------------------------------
- | Driver Status unused.
- ----------------------------------------------------------------------*/
- pascal short DRVRStatus(CntrlParam *ctlPB, DCtlPtr dCtl)
- {
- #pragma unused (ctlPB, dCtl)
- return noErr;
- }
-
- /*----------------------------------------------------------------------
- | Driver Control: main entry point for DA actions.
- ----------------------------------------------------------------------*/
- pascal short DRVRControl(CntrlParam *ctlPB, DCtlPtr dCtl)
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
-
- if( DA_inactive ) return noErr;
-
- /*
- * The current grafPort is saved & restored by the Desk Manager.
- * Lock the handle since it will be dereferenced.
- */
- HLock(dCtl->dCtlStorage);
- doIdle( dCtl );
- /*
- * Main action switch:
- */
- switch (ctlPB->csCode) {
- case accEvent:
- doCtlEvent( *((EventRecord **) &ctlPB->csParam[0]), dCtl );
- break;
-
- case accRun:
- doPeriodic( dCtl );
- break;
-
- case accCursor:
- adjustCursor( dCtl );
- break;
-
- case accUndo:
- doUndo( dCtl );
- break;
-
- case accCut:
- doCut( dCtl );
- break;
-
- case accCopy:
- doCopy( dCtl );
- break;
-
- case accPaste:
- doPaste( dCtl );
- break;
-
- case accClear:
- doClear( dCtl );
- break;
-
- default:
- break;
- }
- HUnlock(dCtl->dCtlStorage);
- SetPort(savePort);
- return 0;
- }
-
- /*----------------------------------------------------------------------
- | Close the DA.
- ----------------------------------------------------------------------*/
- pascal short DRVRClose(char *ctlPB, DCtlPtr dCtl)
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
-
- #pragma unused (ctlPB)
-
- if( DA_window != nil)
- doCloseWindow( dCtl );
-
- DisposHandle(dCtl->dCtlStorage);
-
- SetPort(savePort);
- return 0;
- }
-
- /*----------------------------------------------------------------------
- | Startup code: puts up the 'select service' window, then opens the
- | connection and makes a TE window.
- ----------------------------------------------------------------------*/
- void startDA( DCtlPtr dCtl )
- {
- long inet_addr;
- char selection[400];
-
- /*
- * Select a service to run
- */
- if( selectService( dCtl, &inet_addr, selection ) == 1 )
- {
- /*
- * Open a window for the DA & send the command to the superserver
- */
- doNew( dCtl );
- connectToServer( dCtl, inet_addr, selection );
- }
- }
-
- /*----------------------------------------------------------------------
- | DA event handling.
- ----------------------------------------------------------------------*/
- static void doCtlEvent(register EventRecord *event, DCtlPtr dCtl)
- {
- extern void drawWindow();
-
- switch ( event->what )
- {
- case nullEvent:
- doIdle( dCtl );
- break;
- case mouseDown:
- doContentClick( dCtl, event);
- break;
- case keyDown:
- if( event->modifiers & cmdKey )
- doCommandKey(event, dCtl);
- case autoKey:
- if( !(event->modifiers & cmdKey) )
- doKeyDown(event, dCtl);
- break;
- case activateEvt:
- doActivate( dCtl , (event->modifiers & activeFlag) != 0);
- break;
- case updateEvt:
- doUpdate( dCtl );
- break;
- }
- }
-
- /*----------------------------------------------------------------------
- | This routine is called every now and again. All it does at the
- | moment is insure that the TextEdit cursor flashes.
- ----------------------------------------------------------------------*/
- void doIdle( DCtlPtr dCtl )
- {
- if( DA_te ) TEIdle( DA_te );
- }
-
- /*----------------------------------------------------------------------
- | Periodic action.
- ----------------------------------------------------------------------*/
- static void doPeriodic(DCtlPtr dCtl)
- {
- SetPort( DA_window );
- /*
- * Do we have memory for our control record?
- */
- if( DA_tcp != nil )
- checkTCP( dCtl );
- }
-
- /*----------------------------------------------------------------------
- | Abort the process in progress.
- ----------------------------------------------------------------------*/
- void doAbort( DCtlPtr dCtl )
- {
- if( connectionState( dCtl ) >= 8 )
- CloseTCP( dCtl );
-
- DA_global->windButton = eReconnectButton;
- drawWInfo( dCtl );
- }
-
- /*----------------------------------------------------------------------
- | Reconnect (start a new session) -- but only if the current one
- | has already closed.
- ----------------------------------------------------------------------*/
- void doReconnect( DCtlPtr dCtl )
- {
- if( connectionState( dCtl ) < 8 )
- {
- doCloseWindow( dCtl );
- startDA( dCtl );
- }
- }
-
- /*----------------------------------------------------------------------
- | Print a number.
- ----------------------------------------------------------------------*/
- static void printNum(unsigned long num)
- {
- unsigned char numStr[32];
-
- TextFace(normal);
- NumToString(num, numStr);
- DrawString(numStr);
- TextFace(bold);
- }
-
- /*----------------------------------------------------------------------
- | Takes string number 'index' from the STR# resource and copies it to
- | the global string buffer.
- ----------------------------------------------------------------------*/
- StringPtr text(int index, Globals *globals)
- {
- GetIndString(globals->strBuf, globals->rsrcID, index);
- return(globals->strBuf);
- }
-
- /*----------------------------------------------------------------------
- | Takes string number 'index' from the STR# resource and copies it to
- | the C string passed in.
- ----------------------------------------------------------------------*/
- void getCstr(int index, char *str, Globals *globals)
- {
- Str255 pstring;
-
- GetIndString(pstring,globals->rsrcID, index);
- P2Cstr(str,pstring);
- }
-
- /*----------------------------------------------------------------------
- | Replace LF with CR. Note that \r and \n are backwards because
- | MPW defined them that way.
- |
- | This routine also translates any CR LF or LF CR pair into a single
- | CR. CR LF LF will be translated to two CR's.
- ----------------------------------------------------------------------*/
- int fix_cr(char *str)
- {
- char cr = 0,
- *new;
- int len = 0;
-
- new = str;
- while( *str )
- {
- if( *str == cr )
- {
- cr = 0;
- ++str;
- continue;
- }
- cr = 0;
- if( *str == '\n' )
- cr = '\r';
- /*
- * Is the next character a LF?
- */
- if( *str == '\r' )
- {
- cr = '\n';
- *new = '\n';
- }
- else
- {
- *new = *str;
- }
- ++str;
- ++new;
- ++len;
- }
- *new = 0;
-
- return(len);
- }
-
- /*----------------------------------------------------------------------
- | Avoid linking with the string library:
- ----------------------------------------------------------------------*/
- void strcpy(char *a, char *b)
- {
- while( *b )
- *a++ = *b++;
- *a = 0;
- }
-
- void pstrcpy(char *a, char *b)
- {
- int i = pstrlen(b) + 1;
-
- while( i-- )
- *a++ = *b++;
- }
-
- int strlen(char *str)
- {
- int i = 0;
-
- while( *str )
- {
- ++i;
- ++str;
- }
-
- return(i);
- }
-
- long lstrlen(char *str)
- {
- long i = 0;
-
- while( *str )
- {
- ++i;
- ++str;
- }
-
- return(i);
- }
-
- void strcat(char *a,char *b)
- {
- strcpy(a+strlen(a),b);
- }
-
- void straddc(char *a,char c)
- {
- while( *a ) ++a;
- *a++ = c;
- *a++ = 0;
- }
-
- int strcmp(char *a,char *b)
- {
- while( *a && *b )
- {
- if( *a != *b )
- return( (*a > *b) - (*a < *b) );
- ++a;
- ++b;
- }
-
- if( *a ) return(1);
- if( *b ) return(-1);
- return( 0 );
- }
-
- int cistrcmp(char *a,char *b)
- {
- int test;
-
- while( *a && *b )
- {
- test = (toupper(*a) > toupper(*b)) - ( toupper(*a) < toupper(*b) );
- if( test )
- return( test );
- ++a;
- ++b;
- }
-
- if( *a ) return(1);
- if( *b ) return(-1);
- return( 0 );
- }
-
- int pstrcmp( char *a,char *b )
- {
- int minL = min( pstrlen(a), pstrlen(b) ),
- eq_len = ( (pstrlen(a) > pstrlen(b)) - (pstrlen(a) < pstrlen(b)) );
-
- ++a;
- ++b;
-
- while( minL-- )
- {
- if( *a != *b )
- return( (*a > *b) - (*a < *b) );
- ++a;
- ++b;
- }
- return( eq_len );
- }
-
- /*-----------------------------------------------------------------
- | Convert a pascal string to a C string. Note that it is legal to
- | call with two pointers to the same string if desired.
- -----------------------------------------------------------------*/
- char *P2Cstr(char *cstr,char *pstr)
- {
- int len,i;
-
- len = pstrlen(pstr);
- for(i=0;i<len;++i)
- cstr[i] = pstr[i+1];
- cstr[len] = 0;
- return(cstr);
- }
-
- /*-----------------------------------------------------------------
- | Convert a C string to a pascal string.
- -----------------------------------------------------------------*/
- char *C2Pstr(char *pstr,char *cstr)
- {
- pstr[0] = strlen(cstr);
- while(*cstr)
- *(++pstr) = *cstr++;
- return(pstr);
- }
-