Skip to content

Commit

Permalink
main: rejig control token descriptor handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mofosyne committed May 21, 2024
1 parent 90456a5 commit 50048f5
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions examples/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,11 @@ int main(int argc, char ** argv) {
exit(1);
}

const bool control_token_allowed_on_standard_stream = !params.conversation && sparams.grammar.empty();

#ifndef _MSC_VER
const bool control_token_descriptor_is_attached = fcntl(CONTROL_TOKEN_FILENO, F_GETFL) != -1;
if (!control_token_descriptor_is_attached && !params.conversation && sparams.grammar.empty()) {
if (control_token_allowed_on_standard_stream && !control_token_descriptor_is_attached) {
// Control Token File Descriptor has nothing attached to it so make control token file descriptor be an alias of stdout
// This is not done however if we are in conversation mode or grammar mode as that is typically discarded
dup2(STDOUT_FILENO, CONTROL_TOKEN_FILENO);
Expand Down Expand Up @@ -759,19 +761,19 @@ int main(int argc, char ** argv) {
fflush(stdout);
fprintf(stdout, "%s", token_str.c_str());
} else if (!params.ctrl_token_no_out) {
if (!params.conversation && sparams.grammar.empty())
{
// Stream Control Token To Special Token Output. Useful for debugging control token behaviour
fflush(stdout);
fprintf(stdout, "%s", token_str.c_str());
}
#ifndef _MSC_VER
else {
if (control_token_descriptor_is_attached) {
// Stream Control Token To Special Token Output. Useful for debugging control token behaviour
ssize_t result = write(CONTROL_TOKEN_FILENO, token_str.c_str(), token_str.length());
(void) result;
}
} else
#endif
if (control_token_allowed_on_standard_stream)
{
// Stream Control Token To Standard Output Stream
fflush(stdout);

This comment has been minimized.

Copy link
@mrdomino

mrdomino May 21, 2024

You want to fflush before the write to the control fd, not before the fprintfs to stdout. fflush will cause the stdout buffer to be written out to the kernel, which will ensure that the write call will go in the right place.

Basically think of the control tokens as acting like std::endl if you're familiar with that.

This comment has been minimized.

Copy link
@mofosyne

mofosyne May 21, 2024

Author

thanks, sorted it out now. Have a check of the updated PR

fprintf(stdout, "%s", token_str.c_str());
}
}
// Record Displayed Tokens To Log
// Note: Generated tokens are created one by one hence this check
Expand Down

0 comments on commit 50048f5

Please sign in to comment.