1/* $NetBSD: screen.c,v 1.1.1.2 2008/05/18 14:29:51 aymeric Exp $ */ 2 3/*- 4 * Copyright (c) 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1993, 1994, 1995, 1996 7 * Keith Bostic. All rights reserved. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12#include "config.h" 13 14#ifndef lint 15static const char sccsid[] = "Id: screen.c,v 10.22 2001/06/25 15:19:12 skimo Exp (Berkeley) Date: 2001/06/25 15:19:12"; 16#endif /* not lint */ 17 18#include <sys/types.h> 19#include <sys/queue.h> 20#include <sys/time.h> 21 22#include <bitstring.h> 23#include <errno.h> 24#include <limits.h> 25#include <stdio.h> 26#include <stdlib.h> 27#include <string.h> 28#include <unistd.h> 29 30#include "common.h" 31#include "../vi/vi.h" 32 33/* 34 * screen_init -- 35 * Do the default initialization of an SCR structure. 36 * 37 * PUBLIC: int screen_init __P((GS *, SCR *, SCR **)); 38 */ 39int 40screen_init(GS *gp, SCR *orig, SCR **spp) 41{ 42 SCR *sp; 43 size_t len; 44 45 *spp = NULL; 46 CALLOC_RET(orig, sp, SCR *, 1, sizeof(SCR)); 47 *spp = sp; 48 49/* INITIALIZED AT SCREEN CREATE. */ 50 sp->id = ++gp->id; 51 sp->refcnt = 1; 52 53 sp->gp = gp; /* All ref the GS structure. */ 54 55 sp->ccnt = 2; /* Anything > 1 */ 56 57 /* 58 * XXX 59 * sp->defscroll is initialized by the opts_init() code because 60 * we don't have the option information yet. 61 */ 62 63 CIRCLEQ_INIT(&sp->tiq); 64 65/* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */ 66 if (orig == NULL) { 67 sp->searchdir = NOTSET; 68 } else { 69 sp->wp = orig->wp; 70 71 /* Alternate file name. */ 72 if (orig->alt_name != NULL && 73 (sp->alt_name = strdup(orig->alt_name)) == NULL) 74 goto mem; 75 76 /* Last executed at buffer. */ 77 if (F_ISSET(orig, SC_AT_SET)) { 78 F_SET(sp, SC_AT_SET); 79 sp->at_lbuf = orig->at_lbuf; 80 } 81 82 /* Retain searching/substitution information. */ 83 sp->searchdir = orig->searchdir == NOTSET ? NOTSET : FORWARD; 84 if (orig->re != NULL && (sp->re = 85 v_wstrdup(sp, orig->re, orig->re_len)) == NULL) 86 goto mem; 87 sp->re_len = orig->re_len; 88 if (orig->subre != NULL && (sp->subre = 89 v_wstrdup(sp, orig->subre, orig->subre_len)) == NULL) 90 goto mem; 91 sp->subre_len = orig->subre_len; 92 if (orig->repl != NULL && (sp->repl = 93 v_wstrdup(sp, orig->repl, orig->repl_len)) == NULL) 94 goto mem; 95 sp->repl_len = orig->repl_len; 96 if (orig->newl_len) { 97 len = orig->newl_len * sizeof(size_t); 98 MALLOC(sp, sp->newl, size_t *, len); 99 if (sp->newl == NULL) { 100mem: msgq(orig, M_SYSERR, NULL); 101 goto err; 102 } 103 sp->newl_len = orig->newl_len; 104 sp->newl_cnt = orig->newl_cnt; 105 memcpy(sp->newl, orig->newl, len); 106 } 107 108 if (opts_copy(orig, sp)) 109 goto err; 110 111 F_SET(sp, F_ISSET(orig, SC_EX | SC_VI)); 112 } 113 114 if (ex_screen_copy(orig, sp)) /* Ex. */ 115 goto err; 116 if (v_screen_copy(orig, sp)) /* Vi. */ 117 goto err; 118 sp->cl_private = 0; /* XXX */ 119 conv_init(orig, sp); /* XXX */ 120 121 *spp = sp; 122 return (0); 123 124err: screen_end(sp); 125 return (1); 126} 127 128/* 129 * screen_end -- 130 * Release a screen, no matter what had (and had not) been 131 * initialized. 132 * 133 * PUBLIC: int screen_end __P((SCR *)); 134 */ 135int 136screen_end(SCR *sp) 137{ 138 int rval; 139 140 /* If multiply referenced, just decrement the count and return. */ 141 if (--sp->refcnt != 0) 142 return (0); 143 144 /* 145 * Remove the screen from the displayed queue. 146 * 147 * If a created screen failed during initialization, it may not 148 * be linked into the chain. 149 */ 150 if (sp->q.cqe_next != NULL) 151 CIRCLEQ_REMOVE(&sp->wp->scrq, sp, q); 152 153 /* The screen is no longer real. */ 154 F_CLR(sp, SC_SCR_EX | SC_SCR_VI); 155 156 rval = 0; 157#ifdef HAVE_PERL_INTERP 158 if (perl_screen_end(sp)) /* End perl. */ 159 rval = 1; 160#endif 161 if (v_screen_end(sp)) /* End vi. */ 162 rval = 1; 163 if (ex_screen_end(sp)) /* End ex. */ 164 rval = 1; 165 166 /* Free file names. */ 167 { char **ap; 168 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) { 169 for (ap = sp->argv; *ap != NULL; ++ap) 170 free(*ap); 171 free(sp->argv); 172 } 173 } 174 175 /* Free any text input. */ 176 if (sp->tiq.cqh_first != NULL) 177 text_lfree(&sp->tiq); 178 179 /* Free alternate file name. */ 180 if (sp->alt_name != NULL) 181 free(sp->alt_name); 182 183 /* Free up search information. */ 184 if (sp->re != NULL) 185 free(sp->re); 186 if (F_ISSET(sp, SC_RE_SEARCH)) 187 regfree(&sp->re_c); 188 if (sp->subre != NULL) 189 free(sp->subre); 190 if (F_ISSET(sp, SC_RE_SUBST)) 191 regfree(&sp->subre_c); 192 if (sp->repl != NULL) 193 free(sp->repl); 194 if (sp->newl != NULL) 195 free(sp->newl); 196 197 /* Free all the options */ 198 opts_free(sp); 199 200 /* Free the screen itself. */ 201 free(sp); 202 203 return (rval); 204} 205 206/* 207 * screen_next -- 208 * Return the next screen in the queue. 209 * 210 * PUBLIC: SCR *screen_next __P((SCR *)); 211 */ 212SCR * 213screen_next(SCR *sp) 214{ 215 GS *gp; 216 WIN *wp; 217 SCR *next; 218 219 /* Try the display queue, without returning the current screen. */ 220 gp = sp->gp; 221 wp = sp->wp; 222 for (next = wp->scrq.cqh_first; 223 next != (void *)&wp->scrq; next = next->q.cqe_next) 224 if (next != sp) 225 break; 226 if (next != (void *)&wp->scrq) 227 return (next); 228 229 /* Try the hidden queue; if found, move screen to the display queue. */ 230 if (gp->hq.cqh_first != (void *)&gp->hq) { 231 next = gp->hq.cqh_first; 232 CIRCLEQ_REMOVE(&gp->hq, next, q); 233 CIRCLEQ_INSERT_HEAD(&wp->scrq, next, q); 234 next->wp = sp->wp; 235 return (next); 236 } 237 return (NULL); 238} 239