/* */
This source file includes following definitions.
- setup_langmap
- decide_lang
- match_suffix_list
- make_suffixes
1 /*
2 * Copyright (c) 2002, 2004, 2005, 2008
3 * Tama Communications Corporation
4 *
5 * This file is part of GNU GLOBAL.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #ifdef STDC_HEADERS
25 #include <stdlib.h>
26 #endif
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #else
30 #include <strings.h>
31 #endif
32
33 #include "die.h"
34 #include "locatestring.h"
35 #include "strbuf.h"
36 #include "langmap.h"
37
38 static int match_suffix_list(const char *, const char *);
39
40 static STRBUF *active_map;
41
42 /**
43 * construct language map.
44 *
45 * copy string langmap (@a map) and convert it to language map like this:
46 *
47 * @code{.txt}
48 * langmap (string) "c:.c.h,java:.java,cpp:.C.H"
49 * |
50 * v
51 * language map c\0.c.h\0java\0.java\0cpp\0.C.H\0
52 * @endcode
53 */
54 void
55 setup_langmap(const char *map)
56 {
57 char *p;
58 int onsuffix = 0; /* not on suffix string */
59
60 active_map = strbuf_open(0);
61 strbuf_puts(active_map, map);
62 for (p = strbuf_value(active_map); *p; p++) {
63 /*
64 * "c:.c.h,java:.java,cpp:.C.H"
65 */
66 if ((onsuffix == 0 && *p == ',') || (onsuffix == 1 && *p == ':'))
67 die_with_code(2, "syntax error in langmap '%s'.", map);
68 if (*p == ':' || *p == ',') {
69 onsuffix ^= 1;
70 *p = '\0';
71 }
72 }
73 if (onsuffix == 0)
74 die_with_code(2, "syntax error in langmap '%s'.", map);
75 /* strbuf_close(active_map); */
76 }
77
78 /**
79 * decide the language of the @a suffix.
80 *
81 * @remark
82 * Though @FILE{*.h} files are shared by C and C++, @NAME{GLOBAL} treats them
83 * as C source files by default. If you set an environment variable
84 * @FILE{GTAGSFORCECPP} then C++ parser will be invoked.
85 */
86 const char *
87 decide_lang(const char *suffix)
88 {
89 const char *lang, *list, *tail;
90
91 /*
92 * Though '*.h' files are shared by C and C++, GLOBAL treats them
93 * as C source files by default. If you set an environment variable
94 * 'GTAGS_FORCECPP' then C++ parser will be invoked.
95 */
96 if (!strcmp(suffix, ".h") && getenv("GTAGSFORCECPP") != NULL)
97 return "cpp";
98 lang = strbuf_value(active_map);
99 tail = lang + strbuf_getlen(active_map);
100
101 /* check whether or not list includes suffix. */
102 while (lang < tail) {
103 list = lang + strlen(lang) + 1;
104 if (match_suffix_list(suffix, list))
105 return lang;
106 lang = list + strlen(list) + 1;
107 }
108
109 return NULL;
110 }
111
112 /**
113 * return true if the @a suffix matches with one in the @a list.
114 */
115 static int
116 match_suffix_list(const char *suffix, const char *list)
117 {
118 const char *p;
119
120 while (*list) {
121 if ((p = locatestring(list, suffix, MATCH_AT_FIRST
122 #if defined(_WIN32) || defined(__DJGPP__)
123 |IGNORE_CASE
124 #endif
125 )) != NULL && (*p == '\0' || *p == '.'))
126 return 1;
127 for (list++; *list && *list != '.'; list++)
128 ;
129 }
130 return 0;
131 }
132
133 /**
134 * make suffix value from @a langmap value.
135 */
136 void
137 make_suffixes(const char *langmap, STRBUF *sb)
138 {
139 const char *p;
140 int onsuffix = 0; /* not on suffix string */
141 int first_dot = 1;
142
143 for (p = langmap; *p; p++) {
144 /*
145 * "c:.c.h,java:.java,cpp:.C.H"
146 */
147 if ((onsuffix == 0 && *p == ',') || (onsuffix == 1 && *p == ':'))
148 die_with_code(2, "syntax error in langmap '%s'.", langmap);
149 if (*p == ':')
150 onsuffix = 1;
151 else if (*p == ',')
152 onsuffix = 0;
153 else if (onsuffix) {
154 if (*p == '.') {
155 if (first_dot)
156 first_dot = 0;
157 else
158 strbuf_putc(sb, ',');
159 } else
160 strbuf_putc(sb, *p);
161 }
162 }
163 if (onsuffix == 0)
164 die_with_code(2, "syntax error in langmap '%s'.", langmap);
165 }
/* */