xref: /original-bsd/usr.bin/ctags/print.c (revision 4b565e61)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)print.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include "ctags.h"
17 
18 extern char	searchar;		/* ex search character */
19 
20 /*
21  * getline --
22  *	get the line the token of interest occurred on,
23  *	prepare it for printing.
24  */
25 getline()
26 {
27 	register long	saveftell;
28 	register int	c,
29 			cnt;
30 	register char	*cp;
31 
32 	saveftell = ftell(inf);
33 	(void)fseek(inf,lineftell,L_SET);
34 	if (xflag)
35 		for (cp = lbuf;GETC(!=,'\n');*cp++ = c);
36 	/*
37 	 * do all processing here, so we don't step through the
38 	 * line more than once; means you don't call this routine
39 	 * unless you're sure you've got a keeper.
40 	 */
41 	else for (cnt = 0,cp = lbuf;GETC(!=,EOF) && cnt < ENDLINE;++cnt) {
42 		if (c == (int)'\\') {		/* backslashes */
43 			if (cnt > ENDLINE - 2)
44 				break;
45 			*cp++ = '\\'; *cp++ = '\\';
46 			++cnt;
47 		}
48 		else if (c == (int)searchar) {	/* search character */
49 			if (cnt > ENDLINE - 2)
50 				break;
51 			*cp++ = '\\'; *cp++ = c;
52 			++cnt;
53 		}
54 		else if (c == (int)'\n') {	/* end of keep */
55 			*cp++ = '$';		/* can find whole line */
56 			break;
57 		}
58 		else
59 			*cp++ = c;
60 	}
61 	*cp = EOS;
62 	(void)fseek(inf,saveftell,L_SET);
63 }
64 
65 /*
66  * put_entries --
67  *	write out the tags
68  */
69 put_entries(node)
70 	register NODE	*node;
71 {
72 	extern FILE	*outf;		/* ioptr for tags file */
73 	extern int	vflag;		/* -v: vgrind style output */
74 
75 	if (node->left)
76 		put_entries(node->left);
77 	if (vflag)
78 		printf("%s %s %d\n",
79 		    node->entry,node->file,(node->lno + 63) / 64);
80 	else if (xflag)
81 		printf("%-16s%4d %-16s %s\n",
82 		    node->entry,node->lno,node->file,node->pat);
83 	else
84 		fprintf(outf,"%s\t%s\t%c^%s%c\n",
85 		    node->entry,node->file,searchar,node->pat,searchar);
86 	if (node->right)
87 		put_entries(node->right);
88 }
89