00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "thd_iochan.h"
00034 #define COM_LENGTH 1000
00035
00036
00037
00038
00039 static char afni_host[128] = "." ;
00040 static char afni_name[128] = "\0" ;
00041 static int afni_port = 8099 ;
00042 static int afni_verbose = 0 ;
00043 static int DontWait = 0;
00044 static int N_com = 0;
00045 static int I_com = 0;
00046 static char *com[1024];
00047
00048
00049 int afni_io(void) ;
00050
00051
00052
00053
00054
00055
00056
00057 int main( int argc , char *argv[] )
00058 {
00059 int narg , ii;
00060
00061
00062
00063 if( argc == 2 && strncmp(argv[1],"-help",5) == 0 ){
00064 printf("Usage: plugout_drive [-host name] [-v]\n"
00065 "This program connects to AFNI and sends commands\n"
00066 " that the user specifies interactively or on command line\n"
00067 " over to AFNI to be executed.\n"
00068 "\n"
00069 "Options:\n"
00070 " -host name Means to connect to AFNI running on the\n"
00071 " computer 'name' using TCP/IP. The default is to\n"
00072 " connect on the current host using shared memory.\n"
00073 " -v Verbose mode.\n"
00074 " -port pp Use TCP/IP port number 'pp'. The default is\n"
00075 " 8099, but if two plugouts are running on the\n"
00076 " same computer, they must use different ports.\n"
00077 " -name sss Use the string 'sss' for the name that AFNI assigns\n"
00078 " to this plugout. The default is something stupid.\n"
00079 " -com 'ACTION DATA' Execute the following command. For example:\n"
00080 " -com 'SET_FUNCTION SomeFunction'\n"
00081 " will switch AFNI's function (overlay) to\n"
00082 " dataset with prefix SomeFunction. \n"
00083 " Make sure ACTION and DATA are together enclosed\n"
00084 " in one pair of single quotes.\n"
00085 " There are numerous actions listed in AFNI's\n"
00086 " README.driver file.\n"
00087 " You can use the option -com repeatedly. \n"
00088 " -quit Quit after you are done with all the -com commands.\n"
00089 " The default is for the program to wait for more\n"
00090 " commands to be typed at the terminal's prompt.\n"
00091 "\n"
00092 "NOTE:\n"
00093 "You will need to turn plugouts on in AFNI using one of the\n"
00094 "following methods: \n"
00095 " 1. Including '-yesplugouts' as an option on AFNI's command line\n"
00096 " 2. From AFNI: Define Datamode->Misc->Start Plugouts\n"
00097 " 3. Set environment variable AFNI_YESPLUGOUTS to YES in .afnirc\n"
00098 "Otherwise, AFNI won't be listening for a plugout connection.\n"
00099 "\n"
00100 ) ;
00101 exit(0) ;
00102 }
00103
00104
00105
00106 N_com = 0;
00107 DontWait = 0;
00108 narg = 1 ;
00109 while( narg < argc ){
00110
00111
00112
00113 if( strncmp(argv[narg],"-host",5) == 0 ){
00114 narg++ ;
00115 if( narg >= argc ){
00116 fprintf(stderr,"** -host needs a following name!\a\n"); exit(1);
00117 }
00118 strcpy( afni_host , argv[narg] ) ;
00119 narg++ ; continue ;
00120 }
00121
00122
00123
00124 if( strncmp(argv[narg],"-name",5) == 0 ){
00125 narg++ ;
00126 if( narg >= argc ){
00127 fprintf(stderr,"** -name needs a following string!\a\n"); exit(1);
00128 }
00129 strcpy( afni_name , argv[narg] ) ;
00130 narg++ ; continue ;
00131 }
00132
00133
00134
00135 if( strncmp(argv[narg],"-v",2) == 0 ){
00136 afni_verbose = 1 ;
00137 narg++ ; continue ;
00138 }
00139
00140
00141
00142 if( strncmp(argv[narg],"-port",4) == 0 ){
00143 narg++ ;
00144 if( narg >= argc ){
00145 fprintf(stderr,"** -port needs a following argument!\a\n"); exit(1);
00146 }
00147 afni_port = strtol( argv[narg] , NULL , 10 ) ;
00148 if( afni_port <= 1024 ){
00149 fprintf(stderr,"** -port needs an argument > 1024!\a\n"); exit(1);
00150 }
00151 if( strcmp(afni_host,".") == 0 ) strcpy(afni_host,"localhost") ;
00152 narg++ ; continue ;
00153 }
00154
00155
00156 if( strncmp(argv[narg],"-com",4) == 0 ){
00157 narg++ ;
00158 if( narg >= argc ){
00159 fprintf(stderr,"** -com needs a following argument!\a\n"); exit(1);
00160 }
00161
00162 if (argv[narg] && strlen(argv[narg]) >= COM_LENGTH) {
00163 fprintf(stderr,"** Command length must be smaller than %d characters.\n", COM_LENGTH);
00164 }
00165
00166 if (N_com < 1024) {
00167 com[N_com] = argv[narg];
00168 ++N_com;
00169 } else {
00170 fprintf(stderr,"** Only 1024 -com options allowed. Are you nuts?\a\n"); exit(1);
00171 }
00172
00173 narg++ ; continue ;
00174 }
00175
00176
00177 if( strncmp(argv[narg],"-quit",5) == 0 ){
00178 DontWait = 1 ;
00179 narg++ ; continue ;
00180 }
00181
00182
00183
00184 fprintf(stderr,"** Unrecognized option: %s\a\n",argv[narg]) ;
00185 exit(1) ;
00186 }
00187
00188 if (DontWait && !N_com) {
00189 fprintf(stderr,"** WARNING: -quit option is meaningless without -com option.\n");
00190 DontWait = 0;
00191 }
00192
00193
00194
00195 while( 1 ){
00196 ii = afni_io() ;
00197 if( ii < 0 ) exit(0) ;
00198 iochan_sleep(100) ;
00199 }
00200
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 #define AFNI_OPEN_CONTROL_MODE 1
00227 #define AFNI_WAIT_CONTROL_MODE 2
00228 #define AFNI_OPEN_DATA_MODE 3
00229 #define AFNI_WAIT_DATA_MODE 4
00230 #define AFNI_CONTINUE_MODE 5
00231
00232
00233
00234 #define POACKSIZE 4
00235
00236 #define PO_ACK_BAD(ic) iochan_sendall( (ic) , "BAD" , POACKSIZE )
00237 #define PO_ACK_OK(ic) iochan_sendall( (ic) , "OK!" , POACKSIZE )
00238 #define PO_SEND(ic,str) iochan_sendall( (ic) , (str) , strlen((str))+1 )
00239
00240
00241 int afni_io(void)
00242 {
00243 static int afni_mode = AFNI_OPEN_CONTROL_MODE ;
00244 static IOCHAN * afni_ioc = NULL ;
00245 int ii ;
00246
00247
00248
00249
00250
00251 if( afni_mode <= 0 ) return -1 ;
00252
00253
00254
00255
00256 if( afni_mode == AFNI_OPEN_CONTROL_MODE ){
00257 char afni_iocname[128] ;
00258
00259
00260
00261
00262 if( strcmp(afni_host,".") == 0 )
00263 sprintf( afni_iocname , "tcp:%s:7955" , "localhost" );
00264 else
00265 sprintf( afni_iocname , "tcp:%s:7955" , afni_host ) ;
00266
00267 afni_ioc = iochan_init( afni_iocname , "create" ) ;
00268 if( afni_ioc == NULL ){
00269 fprintf(stderr,
00270 "** Can't create control channel %s to AFNI!\n",afni_iocname) ;
00271 afni_mode = 0 ;
00272 return -1 ;
00273 }
00274 afni_mode = AFNI_WAIT_CONTROL_MODE ;
00275 if( afni_verbose )
00276 fprintf(stderr,"++ AFNI control channel created\n") ;
00277 }
00278
00279
00280
00281
00282 if( afni_mode == AFNI_WAIT_CONTROL_MODE ){
00283 ii = iochan_writecheck( afni_ioc , 5 ) ;
00284
00285
00286
00287
00288
00289
00290 if( ii < 0 ){
00291 fprintf(stderr,"** Control channel to AFNI failed!\a\n") ;
00292 IOCHAN_CLOSE(afni_ioc) ;
00293 afni_mode = 0 ;
00294 return -1 ;
00295 } else if( ii > 0 ){
00296 afni_mode = AFNI_OPEN_DATA_MODE ;
00297 if( afni_verbose )
00298 fprintf(stderr,"++ AFNI control channel connected\n");
00299 } else {
00300 return 0 ;
00301 }
00302 }
00303
00304
00305
00306
00307 if( afni_mode == AFNI_OPEN_DATA_MODE ){
00308 char afni_iocname[128] ;
00309 char afni_buf[256] ;
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 if( strcmp(afni_host,".") == 0 )
00321 strcpy( afni_iocname , "shm:test_plugout:1K+1K" ) ;
00322 else
00323 sprintf( afni_iocname , "tcp:%s:%d" , afni_host , afni_port ) ;
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 if( afni_name[0] == '\0' ) strcpy(afni_name,"ManOfLaMancha") ;
00334
00335 sprintf( afni_buf , "PONAME %s\n"
00336 "IOCHAN %s" ,
00337 afni_name , afni_iocname ) ;
00338
00339 if( afni_verbose )
00340 fprintf(stderr,"++ Sending control information to AFNI\n") ;
00341
00342
00343
00344 ii = iochan_sendall( afni_ioc , afni_buf , strlen(afni_buf)+1 ) ;
00345
00346
00347
00348
00349 if( ii < 0 ){
00350 fprintf(stderr,"** Transmission of control data to AFNI failed!\a\n") ;
00351 IOCHAN_CLOSE(afni_ioc) ;
00352 afni_mode = 0 ;
00353 return -1 ;
00354
00355 } else {
00356
00357
00358
00359 ii = iochan_recvall( afni_ioc , afni_buf , POACKSIZE ) ;
00360 IOCHAN_CLOSE(afni_ioc) ;
00361
00362 if( ii < 0 || strncmp(afni_buf,"OK!",3) != 0 ){
00363 fprintf(stderr,"** AFNI didn't like control information!\a\n") ;
00364 afni_mode = 0 ;
00365 return -1 ;
00366 }
00367
00368
00369
00370 afni_ioc = iochan_init( afni_iocname , "create" ) ;
00371 if( afni_ioc == NULL ){
00372 fprintf(stderr,
00373 "** Can't open data channel %s to AFNI!\a\n",afni_iocname) ;
00374 afni_mode = 0 ;
00375 return -1 ;
00376 } else {
00377 afni_mode = AFNI_WAIT_DATA_MODE ;
00378 if( afni_verbose ) fprintf(stderr,"++ AFNI data channel created\n") ;
00379 }
00380 }
00381 }
00382
00383
00384
00385
00386 if( afni_mode == AFNI_WAIT_DATA_MODE ){
00387
00388 ii = iochan_goodcheck( afni_ioc , 5 ) ;
00389 if( ii < 0 ){
00390 fprintf(stderr,
00391 "** AFNI data channel aborted before any data was sent!\a\n") ;
00392 IOCHAN_CLOSE( afni_ioc ) ;
00393 afni_mode = 0 ;
00394 return -1 ;
00395 } else if( ii > 0 ){
00396 afni_mode = AFNI_CONTINUE_MODE ;
00397 if( afni_verbose ) fprintf(stderr,"++ AFNI data channel is open\n") ;
00398 } else {
00399 return 0 ;
00400 }
00401 }
00402
00403
00404
00405
00406
00407 if( afni_mode == AFNI_CONTINUE_MODE ){
00408 char cmd_buf[COM_LENGTH] , afni_buf[COM_LENGTH+56];
00409
00410 if( I_com < N_com ){
00411 strcpy(afni_buf, "DRIVE_AFNI ") ;
00412 strcat(afni_buf, com[I_com] ) ; strcpy(cmd_buf,com[I_com]) ;
00413 I_com++ ;
00414 } else {
00415 if (DontWait) exit(0);
00416
00417
00418 printf("Enter command: ") ; fflush(stdout) ; fgets(cmd_buf,COM_LENGTH,stdin) ;
00419
00420
00421
00422 strcpy(afni_buf,"DRIVE_AFNI ") ;
00423 strcat(afni_buf,cmd_buf) ;
00424 }
00425
00426
00427
00428 ii = iochan_sendall( afni_ioc , afni_buf , strlen(afni_buf)+1 ) ;
00429
00430 if( strcmp(cmd_buf,"QUIT") == 0 ){
00431 iochan_sleep(222) ; exit(0) ;
00432 }
00433
00434 if( ii > 0 ){
00435 ii = iochan_recvall( afni_ioc , afni_buf , POACKSIZE ) ;
00436 if( ii > 0 && afni_verbose )
00437 printf("++ AFNI response string: %s\n",afni_buf) ;
00438 }
00439
00440 if( ii < 0 ){
00441 fprintf(stderr,"** AFNI data channel aborted!\a\n") ;
00442 IOCHAN_CLOSE(afni_ioc) ;
00443 afni_mode = 0 ;
00444 return -1 ;
00445 }
00446 return 0 ;
00447 }
00448
00449 return 0 ;
00450 }