aboutsummaryrefslogtreecommitdiff
path: root/vendor/x11iraf/ximtool/clients.old/wcspix/t_wcspix.x
blob: 675fb57a91e6498ae760d9fe97d0eea30ec6564f (plain) (blame)
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <ctype.h>
include <time.h>
include "wcspix.h"


# T_WCSPIX --  Entry point for the WCSPIX Image Support Module for XImtool.
# The WCSPIX task is responsible for converting image coordinates and getting
# pixel values from images of various types.  Results are returned to the
# GUI directly using ISM messaging.

procedure t_wcspix ()

pointer	wp
int	len, disconnect, ncmd
char	socket[SZ_FNAME], cmd[SZ_FNAME], message[SZ_LINE], buf[SZ_DATE]

int	objid, regid
real	x, y
char	ref[SZ_FNAME], template[SZ_LINE], param[SZ_FNAME]

bool	debug

long	clktime()
pointer	wp_init()
int	envgets(), envgeti(), strdic()

# Standard declarations for the Ximtool WCSPIX client interface.
int	xim_connect(), wp_read(), xim_intrhandler()
errchk	wp_read, envgets, envgeti

begin
	call aclrc (message, SZ_LINE)
	call aclrc (cmd, SZ_FNAME)
	call aclrc (socket, SZ_FNAME)

	# Get the connection socket name from the environment if defined
	# or else use the default socket.
        if (envgets ("ISMDEV", socket, SZ_FNAME) <= 0)
	    call strcpy (WCSPIX_CONNECT, socket, SZ_FNAME)

	# Open the socket connection on a negotiated socket.
	if (xim_connect (socket, WCSPIX_NAME, WCSPIX_MODE) == ERR)
	    return

        # Install an interrupt exception handler so we can exit cleanly.
	if (xim_intrhandler() == ERR)
	    return


	# Initialize the task data structures.
	wp = wp_init ()

	# Check for a runtime debug level.
	iferr (WP_DBGLEVEL(wp) = envgeti ("WCSPIX_DEBUG"))
	    WP_DBGLEVEL(wp) = 0

	# Log the connection.
	call wp_cnvdate (clktime(0), buf, SZ_DATE)
	call sprintf (message, SZ_LINE, "info { %s: WCSPIX Connect}\n") 
	    call pargstr (buf)
        call xim_message ("ism_msg", message)

	# Loop over the commands read on the connection and process.
	disconnect = 1
	debug = (WCSPIX_DBG || WP_DBGLEVEL(wp) > 0)
	while (wp_read (message, len) != EOF) {

	    if (debug) {
		call eprintf("message: '%s' len=%d\n")
		    call pargstr (message);  call pargi (len)
	    }
            if (len <= 0) {
		# Server has disconnected.
		disconnect = 0
		break
	    }

            # Scan the command string and get the first word.
            call sscan (message)
            call gargwrd (cmd, SZ_LINE)
            ncmd = strdic (cmd, cmd, SZ_LINE, WCSPIX_CMDS)

            switch (ncmd) {
	    case QUIT:
		# Server wants us to shut down.
		disconnect = 0
		break

	    case INITIALIZE:
		call wp_cnvdate (clktime(0), buf, SZ_DATE)
		call sprintf (message, SZ_LINE,
		    "info { %s: WCSPIX Initialize}\n") 
		        call pargstr (buf)
		call wp_initialize (wp)

	    case CACHE:
		# <ref> <objid> <regid>
		call gargwrd (ref, SZ_FNAME)
		call gargi (objid)
		call gargi (regid)
		if (debug) {
		    call printf ("cache: objid=%d regid=%d ref='%s'\n")
			call pargi(objid); call pargi(regid); call pargstr(ref)
		}

		# Log the event.
		call wp_cnvdate (clktime(0), buf, SZ_DATE)
		call sprintf (message, SZ_LINE,
		    "info { %s: WCSPIX Cache   objid=%3d %s}\n") 
		        call pargstr (buf)
		        call pargi (objid)
		        call pargstr (ref)
        	call xim_message ("ism_msg", message)

		call wp_cache (wp, objid, regid, ref)

	    case UNCACHE:
		# <id>
		call gargi (objid)
		if (debug) { call printf("uncache: id=%d\n");call pargi(objid) }

		# Log the event.
		call wp_cnvdate (clktime(0), buf, SZ_DATE)
		call sprintf (message, SZ_LINE,
		    "info { %s: WCSPIX Uncache objid=%3d}\n") 
		        call pargstr (buf)
		        call pargi (objid)
        	call xim_message ("ism_msg", message)

		call wp_uncache (wp, objid)

	    case WCSTRAN:
		# <id> <x> <y> [[<region> <x> <y>] ["NDC" <x> <y> ]]
		call gargi (objid)
		call gargr (x)  ; call gargr (y)
		if (debug) {
		    call printf ("wcstran: id=%d  (%g,%g)\n")
		        call pargi(objid); call pargr (x); call pargr (y) 
		}
		call wp_wcstran (wp, objid, x, y)

	    case WCSLIST:
		# <id>
		call gargi (objid)
		if (debug) { call printf ("wcslist: id=%d\n");call pargi(objid)}
		call wp_wcslist (wp, objid)

	    case OBJINFO:
		# <id> <template_list>
		call gargi (objid)
		call gargwrd (template, SZ_FNAME)
		if (debug) {
		    call printf ("objinfo: id=%d  temp='%s'\n")
		        call pargi(objid); call pargstr (template); 
		}
		call wp_objinfo (wp, objid, template)

	    case SET:
		# <param> <value>
		call gargwrd (param, SZ_FNAME)
		call wp_setpar (wp, param)

	    case GET:
		# <param>

	    case DEBUG:
		debug = !(debug)

	    default:
		if (debug) { 
	            call eprintf ("ISM default: len=%d msg='%s'\n")
	                call pargi(len); call pargstr(message)
		}
	    }

	    # Clear the buffer for the next read.
	    call aclrc (message, SZ_LINE)
	}

	# Disconnect from the server and clean up.
	call xim_disconnect (disconnect)
	call wp_shutdown (wp)
