File: mac.c

package info (click to toggle)
xconq 7.2.2-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 8,296 kB
  • ctags: 9,199
  • sloc: ansic: 107,849; sh: 2,108; perl: 2,057; makefile: 1,177; sed: 161; csh: 50; awk: 49; lisp: 39
file content (978 lines) | stat: -rw-r--r-- 21,669 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/* Mac-specific code for Xconq kernel.
   Copyright (C) 1992-1997 Stanley T. Shebs.

Xconq is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.  See the file COPYING.  */

/* The code in this file is Mac-specific, but not necessarily specific
   to any particular app; it should be usable with utilities and with
   apps that don't have fancy interface. */

#include "config.h"
#include "misc.h"
#include "dir.h"
#include "lisp.h"
#include "module.h"
#include "system.h"
void mac_abort PARAMS ((void));

/* Following are from tp.h, but tp.h is too high-level to include. */

extern int numremotes;
extern int my_rid;
extern int master_rid;
extern int tmprid;

extern void init_file_port PARAMS ((int hosting));
extern void close_file_port PARAMS ((void));
extern void low_file_send PARAMS ((int id, char *buf));
extern int low_file_receive PARAMS ((int *id, char *buf, int maxchars, int timeout));

extern int serial_port_dialog(void);

static void init_serial_port(int port);
static void close_serial_port(void);

static void open_appletalk(void);
extern int low_appletalk_receive(int *id, char *buf, int maxchars, int timout);
static void low_appletalk_send(int id, char *buf);
static void close_appletalk(void);

#ifdef THINK_C
#include <MacHeaders>
#endif /* THINK_C */

#ifdef MPW
#include <Types.h>
#include <Resources.h>
#include <Events.h>  /* for TickCount */
#include <TextUtils.h>  /* for GetIndString */
#endif /* MPW */

#include <signal.h>

/* We need this in order to find string resources that have filenames
   in them. */
#include "macdefs.h"

#include <Serial.h>

#include <AppleTalk.h>

#ifdef MPW_C
#define QD(whatever) (qd.##whatever)
#define QDPat(whatever) (&(qd.##whatever))
#endif
#ifdef THINK_C
#define QD(whatever) (whatever)
#define QDPat(whatever) (whatever)
#endif
#ifdef __MWERKS__
#define QD(whatever) (qd.##whatever)
#define QDPat(whatever) (&(qd.##whatever))
#endif

#ifndef c2p
#define c2p(STR,PBUF) \
  strcpy(((char *) PBUF) + 1, STR);  \
  PBUF[0] = strlen(STR);
#endif

#ifndef p2c
#define p2c(PSTR,BUF)  \
  strncpy(BUF, ((char *) (PSTR) + 1), PSTR[0]);  \
  BUF[PSTR[0]] = '\0';
#endif

extern CursHandle sendcursor;
extern CursHandle receivecursor;
extern CursHandle current_cursor;

static void low_serial_send(int id, char *buf);
static int low_serial_readchars(char *buf, int maxchars, int timeout);

char *connection_method_name;

int connection_method;

int hosting;

/* The HFS volume that the program started with. */

short initialvrefnum;

static char *news_fname;
static char *save_fname;
static char *checkpoint_fname;
static char *error_save_fname;
static char *statistics_fname;

#ifndef XCONQLIB
#define XCONQLIB ":lib"
#endif

char *
default_library_filename()
{
    return XCONQLIB;
}

char *
news_filename()
{
    Str255 tmpstr;
    char tmpbuf[255];
    
    if (news_fname == NULL) {
	GetIndString(tmpstr, sFilenames, siNews);
	p2c(tmpstr, tmpbuf);
	if (!empty_string(tmpbuf))
	  news_fname = copy_string(tmpbuf);
	else
	  news_fname = NEWSFILE;
    }
    return news_fname;
}

