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