end


# WP_INITIALIZE -- Initialize the WCSPIX, uncache any previously cached images.

procedure wp_initialize (wp)

pointer	wp					#i WCSPIX structure

pointer	cp, wp_id2obj()
int	i

begin
	for (i=0; i < SZ_CACHE; i=i+1) {
	    cp = wp_id2obj (wp, i)
	    if (cp != NULL && C_OBJID(cp) != NULL)
		call wp_uncache (wp, C_OBJID(cp))
	}
end


# WP_CACHE -- Associate and object reference with a unique object id.

procedure wp_cache (wp, objid, regid, ref)

pointer	wp					#i WCSPIX structure
int	objid					#i object id
int	regid					#i region id
char	ref[ARB]				#i object ref

pointer cp
int	i, class
char	alert[SZ_FNAME]

int	wp_class()

include	"class.com"

begin
	# Find an unused slot in the object cache.
	for (i=0; i < SZ_CACHE; i=i+1) {
	    cp = OBJCACHE(wp,i)
	    if (C_NREF(cp) == 0)
		break
	}

	# Get the object class.
	class = wp_class (ref)
	if (class == ERR) {
	    # Send alert to the GUI.
	    call sprintf (alert, SZ_FNAME, "wp_cache: Unable to cache\n%s")
		call pargstr (ref)
	    call xim_alert (alert, "", "")

	    # Setup for linear system.
	    return
	}
	C_CLASS(cp) = class

	# Initialize the object.
	if (class != NULL && CL_INIT(class) != NULL)
	    call zcall2 (CL_INIT(class), cp, wp)

	# Call the cache function.
	if (class != NULL && CL_CACHE(class) != NULL)
	    call zcall4 (CL_CACHE(class), cp, objid, regid, ref)
end


# WP_UNCACHE -- Remove an object from the WCSPIX cache.

procedure wp_uncache (wp, id)