char *
saved_game_filename()
{
    Str255 tmpstr;
    char tmpbuf[255];
    
    if (save_fname == NULL) {
	GetIndString(tmpstr, sFilenames, siSavedGame);
	p2c(tmpstr, tmpbuf);
	if (!empty_string(tmpbuf))
	  save_fname = copy_string(tmpbuf);
	else
	  save_fname = "Saved Game";
    }
    return save_fname;
}

char *
checkpoint_filename()
{
    Str255 tmpstr;
    char tmpbuf[255];

    if (checkpoint_fname == NULL) {
	GetIndString(tmpstr, sFilenames, siCheckpoint);
	p2c(tmpstr, tmpbuf);
	if (!empty_string(tmpbuf))
	  checkpoint_fname = copy_string(tmpbuf);
	else
	  checkpoint_fname = "Checkpoint";
    }
    return checkpoint_fname;
}

char *
error_save_filename()
{
    Str255 tmpstr;
    char tmpbuf[255];

    if (error_save_fname == NULL) {
	GetIndString(tmpstr, sFilenames, siErrorSave);
	p2c(tmpstr, tmpbuf);
	if (!empty_string(tmpbuf))
	  error_save_fname = copy_string(tmpbuf);
	else
	  error_save_fname = "Error Save";
    }
    return error_save_fname;
}

char *
statistics_filename()
{
    Str255 tmpstr;
    char namebuf[256];
    Point pnt;
    SFReply reply;

    if (statistics_fname == NULL) {
	GetIndString(tmpstr, sFilenames, siStatistics);
	p2c(tmpstr, namebuf);
	if (!empty_string(namebuf))
	  statistics_fname = copy_string(namebuf);
	else
	  statistics_fname = "Statistics";
	/* Collect the file and path to save to. */
	SetPt(&pnt, 100, 100);
	c2p(statistics_fname, tmpstr);
	SFPutFile(pnt, "\pSave game statistics as:", tmpstr, /*(DlgHookProcPtr)*/ nil, &reply);
	if (!reply.good)
	  return NULL;
	/* Make the location of the file be the current volume. */
	SetVol(reply.fName, reply.vRefNum);
	p2c(((char *) reply.fName), namebuf);
	statistics_fname = copy_string(namebuf);
    }
    return statistics_fname;
}

/* Attempt to open a library file. */

FILE *
open_module_library_file(Module *module)
{
    short curvrefnum;
    char fullnamebuf[255];
    LibraryPath *p;
    FILE *fp;
    
    /* Can't open anonymous library modules. */
    if (module->name == NULL)
      return NULL;
    /* Generate library pathname. */
    for_all_library_paths(p) {
	make_pathname(p->path, module->name, "g", fullnamebuf);
	/* Now try to open the file. */
	fp = fopen(fullnamebuf, "r");
	if (fp != NULL) {
	    /* Remember the filename where we found it. */
	    module->filename = copy_string(fullnamebuf);
	    return fp;
	}
	/* Try the same name in a different directory. */
	GetVol(NULL, &curvrefnum);
	SetVol(NULL, initialvrefnum);
	fp = fopen(fullnamebuf, "r");
	SetVol(NULL, curvrefnum);
	if (fp != NULL) {
	    /* Remember the filename (what about volume?) where we found it. */
	    module->filename = copy_string(fullnamebuf);
	    return fp;
	}
    }
    return NULL;
}

