1 /*- 2 * Copyright (c) 1992, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char sccsid[] = "@(#)ex_print.c 8.16 (Berkeley) 8/17/94"; 36 #endif /* not lint */ 37 38 #include <sys/types.h> 39 #include <sys/queue.h> 40 #include <sys/time.h> 41 42 #include <bitstring.h> 43 #include <ctype.h> 44 #include <limits.h> 45 #include <signal.h> 46 #include <stdio.h> 47 #include <string.h> 48 #include <termios.h> 49 50 #include "compat.h" 51 #include <db.h> 52 #include <regex.h> 53 54 #include "vi.h" 55 #include "excmd.h" 56 57 /* 58 * ex_list -- :[line [,line]] l[ist] [count] [flags] 59 * 60 * Display the addressed lines such that the output is unambiguous. 61 */ 62 int 63 ex_list(sp, ep, cmdp) 64 SCR *sp; 65 EXF *ep; 66 EXCMDARG *cmdp; 67 { 68 if (ex_print(sp, ep, 69 &cmdp->addr1, &cmdp->addr2, cmdp->flags | E_F_LIST)) 70 return (1); 71 sp->lno = cmdp->addr2.lno; 72 sp->cno = cmdp->addr2.cno; 73 return (0); 74 } 75 76 /* 77 * ex_number -- :[line [,line]] nu[mber] [count] [flags] 78 * 79 * Display the addressed lines with a leading line number. 80 */ 81 int 82 ex_number(sp, ep, cmdp) 83 SCR *sp; 84 EXF *ep; 85 EXCMDARG *cmdp; 86 { 87 if (ex_print(sp, ep, 88 &cmdp->addr1, &cmdp->addr2, cmdp->flags | E_F_HASH)) 89 return (1); 90 sp->lno = cmdp->addr2.lno; 91 sp->cno = cmdp->addr2.cno; 92 return (0); 93 } 94 95 /* 96 * ex_pr -- :[line [,line]] p[rint] [count] [flags] 97 * 98 * Display the addressed lines. 99 */ 100 int 101 ex_pr(sp, ep, cmdp) 102 SCR *sp; 103 EXF *ep; 104 EXCMDARG *cmdp; 105 { 106 if (ex_print(sp, ep, &cmdp->addr1, &cmdp->addr2, cmdp->flags)) 107 return (1); 108 sp->lno = cmdp->addr2.lno; 109 sp->cno = cmdp->addr2.cno; 110 return (0); 111 } 112 113 /* 114 * ex_print -- 115 * Print the selected lines. 116 */ 117 int 118 ex_print(sp, ep, fp, tp, flags) 119 SCR *sp; 120 EXF *ep; 121 MARK *fp, *tp; 122 register int flags; 123 { 124 recno_t from, to; 125 size_t col, len; 126 char *p; 127 128 F_SET(sp, S_INTERRUPTIBLE); 129 for (from = fp->lno, to = tp->lno; from <= to; ++from) { 130 /* 131 * Display the line number. The %6 format is specified 132 * by POSIX 1003.2, and is almost certainly large enough. 133 * Check, though, just in case. 134 */ 135 if (LF_ISSET(E_F_HASH)) 136 if (from <= 999999) 137 col = ex_printf(EXCOOKIE, "%6ld ", from); 138 else 139 col = ex_printf(EXCOOKIE, "TOOBIG "); 140 else 141 col = 0; 142 143 /* 144 * Display the line. The format for E_F_PRINT isn't very good, 145 * especially in handling end-of-line tabs, but they're almost 146 * backward compatible. 147 */ 148 if ((p = file_gline(sp, ep, from, &len)) == NULL) { 149 GETLINE_ERR(sp, from); 150 return (1); 151 } 152 153 if (len == 0 && !LF_ISSET(E_F_LIST)) 154 (void)ex_printf(EXCOOKIE, "\n"); 155 else if (ex_ldisplay(sp, p, len, col, flags)) 156 return (1); 157 158 if (INTERRUPTED(sp)) 159 break; 160 } 161 162 return (0); 163 } 164 165 /* 166 * ex_ldisplay -- 167 * Display a line. 168 */ 169 int 170 ex_ldisplay(sp, lp, len, col, flags) 171 SCR *sp; 172 CHAR_T *lp; 173 size_t len, col; 174 u_int flags; 175 { 176 CHAR_T ch, *kp; 177 u_long ts; 178 size_t tlen; 179 180 ts = O_VAL(sp, O_TABSTOP); 181 for (;; --len) { 182 if (len > 0) 183 ch = *lp++; 184 else if (LF_ISSET(E_F_LIST)) 185 ch = '$'; 186 else 187 break; 188 if (ch == '\t' && !LF_ISSET(E_F_LIST)) 189 for (tlen = ts - col % ts; 190 col < sp->cols && tlen--; ++col) 191 (void)ex_printf(EXCOOKIE, " "); 192 else { 193 kp = KEY_NAME(sp, ch); 194 tlen = KEY_LEN(sp, ch); 195 if (col + tlen < sp->cols) { 196 (void)ex_printf(EXCOOKIE, "%s", kp); 197 col += tlen; 198 } else 199 for (; tlen--; ++kp, ++col) { 200 if (col == sp->cols) { 201 col = 0; 202 (void)ex_printf(EXCOOKIE, "\n"); 203 } 204 (void)ex_printf(EXCOOKIE, "%c", *kp); 205 } 206 } 207 if (len == 0) 208 break; 209 } 210 (void)ex_printf(EXCOOKIE, "\n"); 211 return (0); 212 } 213