fixed compiler hints: -Wwrite-strings option helped a lot
[midnight-commander.git] / edit / etags.c
blobc9007e3c22b8f4e6e574190ba6377a65790358a7
1 /* editor C-code navigation via tags.
2 make TAGS file via command:
3 $ find . -type f -name "*.[ch]" | etags -l c --declarations -
5 or, if etags utility not installed:
6 $ find . -type f -name "*.[ch]" | ctags --c-kinds=+p --fields=+iaS --extra=+q -e -L-
8 Copyright (C) 2009 Free Software Foundation, Inc.
10 Authors:
11 Ilia Maslakov <il.smind@gmail.com>, 2009
12 Slava Zanko <slavazanko@gmail.com>, 2009
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software; you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation; either version 2 of the
20 License, or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be
23 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
24 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
30 MA 02110-1301, USA.
33 #include <config.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <inttypes.h>
38 #include <string.h>
39 #include <ctype.h>
41 #include "../src/global.h"
42 #include "../src/util.h" /* canonicalize_pathname() */
43 #include "../edit/etags.h"
45 /*** file scope functions **********************************************/
47 static gboolean parse_define(char *buf, char **long_name, char **short_name, long *line)
49 enum {in_longname, in_shortname, in_line, finish} def_state = in_longname;
51 static char longdef[LONG_DEF_LEN];
52 static char shortdef[SHORT_DEF_LEN];
53 static char linedef[LINE_DEF_LEN];
54 int nlong = 0;
55 int nshort = 0;
56 int nline = 0;
57 char c = *buf;
59 while ( !(c =='\0' || c =='\n') ) {
60 switch ( def_state ) {
61 case in_longname:
62 if ( c == 0x01 ) {
63 def_state = in_line;
64 } else if ( c == 0x7F ) {
65 def_state = in_shortname;
66 } else {
67 if ( nlong < LONG_DEF_LEN - 1 ) {
68 longdef[nlong++] = c;
71 break;
72 case in_shortname:
73 if ( isdigit(c) ) {
74 nshort = 0;
75 buf--;
76 def_state = in_line;
77 } else if ( c == 0x01 ) {
78 def_state = in_line;
79 } else {
80 if ( nshort < SHORT_DEF_LEN - 1 ) {
81 shortdef[nshort++] = c;
84 break;
85 case in_line:
86 if ( c == ',' ) {
87 def_state = finish;
88 } else if ( isdigit(c) ) {
89 if ( nline < LINE_DEF_LEN - 1 ) {
90 linedef[nline++] = c;
93 break;
94 case finish:
95 longdef[nlong] = '\0';
96 shortdef[nshort] = '\0';
97 linedef[nline] = '\0';
98 *long_name = g_strdup (longdef);
99 *short_name = g_strdup (shortdef);
100 *line = atol (linedef);
101 return TRUE;
102 break;
104 buf++;
105 c = *buf;
107 *long_name = NULL;
108 *short_name = NULL;
109 *line = 0;
110 return FALSE;
113 /*** public functions **************************************************/
115 int etags_set_definition_hash(const char *tagfile, const char *start_path,
116 const char *match_func,
117 etags_hash_t *def_hash)
119 enum {start, in_filename, in_define} state = start;
121 FILE *f;
122 static char buf[BUF_LARGE];
124 char *longname = NULL;
125 char *shortname = NULL;
126 long line;
128 char *chekedstr = NULL;
130 int num = 0; /* returned value */
131 int pos;
132 char *filename = NULL;
134 if ( !match_func || !tagfile )
135 return 0;
137 /* open file with positions */
138 f = fopen (tagfile, "r");
139 if (!f)
140 return 0;
142 while (fgets (buf, sizeof (buf), f)) {
144 switch ( state ) {
145 case start:
146 if ( buf[0] == 0x0C ) {
147 state = in_filename;
149 break;
150 case in_filename:
151 pos = strcspn(buf, ",");
152 g_free(filename);
153 filename = g_malloc (pos + 2);
154 g_strlcpy(filename, (char *)buf, pos + 1);
155 state = in_define;
156 break;
157 case in_define:
158 if ( buf[0] == 0x0C ) {
159 state = in_filename;
160 break;
162 /* check if the filename matches the define pos */
163 chekedstr = strstr (buf, match_func);
164 if ( chekedstr ) {
165 parse_define (chekedstr, &longname, &shortname, &line);
166 if ( num < MAX_DEFINITIONS - 1 ) {
167 def_hash[num].filename_len = strlen (filename);
168 def_hash[num].fullpath = g_build_filename ( start_path, filename, (char *) NULL);
170 canonicalize_pathname (def_hash[num].fullpath);
171 def_hash[num].filename = g_strdup (filename);
172 if ( shortname ) {
173 def_hash[num].short_define = g_strdup (shortname);
174 } else {
175 def_hash[num].short_define = g_strdup (longname);
177 def_hash[num].line = line;
178 g_free(shortname);
179 g_free(longname);
180 num++;
183 break;
187 g_free(filename);
188 return num;