FILE *
open_module_explicit_file(Module *module)
{
    short curvrefnum;
    char fullnamebuf[255];
    LibraryPath *p;
    FILE *fp = NULL;
    
    if (module->filename == NULL) {
	/* Try guessing a filename, since none supplied. */
	if (module->name != NULL) {
	    for_all_library_paths(p) {
		make_pathname(p->path, module->name, "g", fullnamebuf);
		/* Now try to open the file. */
		fp = fopen(fullnamebuf, "r");
		if (fp != NULL)
		  return fp;
		GetVol(NULL, &curvrefnum);
		SetVol(NULL, initialvrefnum);
		fp = fopen(fullnamebuf, "r");
		SetVol(NULL, curvrefnum);
		if (fp != NULL)
		  return fp;
	    }
	}
    } else {
	/* Try some other random ideas. */
	sprintf(fullnamebuf, "%s", module->filename);
	fp = fopen(module->filename, "r");
	if (fp != NULL) {
	    add_library_path("");
	    return fp;
	}
	sprintf(fullnamebuf, ":%s", module->filename);
	fp = fopen(fullnamebuf, "r");
	if (fp != NULL)
	  return fp;
	sprintf(fullnamebuf, "%s%s", ":lib:", module->filename);
	fp = fopen(fullnamebuf, "r");
	if (fp != NULL)
	  return fp;
	/* Try opening a library module under where the program started. */
	GetVol(NULL, &curvrefnum);
	SetVol(NULL, initialvrefnum);
	fp = fopen(fullnamebuf, "r");
	SetVol(NULL, curvrefnum);
    }
    return fp;
}

FILE *
open_library_file(char *filename)
{
    short curvrefnum;
    char fullnamebuf[255];
    LibraryPath *p;
    FILE *fp;
	
    /* Now try to open the file. */
    fp = fopen(filename, "r");
    if (fp != NULL)
      return fp;
    /* Generate library pathname. */
    for_all_library_paths(p) {
	make_pathname(p->path, filename, NULL, fullnamebuf);
	/* Now try to open the file. */
	fp = fopen(fullnamebuf, "r");
	if (fp != NULL)
	  return fp;
	/* Change to volume where the program started. */
	GetVol(NULL, &curvrefnum);
	SetVol(NULL, initialvrefnum);
	fp = fopen(fullnamebuf, "r");
	SetVol(NULL, curvrefnum);
	if (fp != NULL)
	  return fp;
    }
    return NULL;
}

FILE *
open_scorefile_for_reading(char *name)
{
    char buf[255];
    short curvrefnum;
    FILE *fp;
	
    GetVol(NULL, &curvrefnum);
    SetVol(NULL, initialvrefnum);
    /* Now try to open the file. */
    sprintf(buf, ":Scores:%s", name);
    fp = fopen(buf, "r");
    SetVol(NULL, curvrefnum);
    return fp;
}

FILE *
open_scorefile_for_writing(char *name)
{
    char buf[255];
    short curvrefnum;
    FILE *fp;
	
    GetVol(NULL, &curvrefnum);
    SetVol(NULL, initialvrefnum);
    /* Now try to open the file. */
    sprintf(buf, ":Scores:%s", name);
    fp = fopen(buf, "a");
    SetVol(NULL, curvrefnum);
    return fp;
}

/* Close scorefile after having written to it; nothing special to do on Macs. */

void
close_scorefile_for_writing(fp)
FILE *fp;
{
    fclose(fp);
}

void
make_pathname(char *path, char *name, char *extn, char *pathbuf)
{
    strcpy(pathbuf, "");
    if (!empty_string(path)) {
	strcat(pathbuf, path);
	strcat(pathbuf, ":");
    }
    strcat(pathbuf, name);
    /* Don't add a second identical extension, but do add if extension
       is different (in case we want "foo.12" -> "foo.12.g" for instance) */
    if (strrchr(name, '.')
	&& extn
	&& strcmp(strrchr(name, '.') + 1, extn) == 0)
      return;
    if (!empty_string(extn)) {
	strcat(pathbuf, ".");
	strcat(pathbuf, extn);
    }
}

/* Remove a saved game from the system. */

void
remove_saved_game()
{
    /* (should implement) */
}

void
init_signal_handlers()
{
}

int last_ticks = 0;

int
n_seconds_elapsed(n)
int n;
{
    int ticks = TickCount();

    if (((ticks - last_ticks) / 60) > n) {
	last_ticks = ticks;
    	return TRUE;
    } else {
	return FALSE;
    }
}

int last_ticks_for_ms = 0;

int
n_ms_elapsed(n)
int n;
{
    return (((TickCount() - last_ticks_for_ms) * 16) > n);
}

