inline int buf_del(struct buffer *b) { if( b->e == b->s ){ return -1; } b->s = buf_inc(b->s, b->size); return 0; }
inline int buf_add(struct buffer *b) { unsigned int tmp = buf_inc(b->e, b->size); if( tmp != b->s ){ b->e = tmp; return 0; } return -1; }
/* Puts the given char into the given terminal's buffer, moving the * cursor accordingly for control chars Returns 1 if char can be * echoed (non-control char), 0 otherwise */ static int vt_handle_char(virtterm_t *vt, char c) { KASSERT(NULL != vt); /* Start where the cursor currently is located */ int new_cursor = vt->vt_cursor; int ret = 0; switch (c) { case '\b': /* Move cursor back one space */ /* the user can't backspace past the beginning */ if (new_cursor != vt->vt_head) buf_dec(new_cursor); break; case '\r': new_cursor = (new_cursor / DISPLAY_WIDTH) * DISPLAY_WIDTH; break; /* In the next two cases, the cursor advances, and we * need to compare it with the end of the buffer to * determine whether or not to advance the buffer * tail */ case '\n': /* To beginning of next line */ new_cursor = next_row(new_cursor); goto handle_tail; default: /* Actually put a char into the buffer */ vt->vt_buf[new_cursor] = c; /* And increment */ buf_inc(new_cursor); ret = 1; handle_tail: /* Yuck */ if (circ_dist(new_cursor, vt->vt_cursor) >= circ_dist(vt->vt_tail, vt->vt_cursor)) { /* Current cursor pos past tail of the current * buffer, so advance tail */ int new_tail = next_row(new_cursor); /* Check for head adjusting (if we write * enough that the scroll buffer fills up, we * sacrifice chars near the head) */ if (circ_dist(vt->vt_tail, vt->vt_head) >= circ_dist(vt->vt_tail, new_tail)) { vt->vt_head = next_row(new_tail); } /* Remember to clear space we may have acquired */ if (vt->vt_tail <= new_tail) { memset(vt->vt_buf + vt->vt_tail, 0, new_tail - vt->vt_tail); } else { memset(vt->vt_buf + vt->vt_tail, 0, SCROLL_BUFSIZE - vt->vt_tail); memset(vt->vt_buf, 0, new_tail); } /* Finally, set the new tail */ vt->vt_tail = new_tail; } break; } vt->vt_cursor = new_cursor; return ret; }