Mercurial > hg > xemacs-beta
comparison src/lstream.c @ 430:a5df635868b2 r21-2-23
Import from CVS: tag r21-2-23
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:29:08 +0200 |
parents | 3ecd8885ac67 |
children | 8de8e3f6228a |
comparison
equal
deleted
inserted
replaced
429:8305706cbb93 | 430:a5df635868b2 |
---|---|
390 up until they can be written, so as to avoid losing data. */ | 390 up until they can be written, so as to avoid losing data. */ |
391 | 391 |
392 static size_t | 392 static size_t |
393 Lstream_adding (Lstream *lstr, size_t num, int force) | 393 Lstream_adding (Lstream *lstr, size_t num, int force) |
394 { | 394 { |
395 /* Compute the size that the outbuffer needs to be after the | 395 size_t size = num + lstr->out_buffer_ind; |
396 chars are added. */ | 396 |
397 size_t size_needed = max (lstr->out_buffer_size, | 397 if (size <= lstr->out_buffer_size) |
398 num + lstr->out_buffer_ind); | 398 return num; |
399 | |
399 /* Maybe chop it down so that we don't buffer more characters | 400 /* Maybe chop it down so that we don't buffer more characters |
400 than our advertised buffering size. */ | 401 than our advertised buffering size. */ |
401 if (!force) | 402 if ((size > lstr->buffering_size) && !force) |
402 size_needed = min (lstr->buffering_size, size_needed); | 403 { |
403 DO_REALLOC (lstr->out_buffer, lstr->out_buffer_size, | 404 size = lstr->buffering_size; |
404 size_needed, unsigned char); | 405 /* There might be more data buffered than the buffering size. */ |
405 /* There might be more data buffered than the buffering size, | 406 if (size <= lstr->out_buffer_ind) |
406 so make sure we don't return a negative number here. */ | 407 return 0; |
407 return max (0, min (num, size_needed - lstr->out_buffer_ind)); | 408 } |
409 | |
410 DO_REALLOC (lstr->out_buffer, lstr->out_buffer_size, size, unsigned char); | |
411 | |
412 return size - lstr->out_buffer_ind; | |
408 } | 413 } |
409 | 414 |
410 /* Like Lstream_write(), but does not handle line-buffering correctly. */ | 415 /* Like Lstream_write(), but does not handle line-buffering correctly. */ |
411 | 416 |
412 static ssize_t | 417 static ssize_t |
932 { | 937 { |
933 ssize_t nread; | 938 ssize_t nread; |
934 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | 939 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
935 if (str->end_pos >= 0) | 940 if (str->end_pos >= 0) |
936 size = min (size, (size_t) (str->end_pos - str->current_pos)); | 941 size = min (size, (size_t) (str->end_pos - str->current_pos)); |
937 nread = (str->allow_quit ? read_allowing_quit : read) (str->fd, data, size); | 942 nread = str->allow_quit ? |
943 read_allowing_quit (str->fd, data, size) : | |
944 read (str->fd, data, size); | |
938 if (nread > 0) | 945 if (nread > 0) |
939 str->current_pos += nread; | 946 str->current_pos += nread; |
940 return nread; | 947 return nread; |
941 } | 948 } |
942 | 949 |
985 size = ptr - data; | 992 size = ptr - data; |
986 } | 993 } |
987 | 994 |
988 /**** start of non-PTY-crap ****/ | 995 /**** start of non-PTY-crap ****/ |
989 if (size > 0) | 996 if (size > 0) |
990 retval = ((str->allow_quit ? write_allowing_quit : write) | 997 retval = str->allow_quit ? |
991 (str->fd, data, size)); | 998 write_allowing_quit (str->fd, data, size) : |
999 write (str->fd, data, size); | |
992 else | 1000 else |
993 retval = 0; | 1001 retval = 0; |
994 if (retval < 0 && errno_would_block_p (errno) && str->blocked_ok) | 1002 if (retval < 0 && errno_would_block_p (errno) && str->blocked_ok) |
995 { | 1003 { |
996 str->blocking_error_p = 1; | 1004 str->blocking_error_p = 1; |
1009 and flush with an EOF if necessary. Be careful to | 1017 and flush with an EOF if necessary. Be careful to |
1010 keep track of write errors as we go along and look | 1018 keep track of write errors as we go along and look |
1011 out for EWOULDBLOCK. */ | 1019 out for EWOULDBLOCK. */ |
1012 if (str->chars_sans_newline >= str->pty_max_bytes) | 1020 if (str->chars_sans_newline >= str->pty_max_bytes) |
1013 { | 1021 { |
1014 ssize_t retval2 = ((str->allow_quit ? write_allowing_quit : write) | 1022 ssize_t retval2 = str->allow_quit ? |
1015 (str->fd, &str->eof_char, 1)); | 1023 write_allowing_quit (str->fd, &str->eof_char, 1) : |
1024 write (str->fd, &str->eof_char, 1); | |
1025 | |
1016 if (retval2 > 0) | 1026 if (retval2 > 0) |
1017 str->chars_sans_newline = 0; | 1027 str->chars_sans_newline = 0; |
1018 else if (retval2 < 0) | 1028 else if (retval2 < 0) |
1019 { | 1029 { |
1020 /* Error writing the EOF char. If nothing got written, | 1030 /* Error writing the EOF char. If nothing got written, |
1040 first byte is a newline, we'd get stuck never writing anything | 1050 first byte is a newline, we'd get stuck never writing anything |
1041 in pty-flushing mode. */ | 1051 in pty-flushing mode. */ |
1042 if (need_newline) | 1052 if (need_newline) |
1043 { | 1053 { |
1044 Bufbyte nl = '\n'; | 1054 Bufbyte nl = '\n'; |
1045 ssize_t retval2 = ((str->allow_quit ? write_allowing_quit : write) | 1055 ssize_t retval2 = str->allow_quit ? |
1046 (str->fd, &nl, 1)); | 1056 write_allowing_quit (str->fd, &nl, 1) : |
1057 write (str->fd, &nl, 1); | |
1058 | |
1047 if (retval2 > 0) | 1059 if (retval2 > 0) |
1048 { | 1060 { |
1049 str->chars_sans_newline = 0; | 1061 str->chars_sans_newline = 0; |
1050 retval++; | 1062 retval++; |
1051 } | 1063 } |