pointer	wp					#i WCSPIX structure
int	id					#i object id

pointer cp, wp_id2obj()
int	class

include	"class.com"

begin
	cp = wp_id2obj (wp, id)
	if (cp == NULL)
	    return

	# Call the uncache function.
	class = C_CLASS(cp)
	if (class != NULL && CL_UNCACHE(class) != NULL)
	    call zcall2 (CL_UNCACHE(class), cp, id)

	C_NREF(cp) = 0
end


# WP_WCSTRAN -- Translate image coords to WCS values.

procedure wp_wcstran (wp, id, x, y)

pointer	wp					#i WCSPIX structure
int	id					#i object id
real	x, y					#i image coords

pointer	cp, wp_id2obj()
int	class

include	"class.com"

begin
	cp = wp_id2obj (wp, id)
	if (cp == NULL)
	    return

	# Call the uncache function.
	class = C_CLASS(cp)
	if (class != NULL && CL_WCSTRAN(class) != NULL)
	    call zcall4 (CL_WCSTRAN(class), cp, id, x, y)
end


# WP_WCSLIST -- List the available world coordinate systems for the given
# object.

procedure wp_wcslist (wp, id)

pointer	wp					#i WCSPIX structure
int	id					#i object id

pointer	cp, wp_id2obj()
int	class

include	"class.com"

begin
	cp = wp_id2obj (wp, id)
	if (cp == NULL)
	    return

	# Call the uncache function.
	class = C_CLASS(cp)
	if (class != NULL && CL_WCSLIST(class) != NULL)
	    call zcall2 (CL_WCSLIST(class), cp, id)
end


# WP_OBJINFO -- Get and image header or keyword templates for the given
# object.

procedure wp_objinfo (wp, id, template)

pointer	wp					#i WCSPIX structure
int	id					#i object id
char	template[ARB]				#i keyword template

pointer cp, wp_id2obj()
int	class

include	"class.com"

begin
	cp = wp_id2obj (wp, id)
	if (cp == NULL)
	    return

	# Call the uncache function.
	class = C_CLASS(cp)
	if (class != NULL && CL_OBJINFO(class) != NULL)
	    call zcall3 (CL_OBJINFO(class), cp, id, template)
end


# WP_SETPAR -- Set the value of a WCSPIX ISM parameter.

procedure wp_setpar (wp, param)

pointer	wp					#i WCSPIX structure pointer
char	param[SZ_FNAME]				#i WCSPIX param name

char	arg[SZ_PARAM], buf[SZ_PARAM], msg[SZ_PARAM]
int	line

int	strdic()

include	"class.com"

