1 /*- 2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 3 * Copyright (c) 1992-1998 Søren Schmidt 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer, 11 * without modification, immediately at the beginning of the file. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/dev/syscons/schistory.c,v 1.8.2.1 2001/07/19 06:38:53 dd Exp $ 30 * $DragonFly: src/sys/dev/misc/syscons/schistory.c,v 1.8 2006/09/05 00:55:38 dillon Exp $ 31 */ 32 33 #include "use_sc.h" 34 #include "opt_syscons.h" 35 36 #ifndef SC_NO_HISTORY 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/conf.h> 41 #include <sys/tty.h> 42 #include <sys/kernel.h> 43 44 #include <machine/console.h> 45 #include <machine/pc/display.h> 46 47 #include "syscons.h" 48 49 #if !defined(SC_MAX_HISTORY_SIZE) 50 #define SC_MAX_HISTORY_SIZE (1000 * MAXCONS * NSC) 51 #endif 52 53 #if !defined(SC_HISTORY_SIZE) 54 #define SC_HISTORY_SIZE (ROW * 10) 55 #endif 56 57 #if (SC_HISTORY_SIZE * MAXCONS * NSC) > SC_MAX_HISTORY_SIZE 58 #undef SC_MAX_HISTORY_SIZE 59 #define SC_MAX_HISTORY_SIZE (SC_HISTORY_SIZE * MAXCONS * NSC) 60 #endif 61 62 /* local variables */ 63 static int extra_history_size = SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS; 64 65 /* local functions */ 66 static void copy_history(sc_vtb_t *from, sc_vtb_t *to); 67 static void history_to_screen(scr_stat *scp); 68 static void history_to_screen_lines(scr_stat *scp, int cnt); 69 70 /* allocate a history buffer */ 71 int 72 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait) 73 { 74 /* 75 * syscons unconditionally allocates buffers upto 76 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever 77 * is larger. A value greater than that is allowed, 78 * subject to extra_history_size. 79 */ 80 sc_vtb_t *history; 81 sc_vtb_t *prev_history; 82 int cur_lines; /* current buffer size */ 83 int min_lines; /* guaranteed buffer size */ 84 int delta; /* lines to put back */ 85 int ok; 86 87 if (lines <= 0) 88 lines = SC_HISTORY_SIZE; /* use the default value */ 89 90 /* make it at least as large as the screen size */ 91 lines = imax(lines, scp->ysize); 92 93 /* remove the history buffer while we update it */ 94 history = prev_history = scp->history; 95 scp->history = NULL; 96 97 /* calculate the amount of lines to put back to extra_history_size */ 98 delta = 0; 99 if (prev_history) { 100 cur_lines = sc_vtb_rows(history); 101 min_lines = imax(SC_HISTORY_SIZE, prev_ysize); 102 if (cur_lines > min_lines) 103 delta = cur_lines - min_lines; 104 } 105 106 /* lines upto min_lines are always allowed. */ 107 min_lines = imax(SC_HISTORY_SIZE, scp->ysize); 108 if (lines > min_lines) { 109 if (lines - min_lines > extra_history_size + delta) { 110 /* too many lines are requested */ 111 scp->history = prev_history; 112 return EINVAL; 113 } 114 } 115 116 /* allocate a new buffer */ 117 ok = 0; 118 history = kmalloc(sizeof(*history), M_SYSCONS, 119 (wait) ? M_WAITOK : M_NOWAIT); 120 if (history != NULL) { 121 if (lines > min_lines) 122 extra_history_size -= lines - min_lines; 123 /* XXX error check? */ 124 sc_vtb_init(history, VTB_RINGBUFFER, scp->xsize, lines, 125 NULL, wait); 126 if (history->vtb_flags & VTB_VALID) { 127 /* FIXME: XXX no good? */ 128 sc_vtb_clear(history, scp->sc->scr_map[0x20], 129 SC_NORM_ATTR << 8); 130 if (prev_history != NULL) 131 copy_history(prev_history, history); 132 scp->history_pos = sc_vtb_tail(history); 133 ok = 1; 134 } 135 } 136 137 if (ok) { 138 /* 139 * Install new buffer, destroy previous buffer 140 */ 141 if (prev_history != NULL) { 142 extra_history_size += delta; 143 sc_vtb_destroy(prev_history); 144 kfree(prev_history, M_SYSCONS); 145 } 146 scp->history = history; 147 } else { 148 /* 149 * Destroy partially initialized new buffer, keep 150 * previous buffer. Anything could have happened 151 * in the mean time so reset the history position. 152 */ 153 if (history) { 154 sc_vtb_destroy(history); 155 kfree(history, M_SYSCONS); 156 } 157 scp->history_pos = 0; 158 scp->history = prev_history; 159 } 160 return 0; 161 } 162 163 static void 164 copy_history(sc_vtb_t *from, sc_vtb_t *to) 165 { 166 int lines; 167 int cols; 168 int cols1; 169 int cols2; 170 int pos; 171 int i; 172 173 lines = sc_vtb_rows(from); 174 cols1 = sc_vtb_cols(from); 175 cols2 = sc_vtb_cols(to); 176 cols = imin(cols1, cols2); 177 pos = sc_vtb_tail(from); 178 for (i = 0; i < lines; ++i) { 179 sc_vtb_append(from, pos, to, cols); 180 if (cols < cols2) 181 sc_vtb_seek(to, sc_vtb_pos(to, 182 sc_vtb_tail(to), 183 cols2 - cols)); 184 pos = sc_vtb_pos(from, pos, cols1); 185 } 186 } 187 188 void 189 sc_free_history_buffer(scr_stat *scp, int prev_ysize) 190 { 191 sc_vtb_t *history; 192 int cur_lines; /* current buffer size */ 193 int min_lines; /* guaranteed buffer size */ 194 195 history = scp->history; 196 scp->history = NULL; 197 if (history == NULL) 198 return; 199 200 cur_lines = sc_vtb_rows(history); 201 min_lines = imax(SC_HISTORY_SIZE, prev_ysize); 202 extra_history_size += (cur_lines > min_lines) ? 203 cur_lines - min_lines : 0; 204 205 sc_vtb_destroy(history); 206 kfree(history, M_SYSCONS); 207 } 208 209 /* copy entire screen into the top of the history buffer */ 210 void 211 sc_hist_save(scr_stat *scp) 212 { 213 sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize); 214 scp->history_pos = sc_vtb_tail(scp->history); 215 } 216 217 /* restore the screen by copying from the history buffer */ 218 int 219 sc_hist_restore(scr_stat *scp) 220 { 221 int ret; 222 223 if (scp->history_pos != sc_vtb_tail(scp->history)) { 224 scp->history_pos = sc_vtb_tail(scp->history); 225 history_to_screen(scp); 226 ret = 0; 227 } else { 228 ret = 1; 229 } 230 sc_vtb_seek(scp->history, sc_vtb_pos(scp->history, 231 sc_vtb_tail(scp->history), 232 -scp->xsize*scp->ysize)); 233 return ret; 234 } 235 236 /* copy screen-full of saved lines */ 237 static void 238 history_to_screen(scr_stat *scp) 239 { 240 history_to_screen_lines(scp, scp->ysize); 241 } 242 243 /* copy cnt of saved lines */ 244 static void 245 history_to_screen_lines(scr_stat *scp, int cnt) 246 { 247 int pos; 248 int i; 249 int lines = imin(cnt, scp->ysize); 250 251 pos = scp->history_pos; 252 for (i = 1; i <= lines; ++i) { 253 pos = sc_vtb_pos(scp->history, pos, -scp->xsize); 254 sc_vtb_copy(scp->history, pos, 255 &scp->vtb, scp->xsize*(lines - i), 256 scp->xsize); 257 } 258 mark_all(scp); 259 } 260 261 /* go to the tail of the history buffer */ 262 void 263 sc_hist_home(scr_stat *scp) 264 { 265 scp->history_pos = sc_vtb_tail(scp->history); 266 history_to_screen(scp); 267 } 268 269 /* Restore old_ysize lines from the tail of the history buffer */ 270 void 271 sc_hist_getback(scr_stat *scp, int old_ysize) 272 { 273 scp->history_pos = sc_vtb_tail(scp->history); 274 history_to_screen_lines(scp, old_ysize); 275 } 276 277 /* go to the top of the history buffer */ 278 void 279 sc_hist_end(scr_stat *scp) 280 { 281 scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history), 282 scp->xsize*scp->ysize); 283 history_to_screen(scp); 284 } 285 286 /* move one line up */ 287 int 288 sc_hist_up_line(scr_stat *scp) 289 { 290 if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize)) 291 == sc_vtb_tail(scp->history)) 292 return -1; 293 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos, 294 -scp->xsize); 295 history_to_screen(scp); 296 return 0; 297 } 298 299 /* move one line down */ 300 int 301 sc_hist_down_line(scr_stat *scp) 302 { 303 if (scp->history_pos == sc_vtb_tail(scp->history)) 304 return -1; 305 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos, 306 scp->xsize); 307 history_to_screen(scp); 308 return 0; 309 } 310 311 int 312 sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag) 313 { 314 scr_stat *scp; 315 int error; 316 317 switch (cmd) { 318 319 case CONS_HISTORY: /* set history size */ 320 scp = SC_STAT(tp->t_dev); 321 if (*(int *)data <= 0) 322 return EINVAL; 323 if (scp->status & BUFFER_SAVED) 324 return EBUSY; 325 DPRINTF(5, ("lines:%d, ysize:%d, pool:%d\n", 326 *(int *)data, scp->ysize, extra_history_size)); 327 error = sc_alloc_history_buffer(scp, 328 imax(*(int *)data, scp->ysize), 329 scp->ysize, TRUE); 330 DPRINTF(5, ("error:%d, rows:%d, pool:%d\n", error, 331 sc_vtb_rows(scp->history), extra_history_size)); 332 return error; 333 334 case CONS_CLRHIST: 335 scp = SC_STAT(tp->t_dev); 336 sc_vtb_clear(scp->history, scp->sc->scr_map[0x20], 337 SC_NORM_ATTR << 8); 338 return 0; 339 } 340 341 return ENOIOCTL; 342 } 343 344 #endif /* SC_NO_HISTORY */ 345