Ticket #2237: Automatic date and version substitution for man pages
[midnight-commander.git] / src / editor / etags.c
blob945f597e60c731484bbc1df6ff9864c00f5edd3e
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 "lib/global.h"
42 #include "src/editor/etags.h"
44 /*** file scope functions **********************************************/
46 static gboolean parse_define(char *buf, char **long_name, char **short_name, long *line)
48 enum {in_longname, in_shortname, in_shortname_first_char, in_line, finish} def_state = in_longname;
50 static char longdef[LONG_DEF_LEN];
51 static char shortdef[SHORT_DEF_LEN];
52 static char linedef[LINE_DEF_LEN];
53 int nlong = 0;
54 int nshort = 0;
55 int nline = 0;
56 char c = *buf;
58 while ( !(c =='\0' || c =='\n') ) {
59 switch ( def_state ) {
60 case in_longname:
61 if ( c == 0x01 ) {
62 def_state = in_line;
63 } else if ( c == 0x7F ) {
64 def_state = in_shortname;
65 } else {
66 if ( nlong < LONG_DEF_LEN - 1 ) {
67 longdef[nlong++] = c;
70 break;
71 case in_shortname_first_char:
72 if ( isdigit(c) ) {
73 nshort = 0;
74 buf--;
75 def_state = in_line;
76 } else if ( c == 0x01 ) {
77 def_state = in_line;
78 } else {
79 if ( nshort < SHORT_DEF_LEN - 1 ) {
80 shortdef[nshort++] = c;
81 def_state = in_shortname;
84 break;
85 case in_shortname:
86 if ( c == 0x01 ) {
87 def_state = in_line;
88 } else if ( c == '\n' ) {
89 def_state = finish;
90 } else {
91 if ( nshort < SHORT_DEF_LEN - 1 ) {
92 shortdef[nshort++] = c;
95 break;
96 case in_line:
97 if ( c == ',' || c == '\n') {
98 def_state = finish;
99 } else if ( isdigit(c) ) {
100 if ( nline < LINE_DEF_LEN - 1 ) {
101 linedef[nline++] = c;
104 break;
105 case finish:
106 longdef[nlong] = '\0';
107 shortdef[nshort] = '\0';
108 linedef[nline] = '\0';
109 *long_name = g_strdup (longdef);
110 *short_name = g_strdup (shortdef);
111 *line = atol (linedef);
112 return TRUE;
114 buf++;
115 c = *buf;
117 *long_name = NULL;
118 *short_name = NULL;
119 *line = 0;
120 return FALSE;
123 /*** public functions **************************************************/
125 int etags_set_definition_hash(const char *tagfile, const char *start_path,
126 const char *match_func,
127 etags_hash_t *def_hash)
129 enum {start, in_filename, in_define} state = start;
131 FILE *f;
132 static char buf[BUF_LARGE];
134 char *longname = NULL;
135 char *shortname = NULL;
136 long line;
138 char *chekedstr = NULL;
140 int num = 0; /* returned value */
141 int pos;
142 char *filename = NULL;
144 if ( !match_func || !tagfile )
145 return 0;
147 /* open file with positions */
148 f = fopen (tagfile, "r");
149 if (f == NULL)
150 return 0;
152 while (fgets (buf, sizeof (buf), f)) {
154 switch ( state ) {
155 case start:
156 if ( buf[0] == 0x0C ) {
157 state = in_filename;
159 break;
160 case in_filename:
161 pos = strcspn(buf, ",");
162 g_free(filename);
163 filename = g_malloc (pos + 2);
164 g_strlcpy(filename, (char *)buf, pos + 1);
165 state = in_define;
166 break;
167 case in_define:
168 if ( buf[0] == 0x0C ) {
169 state = in_filename;
170 break;
172 /* check if the filename matches the define pos */
173 chekedstr = strstr (buf, match_func);
174 if ( chekedstr ) {
175 parse_define (chekedstr, &longname, &shortname, &line);
176 if ( num < MAX_DEFINITIONS - 1 ) {
177 def_hash[num].filename_len = strlen (filename);
178 def_hash[num].fullpath = g_build_filename ( start_path, filename, (char *) NULL);
180 canonicalize_pathname (def_hash[num].fullpath);
181 def_hash[num].filename = g_strdup (filename);
182 if ( shortname ) {
183 def_hash[num].short_define = g_strdup (shortname);
184 } else {
185 def_hash[num].short_define = g_strdup (longname);
187 def_hash[num].line = line;
188 g_free(shortname);
189 g_free(longname);
190 num++;
193 break;
197 g_free(filename);
198 fclose (f);
199 return num;