简体   繁体   中英

TCL hang on gets

I have a parent C process that creates a child tcl process and re-directs the stdin/stdout of the child to interact with the parent. It seems to work correctly but there is a part where the child never receives what the parent is receiving. If any one can spot the error that causes this I would appreciate it.

Parent sending code:

fprintf(write_to, "%s",outmsg.data); //Received by child
bzero ((char *) &inmsg, sizeof (inmsg));
getmsg (sock, &inmsg);
if (ntohl (inmsg.type) != MATCH)
  protocol_error (MATCH, &inmsg);
strncpy (opphandle, inmsg.data, maxSize);
opphandle[maxSize] = '\0';
fprintf (write_to,"%s",opphandle); //Received by child
fprintf (write_to,"%s",inmsg.board); //Not Received by child

both board and data fields in inmsg are of type char[].

Child recieving code:

set handle [gets stdin] //Received
set ohandle [gets stdin]//Received
set myShape [gets stdin]//Not received

Note, when the parent is killed with ctrl+C the child thinks it received input for myShap and then executes with myShape being ctrl+C.

Again, stdin and stdout of the child have been redirected so that stdin comes from the parent and stdout goes to the parent.

The gets command waits for newline.

It's probable that both outmsg.data and opphandle ends in newlines ( "\\n" or "\\r" or "\\r\\n" ). It would explain why the first two gets works.

Check to see if inmsg.board ends in a newline. If not, you can simply do:

fprintf (write_to,"%s\n",inmsg.board);

For robustness, it's better to strip strings of trailing newlines before sending. Then add the newline again in the fprintf() like the example above. This is to avoid cases where extra newlines accidentally causing your tcl script to read an empty line as data.

An alternative is to implement a proper scanner/parser in the tcl script to ignore empty lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM