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