3 int recv_timeout = 120;
4 int is_from_script = 0;
5 FILE* shell_writer = NULL;
6 FILE* shell_reader = NULL;
8 int main( int argc, char* argv[] ) {
10 /* --------------------------------------------- */
11 /* see if they have a .srfsh.xml in their home directory */
12 char* home = getenv("HOME");
13 int l = strlen(home) + 36;
16 sprintf(fbuf,"%s/.srfsh.xml",home);
18 if(!access(fbuf, R_OK)) {
19 if( ! osrf_system_bootstrap_client(fbuf, "srfsh") ) {
20 fprintf(stderr,"Unable to bootstrap client for requests\n");
21 osrfLogError( OSRF_LOG_MARK, "Unable to bootstrap client for requests");
26 fprintf(stderr,"No Config file found at %s\n", fbuf );
31 /* for now.. the first arg is used as a script file for processing */
33 if( (f = open(argv[1], O_RDONLY)) == -1 ) {
34 osrfLogError( OSRF_LOG_MARK, "Unable to open file %s for reading, exiting...", argv[1]);
38 if(dup2(f, STDIN_FILENO) == -1) {
39 osrfLogError( OSRF_LOG_MARK, "Unable to duplicate STDIN, exiting...");
47 /* --------------------------------------------- */
51 client = osrf_system_get_transport_client();
53 /* open the shell handle */
54 shell_writer = popen( "bash", "w");
55 //shell_reader = popen( "bash", "r");
57 /* main process loop */
59 while((request=readline(prompt))) {
61 if( !strcasecmp(request, "exit") || !strcasecmp(request,"quit"))
64 char* req_copy = strdup(request);
66 parse_request( req_copy );
67 if( request && strlen(request) > 1 ) {
79 if(history_file != NULL )
80 write_history(history_file);
84 osrf_system_shutdown();
88 void sig_child_handler( int s ) {
93 void sig_int_handler( int s ) {
96 signal(SIGINT,sig_int_handler);
102 char* home = getenv("HOME");
103 int l = strlen(home) + 24;
107 sprintf(fbuf,"%s/.srfsh_history",home);
108 history_file = strdup(fbuf);
110 if(!access(history_file, W_OK | R_OK )) {
111 history_length = 999;
112 read_history(history_file);
118 int parse_error( char* words[] ) {
127 memset(buffer, 0, 256);
128 while( (current=words[i++]) ) {
129 strcat(buffer, current);
132 if( ! buffer || strlen(buffer) < 1 )
135 fprintf( stderr, "???: %s\n", buffer );
141 int parse_request( char* request ) {
143 if( request == NULL )
148 char* words[COMMAND_BUFSIZE];
149 memset(words,0,COMMAND_BUFSIZE);
152 char* cur_tok = strtok( req, " " );
154 if( cur_tok == NULL )
157 while(cur_tok != NULL) {
158 words[i++] = cur_tok;
159 cur_tok = strtok( NULL, " " );
163 // not sure why (strtok?), but this is necessary
164 memset( words + i, 0, COMMAND_BUFSIZE - i );
166 /* pass off to the top level command */
167 if( !strcmp(words[0],"router") )
168 ret_val = handle_router( words );
171 else if( !strcmp(words[0],"time") )
172 ret_val = handle_time( words );
175 else if (!strcmp(words[0],"request"))
176 ret_val = handle_request( words, 0 );
178 else if (!strcmp(words[0],"relay"))
179 ret_val = handle_request( words, 1 );
181 else if (!strcmp(words[0],"help"))
182 ret_val = print_help();
184 else if (!strcmp(words[0],"set"))
185 ret_val = handle_set(words);
187 else if (!strcmp(words[0],"print"))
188 ret_val = handle_print(words);
190 else if (!strcmp(words[0],"math_bench"))
191 ret_val = handle_math(words);
193 else if (!strcmp(words[0],"introspect"))
194 ret_val = handle_introspect(words);
196 else if (!strcmp(words[0],"login"))
197 ret_val = handle_login(words);
199 else if (words[0][0] == '!')
200 ret_val = handle_exec( words, 1 );
204 return handle_exec( words, 0 );
206 return parse_error( words );
215 int handle_introspect(char* words[]) {
217 if(words[1] && words[2]) {
218 fprintf(stderr, "--> %s\n", words[1]);
221 sprintf( buf, "request %s opensrf.system.method %s", words[1], words[2] );
222 return parse_request( buf );
227 fprintf(stderr, "--> %s\n", words[1]);
230 sprintf( buf, "request %s opensrf.system.method.all", words[1] );
231 return parse_request( buf );
239 int handle_login( char* words[]) {
241 if( words[1] && words[2]) {
243 char* username = words[1];
244 char* password = words[2];
245 char* type = words[3];
246 char* orgloc = words[4];
248 if(orgloc) orgloci = atoi(orgloc);
249 if(!type) type = "opac";
258 "request open-ils.auth open-ils.auth.authenticate.init \"%s\"", username );
262 if(last_result && last_result->_result_content) {
263 jsonObject* r = last_result->_result_content;
264 hash = jsonObjectGetString(r);
268 char* pass_buf = md5sum(password);
271 memset(both_buf,0,256);
272 sprintf(both_buf,"%s%s",hash, pass_buf);
274 char* mess_buf = md5sum(both_buf);
276 sprintf( buf2, "request open-ils.auth open-ils.auth.authenticate.complete "
277 "\"%s\", \"%s\", \"%s\", %d", username, mess_buf, type, orgloci );
282 parse_request( buf2 );
284 jsonObject* x = last_result->_result_content;
287 char* authtoken = jsonObjectGetString(
288 jsonObjectGetKey(jsonObjectGetKey(x,"payload"), "authtoken"));
289 authtime = jsonObjectGetNumber(
290 jsonObjectGetKey(jsonObjectGetKey(x,"payload"), "authtime"));
291 if(authtoken) login_session = strdup(authtoken);
292 else login_session = NULL;
294 else login_session = NULL;
296 printf("Login Session: %s. Session timeout: %lf\n", login_session, authtime );
305 int handle_set( char* words[]) {
308 if( (variable=words[1]) ) {
311 if( (val=words[2]) ) {
313 if(!strcmp(variable,"pretty_print")) {
314 if(!strcmp(val,"true")) {
316 printf("pretty_print = true\n");
319 if(!strcmp(val,"false")) {
321 printf("pretty_print = false\n");
326 if(!strcmp(variable,"raw_print")) {
327 if(!strcmp(val,"true")) {
329 printf("raw_print = true\n");
332 if(!strcmp(val,"false")) {
334 printf("raw_print = false\n");
346 int handle_print( char* words[]) {
349 if( (variable=words[1]) ) {
350 if(!strcmp(variable,"pretty_print")) {
352 printf("pretty_print = true\n");
355 printf("pretty_print = false\n");
360 if(!strcmp(variable,"login")) {
361 printf("login session = %s\n", login_session );
369 int handle_router( char* words[] ) {
377 if( !strcmp(words[1],"query") ) {
379 if( words[2] && !strcmp(words[2],"servers") ) {
380 for(i=3; i < COMMAND_BUFSIZE - 3 && words[i]; i++ ) {
381 router_query_servers( words[i] );
393 /* if new shell, spawn a new child and subshell to do the work,
394 otherwise pipe the request to the currently open (piped) shell */
395 int handle_exec(char* words[], int new_shell) {
397 if(!words[0]) return 0;
399 if( words[0] && words[0][0] == '!') {
400 int len = strlen(words[0]);
402 memset(command,0,len);
404 int i; /* chop out the ! */
405 for( i=1; i!= len; i++) {
406 command[i-1] = words[0][i];
410 words[0] = strdup(command);
414 signal(SIGCHLD, sig_child_handler);
420 signal(SIGCHLD,sig_child_handler);
425 execvp( words[0], words );
432 growing_buffer* b = buffer_init(64);
435 buffer_fadd( b, "%s ", words[i++] );
437 buffer_add( b, "\n");
440 //int reader = dup2(STDOUT_FILENO, reader);
441 //int reader = dup(STDOUT_FILENO);
442 //close(STDOUT_FILENO);
444 fprintf( shell_writer, b->buf );
447 fflush(shell_writer);
453 read( reader, c, 4095 );
454 fprintf(stderr, "read %s", c);
455 dup2(reader, STDOUT_FILENO);
465 int handle_request( char* words[], int relay ) {
471 char* server = words[1];
472 char* method = words[2];
474 growing_buffer* buffer = NULL;
476 buffer = buffer_init(128);
477 buffer_add(buffer, "[");
478 for(i = 3; words[i] != NULL; i++ ) {
479 /* removes trailing semicolon if user accidentally enters it */
480 if( words[i][strlen(words[i])-1] == ';' )
481 words[i][strlen(words[i])-1] = '\0';
482 buffer_add( buffer, words[i] );
483 buffer_add(buffer, " ");
485 buffer_add(buffer, "]");
488 return send_request( server, method, buffer, relay );
494 int send_request( char* server,
495 char* method, growing_buffer* buffer, int relay ) {
496 if( server == NULL || method == NULL )
499 jsonObject* params = NULL;
501 if( buffer != NULL && buffer->n_used > 0 )
502 params = json_parse_string(buffer->buf);
504 if(!last_result || ! last_result->_result_content) {
505 printf("We're not going to call 'relay' with no result params\n");
509 jsonObject* o = jsonNewObject(NULL);
510 jsonObjectPush(o, last_result->_result_content );
516 if(buffer->n_used > 0 && params == NULL) {
517 fprintf(stderr, "JSON error detected, not executing\n");
521 osrf_app_session* session = osrf_app_client_session_init(server);
523 if(!osrf_app_session_connect(session)) {
524 osrfLogWarning( OSRF_LOG_MARK, "Unable to connect to remote service %s\n", server );
528 double start = get_timestamp_millis();
529 //int req_id = osrf_app_session_make_request( session, params, method, 1, NULL );
530 int req_id = osrf_app_session_make_req( session, params, method, 1, NULL );
533 osrf_message* omsg = osrf_app_session_request_recv( session, req_id, recv_timeout );
536 printf("\nReceived no data from server\n");
539 signal(SIGPIPE, SIG_IGN);
542 if(!is_from_script) less = popen( "less -EX", "w");
545 if( less == NULL ) { less = stdout; }
547 growing_buffer* resp_buffer = buffer_init(4096);
553 if(omsg->_result_content) {
555 osrf_message_free(last_result);
560 if( pretty_print && omsg->_result_content ) {
561 char* j = jsonObjectToJSON(omsg->_result_content);
562 //content = json_printer(j);
563 content = jsonFormatString(j);
566 content = jsonObjectGetString(omsg->_result_content);
568 printf( "\nReceived Data: %s\n", content );
575 sprintf( code, "%d", omsg->status_code );
576 buffer_add( resp_buffer, code );
578 printf( "\nReceived Exception:\nName: %s\nStatus: %s\nStatus: %s\n",
579 omsg->status_name, omsg->status_text, code );
586 if(omsg->_result_content) {
588 osrf_message_free(last_result);
593 if( pretty_print && omsg->_result_content ) {
594 char* j = jsonObjectToJSON(omsg->_result_content);
595 //content = json_printer(j);
596 content = jsonFormatString(j);
599 content = jsonObjectGetString(omsg->_result_content);
601 buffer_add( resp_buffer, "\nReceived Data: " );
602 buffer_add( resp_buffer, content );
603 buffer_add( resp_buffer, "\n" );
608 buffer_add( resp_buffer, "\nReceived Exception:\nName: " );
609 buffer_add( resp_buffer, omsg->status_name );
610 buffer_add( resp_buffer, "\nStatus: " );
611 buffer_add( resp_buffer, omsg->status_text );
612 buffer_add( resp_buffer, "\nStatus: " );
615 sprintf( code, "%d", omsg->status_code );
616 buffer_add( resp_buffer, code );
621 omsg = osrf_app_session_request_recv( session, req_id, recv_timeout );
625 double end = get_timestamp_millis();
627 fprintf( less, resp_buffer->buf );
628 buffer_free( resp_buffer );
629 fprintf( less, "\n------------------------------------\n");
630 if( osrf_app_session_request_complete( session, req_id ))
631 fprintf(less, "Request Completed Successfully\n");
634 fprintf(less, "Request Time in seconds: %.6f\n", end - start );
635 fprintf(less, "------------------------------------\n");
639 osrf_app_session_request_finish( session, req_id );
640 osrf_app_session_disconnect( session );
641 osrf_app_session_destroy( session );
650 int handle_time( char* words[] ) {
657 printf( "%s\n", buf );
662 time_t epoch = (time_t)atoi( words[1] );
663 char* localtime = strdup( ctime( &epoch ) );
664 printf( "%s => %s", words[1], localtime );
676 int router_query_servers( char* router_server ) {
678 if( ! router_server || strlen(router_server) == 0 )
683 sprintf(rbuf,"router@%s/router", router_server );
685 transport_message* send =
686 message_init( "servers", NULL, NULL, rbuf, NULL );
687 message_set_router_info( send, NULL, NULL, NULL, "query", 0 );
689 client_send_message( client, send );
690 message_free( send );
692 transport_message* recv = client_recv( client, -1 );
694 fprintf(stderr, "NULL message received from router\n");
699 "---------------------------------------------------------------------------------\n"
700 "Received from 'server' query on %s\n"
701 "---------------------------------------------------------------------------------\n"
702 "original reg time | latest reg time | last used time | class | server\n"
703 "---------------------------------------------------------------------------------\n"
705 "---------------------------------------------------------------------------------\n"
706 , router_server, recv->body );
708 message_free( recv );
716 "---------------------------------------------------------------------------------\n"
718 "---------------------------------------------------------------------------------\n"
719 "help - Display this message\n"
720 "!<command> [args] - Forks and runs the given command in the shell\n"
721 "time - Prints the current time\n"
722 "time <timestamp> - Formats seconds since epoch into readable format\n"
723 "set <variable> <value> - set a srfsh variable (e.g. set pretty_print true )\n"
724 "print <variable> - Displays the value of a srfsh variable\n"
725 "---------------------------------------------------------------------------------\n"
727 "router query servers <server1 [, server2, ...]>\n"
728 " - Returns stats on connected services\n"
731 "request <service> <method> [ <json formatted string of params> ]\n"
732 " - Anything passed in will be wrapped in a json array,\n"
733 " so add commas if there is more than one param\n"
736 "relay <service> <method>\n"
737 " - Performs the requested query using the last received result as the param\n"
740 "math_bench <num_batches> [0|1|2]\n"
741 " - 0 means don't reconnect, 1 means reconnect after each batch of 4, and\n"
742 " 2 means reconnect after every request\n"
744 "introspect <service>\n"
745 " - prints the API for the service\n"
748 "---------------------------------------------------------------------------------\n"
749 " Commands for Open-ILS\n"
750 "---------------------------------------------------------------------------------\n"
751 "login <username> <password>\n"
752 " - Logs into the 'server' and displays the session id\n"
753 " - To view the session id later, enter: print login\n"
754 "---------------------------------------------------------------------------------\n"
757 "Note: long output is piped through 'less'. To search in 'less', type: /<search>\n"
758 "---------------------------------------------------------------------------------\n"
767 char* tabs(int count) {
768 growing_buffer* buf = buffer_init(24);
770 for(i=0;i!=count;i++)
771 buffer_add(buf, " ");
773 char* final = buffer_data( buf );
778 int handle_math( char* words[] ) {
780 return do_math( atoi(words[1]), 0 );
785 int do_math( int count, int style ) {
787 osrf_app_session* session = osrf_app_client_session_init( "opensrf.math" );
789 jsonObject* params = json_parse_string("[]");
790 jsonObjectPush(params,jsonNewObject("1"));
791 jsonObjectPush(params,jsonNewObject("2"));
793 char* methods[] = { "add", "sub", "mult", "div" };
794 char* answers[] = { "3", "-1", "2", "0.500000" };
796 float times[ count * 4 ];
797 memset(times,0,count*4);
800 for(k=0;k!=100;k++) {
807 fprintf(stderr,"\n\n");
811 for(i=0; i!= count; i++) {
814 for(j=0; j != 4; j++) {
818 double start = get_timestamp_millis();
819 int req_id = osrf_app_session_make_req( session, params, methods[j], 1, NULL );
820 osrf_message* omsg = osrf_app_session_request_recv( session, req_id, 5 );
821 double end = get_timestamp_millis();
823 times[(4*i) + j] = end - start;
827 if(omsg->_result_content) {
828 char* jsn = jsonObjectToJSON(omsg->_result_content);
829 if(!strcmp(jsn, answers[j]))
830 fprintf(stderr, "+");
832 fprintf(stderr, "\n![%s] - should be %s\n", jsn, answers[j] );
837 osrf_message_free(omsg);
839 } else { fprintf( stderr, "\nempty message for tt: %d\n", req_id ); }
841 osrf_app_session_request_finish( session, req_id );
844 osrf_app_session_disconnect( session );
847 fprintf(stderr,"\n");
851 osrf_app_session_disconnect( session );
854 osrf_app_session_destroy( session );
855 jsonObjectFree(params);
859 for(c=0; c!= count*4; c++)
862 float avg = total / (count*4);
863 fprintf(stderr, "\n Average round trip time: %f\n", avg );