jcs
/subtext
/amendments
/256
logger: Better TE overflow calculation, don't allow closing
jcs made amendment 256 over 2 years ago
--- logger.c Fri Sep 16 21:17:37 2022
+++ logger.c Fri Sep 23 23:58:06 2022
@@ -27,7 +27,6 @@ void logger_key_down(struct focusable *focusable, Even
void logger_mouse_down(struct focusable *focusable, EventRecord *event);
void logger_update(struct focusable *focusable, EventRecord *event);
void logger_resume(struct focusable *focusable, EventRecord *event);
-void logger_close(struct focusable *focusable, EventRecord *event);
bool logger_quit(struct focusable *focusable);
void logger_flush_buffer(struct logger *logger);
@@ -71,7 +70,7 @@ logger_init(void)
focusable->cookie = logger;
focusable->mouse_down = logger_mouse_down;
focusable->update = logger_update;
- focusable->close = logger_close;
+ focusable->quit = logger_quit;
focusable->resume = logger_resume;
add_focusable(focusable);
logger->focusable = focusable;
@@ -131,14 +130,16 @@ logger_resume(struct focusable *focusable, EventRecord
InvalRect(logger->win->visRgn);
}
-void
-logger_close(struct focusable *focusable, EventRecord *event)
+bool
+logger_quit(struct focusable *focusable)
{
struct logger *logger = (struct logger *)(focusable->cookie);
destroy_focusable(focusable);
xfree(&logger->buffered_logs);
xfree(&logger);
+
+ return true;
}
void
@@ -316,12 +317,17 @@ logger_flush_buffer(struct logger *logger)
RgnHandle savergn;
Rect zerorect = { 0, 0, 0, 0 };
GrafPtr old_port;
- short line_height = 0;
+ short line_height = 0, new_lines = 1, n;
char *buf;
if (logger->buffered_logs_len == 0)
return;
-
+
+ for (n = 0; n < logger->buffered_logs_len; n++) {
+ if (logger->buffered_logs[n] == '\r')
+ new_lines++;
+ }
+
line_height = LOGGER_FONT_SIZE + 3;
GetPort(&old_port);
@@ -332,16 +338,18 @@ logger_flush_buffer(struct logger *logger)
HLock(logger->messages_te);
/* too many lines */
- if ((*(logger->messages_te))->nLines >=
- (nitems((*(logger->messages_te))->lineStarts) - 10))
+ if ((unsigned long)(*(logger->messages_te))->nLines >=
+ (nitems((*(logger->messages_te))->lineStarts) - new_lines))
goto te_overflow;
/* too many characters */
- if ((*(logger->messages_te))->teLength >= (SHRT_MAX - 500))
+ if ((unsigned long)(*(logger->messages_te))->teLength >=
+ (SHRT_MAX - logger->buffered_logs_len))
goto te_overflow;
- /* rect of all lines is too tall */
- if ((*(logger->messages_te))->nLines * line_height >= (SHRT_MAX - 100))
+ /* destRect of all lines is too tall */
+ if ((unsigned long)(*(logger->messages_te))->nLines * line_height >=
+ (SHRT_MAX - new_lines))
goto te_overflow;
goto no_overflow;
@@ -353,7 +361,7 @@ te_overflow:
ClipRect(&zerorect);
/* select some lines at the start, delete them */
- TESetSelect(0, (*(logger->messages_te))->lineStarts[5],
+ TESetSelect(0, (*(logger->messages_te))->lineStarts[new_lines * 2],
logger->messages_te);
TEDelete(logger->messages_te);
@@ -378,8 +386,12 @@ no_overflow:
}
TEInsert(buf, logger->buffered_logs_len, logger->messages_te);
+
+ if ((unsigned long)(*(logger->messages_te))->nLines >=
+ nitems((*(logger->messages_te))->lineStarts))
+ warn("te overflow!");
- TEPinScroll(0, -INT_MAX, logger->messages_te);
+ TEPinScroll(0, -SHRT_MAX, logger->messages_te);
SetCtlValue(logger->messages_scroller,
GetCtlMax(logger->messages_scroller));
UpdateScrollbarForTE(logger->win, logger->messages_scroller,