void
record_ms()
{
    last_ticks_for_ms = TickCount();
}

/* Instead of coredumping, which is not a normal Mac facility, we
   drop into Macsbug.  If we then "g" from Macsbug, the program will
   exit cleanly. */

void
mac_abort ()
{
    /* Make sure no output still buffered up, then zap into MacsBug. */
#if 0 /* how to know if stdio in use? */
    fflush(stdout);
    fflush(stderr);
    printf("## Abort! ##\n");
#endif
#ifdef MPW_SADE
    SysError(8005);
#else 
    Debugger();
#endif
    /* "g" in MacsBug will then cause a regular error exit. */
    exit(1);
}

void
open_remote_connection(char *methodname)
{
    int serial_port, rslt;

    if (methodname == NULL) {
	connection_method = 0;
    } else if (strcmp(methodname, "serial") == 0) {
	connection_method = 1;
    } else if (strcmp(methodname, "appletalk") == 0) {
	connection_method = 2;
    } else if (strcmp(methodname, "tcp") == 0) {
	connection_method = 3;
    } else if (strcmp(methodname, "file") == 0) {
	connection_method = 4;
    } else {
	connection_method = 0;
    }
    switch (connection_method) {
      case 0:
	break;
      case 1:
	/* Get a decision about which serial port to use. */
	serial_port = serial_port_dialog();
	/* If cancelled here, get out and don't open anything. */
	if (serial_port < 0)
	  break;
	init_serial_port(serial_port);
	/* Serial supports exactly two programs, so we can easily
	   set up all the rids right now. */
	master_rid = 1;
	if (hosting) {
	    my_rid = 1;
	} else {
	    my_rid = 2;
	}
	numremotes = 2;
	break;
      case 2:
	open_appletalk();
	break;
      case 3:
	init_warning("No TCP/IP support yet, ignoring");
	break;
      case 4:
	init_file_port(hosting);
	break;
      default:
	break;
    }
}

extern short ser_input_refnum;
extern short ser_output_refnum;

/* Low-level transmission to another program. */

void
low_send(int id, char *buf)
{
    if (sendcursor != nil)
      SetCursor(*sendcursor);
    Dprintf("Sending: %d \"%s\"...", id, (buf ? buf : "<null>"));
    switch (connection_method) {
      case 0:
	/* Not connected anywhere, no need to complain. */
	break;
      case 1:
	low_serial_send(id, buf);
	break;
      case 2:
	low_appletalk_send(id, buf);
	break;
      case 3:
	break;
      case 4:
	low_file_send(id, buf);
	break;
      default:
	case_panic("connection method", connection_method);
	break;
    }
    Dprintf(" sent.\n");
    if (sendcursor != nil) {
	if (current_cursor != nil)
	  SetCursor(*current_cursor);
	else
	  SetCursor(&QD(arrow));
    }
}

int
low_receive(int *idp, char *buf, int maxchars, int timeout)
{
    int rslt = FALSE;

    if (receivecursor != nil)
      SetCursor(*receivecursor);
    switch (connection_method) {
      case 0:
	/* Not connected anywhere, no need to complain. */
	break;
      case 1:
	/* Only one remote if using serial. */
	*idp = (my_rid == 1 ? 2 : 1);
	rslt = low_serial_readchars(buf, maxchars, timeout);
	break;
      case 2:
	/* Only one remote for now. */
	*idp = (my_rid == 1 ? 2 : 1);
	rslt = low_appletalk_receive(idp, buf, maxchars, timeout);
	break;
      case 3:
	break;
      case 4:
	rslt = low_file_receive(idp, buf, maxchars, timeout);
	break;
      default:
	case_panic("connection method", connection_method);
	break;
    }
    if (receivecursor != nil) {
	if (current_cursor != nil)
	  SetCursor(*current_cursor);
	else
	  SetCursor(&QD(arrow));
    }
    return rslt;
}

