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",
54 fprintf( stderr, "We need an IDL file name or a settings server hostname...\n");
57 osrf_settings_retrieve(hostname);
60 if (!oilsInitIDL( idl_filename )) {
61 fprintf(stderr, "IDL file could not be loaded. Exiting...\n");
65 printf("Connected to OpenSRF network...\n");
67 if( username && password &&
68 ( authtoken = oilsUtilsLogin(username, password, "staff", -1 )) ) {
69 printf("Login Session: %s\n", authtoken);
72 while( (request=readline("oils# ")) ) {
73 int retcode = do_request(request);
86 osrf_settings_free_host_config(NULL);
91 static int do_request( char* request ) {
93 if(!strcasecmp(request, "exit") || !strcasecmp(request,"quit"))
96 if(!strcmp(request,""))
103 service = strtok_r(request, " ", &tmp);
104 method = strtok_r(NULL, " ", &tmp);
106 if( service && method ) {
108 jsonObject* params = NULL;
111 growing_buffer* buffer = buffer_init(256);
112 buffer_fadd( buffer, "[%s]", tmp );
113 params = jsonParse( buffer->buf );
117 osrfAppSession* session = osrfAppSessionClientInit(service);
118 int req_id = osrfAppSessionSendRequest( session, params, method, 1 );
121 while( (omsg = osrfAppSessionRequestRecv( session, req_id, 120 )) ) {
122 const jsonObject* res = osrfMessageGetResult(omsg);
123 char* data = format_response(res);
124 printf("%s\n", data);
126 osrfMessageFree(omsg);
129 osrfAppSessionFree(session);
130 jsonObjectFree(params);
133 fprintf(stderr, "STATEMENT DOES NOT PARSE: %s\n", request);
140 static char* format_response( const jsonObject* o ) {
145 if( o->classname && isFieldmapper(o->classname) ) {
149 growing_buffer* buffer = buffer_init(256);
151 buffer_fadd(buffer, " FM Class: %s\n", o->classname);
153 while( (key = fm_pton(o->classname, i++)) ) {
154 char* val = oilsFMGetString(o, key);
155 const jsonObject* item;
157 int l = strlen(key + 2);
158 buffer_fadd(buffer, " %s: ", key);
162 while( l++ < width ) buffer_add(buffer, "-");
163 buffer_fadd(buffer, " %s\n", val);
166 } else if( (item = oilsFMGetObject(o, key))) {
168 if(item->type != JSON_NULL ) {
169 char* d = format_response(item);
170 buffer_add(buffer, "\n====================================\n");
171 buffer_fadd(buffer, "%s\n", d);
172 buffer_add(buffer, "====================================\n");
175 while( l++ < width ) buffer_add(buffer, "-");
176 buffer_add(buffer," NULL \n");
181 while( l++ < width ) buffer_add(buffer, "-");
182 buffer_add(buffer," NULL \n");
188 return buffer_release(buffer);
192 if( o->type == JSON_ARRAY ) {
194 growing_buffer* arrb = buffer_init(256);
195 for( i = 0; i != o->size; i++ ) {
196 char* d = format_response(jsonObjectGetIndex(o, i));
197 buffer_fadd(arrb, "%s\n", d);
201 jjson = buffer_release(arrb);
204 char* json = jsonObjectToJSON(o);
205 jjson = jsonFormatString(json);