begin
	if (WCSPIX_DBG) { call printf ("set: %s = ");call pargstr(param) }

	switch (strdic (param, param, SZ_PARAM, WCSPIX_PARAMS)) {
	case PAR_PSIZE:
	    call gargi (WP_PTABSZ(wp))
	    if (WCSPIX_DBG) { call printf ("%d\n");call pargi(WP_PTABSZ(wp)) }

	case PAR_BPM:
	    call gargi (WP_BPM(wp))
	    if (WCSPIX_DBG) { call printf ("%d\n");call pargi(WP_BPM(wp)) }

	case PAR_WCS:
	    call gargwrd (buf, SZ_FNAME)
	    call gargi (line)

	    call strcpy (buf, arg, SZ_PARAM)
	    call strlwr (buf)
	    switch (strdic (buf, buf, SZ_FNAME, WCSPIX_SYSTEMS)) {
	    case SYS_LOGICAL:	SYSTEMS(wp,line) = SYS_LOGICAL
	    case SYS_PHYSICAL:	SYSTEMS(wp,line) = SYS_PHYSICAL
	    case SYS_WORLD:	SYSTEMS(wp,line) = SYS_WORLD
	    case SYS_NONE:	SYSTEMS(wp,line) = SYS_NONE
	    case SYS_AMP:	SYSTEMS(wp,line) = SYS_AMP
	    case SYS_CCD:	SYSTEMS(wp,line) = SYS_PHYSICAL
	    case SYS_DETECTOR:	SYSTEMS(wp,line) = SYS_DETECTOR
	    default: 		SYSTEMS(wp,line) = SYS_SKY
	    }
	    call strcpy (buf, WCSNAME(wp,line), LEN_WCSNAME)

	    if (WCSPIX_DBG) { 
		call printf("%s line=%d\n");call pargstr(buf);call pargi(line) }

	    call sprintf (msg, SZ_FNAME, "wcstype %s %d")
		call pargstr (arg)
		call pargi (line)
	    call wcspix_message (msg)

	case PAR_FMT:
	    call gargwrd (buf, SZ_FNAME)
	    call gargi (line)

	    call strcpy (buf, arg, SZ_PARAM)
	    call strlwr (buf)
	    switch (strdic (buf, buf, SZ_FNAME, WCSPIX_FMT)) {
	    case FMT_DEFAULT:  	FORMATS(wp,line) = FMT_DEFAULT
	    case FMT_HMS:  	FORMATS(wp,line) = FMT_HMS
	    case FMT_DEG:  	FORMATS(wp,line) = FMT_DEG
	    case FMT_RAD:  	FORMATS(wp,line) = FMT_RAD
	    default:       	FORMATS(wp,line) = FMT_DEFAULT
	    }

	    if (WCSPIX_DBG) { 
		call printf("%s line=%d\n");call pargstr(buf);call pargi(line) }

	    call sprintf (msg, SZ_FNAME, "wcsfmt %s %d")
		call pargstr (arg)
		call pargi (line)
	    call wcspix_message (msg)
	}
end


# WP_GETPAR -- Get the value of a WCSPIX ISM parameter.

procedure wp_getpar (wp, param)

pointer	wp					#i WCSPIX structure pointer
char	param[SZ_FNAME]				#i WCSPIX param name

int	strdic()

begin
	if (WCSPIX_DBG) { call printf ("set: %s = ");call pargstr(param) }

	switch (strdic (param, param, SZ_PARAM, WCSPIX_PARAMS)) {
	case PAR_PSIZE:
	case PAR_BPM:
	case PAR_WCS:
	case PAR_FMT:
	}
end


################################################################################
#
# Private procedures.
#
################################################################################


# WP_INIT -- Initialize the WCSPIX task and data structures.

pointer procedure wp_init ()

pointer	wp					#r WCSPIX structure pointer
int	i

begin
	# Allocate the task structure.
        iferr (call calloc (wp, SZ_WCSPIX, TY_STRUCT))
            call error (0, "Error opening WCSPIX task structure.")

	call calloc (WP_SYSTEMS(wp), MAX_WCSLINES, TY_INT)
	call calloc (WP_FORMATS(wp), MAX_WCSLINES, TY_INT)
	call calloc (WP_WCS(wp), (LEN_WCSNAME*MAX_WCSLINES), TY_CHAR)
	for (i=1; i <= MAX_WCSLINES; i=i+1) {
	    FORMATS(wp,i) = DEF_FMT
	    SYSTEMS(wp,i) = DEF_SYSTEM
	    call strcpy ("none", WCSNAME(wp,i), LEN_WCSNAME)
	}

	# Allocate the object cache.
	call calloc (WP_CPTR(wp), SZ_CACHE, TY_STRUCT)
	for (i=0; i < SZ_CACHE; i=i+1)
	    call calloc (OBJCACHE(wp,i), SZ_CNODE, TY_STRUCT)

	WP_PTABSZ(wp) = DEF_PTABSZ
	WP_BPM(wp)    = DEF_BPM_FLAG

	# Initialize the class modules.
	call wp_class_init()

	return (wp)
end


# WP_READ -- Read messages from the connection and process them optimally for
# this ISM.  This means we segment the messages and handle only the last
# few WCS requests so we can keep up with the server requests.  Presumably
# there are more cursor events coming which are no longer valid so some are
# thrown out.

int procedure wp_read (message, len)

char	message[ARB]				#o message buffer
int	len					#o length of message

int	nread

