[net.unix] code for Selection service question...

biggers@caip.RUTGERS.EDU (Mark Biggers) (10/06/86)

Oops, here it is!


#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	get_select.c
#	put_select.c
# This archive created: Mon Sep 22 17:33:56 1986
export PATH; PATH=/bin:$PATH
if test -f 'get_select.c'
then
	echo shar: will not over-write existing file "'get_select.c'"
else
cat << \SHAR_EOF > 'get_select.c'

#ifndef lint
static char sccs_id[] = "%W% %H% %T% (c) Siemens RTL";
#endif

/*  Selection service example - REQUESTER test
 *  Modified AND fixed from Sun manual "SunView System Programmer's Guide",
 *  Chapter 9, "The Selection Service and Library", pp 117 - 119.
 */
#include <stdio.h>
#include <sys/types.h>
#include <suntool/selection_svc.h>
#include <suntool/selection_attributes.h>
#include "basetype.h"


main(argc, argv)
    int argc;
    char *argv[];
{
    extern Seln_result   read_proc();
    Seln_holder holder;
    Seln_rank rank = SELN_PRIMARY;
    char *context = NULL;
    int				arg_count;

    arg_count = argc;
    
    while (--argc)
    {
	argv++;
	switch (**argv)
	{
	case '1':
	    rank = SELN_PRIMARY;
	    break;
	case '2':
	    rank = SELN_SECONDARY;
	    break;
	case '3':
	    rank = SELN_SHELF;
	    break;
	case 'd':
	    seln_use_test_service();
	    break;
	case 't':
	    seln_use_timeout(atoi(++argv));
	    --argc;
	    break;
	default:
	    arg_count = 1;  /* no valid args, so zap the count */
	    break;
	}
    }

    if (arg_count == 1)
    {
	fprintf(stderr,"Usage: get_select [d]ebug [t]imeout secs 1|2|3\n");
	exit(0);
    }

    /*  find the holder of the desired selection
     */
    holder = seln_inquire(rank);
    if (holder.state == SELN_NONE)
    {
	quit("Selection not there, or failure of Service\n");
    }
    /*  ask for the contents, and let "callback" procedure print them
     */
    (void) seln_query(&holder, read_proc, context,
		      SELN_REQ_CONTENTS_ASCII, NULL, NULL);
    exit(0);
}


quit(str)
    char *str;
{
    fprintf(stderr, str);
    exit(1);

}


/*  Called with each buffer of data returned in response to request
 *  transmitted by 'seln_query()'.
 */
Seln_result
read_proc(buffer)
    Seln_request *buffer;
{
    static bool request_attr_skipped = FALSE;
    static int count = 0;
    char *reply;

    
    /*  On first buffer, we have to skip the request attribute,
     *  and then make sure we don't repeat on subsequent buffers
     */
    if (request_attr_skipped == FALSE)
    {
	if (buffer == (Seln_request *) NULL ||
	    *((Seln_attribute *) buffer->data) != SELN_REQ_CONTENTS_ASCII)
	{
	    fprintf(stderr, "Selection holder error - unrecognized request\n");
	    return SELN_UNRECOGNIZED;
	}
	printf("Portion #%d of selection received: %s", ++count,
	       buffer->data + sizeof(Seln_attribute));

	request_attr_skipped = TRUE;
	return SELN_SUCCESS;
    }
    else	/* buffer->status == SELN_CONTINUED or SELN_SUCCESS */
    {
	printf("Portion #%d of selection received: %s", ++count,
	       buffer->data);

	return SELN_SUCCESS;
    }
}
SHAR_EOF
fi # end of overwriting check
if test -f 'put_select.c'
then
	echo shar: will not over-write existing file "'put_select.c'"
else
cat << \SHAR_EOF > 'put_select.c'

#ifndef lint
static char sccs_id[] = "%W% %H% %T% (c) Siemens RTL";
#endif

/*  Selection service CLIENT test
 */
#include <stdio.h>
#include <sys/types.h>
#include <suntool/selection_svc.h>
#include <suntool/selection_attributes.h>
#include "basetype.h"


main(argc, argv)
    int argc;
    char *argv[];
{
    extern int			dummy_proc();
    Seln_rank			rank;
    Seln_result			reply_proc();
/*    static struct selection	empty_selection =
    {
	SELTYPE_NULL, 0, 0, 0, 0
    };
*/    static Seln_client		seln_client;

    int				arg_count;
    caddr_t			client_data;
    Seln_rank			req_rank;
    Seln_holder			holder;


    arg_count = argc;

    while (--argc)
    {
	argv++;
	switch (**argv)
	{
	case '1':
	    rank = SELN_PRIMARY;
	    break;
	case '2':
	    rank = SELN_SECONDARY;
	    break;
	case '3':
	    rank = SELN_SHELF;
	    break;
	case 'd':
	    seln_use_test_service();
	    break;
	case 't':
	    seln_use_timeout(atoi(++argv));
	    --argc;
	    break;
	case '&':
	    holder = seln_inquire(rank);
	    seln_dump_service(rank);
	    break;
	default:
	    arg_count = 1;  /* no valid args, so zap the count */
	    break;
	}
    }

    if (arg_count == 1)
    {
	fprintf(stderr,"Usage: put_select [d]ebug [t]imeout secs 1|2|3");
	fprintf(stderr,"  <selection-file (from stdin)\n");
	exit(0);
    }

    if ((seln_client = seln_create(dummy_proc, reply_proc, client_data))
	 == (Seln_client) NULL)
    {
	fprintf(stderr, "Can't find selection service\n");
	exit(1);
    }

    if ( (req_rank = seln_acquire(seln_client, rank)) != rank )
	fprintf(stderr, "Did not get requested rank!\n");

    sleep(10);

    seln_destroy(seln_client);
    exit(0);
}

    
int
dummy_proc()
{
    /* do naught */
    return;
}


Seln_result
reply_proc(item, context, length)
    caddr_t		item;
    Seln_replier_data	*context;
    int			length;
{
    static bool		any_reply = FALSE;
    char		my_buffer[SELN_BUFSIZE];	/* Selection buffer */
    Seln_result		my_result;
    char		*bufp;


    switch (item)
    {
    case SELN_REQ_END_REQUEST:
	return SELN_SUCCESS;
	break;
    case SELN_REQ_CONTENTS_ASCII:   /* ok, continue processing this request */
	break;
    default:
	return SELN_UNRECOGNIZED;
	/* NOTREACHED */
	break;
    }


    /* Don't be concerned with other selection ranks - yet.
     */
    if (context->rank != SELN_PRIMARY)
	return SELN_DIDNT_HAVE;


    if (read(0, my_buffer, length) == 0)	/* at EOF, so... */
    {
	my_result = (any_reply == FALSE) ? SELN_UNRECOGNIZED : SELN_SUCCESS;
    }
    else
    {
	any_reply = TRUE;
	my_result = SELN_CONTINUED;
    }


    /*  Point to the first full-word AFTER the end of the selection
     */
    bufp = my_buffer + length;
   *context->response_pointer = ( (int) bufp % 4 == 0 ) ?
	bufp : bufp + (4 - ( (int) bufp % 4 ));

    return my_result;
}
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0