void
close_remote_connection()
{
    switch (connection_method) {
      case 0:
	break;
      case 1:
	close_serial_port();
	break;
      case 2:
	close_appletalk();
	break;
      case 3:
	break;
      case 4:
	close_file_port();
	break;
      default:
	break;
    }
}

short ser_input_refnum;
short ser_output_refnum;

static void
init_serial_port(port)
int port;
{
    OSErr err;

    if (port == 0) {
	/* Open the modem port. */
	err = OpenDriver("\p.AIn", &ser_input_refnum);
	if (err != 0) {
	    return;
	}
	err = OpenDriver("\p.AOut", &ser_output_refnum);
	if (err != 0) {
	    /* This isn't working, so close the input as well. */
	    CloseDriver(ser_input_refnum);
	    return;
	}
    } else {
	/* Open the printer port. */
	err = OpenDriver("\p.BIn", &ser_input_refnum);
	if (err != 0) {
	    return;
	}
	err = OpenDriver("\p.BOut", &ser_output_refnum);
	if (err != 0) {
	    /* This isn't working, so close the input as well. */
	    CloseDriver(ser_input_refnum);
	    return;
	}
    }
#if 0 /* might need this eventually? */
    if (0 /* using custom buffer */)
      SerSetBuf (ser_input_refnum, mac_input_buffer, 256);
#endif
    SerReset (ser_input_refnum,  stop10|noParity|data8|baud9600);
    SerReset (ser_output_refnum, stop10|noParity|data8|baud9600);
    /* (what does this do?) */
    {
	CntrlParam cb;
	struct SerShk *handshake;
	
	cb.ioCRefNum = ser_output_refnum;
	cb.csCode = 14;
	handshake = (struct SerShk *) &cb.csParam[0];
	handshake->fXOn = 0;
	handshake->fCTS = 0;
	handshake->xOn = 0;
	handshake->xOff = 0;
	handshake->errs = 0;
	handshake->evts = 0;
	handshake->fInX = 0;
	handshake->fDTR = 0;
	err = PBControl ((ParmBlkPtr) &cb, 0);
    }
}

static void
low_serial_send(int id, char *buf)
{
    OSErr err;
    IOParam pb;

    pb.ioRefNum = ser_output_refnum;
    pb.ioBuffer = (Ptr) buf;
    pb.ioReqCount = strlen(buf);
    err = PBWrite((ParmBlkPtr) &pb, 0);
}

static int
low_serial_readchars(char *buf, int maxchars, int timeout)
{
    int status, n, n2;
    /* time_t */ unsigned long start_time, now;
    OSErr err;
    CntrlParam cb;
    IOParam pb;

    time (&start_time);

    while (1) {
	cb.ioCRefNum = ser_input_refnum;
	cb.csCode = 2;
	err = PBStatus((ParmBlkPtr) &cb, 0);
	if (err < 0)
	  return FALSE;
	n = *((long *) &cb.csParam[0]);
	if (n > 0) {
	    pb.ioRefNum = ser_input_refnum;
	    pb.ioBuffer = (Ptr) buf;
	    pb.ioReqCount = (n > 64 ? 64 : n);
	    err = PBRead((ParmBlkPtr) &pb, 0);
	    if (err < 0)
	      return FALSE;
	    n2 = pb.ioReqCount;
	    buf[n2] = '\0';
	    return TRUE;
	} else if (timeout == 0) {
	    return FALSE;
	} else if (timeout == -1) {
	    /* Go around again. */
	} else {
	    time (&now);
	    if (now > start_time + timeout) {
		Dprintf("%ul > %ul + %d\n", now, start_time, timeout);
		return FALSE /* timed out */;
	    }
	}
    }
}

static void
close_serial_port()
{
    if (ser_input_refnum)
      CloseDriver(ser_input_refnum);
    if (ser_output_refnum)
      CloseDriver(ser_output_refnum);
}

NamesTableEntry names_table_entry;

int nbp_registered;

unsigned char atp_socket_number;

AddrBlock remote_addr;