int	xim_read()				# low-level i/o
errchk	xim_read

begin
	nread = xim_read (message, len)

	return (nread)
end


# WP_SHUTDOWN -- Shut down the WCSPIX, freeing all storage

procedure wp_shutdown (wp)

pointer	wp					#i WCSPIX structure
int	i

begin
	# Free the structures.
	call mfree (WP_WCS(wp), TY_CHAR)
	call mfree (WP_FORMATS(wp), TY_INT)
	call mfree (WP_SYSTEMS(wp), TY_INT)
	for (i=0; i < SZ_CACHE; i=i+1)
	    call mfree (OBJCACHE(wp,i), TY_STRUCT)

	call mfree (WP_CPTR(wp), TY_STRUCT)
	call mfree (wp, TY_STRUCT)
end


# WP_CLASS -- Determine the object class for the named image/file.

int procedure wp_class (object)

char	object[ARB]				#i object reference

int	n, class
pointer	im
char	ch, buf[SZ_FNAME]

int	strlen(), stridx()
bool	streq()
pointer	immap()

errchk	immap

begin
        # The following kludge is necessary to protect against the case
        # where dev$pix is used as a test image.  The 'object' pathname in
        # this case is "node!/path/dev/pix" which lacks the extension
        # and causes the task to fail to open because of a conflict with
        # the pix.hhh in the same directory.  Most IRAF tasks work since
        # the imio$iki code treats the string "dev$pix" as a special case.

	call imgimage (object, buf, SZ_FNAME)
        n = strlen (buf) - 7
        if (streq (buf[n], "/dev/pix")) {
            call strcpy ("dev$pix", buf, SZ_FNAME)
	    ch = '['
	    n = stridx (ch, object)
	    if (n > 0)
                call strcat (object[n], buf, SZ_FNAME)
            call strcpy (buf, object, SZ_FNAME)
	}


	# See if we can map the image to get at least an image class.  If
	# so then check for special subclasses like Mosaic files, spectra, etc.

	class =  UNKNOWN_CLASS
	ifnoerr (im = immap (object, READ_ONLY, 0)) {
	    class = IMAGE_CLASS

	    # Now check for subclasses.  (TBD)

	    call imunmap (im)
	}

	return (class)
end


# WP_ID2OBJ -- Utility routine to convert and object id to the cache pointer.

pointer procedure wp_id2obj (wp, id)

pointer	wp					#i WCSPIX structure
int	id					#i object id

int	i
pointer	cp

begin
	for (i=0; i < SZ_CACHE; i=i+1) {
	    cp = OBJCACHE(wp,i)
	    if (C_OBJID(cp) == id)
		return (cp)
	}
	return (NULL)
end


# WP_CLASS_INIT -- Initialize the WCSPIX ISM class modules.

procedure wp_class_init()

extern  img_init(), img_cache(), img_uncache()
extern  img_wcstran(), img_wcslist(), img_objinfo()

extern  mef_init(), mef_cache(), mef_uncache()
extern  mef_wcstran(), mef_wcslist(), mef_objinfo()

extern  msp_init(), msp_cache(), msp_uncache()
extern  msp_wcstran(), msp_wcslist(), msp_objinfo()

extern  unk_init(), unk_cache(), unk_uncache()
extern  unk_wcstran(), unk_wcslist(), unk_objinfo()

include	"class.com"
int     locpr()

begin
	cl_nclass = 0

	# Load the class modules.
	call wp_load_class ("unknown",
	    locpr(unk_init), locpr(unk_cache), locpr(unk_uncache),
	    locpr(unk_wcstran), locpr(unk_wcslist), locpr(unk_objinfo))
	call wp_load_class ("image",
	    locpr(img_init), locpr(img_cache), locpr(img_uncache),
	    locpr(img_wcstran), locpr(img_wcslist), locpr(img_objinfo))
	call wp_load_class ("mef",
	    locpr(mef_init), locpr(mef_cache), locpr(mef_uncache),
	    locpr(mef_wcstran), locpr(mef_wcslist), locpr(mef_objinfo))
	call wp_load_class ("multispec",
	    locpr(msp_init), locpr(msp_cache), locpr(msp_uncache),
	    locpr(msp_wcstran), locpr(msp_wcslist), locpr(msp_objinfo))
