19
19
#include < signal.h>
20
20
#include < unistd.h>
21
21
#include < fcntl.h>
22
- #define CONTROL_TOKEN_FILENO ( 3 )
22
+ #define SPECIAL_FILENO 3
23
23
#elif defined (_WIN32)
24
24
#define WIN32_LEAN_AND_MEAN
25
25
#ifndef NOMINMAX
@@ -123,7 +123,11 @@ int main(int argc, char ** argv) {
123
123
#ifndef _MSC_VER
124
124
// Check if we have an external attachment to a file descriptor for out of band control tokens (e.g. bash `3>/dev/null` )
125
125
// Placed here to avoid file descriptor being polluted by gpt_params_parse() opening files
126
- const bool control_token_file_descriptor_is_attached = fcntl (CONTROL_TOKEN_FILENO, F_GETFL) != -1 ;
126
+ const bool control_token_file_descriptor_is_attached = fcntl (SPECIAL_FILENO, F_GETFL) != -1 ;
127
+ if (!control_token_file_descriptor_is_attached) {
128
+ // Duplicate stdout file descriptor to control token file descriptor to merge the two streams
129
+ dup2 (STDOUT_FILENO, SPECIAL_FILENO);
130
+ }
127
131
#endif
128
132
129
133
gpt_params params;
@@ -136,14 +140,6 @@ int main(int argc, char ** argv) {
136
140
137
141
const bool control_token_allowed_on_standard_stream = !params.conversation && sparams.grammar .empty ();
138
142
139
- #ifndef _MSC_VER
140
- // Merge normal token stream and control token streams together only if not in conversation or grammar mode
141
- if (control_token_allowed_on_standard_stream && !control_token_file_descriptor_is_attached) {
142
- // Duplicate stdout file descriptor to control token file descriptor to merge the two streams
143
- dup2 (STDOUT_FILENO, CONTROL_TOKEN_FILENO);
144
- }
145
- #endif
146
-
147
143
#ifndef LOG_DISABLE_LOGS
148
144
log_set_target (log_filename_generator (" main" , " log" ));
149
145
LOG_TEE (" Log start\n " );
@@ -768,8 +764,7 @@ int main(int argc, char ** argv) {
768
764
#ifndef _MSC_VER
769
765
if (control_token_file_descriptor_is_attached) {
770
766
// Stream Control Token To Special Token Output. Useful for debugging control token behaviour
771
- fflush (stdout); // Ensure control token is always appended to stdout stream
772
- (void )! write (CONTROL_TOKEN_FILENO, token_str.c_str (), token_str.length ());
767
+ (void )! write (SPECIAL_FILENO, token_str.c_str (), token_str.length ());
773
768
} else
774
769
#endif
775
770
if (control_token_allowed_on_standard_stream)
@@ -788,8 +783,8 @@ int main(int argc, char ** argv) {
788
783
output_tokens.push_back (id);
789
784
output_ss << token_str;
790
785
}
786
+ fflush (stdout);
791
787
}
792
- fflush (stdout);
793
788
}
794
789
795
790
// reset color to default if there is no pending user input
0 commit comments