1/*	$OpenBSD: print.c,v 1.8 2017/10/11 07:26:40 anton Exp $	*/
2/*	$NetBSD: print.c,v 1.4 1995/09/27 01:06:58 jtc Exp $	*/
3
4/*
5 * Copyright (c) 1987, 1993, 1994
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <limits.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39#include "ctags.h"
40
41/*
42 * get_line --
43 *	get the line the token of interest occurred on,
44 *	prepare it for printing.
45 */
46void
47get_line(void)
48{
49	long	saveftell;
50	int	c;
51	int	cnt;
52	char	*cp;
53
54	saveftell = ftell(inf);
55	(void)fseek(inf, lineftell, SEEK_SET);
56	if (xflag)
57		for (cp = lbuf; GETC(!=, EOF) && c != '\n'; *cp++ = c)
58			continue;
59	/*
60	 * do all processing here, so we don't step through the
61	 * line more than once; means you don't call this routine
62	 * unless you're sure you've got a keeper.
63	 */
64	else for (cnt = 0, cp = lbuf; GETC(!=, EOF) && cnt < ENDLINE; ++cnt) {
65		if (c == '\\') {		/* backslashes */
66			if (cnt > ENDLINE - 2)
67				break;
68			*cp++ = '\\'; *cp++ = '\\';
69			++cnt;
70		}
71		else if (c == (int)searchar) {	/* search character */
72			if (cnt > ENDLINE - 2)
73				break;
74			*cp++ = '\\'; *cp++ = c;
75			++cnt;
76		}
77		else if (c == '\n') {	/* end of keep */
78			*cp++ = '$';		/* can find whole line */
79			break;
80		}
81		else
82			*cp++ = c;
83	}
84	*cp = EOS;
85	(void)fseek(inf, saveftell, SEEK_SET);
86}
87
88/*
89 * put_entries --
90 *	write out the tags
91 */
92void
93put_entries(NODE *node)
94{
95
96	if (node->left)
97		put_entries(node->left);
98	if (vflag)
99		printf("%s %s %d\n",
100		    node->entry, node->file, (node->lno + 63) / 64);
101	else if (xflag)
102		printf("%-16s %4d %-16s %s\n",
103		    node->entry, node->lno, node->file, node->pat);
104	else
105		fprintf(outf, "%s\t%s\t%c^%s%c\n",
106		    node->entry, node->file, searchar, node->pat, searchar);
107	if (node->right)
108		put_entries(node->right);
109}
110