end


# WP_LOAD_CLASS -- Load an object class module for the ISM task.

procedure wp_load_class (name, init, cache, uncache, tran, list, info)

char	name[ARB]			#I module name
int	init				#I initialize procedure
int	cache				#I cache the object procedure
int	uncache				#I uncache the object procedure
int	tran				#I translate WCS procedure
int	list				#I list WCS proedure
int	info				#I get header procedure

errchk	syserrs
include	"class.com"

begin
	# Get a new driver slot.
	if (cl_nclass + 1 > MAX_CLASSES)
	    return
	cl_nclass = cl_nclass + 1

	# Load the driver.
	CL_INIT(cl_nclass) = init
	CL_CACHE(cl_nclass) = cache
	CL_UNCACHE(cl_nclass) = uncache
	CL_WCSTRAN(cl_nclass) = tran
	CL_WCSLIST(cl_nclass) = list
	CL_OBJINFO(cl_nclass) = info
	call strcpy (name, CL_NAME(cl_nclass), SZ_FNAME)
end


# WCSPIX_MESSAGE -- Deliver a message to the ISM callback, tagged with
# our name so it can be passed off to the correct code.

procedure wcspix_message (message)

char    message[ARB]                            #I message to send

pointer sp, msgbuf
int     msglen, mlen, ip 

int	strlen()

begin
        # Get the message length plus some extra for the braces and padding.
        mlen = strlen (message)
        msglen = mlen + 64

        # Allocate and clear the message buffer.
        call smark (sp)
        call salloc (msgbuf, msglen, TY_CHAR)
        call aclrc (Memc[msgbuf], msglen)

	ip = 0
	call amovc ("deliver wcspix { ", Memc[msgbuf], 17)  ; ip = ip + 17
	call amovc (message, Memc[msgbuf+ip], mlen)	    ; ip = ip + mlen
	call amovc (" }\0", Memc[msgbuf+ip], 2)		    ; ip = ip + 2

	call xim_message ("ism_msg", Memc[msgbuf])

	call sfree (sp)
end


define	SZ_WEEKDAY		3
define	SZ_MONTH		3

# WP_CNVDATE -- Convert a time in integer seconds since midnight on Jan 1, 1980
# into a short string such as "5/15 18:24". 

procedure wp_cnvdate (ltime, outstr, maxch)

long	ltime			# seconds since 00:00:00 10-Jan-1980
char	outstr[ARB]
int	maxch

int	tm[LEN_TMSTRUCT]

begin
	call brktime (ltime, tm)

# 	call sprintf (outstr, maxch, "%2d/%2d %2d:%02d")
# 	    call pargi (TM_MONTH(tm))
# 	    call pargi (TM_MDAY(tm))
# 	    call pargi (TM_HOUR(tm))
# 	    call pargi (TM_MIN(tm))

#	call sprintf (outstr, maxch, "%2d:%02d")
#	    call pargi (TM_HOUR(tm))
#	    call pargi (TM_MIN(tm))

	call sprintf (outstr, maxch, "%2d:%02d:%02d")
	    call pargi (TM_HOUR(tm))
	    call pargi (TM_MIN(tm))
	    call pargi (TM_SEC(tm))
end



#----------------
# DEBUG ROUTINES.
#----------------
procedure dbg_printcache (wp, buf)
pointer	wp
char	buf[ARB]
pointer	cp, wp_id2obj()
int	i
begin
	call printf ("%s\n") ; call pargstr (buf)
	for (i=0; i < SZ_CACHE; i=i+1) {
	    cp = wp_id2obj (wp, i)
	    if (C_DATA(cp) != NULL) {
		call printf ("%3d:  id=%d  ref='%s'\n")
		    call pargi(i)
		    call pargi(C_OBJID(cp))
		    call pargstr(C_REF(cp))
	    }
	}
end