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;
13 static char* tz = NULL;
15 static int do_request( char* request );
16 static char* format_response( const jsonObject* o );
18 int main( int argc, char* argv[] ) {
21 char* username = NULL;
22 char* password = NULL;
25 char* idl_filename = NULL;
26 char* hostname = NULL;
29 while( (c = getopt( argc, argv, "f:u:p:s:c:i:h:" )) != -1 ) {
32 case 'f': config = strdup(optarg); break;
33 case 'c': context = strdup(optarg); break;
34 case 'u': username = strdup(optarg); break;
35 case 'p': password = strdup(optarg); break;
36 case 's': script = strdup(optarg); break;
37 case 'i': idl_filename = strdup(optarg); break;
38 case 'h': hostname = strdup(optarg); break;
42 if(!(config && context)) {
43 fprintf(stderr, "Config or config context not provided. Exiting...\n");
47 if( ! osrf_system_bootstrap_client(config, context) ) {
48 fprintf(stderr, "Unable to connect to OpenSRF network... [config:%s : context:%s]\n",
55 fprintf( stderr, "We need an IDL file name or a settings server hostname...\n");
58 osrf_settings_retrieve(hostname);
61 if (!oilsInitIDL( idl_filename )) {
62 fprintf(stderr, "IDL file could not be loaded. Exiting...\n");
66 printf("Connected to OpenSRF network...\n");
70 if( username && password &&
71 ( authtoken = oilsUtilsLogin(username, password, "staff", -1 )) ) {
72 printf("Login Session: %s\n", authtoken);
75 while( (request=readline("oils# ")) ) {
76 int retcode = do_request(request);
89 osrf_settings_free_host_config(NULL);
94 static int do_request( char* request ) {
96 if(!strcasecmp(request, "exit") || !strcasecmp(request,"quit"))
99 if(!strcmp(request,""))
106 service = strtok_r(request, " ", &tmp);
107 method = strtok_r(NULL, " ", &tmp);
109 if( service && method ) {
111 jsonObject* params = NULL;
114 growing_buffer* buffer = buffer_init(256);
115 buffer_fadd( buffer, "[%s]", tmp );
116 params = jsonParse( buffer->buf );
120 osrfAppSession* session = osrfAppSessionClientInit(service);
121 if (tz) osrf_app_session_set_tz(session,tz);
123 int req_id = osrfAppSessionSendRequest( session, params, method, 1 );
126 while( (omsg = osrfAppSessionRequestRecv( session, req_id, 120 )) ) {
127 const jsonObject* res = osrfMessageGetResult(omsg);
128 char* data = format_response(res);
129 printf("%s\n", data);
131 osrfMessageFree(omsg);
134 osrfAppSessionFree(session);
135 jsonObjectFree(params);
138 fprintf(stderr, "STATEMENT DOES NOT PARSE: %s\n", request);
145 static char* format_response( const jsonObject* o ) {
150 if( o->classname && isFieldmapper(o->classname) ) {
154 growing_buffer* buffer = buffer_init(256);
156 buffer_fadd(buffer, " FM Class: %s\n", o->classname);
158 while( (key = fm_pton(o->classname, i++)) ) {
159 char* val = oilsFMGetString(o, key);
160 const jsonObject* item;
162 int l = strlen(key + 2);
163 buffer_fadd(buffer, " %s: ", key);
167 while( l++ < width ) buffer_add(buffer, "-");
168 buffer_fadd(buffer, " %s\n", val);
171 } else if( (item = oilsFMGetObject(o, key))) {
173 if(item->type != JSON_NULL ) {
174 char* d = format_response(item);
175 buffer_add(buffer, "\n====================================\n");
176 buffer_fadd(buffer, "%s\n", d);
177 buffer_add(buffer, "====================================\n");
180 while( l++ < width ) buffer_add(buffer, "-");
181 buffer_add(buffer," NULL \n");
186 while( l++ < width ) buffer_add(buffer, "-");
187 buffer_add(buffer," NULL \n");
193 return buffer_release(buffer);
197 if( o->type == JSON_ARRAY ) {
199 growing_buffer* arrb = buffer_init(256);
200 for( i = 0; i != o->size; i++ ) {
201 char* d = format_response(jsonObjectGetIndex(o, i));
202 buffer_fadd(arrb, "%s\n", d);
206 jjson = buffer_release(arrb);
209 char* json = jsonObjectToJSON(o);
210 jjson = jsonFormatString(json);