]> err.no Git - sope/blob - sope-appserver/NGObjWeb/wod.m
fixed makefiles to search FHS locations at the last resort
[sope] / sope-appserver / NGObjWeb / wod.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #import <Foundation/Foundation.h>
24 #include "WODParser.h"
25
26 @interface MyWODHandler : NSObject < WODParserHandler >
27 @end
28
29 @implementation MyWODHandler
30
31 - (BOOL)parser:(id)_parser willParseDeclarationData:(NSData *)_data {
32   return YES;
33 }
34 - (void)parser:(id)_parser finishedParsingDeclarationData:(NSData *)_data
35   declarations:(NSDictionary *)_decls
36 {
37 }
38 - (void)parser:(id)_parser failedParsingDeclarationData:(NSData *)_data
39   exception:(NSException *)_exception
40 {
41   [_exception raise];
42 }
43
44 - (id)parser:(id)_parser makeAssociationWithValue:(id)_value {
45   return nil;
46 }
47 - (id)parser:(id)_parser makeAssociationWithKeyPath:(NSString *)_keyPath {
48   return nil;
49 }
50 - (id)parser:(id)_parser makeDefinitionForComponentNamed:(NSString *)_cname
51   associations:(id)_entry
52   elementName:(NSString *)_elemName
53 {
54   return nil;
55 }
56
57 @end /* MyWODHandler */
58
59 static void processFile(NSString *path) {
60   NSUserDefaults *ud;
61   NSData         *data;
62   NSDictionary   *mappings;
63   NSException    *e;
64   MyWODHandler   *wodHandler;
65
66   wodHandler = [[[MyWODHandler alloc] init] autorelease];
67   ud         = [NSUserDefaults standardUserDefaults];
68   mappings   = nil;
69   e          = nil;
70   
71   if ((data = [NSData dataWithContentsOfFile:path]) == nil) {
72     fprintf(stderr, "%s: could not open file.\n", [path cString]);
73     fflush(stderr);
74     return;
75   }
76   if ([data length] == 0)
77     /* no content */
78     return;
79   
80   NS_DURING {
81     id parser;
82
83     parser = [[[WODParser alloc] initWithHandler:(id)wodHandler] autorelease];
84     
85     mappings = [parser parseDeclarationData:data];
86   }
87   NS_HANDLER {
88     e = [localException retain];
89     //abort();
90   }
91   NS_ENDHANDLER;
92   
93   if (e) {
94     NSDictionary *ui;
95     int line = -1;
96     
97     if ((ui = [e userInfo]))
98       line = [[ui objectForKey:@"line"] intValue];
99
100     if (line > 0) {
101       fprintf(stderr, "%s:%i: %s\n",
102               [path cString], line,
103               [[e reason] cString]);
104     }
105     else {
106       fprintf(stderr, "%s: %s\n",
107               [path cString],
108               [[e reason] cString]);
109     }
110   }
111   
112   if ([ud objectForKey:@"print"]) {
113     NSEnumerator *e;
114     NSString *key;
115
116     e = [mappings keyEnumerator];
117     while ((key = [e nextObject])) {
118       id value;
119       
120       value = [mappings objectForKey:key];
121       printf("%s: %s\n", [key cString], [[value description] cString]);
122     }
123   }
124   
125   if ([ud objectForKey:@"list"]) {
126     id       keys;
127     NSString *key;
128
129     keys = [mappings allKeys];
130     keys = [keys sortedArrayUsingSelector:@selector(compare:)];
131     keys = [keys objectEnumerator];
132     
133     while ((key = [keys nextObject])) {
134       id value;
135       
136       value = [mappings objectForKey:key];
137       printf("%s: %s\n", [key cString], [[value description] cString]);
138     }
139   }
140 }
141
142 int main(int argc, char **argv, char **env) {
143   NSUserDefaults *ud;
144   NSAutoreleasePool *pool;
145   NSArray      *args;
146   id           paths;
147   NSString     *path;
148   int          i;
149   
150 #if LIB_FOUNDATION_LIBRARY
151   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
152 #endif
153
154   pool = [[NSAutoreleasePool alloc] init];
155   args = [[NSProcessInfo processInfo] arguments];
156
157   if (argc < 1)
158     exit(0);
159   
160   ud    = [NSUserDefaults standardUserDefaults];
161   *(&paths) = [NSMutableArray array];
162
163   for (*(&i) = 1; i < argc; i++) {
164     if (argv[i][0] == '-') {
165       NS_DURING 
166         [ud setObject:[NSNumber numberWithBool:YES]
167             forKey:[NSString stringWithCString:&(argv[i][1])]];
168       NS_HANDLER
169         abort();
170       NS_ENDHANDLER;
171     }
172     else {
173       [paths addObject:[NSString stringWithCString:argv[i]]];
174     }
175   }
176   paths = [paths objectEnumerator];
177   
178   while ((path = [paths nextObject])) {
179     NSAutoreleasePool *pool2;
180
181     pool2 = [[NSAutoreleasePool alloc] init];
182     processFile(path);
183     [pool2 release];
184   }
185   
186   [pool release];
187   exit(0);
188   return 0;
189 }