xref: /openbsd/usr.bin/vi/ex/ex_display.c (revision db3296cf)
1 /*	$OpenBSD: ex_display.c,v 1.4 2002/02/16 21:27:57 millert Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1992, 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_display.c	10.12 (Berkeley) 4/10/96";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "../common/common.h"
28 #include "tag.h"
29 
30 static int	bdisplay(SCR *);
31 static void	db(SCR *, CB *, CHAR_T *);
32 
33 /*
34  * ex_display -- :display b[uffers] | c[onnections] | s[creens] | t[ags]
35  *
36  *	Display cscope connections, buffers, tags or screens.
37  *
38  * PUBLIC: int ex_display(SCR *, EXCMD *);
39  */
40 int
41 ex_display(sp, cmdp)
42 	SCR *sp;
43 	EXCMD *cmdp;
44 {
45 	switch (cmdp->argv[0]->bp[0]) {
46 	case 'b':
47 #undef	ARG
48 #define	ARG	"buffers"
49 		if (cmdp->argv[0]->len >= sizeof(ARG) ||
50 		    memcmp(cmdp->argv[0]->bp, ARG, cmdp->argv[0]->len))
51 			break;
52 		return (bdisplay(sp));
53 	case 'c':
54 #undef	ARG
55 #define	ARG	"connections"
56 		if (cmdp->argv[0]->len >= sizeof(ARG) ||
57 		    memcmp(cmdp->argv[0]->bp, ARG, cmdp->argv[0]->len))
58 			break;
59 		return (cscope_display(sp));
60 	case 's':
61 #undef	ARG
62 #define	ARG	"screens"
63 		if (cmdp->argv[0]->len >= sizeof(ARG) ||
64 		    memcmp(cmdp->argv[0]->bp, ARG, cmdp->argv[0]->len))
65 			break;
66 		return (ex_sdisplay(sp));
67 	case 't':
68 #undef	ARG
69 #define	ARG	"tags"
70 		if (cmdp->argv[0]->len >= sizeof(ARG) ||
71 		    memcmp(cmdp->argv[0]->bp, ARG, cmdp->argv[0]->len))
72 			break;
73 		return (ex_tag_display(sp));
74 	}
75 	ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
76 	return (1);
77 }
78 
79 /*
80  * bdisplay --
81  *
82  *	Display buffers.
83  */
84 static int
85 bdisplay(sp)
86 	SCR *sp;
87 {
88 	CB *cbp;
89 
90 	if (sp->gp->cutq.lh_first == NULL && sp->gp->dcbp == NULL) {
91 		msgq(sp, M_INFO, "123|No cut buffers to display");
92 		return (0);
93 	}
94 
95 	/* Display regular cut buffers. */
96 	for (cbp = sp->gp->cutq.lh_first; cbp != NULL; cbp = cbp->q.le_next) {
97 		if (isdigit(cbp->name))
98 			continue;
99 		if (cbp->textq.cqh_first != (void *)&cbp->textq)
100 			db(sp, cbp, NULL);
101 		if (INTERRUPTED(sp))
102 			return (0);
103 	}
104 	/* Display numbered buffers. */
105 	for (cbp = sp->gp->cutq.lh_first; cbp != NULL; cbp = cbp->q.le_next) {
106 		if (!isdigit(cbp->name))
107 			continue;
108 		if (cbp->textq.cqh_first != (void *)&cbp->textq)
109 			db(sp, cbp, NULL);
110 		if (INTERRUPTED(sp))
111 			return (0);
112 	}
113 	/* Display default buffer. */
114 	if ((cbp = sp->gp->dcbp) != NULL)
115 		db(sp, cbp, "default buffer");
116 	return (0);
117 }
118 
119 /*
120  * db --
121  *	Display a buffer.
122  */
123 static void
124 db(sp, cbp, name)
125 	SCR *sp;
126 	CB *cbp;
127 	CHAR_T *name;
128 {
129 	CHAR_T *p;
130 	GS *gp;
131 	TEXT *tp;
132 	size_t len;
133 
134 	gp = sp->gp;
135 	(void)ex_printf(sp, "********** %s%s\n",
136 	    name == NULL ? KEY_NAME(sp, cbp->name) : name,
137 	    F_ISSET(cbp, CB_LMODE) ? " (line mode)" : " (character mode)");
138 	for (tp = cbp->textq.cqh_first;
139 	    tp != (void *)&cbp->textq; tp = tp->q.cqe_next) {
140 		for (len = tp->len, p = tp->lb; len--; ++p) {
141 			(void)ex_puts(sp, KEY_NAME(sp, *p));
142 			if (INTERRUPTED(sp))
143 				return;
144 		}
145 		(void)ex_puts(sp, "\n");
146 	}
147 }
148