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