1 /* idfile.h -- decls for ID file header and constituent file names
2    Copyright (C) 1986, 1995-1996, 1999, 2007-2012 Free Software Foundation,
3    Inc.
4    Written by Greg McGary <gkm@gnu.ai.mit.edu>
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef _idfile_h_
21 #define _idfile_h_ 1
22 
23 #include <config.h>
24 #include <sys/types.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include "obstack.h"
28 #include "idu-hash.h"
29 #include "dynvec.h"
30 #include "tokflags.h"
31 
32 
33 /****************************************************************************/
34 
35 /* The ID file header is the nexus of all ID file information.  This
36    is an in-core structure, only some of which is read/written to disk.  */
37 
38 struct idhead
39 {
40   unsigned char idh_magic[2];
41 #define	IDH_MAGIC_0 ('I'|0x80)
42 #define	IDH_MAGIC_1 ('D'|0x80)
43   unsigned char idh_version;
44 #define	IDH_VERSION	4
45   unsigned short idh_flags;
46 #define IDH_COUNTS	(1<<0)	/* include occurrence counts for each token */
47 #define IDH_FOLLOW_SL	(1<<1)	/* follow symlinks to directories */
48 #define IDH_COMMENTS	(1<<2)	/* include tokens found in comments */
49 #define IDH_LOCALS	(1<<3)	/* include names of formal params & local vars */
50 #define IDH_DECL_DEFN_USE (1<<4) /* include decl/defn/use info */
51 #define IDH_L_R_VALUE	(1<<5)	/* include lvalue/rvalue info */
52 #define IDH_CALL_ER_EE	(1<<6)	/* include caller/callee relationship info */
53   unsigned long idh_file_links;	/* total # of file links */
54   unsigned long idh_files;	/* total # of constituent source files */
55   unsigned long idh_tokens;	/* total # of constituent tokens */
56   /* idh_*_size: max buffer-sizes for ID file reading programs */
57   unsigned long idh_buf_size;	/* # of bytes in longest entry */
58   unsigned long idh_vec_size;	/* # of hits in longest entry */
59   /* idh_*_offset: ID file offsets for start of various sections */
60   long idh_tokens_offset;	/* constituent tokens section */
61   long idh_flinks_offset;	/* constituent file & directory names section */
62   long idh_end_offset;		/* end of tokens section */
63   unsigned short idh_max_link;	/* longest file name component */
64   unsigned short idh_max_path;	/* largest # of file name components */
65 
66   /* The following are run-time variables and are not stored on disk */
67   char const *idh_file_name;
68   struct hash_table idh_member_file_table;
69   struct hash_table idh_file_link_table;
70   struct obstack idh_member_file_obstack;
71   struct obstack idh_file_link_obstack;
72 #if HAVE_LINK
73   struct hash_table idh_dev_ino_table; /* for detecting file name aliases */
74   struct obstack idh_dev_ino_obstack;
75 #endif
76   FILE *idh_FILE;
77 };
78 
79 /* idhead input/output definitions */
80 
81 #define IO_TYPE_INT	0	/* integer */
82 #define IO_TYPE_STR	1	/* NUL terminated string */
83 #define IO_TYPE_FIX	2	/* fix-sized */
84 
85 
86 /****************************************************************************/
87 
88 /* A file_link represents a single component (file or directory) in a
89    file name.  It has a name, a parent file_link and some flags.  */
90 
91 struct file_link
92 {
93   union {
94     struct file_link *u_parent;
95 #define fl_parent fl_u.u_parent
96     unsigned long u_index;
97 #define fl_index fl_u.u_index
98 #define FL_PARENT_INDEX_BYTES 3
99 #define IS_ROOT_FILE_LINK(flink) ((flink)->fl_parent == (flink))
100   } fl_u;
101   unsigned char fl_flags;
102 #define FL_CMD_LINE_ARG	(1<<0)
103 #define FL_USED		(1<<1)
104 #define FL_MEMBER	(1<<2)	/* has a corresponding member_file entry */
105 #define FL_SCAN_ME	(1<<3)
106 #define FL_SYM_LINK	(1<<4)
107 #define FL_TYPE_MASK	(FL_TYPE_DIR|FL_TYPE_FILE)
108 # define FL_TYPE_DIR	(1<<5)
109 # define FL_IS_DIR(_f_) (((_f_) & FL_TYPE_MASK) == FL_TYPE_DIR)
110 # define FL_TYPE_FILE	(1<<6)
111 # define FL_IS_FILE(_f_) (((_f_) & FL_TYPE_MASK) == FL_TYPE_FILE)
112 #define FL_PRUNE	(1<<7)
113   char fl_name[1];
114 };
115 
116 /* A member_file represents a source file that is treated by mkid.  */
117 
118 struct member_file
119 {
120   struct file_link *mf_link;
121   struct lang_args const *mf_lang_args;
122   long mf_index;	/* order in ID file */
123 };
124 
125 #if HAVE_LINK
126 
127 /* On systems that support multiple names for a single file (via hard
128   and/or soft links), dev_ino records information needed to detect
129   such aliasing.  */
130 
131 struct dev_ino
132 {
133   dev_t di_dev;
134   ino_t	di_ino;
135   struct file_link *di_link;
136 };
137 
138 extern struct hash_table dev_ino_table;
139 
140 #endif /* HAVE_LINK */
141 
142 
143 /******************************************************************************/
144 /* token flags (struct token is defined in scanners.h) */
145 
146 #define token_string(buf) (buf)
147 extern unsigned int token_flags (char const *buf) _GL_ATTRIBUTE_PURE;
148 extern unsigned short token_count (char const *buf) _GL_ATTRIBUTE_PURE;
149 extern unsigned char const *token_hits_addr (char const *buf)
150   _GL_ATTRIBUTE_PURE;
151 
152 #define MAYBE_RETURN_PREFIX_MATCH(arg, str, val) do { \
153     char const *_s_ = (str); \
154     if (strstr (_s_, (arg)) == _s_) \
155       return (val); \
156   } while (0)
157 
158 enum separator_style
159 {
160   ss_bogus,
161   ss_contextual,
162   ss_braces,
163   ss_space,
164   ss_newline
165 };
166 
167 #ifndef DEFAULT_SEPARATOR_STYLE
168 #define DEFAULT_SEPARATOR_STYLE ss_braces
169 #endif
170 
171 typedef int (*io_func_t) (FILE *, void *, unsigned int, int);
172 
173 extern struct file_link **read_id_file (char const *id_file_name, struct idhead *idhp);
174 extern struct file_link **maybe_read_id_file (char const *id_file_name, struct idhead *idhp);
175 extern int read_idhead (struct idhead *idhp);
176 extern int write_idhead (struct idhead *idhp);
177 extern int sizeof_idhead (void);
178 extern struct file_link *init_walker (struct idhead *idhp);
179 extern void init_idh_obstacks (struct idhead *idhp);
180 extern void init_idh_tables (struct idhead *idhp);
181 
182 extern int io_write (FILE *output_FILE, void *addr, unsigned int size, int io_type);
183 extern int io_read (FILE *input_FILE, void *addr, unsigned int size, int io_type);
184 extern int io_idhead (FILE *fp, io_func_t iof, struct idhead *idhp);
185 
186 extern struct file_link *get_current_dir_link (void);
187 extern struct file_link **deserialize_file_links (struct idhead *idhp);
188 extern void serialize_file_links (struct idhead *idhp);
189 
190 extern void mark_member_file_links (struct idhead *idhp);
191 extern int member_file_qsort_compare (void const *x, void const *y)
192   _GL_ATTRIBUTE_PURE;
193 extern struct file_link *parse_file_name (char *file_name, struct file_link *relative_dir_link);
194 extern void print_filenames (struct file_link **flinkv, enum separator_style separator_style);
195 extern enum separator_style parse_separator_style (char const *arg);
196 
197 extern void walk_flink (struct file_link *flink, struct dynvec *sub_dirs_vec);
198 extern int chdir_to_link (struct file_link* dir_link);
199 extern void prune_file_names (char *str, struct file_link *from_link);
200 extern void include_languages (char *lang_names);
201 extern void exclude_languages (char *lang_names);
202 
203 extern char *absolute_file_name (char *buffer, struct file_link const *flink);
204 extern char *maybe_relative_file_name (char *buffer, struct file_link const *to_link, struct file_link const *from_link);
205 extern char const *locate_id_file_name (char const *arg);
206 
207 extern int tree8_count_levels (unsigned int cardinality) _GL_ATTRIBUTE_CONST;
208 extern int gets_past_00 (char *tok, FILE *input_FILE);
209 extern int skip_past_00 (FILE *input_FILE);
210 
211 extern int links_depth (struct file_link const *flink) _GL_ATTRIBUTE_PURE;
212 
213 #if HAVE_LINK
214 
215 extern struct member_file *find_member_file (struct file_link const *flink);
216 
217 #endif
218 
219 extern struct idhead idh;
220 
221 extern int walker_verbose_flag;
222 
223 extern off_t largest_member_file;
224 #define MAX_LARGEST_MEMBER_FILE (2*1024*1024-1)
225 
226 #define DEFAULT_ID_FILE_NAME "ID"
227 
228 #endif /* not _idfile_h_ */
229