Skip to content

Commit 50048f5

Browse files
committed
main: rejig control token descriptor handling
1 parent 90456a5 commit 50048f5

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

examples/main/main.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,11 @@ int main(int argc, char ** argv) {
530530
exit(1);
531531
}
532532

533+
const bool control_token_allowed_on_standard_stream = !params.conversation && sparams.grammar.empty();
534+
533535
#ifndef _MSC_VER
534536
const bool control_token_descriptor_is_attached = fcntl(CONTROL_TOKEN_FILENO, F_GETFL) != -1;
535-
if (!control_token_descriptor_is_attached && !params.conversation && sparams.grammar.empty()) {
537+
if (control_token_allowed_on_standard_stream && !control_token_descriptor_is_attached) {
536538
// Control Token File Descriptor has nothing attached to it so make control token file descriptor be an alias of stdout
537539
// This is not done however if we are in conversation mode or grammar mode as that is typically discarded
538540
dup2(STDOUT_FILENO, CONTROL_TOKEN_FILENO);
@@ -759,19 +761,19 @@ int main(int argc, char ** argv) {
759761
fflush(stdout);
760762
fprintf(stdout, "%s", token_str.c_str());
761763
} else if (!params.ctrl_token_no_out) {
762-
if (!params.conversation && sparams.grammar.empty())
763-
{
764-
// Stream Control Token To Special Token Output. Useful for debugging control token behaviour
765-
fflush(stdout);
766-
fprintf(stdout, "%s", token_str.c_str());
767-
}
768764
#ifndef _MSC_VER
769-
else {
765+
if (control_token_descriptor_is_attached) {
770766
// Stream Control Token To Special Token Output. Useful for debugging control token behaviour
771767
ssize_t result = write(CONTROL_TOKEN_FILENO, token_str.c_str(), token_str.length());
772768
(void) result;
773-
}
769+
} else
774770
#endif
771+
if (control_token_allowed_on_standard_stream)
772+
{
773+
// Stream Control Token To Standard Output Stream
774+
fflush(stdout);
775+
fprintf(stdout, "%s", token_str.c_str());
776+
}
775777
}
776778
// Record Displayed Tokens To Log
777779
// Note: Generated tokens are created one by one hence this check

0 commit comments

Comments
 (0)