1 /* $OpenBSD: ex_util.c,v 1.4 2001/01/29 01:58:45 niklas 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 15 static const char sccsid[] = "@(#)ex_util.c 10.23 (Berkeley) 6/19/96"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/queue.h> 20 #include <sys/stat.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/common.h" 31 32 /* 33 * ex_cinit -- 34 * Create an EX command structure. 35 * 36 * PUBLIC: void ex_cinit __P((EXCMD *, 37 * PUBLIC: int, int, recno_t, recno_t, int, ARGS **)); 38 */ 39 void 40 ex_cinit(cmdp, cmd_id, naddr, lno1, lno2, force, ap) 41 EXCMD *cmdp; 42 int cmd_id, force, naddr; 43 recno_t lno1, lno2; 44 ARGS **ap; 45 { 46 memset(cmdp, 0, sizeof(EXCMD)); 47 cmdp->cmd = &cmds[cmd_id]; 48 cmdp->addrcnt = naddr; 49 cmdp->addr1.lno = lno1; 50 cmdp->addr2.lno = lno2; 51 cmdp->addr1.cno = cmdp->addr2.cno = 1; 52 if (force) 53 cmdp->iflags |= E_C_FORCE; 54 cmdp->argc = 0; 55 if ((cmdp->argv = ap) != NULL) 56 cmdp->argv[0] = NULL; 57 } 58 59 /* 60 * ex_cadd -- 61 * Add an argument to an EX command structure. 62 * 63 * PUBLIC: void ex_cadd __P((EXCMD *, ARGS *, char *, size_t)); 64 */ 65 void 66 ex_cadd(cmdp, ap, arg, len) 67 EXCMD *cmdp; 68 ARGS *ap; 69 char *arg; 70 size_t len; 71 { 72 cmdp->argv[cmdp->argc] = ap; 73 ap->bp = arg; 74 ap->len = len; 75 cmdp->argv[++cmdp->argc] = NULL; 76 } 77 78 /* 79 * ex_getline -- 80 * Return a line from the file. 81 * 82 * PUBLIC: int ex_getline __P((SCR *, FILE *, size_t *)); 83 */ 84 int 85 ex_getline(sp, fp, lenp) 86 SCR *sp; 87 FILE *fp; 88 size_t *lenp; 89 { 90 EX_PRIVATE *exp; 91 size_t off; 92 int ch; 93 char *p; 94 95 exp = EXP(sp); 96 for (errno = 0, off = 0, p = exp->ibp;;) { 97 if (off >= exp->ibp_len) { 98 BINC_RET(sp, exp->ibp, exp->ibp_len, off + 1); 99 p = exp->ibp + off; 100 } 101 if ((ch = getc(fp)) == EOF && !feof(fp)) { 102 if (errno == EINTR) { 103 errno = 0; 104 clearerr(fp); 105 continue; 106 } 107 return (1); 108 } 109 if (ch == EOF || ch == '\n') { 110 if (ch == EOF && !off) 111 return (1); 112 *lenp = off; 113 return (0); 114 } 115 *p++ = ch; 116 ++off; 117 } 118 /* NOTREACHED */ 119 } 120 121 /* 122 * ex_ncheck -- 123 * Check for more files to edit. 124 * 125 * PUBLIC: int ex_ncheck __P((SCR *, int)); 126 */ 127 int 128 ex_ncheck(sp, force) 129 SCR *sp; 130 int force; 131 { 132 char **ap; 133 134 /* 135 * !!! 136 * Historic practice: quit! or two quit's done in succession 137 * (where ZZ counts as a quit) didn't check for other files. 138 */ 139 if (!force && sp->ccnt != sp->q_ccnt + 1 && 140 sp->cargv != NULL && sp->cargv[1] != NULL) { 141 sp->q_ccnt = sp->ccnt; 142 143 for (ap = sp->cargv + 1; *ap != NULL; ++ap); 144 msgq(sp, M_ERR, 145 "167|%d more files to edit", (ap - sp->cargv) - 1); 146 147 return (1); 148 } 149 return (0); 150 } 151 152 /* 153 * ex_init -- 154 * Init the screen for ex. 155 * 156 * PUBLIC: int ex_init __P((SCR *)); 157 */ 158 int 159 ex_init(sp) 160 SCR *sp; 161 { 162 GS *gp; 163 164 gp = sp->gp; 165 166 if (gp->scr_screen(sp, SC_EX)) 167 return (1); 168 (void)gp->scr_attr(sp, SA_ALTERNATE, 0); 169 170 sp->rows = O_VAL(sp, O_LINES); 171 sp->cols = O_VAL(sp, O_COLUMNS); 172 173 F_CLR(sp, SC_VI); 174 F_SET(sp, SC_EX | SC_SCR_EX); 175 return (0); 176 } 177 178 /* 179 * ex_emsg -- 180 * Display a few common ex and vi error messages. 181 * 182 * PUBLIC: void ex_emsg __P((SCR *, char *, exm_t)); 183 */ 184 void 185 ex_emsg(sp, p, which) 186 SCR *sp; 187 char *p; 188 exm_t which; 189 { 190 switch (which) { 191 case EXM_EMPTYBUF: 192 msgq(sp, M_ERR, "168|Buffer %s is empty", p); 193 break; 194 case EXM_FILECOUNT: 195 msgq_str(sp, M_ERR, p, 196 "144|%s: expanded into too many file names"); 197 break; 198 case EXM_NOCANON: 199 msgq(sp, M_ERR, 200 "283|The %s command requires the ex terminal interface", p); 201 break; 202 case EXM_NOCANON_F: 203 msgq(sp, M_ERR, 204 "272|That form of %s requires the ex terminal interface", 205 p); 206 break; 207 case EXM_NOFILEYET: 208 if (p == NULL) 209 msgq(sp, M_ERR, 210 "274|Command failed, no file read in yet."); 211 else 212 msgq(sp, M_ERR, 213 "173|The %s command requires that a file have already been read in", p); 214 break; 215 case EXM_NOPREVBUF: 216 msgq(sp, M_ERR, "171|No previous buffer to execute"); 217 break; 218 case EXM_NOPREVRE: 219 msgq(sp, M_ERR, "172|No previous regular expression"); 220 break; 221 case EXM_NOSUSPEND: 222 msgq(sp, M_ERR, "230|This screen may not be suspended"); 223 break; 224 case EXM_SECURE: 225 msgq(sp, M_ERR, 226 "290|The %s command is not supported when the secure edit option is set", p); 227 break; 228 case EXM_SECURE_F: 229 msgq(sp, M_ERR, 230 "284|That form of %s is not supported when the secure edit option is set", p); 231 break; 232 case EXM_USAGE: 233 msgq(sp, M_ERR, "174|Usage: %s", p); 234 break; 235 } 236 } 237