7
7
//
8
8
// MIT License
9
9
//
10
- // Copyright (c) 2009-2020 Michael Truog <mjtruog at protonmail dot com>
10
+ // Copyright (c) 2009-2023 Michael Truog <mjtruog at protonmail dot com>
11
11
//
12
12
// Permission is hereby granted, free of charge, to any person obtaining a
13
13
// copy of this software and associated documentation files (the "Software"),
@@ -810,37 +810,46 @@ int GEPD::consume_stream(int fd, short & revents,
810
810
return GEPD::ExitStatus::poll_NVAL;
811
811
revents = 0 ;
812
812
813
+ bool flush = false ;
814
+ size_t i_flush = 0 ;
813
815
ssize_t left = stream.size () - i;
814
- ssize_t readBytes;
815
- while ((readBytes = read (fd, &stream[i], left)) == left &&
816
- stream.grow ())
816
+ if (left == 0 )
817
817
{
818
- i += left;
819
- left = stream.size () - i;
820
- bool ready = true ;
821
- data_ready (fd, ready);
822
- if (ready == false )
823
- break ;
818
+ // stream is full, flush it
819
+ flush = true ;
820
+ i_flush = i - 1 ;
824
821
}
825
- if (readBytes == 0 && i == 0 )
826
- return GEPD::ExitStatus::success;
827
- else if (readBytes == -1 )
828
- return errno_read ();
829
- i += readBytes; // i is the next index to read at, always
830
-
831
- // only send stderr output before the last newline character
832
- bool foundNewline = false ;
833
- size_t iNewline = 0 ;
834
- for (ssize_t j = i - 1 ; ! foundNewline && j >= 0 ; --j)
822
+ else
835
823
{
836
- if (stream[j] == ' \n ' )
824
+ ssize_t read_bytes;
825
+ while ((read_bytes = read (fd, &stream[i], left)) == left &&
826
+ stream.grow ())
827
+ {
828
+ i += left;
829
+ left = stream.size () - i;
830
+ bool ready = true ;
831
+ data_ready (fd, ready);
832
+ if (ready == false )
833
+ break ;
834
+ }
835
+ if (read_bytes == 0 && i == 0 )
836
+ return GEPD::ExitStatus::success;
837
+ else if (read_bytes == -1 )
838
+ return errno_read ();
839
+ i += read_bytes; // i is the next index to read at, always
840
+
841
+ // send stdout/stderr output before the last newline character
842
+ for (ssize_t j = i - 1 ; ! flush && j >= 0 ; --j)
837
843
{
838
- foundNewline = true ;
839
- iNewline = j;
844
+ if (stream[j] == ' \n ' )
845
+ {
846
+ flush = true ;
847
+ i_flush = j;
848
+ }
840
849
}
841
850
}
842
851
843
- if (foundNewline )
852
+ if (flush )
844
853
{
845
854
int index = sizeof (OUTPUT_PREFIX_TYPE);
846
855
if (ei_encode_version (send_buffer.get <char >(), &index ))
@@ -851,25 +860,25 @@ int GEPD::consume_stream(int fd, short & revents,
851
860
return GEPD::ExitStatus::ei_encode_error;
852
861
if (ei_encode_ulong (send_buffer.get <char >(), &index , pid))
853
862
return GEPD::ExitStatus::ei_encode_error;
854
- if (send_buffer.reserve (index + (iNewline + 1 ) + 1 ) == false )
863
+ if (send_buffer.reserve (index + (i_flush + 1 ) + 1 ) == false )
855
864
return GEPD::ExitStatus::write_overflow;
856
865
if (ei_encode_string_len (send_buffer.get <char >(), &index ,
857
- stream.get <char >(), iNewline + 1 ))
866
+ stream.get <char >(), i_flush + 1 ))
858
867
return GEPD::ExitStatus::ei_encode_error;
859
868
int status;
860
869
if ((status = write_cmd (send_buffer, index -
861
870
sizeof (OUTPUT_PREFIX_TYPE))))
862
871
return status;
863
872
// keep any data not yet sent (waiting for a newline)
864
- if (iNewline == i - 1 )
873
+ if (i_flush == i - 1 )
865
874
{
866
875
i = 0 ;
867
876
}
868
877
else
869
878
{
870
- size_t const remainingBytes = i - iNewline - 1 ;
871
- stream.move (iNewline + 1 , remainingBytes , 0 );
872
- i = remainingBytes ;
879
+ size_t const remaining_bytes = i - i_flush - 1 ;
880
+ stream.move (i_flush + 1 , remaining_bytes , 0 );
881
+ i = remaining_bytes ;
873
882
}
874
883
}
875
884
return GEPD::ExitStatus::success;
@@ -884,8 +893,9 @@ int GEPD::flush_stream(int fd, short revents,
884
893
return GEPD::ExitStatus::success;
885
894
886
895
ssize_t left = stream.size () - i;
887
- ssize_t readBytes;
888
- while ((readBytes = read (fd, &stream[i], left)) == left &&
896
+ assert (left > 0 );
897
+ ssize_t read_bytes;
898
+ while ((read_bytes = read (fd, &stream[i], left)) == left &&
889
899
stream.grow ())
890
900
{
891
901
i += left;
@@ -895,10 +905,10 @@ int GEPD::flush_stream(int fd, short revents,
895
905
if (ready == false )
896
906
break ;
897
907
}
898
- if (readBytes == 0 && i == 0 )
908
+ if (read_bytes == 0 && i == 0 )
899
909
return GEPD::ExitStatus::success;
900
- else if (readBytes != -1 )
901
- i += readBytes ; // i is the next index to read at, always
910
+ else if (read_bytes != -1 )
911
+ i += read_bytes ; // i is the next index to read at, always
902
912
903
913
size_t const total = i - 1 ;
904
914
i = 0 ;
0 commit comments