static void
open_appletalk()
{
#ifndef __MWERKS__
    int i, numgotten;
    OSErr err;
    ATPParamBlock tmpATPPB;
    MPPParamBlock tmpMPPPB, tmpCancelMPPPB;
    NamesTableEntry lookup;
    char *namesbuffer;
    char *bigbuffer;
    EntityName entityname;
    AddrBlock entityaddr;

    if (IsMPPOpen == false) {
	err = MPPOpen();
	/* (should check result) */
    }

    tmpMPPPB.SETSELF.newSelfFlag = true;
    err = PSetSelfSend(&tmpMPPPB, false);
    /* (should check result) */

    /* (should collect our own network address?) */

    tmpATPPB.ATP.atpSocket = 0;
    tmpATPPB.ATP.addrBlock.aNet = 0;
    tmpATPPB.ATP.addrBlock.aNode = 0;
    tmpATPPB.ATP.addrBlock.aSocket = 0;
    err = POpenATPSkt(&tmpATPPB, false);
    /* (should check result) */

    NBPSetNTE(&names_table_entry, "\pStan", "\pXconq", "\p*", 64);

    tmpMPPPB.NBP.ioCompletion = nil;
    tmpMPPPB.NBP.interval = 0x0F;
    tmpMPPPB.NBP.count = 0x03;
#if 0
    tmpMPPPB.NBP.nbpPtrs.entityPtr = &names_table_entry;
#else
    tmpMPPPB.NBP.NBPPtrs.entityPtr = &names_table_entry;
#endif
    tmpMPPPB.NBP.parm.verifyFlag = true;
	
    err = PRegisterName(&tmpMPPPB, true);
    /* (should check result) */

    {
  	short itemHit;
  	EventRecord theEvent;
  	Point mouse;
	extern void get_global_mouse(Point *mouse);

	do 
	  {
	      itemHit = 0;
		
	      if (GetNextEvent(everyEvent,&theEvent) == true && IsDialogEvent(&theEvent) == true)
		{
			
		    get_global_mouse(&mouse);
		    if (mouse.v == 0 && mouse.h == 0)
		      itemHit = 1;	
			
		    else
		      {
			  itemHit = 0;
		      }
		}
		
	  } while (itemHit != 1 && tmpMPPPB.NBP.ioResult == 1);

	if (itemHit == 1)
	  {
	      tmpCancelMPPPB.NBPKILL.nKillQEl = (Ptr) &tmpMPPPB;
		
	      PKillNBP(&tmpCancelMPPPB, false);
	  } else
	    nbp_registered = TRUE;
    }

    bigbuffer = xmalloc(10000);
    namesbuffer = xmalloc(250 * 33);

    NBPSetEntity((Ptr)&lookup.nt.entityData, "\p=", "\pXconq", "\p*");

    tmpMPPPB.NBP.ioCompletion = nil;
    tmpMPPPB.NBP.interval = 3;
    tmpMPPPB.NBP.count = 3;
    tmpMPPPB.NBPentityPtr = &lookup.nt.entityData;
    tmpMPPPB.NBPretBuffSize = 10000;
    tmpMPPPB.NBPretBuffPtr = bigbuffer;
    tmpMPPPB.NBPmaxToGet = 1000 / sizeof(NTElement);

    err = PLookupName(&tmpMPPPB, false);

    numgotten = tmpMPPPB.NBP.parm.Lookup.numGotten;
    if (numgotten > 0) {
	master_rid = 1;
	if (hosting) {
	    my_rid = 1;
	} else {
	    my_rid = 2;
	}
	numremotes = 1;
	/* (should use this loop to get actual number of remotes) */
	for (i = 0; i < numgotten; ++i) {
	    err = NBPExtract(namesbuffer, numgotten, i + 1, &entityname, &remote_addr); 
	}
    }

    /* (should release big buffers) */

    /* Open an ATP socket. */

    tmpATPPB.ATP.atpSocket = 0;
    tmpATPPB.ATP.addrBlock.aNet = 0;
    tmpATPPB.ATP.addrBlock.aNode = 0;
    tmpATPPB.ATP.addrBlock.aSocket = 0;

    err = POpenATPSkt(&tmpATPPB, false);

    atp_socket_number = tmpATPPB.ATP.atpSocket;
#endif /* __MWERKS__ */
}

