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