xref: /original-bsd/usr.bin/ctags/print.c (revision 92d853e2)
1 /*
2  * Copyright (c) 1987 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)print.c	5.2 (Berkeley) 12/31/88";
20 #endif /* not lint */
21 
22 #include <sys/types.h>
23 #include <sys/file.h>
24 #include <ctags.h>
25 
26 extern char	searchar;		/* ex search character */
27 
28 /*
29  * getline --
30  *	get the line the token of interest occurred on,
31  *	prepare it for printing.
32  */
33 getline()
34 {
35 	register long	saveftell;
36 	register int	c,
37 			cnt;
38 	register char	*cp;
39 
40 	saveftell = ftell(inf);
41 	(void)fseek(inf,lineftell,L_SET);
42 	if (xflag)
43 		for (cp = lbuf;GETC(!=,'\n');*cp++ = c);
44 	/*
45 	 * do all processing here, so we don't step through the
46 	 * line more than once; means you don't call this routine
47 	 * unless you're sure you've got a keeper.
48 	 */
49 	else for (cnt = 0,cp = lbuf;GETC(!=,EOF) && cnt < ENDLINE;++cnt) {
50 		if (c == (int)'\\') {		/* backslashes */
51 			if (cnt > ENDLINE - 2)
52 				break;
53 			*cp++ = '\\'; *cp++ = '\\';
54 			++cnt;
55 		}
56 		else if (c == (int)searchar) {	/* search character */
57 			if (cnt > ENDLINE - 2)
58 				break;
59 			*cp++ = '\\'; *cp++ = c;
60 			++cnt;
61 		}
62 		else if (c == (int)'\n') {	/* end of keep */
63 			*cp++ = '$';		/* can find whole line */
64 			break;
65 		}
66 		else
67 			*cp++ = c;
68 	}
69 	*cp = EOS;
70 	(void)fseek(inf,saveftell,L_SET);
71 }
72 
73 /*
74  * put_entries --
75  *	write out the tags
76  */
77 put_entries(node)
78 	register NODE	*node;
79 {
80 	extern FILE	*outf;		/* ioptr for tags file */
81 	extern int	vflag;		/* -v: vgrind style output */
82 
83 	if (node->left)
84 		put_entries(node->left);
85 	if (vflag)
86 		printf("%s %s %d\n",
87 		    node->entry,node->file,(node->lno + 63) / 64);
88 	else if (xflag)
89 		printf("%-16s%4d %-16s %s\n",
90 		    node->entry,node->lno,node->file,node->pat);
91 	else
92 		fprintf(outf,"%s\t%s\t%c^%s%c\n",
93 		    node->entry,node->file,searchar,node->pat,searchar);
94 	if (node->right)
95 		put_entries(node->right);
96 }
97 
98 char *
99 savestr(str)
100 	char	*str;
101 {
102 	register u_int	len;
103 	register char	*space;
104 	char	*malloc();
105 
106 	len = strlen(str) + 1;
107 	if (!(space = malloc((u_int)len))) {
108 		/*
109 		 * should probably free up the tree, here,
110 		 * we're just as likely to fail here as we
111 		 * are when getting the NODE structure
112 		 */
113 		fputs("ctags: no more space.\n",stderr);
114 		exit(1);
115 	}
116 	bcopy(str,space,len);
117 	return(space);
118 }
119