1 /* $Id: tmesh-impl.h,v 1.3 2006/11/15 23:11:56 fredette Exp $ */
2 
3 /* tmesh/tmesh-impl.h - private header file for the tmesh implementation: */
4 
5 /*
6  * Copyright (c) 2003 Matt Fredette
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Matt Fredette.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _TMESH_IMPL_H
37 #define _TMESH_IMPL_H
38 
39 #include <tme/common.h>
40 _TME_RCSID("$Id: tmesh-impl.h,v 1.3 2006/11/15 23:11:56 fredette Exp $");
41 
42 /* includes: */
43 #include <tme/element.h>
44 #include <tme/tmesh.h>
45 #include <errno.h>
46 
47 /* macros: */
48 
49 /* commands: */
50 #define TMESH_COMMAND_NOP	(0)
51 #define TMESH_COMMAND_SOURCE	(1)
52 #define TMESH_COMMAND_MKDIR	(2)
53 #define TMESH_COMMAND_RMDIR	(3)
54 #define TMESH_COMMAND_CD	(4)
55 #define TMESH_COMMAND_PWD	(5)
56 #define TMESH_COMMAND_LS	(6)
57 #define TMESH_COMMAND_CONNECT	(7)
58 #define TMESH_COMMAND_RM	(8)
59 #define TMESH_COMMAND_MV	(9)
60 #define TMESH_COMMAND_COMMAND	(10)
61 #define TMESH_COMMAND_LOG	(11)
62 #define TMESH_COMMAND_ALIAS	(12)
63 
64 /* directory entry types: */
65 #define TMESH_FS_DIRENT_DIR	(0)
66 #define TMESH_FS_DIRENT_ELEMENT	(1)
67 
68 /* lookup/find flags: */
69 #define TMESH_SEARCH_NORMAL		(0)
70 #define TMESH_SEARCH_LAST_PART_OK	TME_BIT(0)
71 #define TMESH_SEARCH_NO_RECURSE		TME_BIT(1)
72 
73 /* memory allocation: */
74 #define _tmesh_gc_new(s, t, x)		((t *) _tmesh_gc_malloc(s, sizeof(t) * (x)))
75 #define _tmesh_gc_renew(s, t, m, x)	((t *) _tmesh_gc_realloc(s, m, sizeof(t) * (x)))
76 
77 /* a garbage collection record: */
78 struct tmesh_gc_record {
79 
80   /* the next and previous records on the list: */
81   struct tmesh_gc_record *tmesh_gc_record_next;
82   struct tmesh_gc_record **tmesh_gc_record_prev;
83 
84   /* the memory to garbage collect: */
85   void *tmesh_gc_record_mem;
86 };
87 
88 /* a stack of ios: */
89 struct tmesh_io_stack {
90 
91   /* the next io on the stack: */
92   struct tmesh_io_stack *tmesh_io_stack_next;
93 
94   /* the io itself: */
95   struct tmesh_io tmesh_io_stack_io;
96 };
97 
98 /* the scanner state: */
99 struct tmesh_scanner {
100 
101   /* nonzero iff we need to increment the line number: */
102   unsigned int tmesh_scanner_next_line;
103 
104   /* any next token to return: */
105   int tmesh_scanner_token_next;
106 
107   /* any next character to get: */
108   int tmesh_scanner_c_next;
109 
110   /* nonzero iff we are inside a comment: */
111   int tmesh_scanner_in_comment;
112 
113   /* nonzero iff we are inside quotes: */
114   int tmesh_scanner_in_quotes;
115 
116   /* nonzero iff we are in arguments: */
117   int tmesh_scanner_in_args;
118 
119   /* the collected token: */
120   char *tmesh_scanner_token_string;
121   unsigned int tmesh_scanner_token_string_len;
122   unsigned int tmesh_scanner_token_string_size;
123 };
124 
125 /* a parser argv: */
126 struct tmesh_parser_argv {
127 
128   /* the argument count: */
129   unsigned int tmesh_parser_argv_argc;
130 
131   /* the size of the argument vector: */
132   unsigned int tmesh_parser_argv_size;
133 
134   /* the argument vector: */
135   char **tmesh_parser_argv_argv;
136 };
137 
138 /* the parser value structure: */
139 struct tmesh_parser_value {
140 
141   /* a token: */
142   int tmesh_parser_value_token;
143 #define tmesh_parser_value_command tmesh_parser_value_token
144 
145   /* up to two strings: */
146   char *tmesh_parser_value_strings[2];
147 #define tmesh_parser_value_pathname0 tmesh_parser_value_strings[0]
148 #define tmesh_parser_value_pathname1 tmesh_parser_value_strings[1]
149 #define tmesh_parser_value_arg tmesh_parser_value_strings[0]
150 
151   /* up to three argument vectors: */
152   struct tmesh_parser_argv tmesh_parser_value_argvs[3];
153 };
154 
155 /* a directory entry: */
156 struct tmesh_fs_dirent {
157 
158   /* the next and previous entries in this directory: */
159   struct tmesh_fs_dirent *tmesh_fs_dirent_next;
160   struct tmesh_fs_dirent **tmesh_fs_dirent_prev;
161 
162   /* the type of this directory entry: */
163   int tmesh_fs_dirent_type;
164 
165   /* the name in this directory entry: */
166   char *tmesh_fs_dirent_name;
167 
168   /* the value in this directory entry: */
169   void *tmesh_fs_dirent_value;
170 };
171 
172 /* an element: */
173 struct tmesh_fs_element {
174 
175   /* the parent directory of this element: */
176   struct tmesh_fs_dirent *tmesh_fs_element_parent;
177 
178   /* the real element: */
179   struct tme_element tmesh_fs_element_element;
180 
181   /* the generation number of this element: */
182   unsigned long tmesh_fs_element_gen;
183 
184   /* the arguments for this element: */
185   struct tmesh_parser_argv tmesh_fs_element_argv;
186 
187   /* the element connections: */
188   struct tmesh_fs_element_conn {
189 
190     /* the next element connection: */
191     struct tmesh_fs_element_conn *tmesh_fs_element_conn_next;
192 
193     /* backpointer to the element: */
194     struct tmesh_fs_element *tmesh_fs_element_conn_element;
195 
196     /* the generation number of this connection: */
197     unsigned long tmesh_fs_element_conn_gen;
198 
199     /* the other side of this element connection: */
200     struct tmesh_fs_element_conn *tmesh_fs_element_conn_other;
201 
202     /* the arguments for this side of the connection: */
203     struct tmesh_parser_argv tmesh_fs_element_conn_argv;
204   } *tmesh_fs_element_conns;
205 };
206 
207 /* the tmesh structure: */
208 struct tmesh {
209 
210   /* the stack of ios: */
211   struct tmesh_io_stack *tmesh_io_stack;
212 
213   /* the scanner: */
214   struct tmesh_scanner tmesh_scanner;
215 
216   /* the root directory: */
217   struct tmesh_fs_dirent *tmesh_root;
218 
219   /* the current working directory: */
220   struct tmesh_fs_dirent *tmesh_cwd;
221 
222   /* garbage-collectable memory: */
223   struct tmesh_gc_record *tmesh_gc_record;
224 
225   /* the last generation number: */
226   unsigned long tmesh_gen_last;
227 
228   /* the support: */
229   struct tmesh_support tmesh_support;
230 };
231 
232 /* prototypes: */
233 int _tmesh_yyparse _TME_P((struct tmesh *, struct tmesh_parser_value *, char **, int *));
234 void _tmesh_io_collect_output _TME_P((char **, const char *));
235 void _tmesh_io_collect_outputn _TME_P((char **, const char *, unsigned int));
236 int _tmesh_fs_lookup _TME_P((struct tmesh *, char **, struct tmesh_fs_dirent **, struct tmesh_fs_dirent **, char **, int));
237 struct tmesh_fs_dirent * _tmesh_fs_link _TME_P((struct tmesh_fs_dirent *, char *, int, void *));
238 void _tmesh_fs_unlink _TME_P((struct tmesh_fs_dirent *));
239 struct tmesh_fs_dirent * _tmesh_fs_mkdir _TME_P((struct tmesh_fs_dirent *, char *));
240 void _tmesh_fs_pathname_dir _TME_P((struct tmesh_fs_dirent *, char **, struct tmesh_fs_dirent *));
241 void _tmesh_fs_pathname_element _TME_P((struct tmesh_fs_element *, char **, struct tmesh_fs_dirent *));
242 void *_tmesh_gc_malloc _TME_P((struct tmesh *, unsigned int));
243 void *_tmesh_gc_realloc _TME_P((struct tmesh *, void *, unsigned int));
244 void _tmesh_gc_free _TME_P((struct tmesh *, void *));
245 void _tmesh_gc_release _TME_P((struct tmesh *, void *));
246 void _tmesh_gc_release_argv _TME_P((struct tmesh *, struct tmesh_parser_argv *));
247 void _tmesh_gc_gc _TME_P((struct tmesh *));
248 
249 #endif /* !_TMESH_IMPL_H */
250