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