trivial hack to do cmdline oaf queries



Hi all, 

I wrote a little program to do command line oaf queries. It's rather
trivial, but I've found it useful, so maybe you will do so as
well.

There are some improvements that could be made, such as some shortcuts
for asking 'repo_ids.has()' etc. If typing OAF queries irritates me
enough I might make those improvements.
 
[Hmmm... I assume this is the appropriate list for OAF related
stuff. If not, I'm sorry.]

Cheers,
	Dirk-Jan.


/*
** lsoaf.c -- trivial hack to do cmdline oaf queries
**  
** compile with:
**      gcc -o lsoaf lsoaf.c `oaf-config --libs` `oaf-config --cflags` 
**
** Copyright (C) 2000 Dirk-Jan C. Binnema <dirkjan gnome org>
**  
** This program 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 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**  
*/



#include <liboaf/liboaf.h>
#include <popt.h>


static int    do_query        (gchar *query, gint argc, gchar *argv[]);
static gchar* parse_arguments (gint argc, gchar *argv[]);

static gboolean
	display_server_type= FALSE,
	display_properties = FALSE, 
	display_location   = FALSE,
	display_username   = FALSE,
	display_hostname   = FALSE,
	display_domain     = FALSE,
	display_all        = FALSE;

int
main (int argc, char *argv[])
{
	gboolean retval;
	gchar *query;
	
	query = parse_arguments (argc, argv);
	if (!query) 

		return 1;
	else {

		retval = do_query (query, argc, argv);
		g_free (query);
		return retval;
	}
}




static gchar*
parse_arguments (int argc, gchar *argv[])
{
	poptContext option_context;
	gchar *query;
	gchar c;
			
	const struct poptOption options[] = {
		{
			"location",
			'l',
			POPT_ARG_NONE,
			&display_location,
			0,
			"show location information",
			NULL
		},
	        {
			"properties",
			'p',
			POPT_ARG_NONE,
			&display_properties,
			0,
			"show properties",
			NULL
		},
	        {
			"username",
			'u',
			POPT_ARG_NONE,
			&display_username,
			0,
			"show the username",
			NULL
		},
	        {
			"hostname",
			'h',
			POPT_ARG_NONE,
			&display_hostname,
			0,
			"show the hostname",
			NULL
		},
	        {
			"domain",
			'd',
			POPT_ARG_NONE,
			&display_domain,
			0,
			"show domain",
			NULL
		},
		{
			"query",
			'q',
			POPT_ARG_STRING,
			&query,
			0,
			"search for objects using the given oaf query",
			NULL
		},
		{
			NULL,
			0,
			0,
			NULL,
			0
		}
	};

	static gchar* usage_msg =
		"\n-l        : display location"
		"\n-p        : display properties"
		"\n-u        : display user name"
		"\n-h        : display hostname"
		"\n-d        : display domain"
		"\n-q <query>: an OAF query"
		"\n\nexample: $ lsoaf -lp -q \"repo_ids.has('IDL:Bonobo/Control:1.0')\"\n";
	
	option_context = poptGetContext (NULL, argc, (const char**)argv,
					 options, 0);
	poptSetOtherOptionHelp (option_context, usage_msg);
	if (argc < 2) {
		poptPrintUsage (option_context, stderr, 0);
		exit (1);
	}

	while ((c = poptGetNextOpt(option_context)) >= 0) 
		g_print ("*** %c\n", c);
		
	poptFreeContext (option_context);
	return g_strdup(query);
}



static gboolean
do_query (gchar* query, gint argc, gchar *argv[])
{
	CORBA_ORB orb;
	CORBA_Environment ev;
	
	OAF_Property oaf_prop;
	OAF_ServerInfoList *query_result;
	OAF_ServerInfo server_info;

	gint i,j,k, prop_num, string_num;
	
	gchar *order_by = NULL;
	
	gboolean display_anything =
		display_server_type ||
		display_properties  || 
		display_location    ||
		display_username    ||
		display_hostname    ||
		display_domain;        
	
	CORBA_exception_init(&ev);	 
			
	orb = oaf_init (argc,argv);
	if (!orb) 
		g_error ("%s", "failed to initialize orb");
	if (!oaf_is_initialized())
		g_error ("%s", "failed to initialize oaf");
	
	query_result = oaf_query (query, &order_by, &ev);
	if (ev._major != CORBA_NO_EXCEPTION) {
		g_print ( "CORBA exception: %s\n",
				CORBA_exception_id(&ev));
		CORBA_exception_free (&ev);
		return 1;
	} 
	
	for (i = 0; i < query_result->_length; ++i) {

		server_info =  query_result->_buffer[i]; 
		
		if (display_server_type)
			g_print ("type     : %s\n", server_info.server_type);
		if (display_location)
			g_print ("location : %s\n", server_info.location_info);
		if (display_username)
			g_print ("username : %s\n", server_info.username);
		if (display_hostname)
			g_print ("hostname : %s\n", server_info.hostname);
		if (display_domain)
			g_print ("domain   : %s\n", server_info.domain);

		if (display_properties) {

			prop_num = query_result->_buffer[i].props._length;

			for (j=0; j < prop_num; ++j) {

				oaf_prop = query_result->_buffer[i].props._buffer[j];
				
				g_print ("property [%d]: '%s' => ", j+1, oaf_prop.name);
				switch (oaf_prop.v._d) {

				case OAF_P_STRING:
					g_print ("\"%s\" (string)\n",oaf_prop.v._u.value_string);
					break;

				case OAF_P_NUMBER:
					g_print ("%d (number)\n",oaf_prop.v._u.value_number);
					break;

				case OAF_P_BOOLEAN:
					g_print ("%s (boolean)\n",oaf_prop.v._u.value_number?"true":"false");
					break;

				case OAF_P_STRINGV:
					string_num = oaf_prop.v._u.value_stringv._length;
					for (k = 0; k < string_num; ++k) {
						g_print ("\"%s\"", oaf_prop.v._u.value_stringv._buffer[k]);
						if ( k < string_num - 1)
							g_print (", ");
					}
					g_print (" (string list [%d])\n", string_num);
					break;

				default:
					g_print ("error: unknown type\n");
				}
			}
		}

		if (display_anything)
			g_print ("\n");
	}

	if (CORBA_sequence_get_release (query_result)) 
		CORBA_free(query_result->_buffer);
	
	CORBA_exception_free (&ev);	
	
	return 0;
}



+--------------------------------------------------+
          Dirk-Jan C. Binnema <djcb dds nl> 
           http://www.casema.net/~devnull
 --------------------------------------------------
                 PGP/GPG fingerprint:
 BB49 41D7 053D E5F1 F333 586E C530 CBC3 4352 A39F
+--------------------------------------------------+




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]