8a8923ab720576533de36d482639dc9c052e6f44
[dragonfly.git] / usr.bin / ctags / fortran.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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)fortran.c        8.3 (Berkeley) 4/2/94
34  * $FreeBSD: src/usr.bin/ctags/fortran.c,v 1.3.6.2 2002/07/30 00:55:07 tjr Exp $
35  * $DragonFly: src/usr.bin/ctags/fortran.c,v 1.3 2003/10/02 17:42:27 hmp Exp $
36  */
37
38 #include <ctype.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 #include "ctags.h"
44
45 static void takeprec(void);
46
47 char *lbp;                              /* line buffer pointer */
48
49 int
50 PF_funcs(void)
51 {
52         bool    pfcnt;                  /* pascal/fortran functions found */
53         char    *cp;
54         char    tok[MAXTOKEN];
55
56         for (pfcnt = NO;;) {
57                 lineftell = ftell(inf);
58                 if (!fgets(lbuf, sizeof(lbuf), inf))
59                         return (pfcnt);
60                 ++lineno;
61                 lbp = lbuf;
62                 if (*lbp == '%')        /* Ratfor escape to fortran */
63                         ++lbp;
64                 for (; isspace(*lbp); ++lbp)
65                         continue;
66                 if (!*lbp)
67                         continue;
68                 switch (*lbp | ' ') {   /* convert to lower-case */
69                 case 'c':
70                         if (cicmp("complex") || cicmp("character"))
71                                 takeprec();
72                         break;
73                 case 'd':
74                         if (cicmp("double")) {
75                                 for (; isspace(*lbp); ++lbp)
76                                         continue;
77                                 if (!*lbp)
78                                         continue;
79                                 if (cicmp("precision"))
80                                         break;
81                                 continue;
82                         }
83                         break;
84                 case 'i':
85                         if (cicmp("integer"))
86                                 takeprec();
87                         break;
88                 case 'l':
89                         if (cicmp("logical"))
90                                 takeprec();
91                         break;
92                 case 'r':
93                         if (cicmp("real"))
94                                 takeprec();
95                         break;
96                 }
97                 for (; isspace(*lbp); ++lbp)
98                         continue;
99                 if (!*lbp)
100                         continue;
101                 switch (*lbp | ' ') {
102                 case 'f':
103                         if (cicmp("function"))
104                                 break;
105                         continue;
106                 case 'p':
107                         if (cicmp("program") || cicmp("procedure"))
108                                 break;
109                         continue;
110                 case 's':
111                         if (cicmp("subroutine"))
112                                 break;
113                 default:
114                         continue;
115                 }
116                 for (; isspace(*lbp); ++lbp)
117                         continue;
118                 if (!*lbp)
119                         continue;
120                 for (cp = lbp + 1; *cp && intoken(*cp); ++cp)
121                         continue;
122                 if ((cp = lbp + 1))
123                         continue;
124                 *cp = EOS;
125                 (void)strlcpy(tok, lbp, sizeof(tok));   /* possible trunc */
126                 getline();                      /* process line for ex(1) */
127                 pfnote(tok, lineno);
128                 pfcnt = YES;
129         }
130         /*NOTREACHED*/
131 }
132
133 /*
134  * cicmp --
135  *      do case-independent strcmp
136  */
137 int
138 cicmp(const char *cp)
139 {
140         int     len;
141         char    *bp;
142
143         for (len = 0, bp = lbp; *cp && (*cp &~ ' ') == (*bp++ &~ ' ');
144             ++cp, ++len)
145                 continue;
146         if (!*cp) {
147                 lbp += len;
148                 return (YES);
149         }
150         return (NO);
151 }
152
153 static void
154 takeprec(void)
155 {
156         for (; isspace(*lbp); ++lbp)
157                 continue;
158         if (*lbp == '*') {
159                 for (++lbp; isspace(*lbp); ++lbp)
160                         continue;
161                 if (!isdigit(*lbp))
162                         --lbp;                  /* force failure */
163                 else
164                         while (isdigit(*++lbp))
165                                 continue;
166         }
167 }