aboutsummaryrefslogtreecommitdiff
path: root/pkg/vocl/sampHandlers.c
blob: 8e15898ae19d34c0dd8168b31046c324d765dfd6 (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
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

/**
 *  SAMPHANDLERS.C -- Mtype message handlers.
 */

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
#include <pthread.h>
#include <stdio.h>
#include <readline/readline.h>		/* to install rl_event_hook	*/

#define import_spp
#define import_libc
#define import_stdio
#define import_prstat
#define import_xwhen
#include <iraf.h>

#include "config.h"			/* CL declarations		*/
#include "clmodes.h"
#include "operand.h"
#include "mem.h"
#include "grammar.h"
#include "opcodes.h"
#include "param.h"
#include "task.h"
#include "errs.h"
#include "clsamp.h"



extern Handler	userHandlers[];
extern int	numHandlers;

extern int   samp_registered;
extern char  samp_cmd[SZ_CMDBLK];
extern pid_t cl_pid;

extern int   optbl[];
extern char *ifnames[];



/**
 *  MType Handler Declarations	
 */
int 	cl_genericHandler (char *sender, char *mtype, char *msg_id, Map map);
int 	cl_cmdExecHandler (char *cmd);
int 	cl_envSetHandler (char *name, char *value);
int 	cl_envGetHandler (char *name, char *value, int maxch);
int 	cl_paramSetHandler (char *name, char *value);
int 	cl_paramGetHandler (char *name, char *value, int maxch);
int 	cl_pingHandler (char *sender);
int 	cl_imgLoadHandler (char *url, char *imgId, char *name);
int 	cl_tblLoadHandler (char *url, char *tblId, char *name);

int     cl_addUserHandler (char *mtype, char *cmd);
char   *cl_getUserHandler (char *mtype);

void    str_replace (char **string, char *substr, char *replacement);
int 	is_stdMType (char *mtype);

extern	XINT	samp;				/* SAMP handle 		*/
extern  pthread_mutex_t samp_mutex;		/* global data mutex	*/

extern	char   *voGetStrArg ();





/*****************************************************************************
 *  Utility procedures.
 ****************************************************************************/

/**
 *  CL_ADDUSERHANDLER -- Associate an mtype with a user-defined handler.
 */
int
cl_addUserHandler (char *mtype, char *cmd)
{
    register int i = 0;
    int   len = strlen (mtype);


    /*  See if it's a generic message.
     */
    if (!is_stdMType (mtype)) {
	extern XINT  samp;

        strcpy (userHandlers[numHandlers].mtype, mtype);
        strcpy (userHandlers[numHandlers].cmd, cmd);
        numHandlers++;

	samp_Subscribe (samp, mtype, cl_genericHandler);
	samp_DeclareSubscriptions (samp);

        return ( ((numHandlers < MAX_HANDLERS) ? 0 : -1) );
    }

    /* Check for an existing definition, if found, overwrite it.
     */
    for (i=0; i < numHandlers; i++) {
	if (strncmp (mtype, userHandlers[i].mtype, len) == 0) {
	    memset (userHandlers[i].cmd, 0, SZ_FNAME);
	    strcpy (userHandlers[i].cmd, cmd);
	    return (0);
	}
    }

    /*  No handler found, so add it to the list.
     */
    strcpy (userHandlers[numHandlers].mtype, mtype);
    strcpy (userHandlers[numHandlers].cmd, cmd);
    numHandlers++;

    return ( ((numHandlers < MAX_HANDLERS) ? 0 : -1) );
}


/**
 *  CL_DELUSERHANDLER -- Delete a user-defined handler for the named mtype.
 */
int
cl_delUserHandler (char *mtype)
{
    register int i = 0, j = 0;


    if (mtype == NULL) {
	/*  Delete all handlers.
	 */
        for (i=0; i < numHandlers; i++) {
	    memset (userHandlers[i].mtype, 0, SZ_FNAME);
	    memset (userHandlers[i].cmd, 0, SZ_FNAME);
	}
	numHandlers = 0;

    } else {
        int   len = strlen (mtype);

        /* Check for an existing definition, if found, delete it.
         */
        for (i=0; i < numHandlers; i++) {
	    if (strncmp (mtype, userHandlers[i].mtype, len) == 0) {
	        /*  Found a match.
	         */
	        memset (userHandlers[i].mtype, 0, SZ_FNAME);
	        memset (userHandlers[i].cmd, 0, SZ_FNAME);
    	        for (j=i; j < numHandlers; j++) {
		    /*  Shift remaining list.
		     */
	            strcpy (userHandlers[j].cmd, userHandlers[j+1].cmd);
	            strcpy (userHandlers[j].mtype,userHandlers[j+1].mtype);
    	        }

	        numHandlers--;
	    }
        }
    }

    return (0);
}


/**
 *  CL_GETUSERHANDLER -- Get any user-defined handler command for the given
 *  mtype, or NULL of not found.  Match up to the length of the input mtype
 *  to allow general matches, e.g. "image.load" instead of requiring the
 *  full "image.load.fits".
 */
char *
cl_getUserHandler (char *mtype)
{
    register int i = 0;
    int   len = strlen (mtype);
  
    for (i=0; i < numHandlers; i++) {
	if (strncmp (mtype, userHandlers[i].mtype, len) == 0) {
	    return (userHandlers[i].cmd);
	}
    }

    return (NULL);
}




/*****************************************************************************
 *  SAMP message handlers, these are server-side commands.
 ****************************************************************************/

int
cl_genericHandler (char *sender, char *mtype, char *msg_id, Map map)
{
    register int i, npars = 0;
    char  *cmd, *key, *val;


    /*  Check for a user-defined handler command.
     */
    if ( (cmd = cl_getUserHandler (mtype)) ) {
        char   *newstr = calloc (1, SZ_CMDBLK);
	char    arg[SZ_FNAME];

        /*  Do any command string replacements.
         */
        npars = samp_getMapSize (map);
        strcpy (newstr, cmd);
        for (i=0; i < npars; i++ ) {
	    key = (char *) samp_getMapKey (map, i);
	    val = (char *) samp_getMapVal (map, i);

	    memset (arg, 0, SZ_FNAME);
	    sprintf (arg, "$%s", key);
            str_replace (&newstr, arg, val);
        }

        /*  Execute as if it were sent as an exec message.
         */
        cl_cmdExecHandler (newstr);

        free (newstr);

    } else {
        /* Nothing to do..... */
    }

    return (0);
}


int
cl_cmdExecHandler (char *cmd)
{
    /*  Save the command to the buffer.
     */
    pthread_mutex_lock (&samp_mutex);
    strcat (samp_cmd, cmd);
    strcat (samp_cmd, "\n");
    pthread_mutex_unlock (&samp_mutex);

    /* Send the signal to the parent CL thread to notify it of the command.
     */
    kill (cl_pid, SIGIO);

    return (0);
}

int
cl_envSetHandler (char *name, char *value)
{
    c_envputs (name, value);
    pr_envset (0, name, value);
    if (strcmp ("erract", name) == 0)
        erract_init();

    return (0);
}

int
cl_envGetHandler (char *name, char *value, int maxch)
{
    char *s = NULL;
    char  val[SZ_LINE];

    memset (val, 0, SZ_LINE);
    if ((s = envget (name)))
        strncpy (value, s, maxch);
    else {
        if (c_envfind (name, val, SZ_LINE) < 0)
            strncpy (value, "INDEF", maxch);
        else
            strncpy (value, val, maxch);
    }

    return (0);
}

int
cl_paramSetHandler (char *name, char *value)
{
    char *pk, *t, *p, *f;
    struct param *pp;
    struct operand o;
    char  cmd[SZ_LINE], val[SZ_LINE];

    breakout (name, &pk, &t, &p, &f);
    strcpy (val, value);

    /* We can't use paramsrch here because the string we are looking
     * for might be a builtin or not exist, and paramsrch would fail
     * to return a reply.
     */
    if (t[0] && deftask (t) == YES) {
        if ((pp = lookup_param (pk, t, p))) {

            if (*f == FN_NULL && (pp->p_type & PT_LIST)) {
                /* Hitting EOF from a list is ok during an inspect stmt so avoid
                 * so avoid using paramget() with its EOF error. readlist() may
                 * set P_LEOF.
                 */
                o = readlist (pp);
                if ((pp->p_flags & P_LEOF) || inrange (pp, &o))
                    pushop (&o);
                else
                    query (pp);
            } else
                paramget (pp, FN_VALUE);/* get the parameter value field    */
            o = popop();

            /* Quote string params.
             */
            if ((o.o_type & OT_BASIC) == OT_STRING)
	        sprintf (val, "\"%s\"", value);
        }
    }


    /*  Cheat and make this a command string to be executed.
     */
    memset (cmd, 0, SZ_LINE);
    sprintf (cmd, "%s = %s\n", name, val);

    return ( cl_cmdExecHandler (cmd) );
}

int
cl_paramGetHandler (char *name, char *value, int maxch)
{
    char *pk, *t, *p, *f;
    struct param *pp;
    struct operand o;

    breakout (name, &pk, &t, &p, &f);
    strncpy (value, "INDEF", maxch);

    /* We can't use paramsrch here because the string we are looking
     * for might be a builtin or not exist, and paramsrch would fail
     * to return a reply.
     */
    if (t[0] && deftask (t) == NO)
	return (0);
    if (! (pp = lookup_param (pk, t, p)))
	return (0);

    if (*f == FN_NULL && (pp->p_type & PT_LIST)) {
        /*  Hitting EOF from a list is ok during an inspect stmt so avoid
         *  using paramget() with its EOF error. readlist() may set P_LEOF.
         */
        o = readlist (pp);
        if ((pp->p_flags & P_LEOF) || inrange (pp, &o))
            pushop (&o);
        else
            query (pp);
    } else {
        paramget (pp, FN_VALUE);	/* get the parameter value field    */
        opcast (OT_STRING);		/* cast as a string		    */
    }
    o = popop();

    memset (value, 0, maxch);
    if ((o.o_type & OT_BASIC) == OT_STRING)
        strncpy (value, o.o_val.v_s, maxch);
    else
        strncpy (value, "INDEF", maxch);

    return (0);
}

int
cl_pingHandler (char *sender)
{
    /*  no-op  */

    return (0);
}

int
cl_imgLoadHandler (char *url, char *imgId, char *name)
{
    char  *cmd = (char *) NULL;


    /*  Check for a user-defined handler command.
     */
    if ( (cmd = cl_getUserHandler ("image.load.")) ) {
 	char   *newstr = calloc (1, SZ_CMDBLK);

	/*  Do any command string replacements.
	 */
	strcpy (newstr, cmd);
	str_replace (&newstr, "$url", url);
	str_replace (&newstr, "$imageId", imgId);
	str_replace (&newstr, "$name", name);

	/*  Execute as if it were sent as an exec message. 
	 */
	cl_cmdExecHandler (newstr);

 	free (newstr);

    } else {
	/* Nothing to do..... */
    }

    return (0);
}

int
cl_tblLoadHandler (char *url, char *tblId, char *name)
{
    char  *cmd = (char *) NULL;


if (!url)
    return (1);
    /*  Check for a user-defined handler command.
     */
    if ( (cmd = cl_getUserHandler ("table.load.")) ) {
 	char   *newstr = calloc (1, SZ_CMDBLK);

	/*  Do any command string replacements.
	 */
	strcpy (newstr, cmd);
	str_replace (&newstr, "$url", url);
	str_replace (&newstr, "$imageId", tblId);
	str_replace (&newstr, "$name", name);

	/*  Execute as if it were sent as an exec message. 
	 */
	cl_cmdExecHandler (newstr);

 	free (newstr);

    } else {
	/* Nothing to do..... */
    }

    return (0);
}



/******************************************************************************
**  Local Utilities
******************************************************************************/

/**
 *  STR_REPLACE --  Replace the input string, substituting 'replacement' for
 *  all occurrances of 'substr'.  The input string is modified and is assumed
 *  to be at least SZ_CMDBLK long.
 */

void
str_replace (char **string, char *substr, char *replacement )
{
    char  *tok = (char *) NULL, *newstr = (char *) NULL, *str = *string;
    int    n = 0;


    for (n=0; n < MAX_SUBS; n++) {
        tok = strstr (str, substr);		/* check for no subs	*/
        if (tok == NULL) 
	    break;

        if ((newstr = calloc (1, SZ_CMDBLK)) == NULL)
	    break;

	/*  Do the replacements.
	 */
        memcpy (newstr, str, tok - str);	
        memcpy (newstr + (tok - str), replacement, strlen(replacement));
        memcpy (newstr + (tok - str) + strlen(replacement), 
	    tok + strlen(substr), strlen(str) - strlen(substr) - 
	    (tok - str));
        memset (newstr + strlen(str) - strlen(substr) + strlen(replacement), 
	    0, 1);

        strcpy (*string, newstr);
        free (newstr);
    }
}


/**
 * IS_STDMTYPE - See if the mtype is one of the well-known message type.
 */
int
is_stdMType (char *mtype)
{
    int   i = 0, len = 0;
    char *stdMTypes[] = {
	  "samp.app.ping", 	"samp.app.status", 	"samp.hub.event.*",
	  "table.load.fits", 	"table.load.votable", 	"table.highlight.row",
	  "image.load.fits", 	"coord.pointAt.sky", 	"client.cmd.exec",
	  "client.env.get", 	"client.env.set", 	"client.param.get",
	  "client.param.set", 	"bibcode.load", 	"table.select.rowList",
	  "voresource.loadlist",
	  "spectrum.load.ssa-generic",
	  NULL
	};

    for (i=0; stdMTypes[i]; i++) {
	len = min (strlen (mtype), strlen (stdMTypes[i]));
	if (strncasecmp (mtype, stdMTypes[i], len) == 0)
	    return (1);
    }
    return (0);
}