1 #include "niml.h"
2 #include "afni_environ.h"
3 
4 #undef  AFNI_SERVER
5 #undef  AFNI_REQUEST
6 
7 /*! Stream name to connect to AFNI server. */
8 
9 #define AFNI_SERVER  "tcp:afni.nimh.nih.gov:80"
10 
11 /*! Format for AFNI logging request. */
12 
13 #define AFNI_REQUEST "HEAD /AFNIlogpath HTTP/1.0\r\n"  \
14                      "User-Agent: %s\r\n\r\n"
15 
16 /*------------------------------------------------------------------------*/
17 /*! Log the input string to the AFNI server.  Sends an HTTP request with
18     the input string as the 'user agent', which will be saved in the
19     Web server logs.
20 --------------------------------------------------------------------------*/
21 
AFNI_serverlog(char * str)22 void AFNI_serverlog( char *str )
23 {
24    pid_t child_pid ;
25    NI_stream ns ;
26    int nbuf=0 , jj ;
27    char *sbuf , *rbuf , *ss ;
28 
29    if( str == NULL || *str == '\0'         ) return ;
30    if( ! AFNI_yesenv("AFNI_VERSION_CHECK") ) return ;
31 
32    /*-- start the child process --*/
33 
34    child_pid = fork() ;
35    if( child_pid != (pid_t)0 ) return ;   /* parent is done */
36 
37    /*-- child will never return alive! --*/
38 
39    /* open stream to server */
40 
41    ns = NI_stream_open( AFNI_SERVER , "w" ) ;
42    if( ns == (NI_stream)NULL ) _exit(0) ;
43 
44    /* copy input string, replace bad chars with spaces */
45 
46    sbuf = strdup(str) ;
47    for( ss=sbuf ; *ss != '\0' ; ss++ ) if( !isgraph(*ss) ) *ss = ' ' ;
48 
49    /* truncate trailing spaces */
50 
51    nbuf = strlen(sbuf) ;
52    for( ss=sbuf+(nbuf-1) ; isspace(*ss) ; ss-- ) *ss = '\0' ;
53 
54    /* build request to AFNI server */
55 
56    nbuf = strlen(sbuf)+strlen(AFNI_REQUEST)+32 ;
57    rbuf = (char *)malloc(nbuf) ;
58    sprintf(rbuf,AFNI_REQUEST,sbuf) ;
59 
60    /* wait for stream to get good for writing */
61 
62    jj = NI_stream_writecheck( ns , 1234 ) ;
63    if( jj < 1 ) _exit(0) ;
64 
65    /* send the request */
66 
67    NI_stream_write( ns , rbuf , strlen(rbuf) ) ;
68 
69    /* don't read response: wait a decent interval, close connection, quit */
70 
71    NI_sleep(1) ; NI_stream_closenow(ns) ; _exit(0) ;
72 }
73