static int
low_appletalk_receive(int *idp, char *buf, int maxchars, int timeout)
{
#ifndef __MWERKS__
    int status, n;
    /* time_t */ unsigned long start_time, now;
    OSErr err;
    int numbds;
    char atprequestbuf[256];
    BDSType bds;
    ATPParamBlock requestATPPB, replyATPPB;

    time (&start_time);

    while (1) {
	requestATPPB.ATP.ioCompletion = nil;
	requestATPPB.ATP.atpSocket = atp_socket_number;
	requestATPPB.ATP.reqLength = maxchars;
	requestATPPB.ATP.reqPointer = buf;

	err = PGetRequest(&requestATPPB, false);

	numbds = BuildBDS("OK", &bds, 256);

	replyATPPB.ATP.ioCompletion = nil;
	replyATPPB.ATP.atpFlags = atpEOMvalue;
	replyATPPB.ATP.atpSocket = atp_socket_number;
	replyATPPB.ATP.addrBlock = requestATPPB.ATP.addrBlock;
	replyATPPB.ATP.reqLength = strlen(buf) + 1;
	replyATPPB.ATP.bdsPointer = &bds;
	replyATPPB.OTH1.u0.numOfBuffs = 1;
	replyATPPB.OTH2.bdsSize = 256;
	replyATPPB.OTH2.transID = requestATPPB.ATP.reqTID;

	err = PSendResponse(&replyATPPB, false);

	n = requestATPPB.ATP.reqLength;
	if (n > 0) {
	    buf[n] = '\0';
	    return TRUE;
	} else if (timeout == 0) {
	    return FALSE;
	} else if (timeout == -1) {
	    /* Go around again. */
	} else {
	    time (&now);
	    if (now > start_time + timeout) {
		Dprintf("%ul > %ul + %d\n", now, start_time, timeout);
		return FALSE /* timed out */;
	    }
	}
    }
#else
    return 0;
#endif /* __MWERKS__ */
}

static void
low_appletalk_send(int id, char *buf)
{
#ifndef __MWERKS__
    int numbds;
    char atpresponsebuf[256];
    OSErr err;
    BDSType bds;
    ATPParamBlock tmpATPPB;

    numbds = BuildBDS(atpresponsebuf, &bds, 256);

    tmpATPPB.ATP.ioCompletion = nil;
    tmpATPPB.ATP.atpFlags = 0;

    tmpATPPB.ATP.atpSocket = atp_socket_number;
    tmpATPPB.ATP.addrBlock.aNet = remote_addr.aNet;
    tmpATPPB.ATP.addrBlock.aNode = remote_addr.aNode;
    tmpATPPB.ATP.addrBlock.aSocket = remote_addr.aSocket;
    tmpATPPB.ATP.reqLength = strlen(buf) + 1;
    tmpATPPB.ATP.reqPointer = buf;
    tmpATPPB.ATP.bdsPointer = &bds;
    tmpATPPB.SREQ.timeOutVal = 10;
    tmpATPPB.SREQ.retryCount = 3;

    err = PSendRequest(&tmpATPPB, false);
#endif /* __MWERKS __ */
}

static void
close_appletalk()
{
#ifndef __MWERKS__
    OSErr err;
    ATPParamBlock tmpATPPB;
    MPPParamBlock tmpMPPPB;

    tmpATPPB.ATP.atpSocket = atp_socket_number;

    err = PCloseATPSkt(&tmpATPPB, false);

    if (nbp_registered) {
	tmpMPPPB.NBP.NBPPtrs.entityPtr = (Ptr) &names_table_entry.nt.entityData;
		
	err = PRemoveName(&tmpMPPPB, false);
	nbp_registered = FALSE;
    }
#endif /* __MWERKS __ */
}