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( jsonObject* o );
17 int main( int argc, char* argv[] ) {
20 char* username = NULL;
21 char* password = NULL;
24 char* idl_filename = NULL;
27 while( (c = getopt( argc, argv, "f:u:p:s:c:i:" )) != -1 ) {
30 case 'f': config = strdup(optarg); break;
31 case 'c': context = strdup(optarg); break;
32 case 'u': username = strdup(optarg); break;
33 case 'p': password = strdup(optarg); break;
34 case 's': script = strdup(optarg); break;
35 case 'i': idl_filename = strdup(optarg); break;
40 fprintf(stderr, "IDL file not provided. Exiting...\n");
44 if (!oilsInitIDL( idl_filename )) {
45 fprintf(stderr, "IDL file could not be loaded. Exiting...\n");
49 if(!(config && context)) {
50 fprintf(stderr, "Config or config context not provided. Exiting...\n");
54 if( ! osrf_system_bootstrap_client(config, context) ) {
55 fprintf(stderr, "Unable to connect to OpenSRF network... [config:%s : context:%s]\n", config, context);
59 printf("Connected to OpenSRF network...\n");
61 if( username && password &&
62 ( authtoken = oilsUtilsLogin(username, password, "staff", -1 )) ) {
63 printf("Login Session: %s\n", authtoken);
66 while( (request=readline("oils# ")) ) {
67 int retcode = do_request(request);
83 static int do_request( char* request ) {
85 if(!strcasecmp(request, "exit") || !strcasecmp(request,"quit"))
88 if(!strcmp(request,"")) return 0;
94 service = strtok_r(request, " ", &tmp);
95 method = strtok_r(NULL, " ", &tmp);
97 if( service && method ) {
99 jsonObject* params = NULL;
102 growing_buffer* buffer = buffer_init(256);
103 buffer_fadd( buffer, "[%s]", tmp );
104 params = jsonParseString( buffer->buf );
108 osrfAppSession* session = osrf_app_client_session_init(service);
109 int req_id = osrf_app_session_make_req( session, params, method, 1, NULL );
112 while( (omsg = osrfAppSessionRequestRecv( session, req_id, 120 )) ) {
113 jsonObject* res = osrfMessageGetResult(omsg);
114 char* data = format_response(res);
115 printf("%s\n", data);
117 osrfMessageFree(omsg);
120 osrfAppSessionFree(session);
121 jsonObjectFree(params);
124 fprintf(stderr, "STATEMENT DOES NOT PARSE: %s\n", request);
131 static char* format_response( jsonObject* o ) {
136 if( o->classname && isFieldmapper(o->classname) ) {
140 growing_buffer* buffer = buffer_init(256);
142 buffer_fadd(buffer, " FM Class: %s\n", o->classname);
144 while( (key = fm_pton(o->classname, i++)) ) {
145 char* val = oilsFMGetString(o, key);
148 int l = strlen(key + 2);
149 buffer_fadd(buffer, " %s: ", key);
153 while( l++ < width ) buffer_add(buffer, "-");
154 buffer_fadd(buffer, " %s\n", val);
157 } else if( (item = oilsFMGetObject(o, key))) {
159 if(item->type != JSON_NULL ) {
160 char* d = format_response(item);
161 buffer_add(buffer, "\n====================================\n");
162 buffer_fadd(buffer, "%s\n", d);
163 buffer_add(buffer, "====================================\n");
166 while( l++ < width ) buffer_add(buffer, "-");
167 buffer_add(buffer," NULL \n");
172 while( l++ < width ) buffer_add(buffer, "-");
173 buffer_add(buffer," NULL \n");
179 char* data = buffer_data(buffer);
185 if( o->type == JSON_ARRAY ) {
187 growing_buffer* arrb = buffer_init(256);
188 for( i = 0; i != o->size; i++ ) {
189 char* d = format_response(jsonObjectGetIndex(o, i));
190 buffer_fadd(arrb, "%s\n", d);
193 jjson = buffer_data(arrb);
197 char* json = jsonObjectToJSON(o);
198 jjson = jsonFormatString(json);