4 #include <readline/readline.h>
5 #include "opensrf/utils.h"
6 #include "opensrf/osrf_system.h"
7 #include "opensrf/osrf_app_session.h"
8 #include "openils/oils_event.h"
9 #include "openils/oils_utils.h"
12 char* authtoken = NULL;
14 static int do_request( char* request );
15 static char* format_response( const jsonObject* o );
17 int main( int argc, char* argv[] ) {
20 char* username = NULL;
21 char* password = NULL;
24 char* idl_filename = NULL;
25 char* hostname = NULL;
28 while( (c = getopt( argc, argv, "f:u:p:s:c:i:h:" )) != -1 ) {
31 case 'f': config = strdup(optarg); break;
32 case 'c': context = strdup(optarg); break;
33 case 'u': username = strdup(optarg); break;
34 case 'p': password = strdup(optarg); break;
35 case 's': script = strdup(optarg); break;
36 case 'i': idl_filename = strdup(optarg); break;
37 case 'h': hostname = strdup(optarg); break;
41 if(!(config && context)) {
42 fprintf(stderr, "Config or config context not provided. Exiting...\n");
46 if( ! osrf_system_bootstrap_client(config, context) ) {
47 fprintf(stderr, "Unable to connect to OpenSRF network... [config:%s : context:%s]\n", config, context);
53 fprintf(stderr, "We need an IDL file name or a settings server hostname...\n");
56 osrf_settings_retrieve(hostname);
59 if (!oilsInitIDL( idl_filename )) {
60 fprintf(stderr, "IDL file could not be loaded. Exiting...\n");
64 printf("Connected to OpenSRF network...\n");
66 if( username && password &&
67 ( authtoken = oilsUtilsLogin(username, password, "staff", -1 )) ) {
68 printf("Login Session: %s\n", authtoken);
71 while( (request=readline("oils# ")) ) {
72 int retcode = do_request(request);
84 osrf_settings_free_host_config(NULL);
89 static int do_request( char* request ) {
91 if(!strcasecmp(request, "exit") || !strcasecmp(request,"quit"))
94 if(!strcmp(request,"")) return 0;
100 service = strtok_r(request, " ", &tmp);
101 method = strtok_r(NULL, " ", &tmp);
103 if( service && method ) {
105 jsonObject* params = NULL;
108 growing_buffer* buffer = buffer_init(256);
109 buffer_fadd( buffer, "[%s]", tmp );
110 params = jsonParseString( buffer->buf );
114 osrfAppSession* session = osrfAppSessionClientInit(service);
115 int req_id = osrfAppSessionMakeRequest( session, params, method, 1, NULL );
118 while( (omsg = osrfAppSessionRequestRecv( session, req_id, 120 )) ) {
119 jsonObject* res = osrfMessageGetResult(omsg);
120 char* data = format_response(res);
121 printf("%s\n", data);
123 osrfMessageFree(omsg);
126 osrfAppSessionFree(session);
127 jsonObjectFree(params);
130 fprintf(stderr, "STATEMENT DOES NOT PARSE: %s\n", request);
137 static char* format_response( const jsonObject* o ) {
142 if( o->classname && isFieldmapper(o->classname) ) {
146 growing_buffer* buffer = buffer_init(256);
148 buffer_fadd(buffer, " FM Class: %s\n", o->classname);
150 while( (key = fm_pton(o->classname, i++)) ) {
151 char* val = oilsFMGetString(o, key);
152 const jsonObject* item;
154 int l = strlen(key + 2);
155 buffer_fadd(buffer, " %s: ", key);
159 while( l++ < width ) buffer_add(buffer, "-");
160 buffer_fadd(buffer, " %s\n", val);
163 } else if( (item = oilsFMGetObject(o, key))) {
165 if(item->type != JSON_NULL ) {
166 char* d = format_response(item);
167 buffer_add(buffer, "\n====================================\n");
168 buffer_fadd(buffer, "%s\n", d);
169 buffer_add(buffer, "====================================\n");
172 while( l++ < width ) buffer_add(buffer, "-");
173 buffer_add(buffer," NULL \n");
178 while( l++ < width ) buffer_add(buffer, "-");
179 buffer_add(buffer," NULL \n");
185 return buffer_release(buffer);
189 if( o->type == JSON_ARRAY ) {
191 growing_buffer* arrb = buffer_init(256);
192 for( i = 0; i != o->size; i++ ) {
193 char* d = format_response(jsonObjectGetIndex(o, i));
194 buffer_fadd(arrb, "%s\n", d);
198 jjson = buffer_release(arrb);
201 char* json = jsonObjectToJSON(o);
202 jjson = jsonFormatString(json);