File: mdfind.m

package info (click to toggle)
gworkspace 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 20,832 kB
  • sloc: objc: 69,382; sh: 488; makefile: 39
file content (281 lines) | stat: -rw-r--r-- 7,236 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* mdfind.m
 *  
 * Copyright (C) 2006 Free Software Foundation, Inc.
 *
 * Author: Enrico Sersale <enrico@dtedu.net>
 * Date: October 2006
 *
 * This file is part of the GNUstep GWorkspace application
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
 */

#include <Foundation/Foundation.h>
#include "MDKQuery.h"

@interface MDFind : NSObject
{ 
  MDKQuery *query;
  unsigned rescount;
  NSString *searchdir;
  BOOL repscore;
  BOOL onlycount;
}

- (id)initWithArguments:(NSArray *)args;

- (void)queryDidStartGathering:(MDKQuery *)query;

- (void)appendRawResults:(NSArray *)lines;

- (void)queryDidEndGathering:(MDKQuery *)query;

- (void)printAttributesList;

- (void)printAttributeDescription:(NSString *)attribute;

- (void)printHelp;

@end


@implementation MDFind

- (id)initWithArguments:(NSArray *)args
{
  self = [super init];

  if (self) {
    unsigned count = [args count];
    unsigned pos = 1;
    BOOL runquery = YES;
    unsigned i;
    
    if (count <= 1) {
      GSPrintf(stderr, @"mdfind: too few arguments supplied!\n");
      [self printHelp];
      return self;
    }

    searchdir = nil;
    repscore = NO;
    onlycount = NO;
    rescount = 0;
    
    for (i = 1; i < count; i++) {
      NSString *arg = [args objectAtIndex: i];
  
      if ([arg isEqual: @"-h"]) {
        [self printHelp];
        runquery = NO;

      } else if ([arg isEqual: @"-a"]) {
        if ((i + 1) < count) {
          [self printAttributeDescription: [args objectAtIndex: (i + 1)]];
        } else {
          [self printAttributesList];
        }
        runquery = NO;

      } else if ([arg isEqual: @"-s"]) {
        repscore = YES; 
        pos++;

      } else if ([arg isEqual: @"-c"]) {
        onlycount = YES; 
        pos++;

      } else if ([arg isEqual: @"-onlyin"]) {
        BOOL pathok = YES;

        if (i++ < count) {
          arg = [args objectAtIndex: i];

          if ([[NSFileManager defaultManager] fileExistsAtPath: arg]) {
            ASSIGN (searchdir, arg);
            pos += 2;
          } else {
            pathok = NO;
          }
        } else {
          pathok = NO;
        }

        if (pathok == NO) {
          GSPrintf(stderr, @"mdfind: no search path or invalid path supplied!\n");
          runquery = NO;
        }
      }   
    }
  
    if ((pos < count) && runquery) {
      NSArray *queryargs = [args subarrayWithRange: NSMakeRange(pos, count - pos)];
      NSString *qstr = [queryargs componentsJoinedByString: @" "];

	    NS_DURING
	      {
      NSArray *dirs = (searchdir ? [NSArray arrayWithObject: searchdir] : nil);  
      
      ASSIGN (query, [MDKQuery queryFromString: qstr inDirectories: dirs]);            
      [query setDelegate: self];
      [query setReportRawResults: YES];
      [query startGathering];
        }
	    NS_HANDLER
	      {
      GSPrintf(stderr, @"mdfind: %@\n", localException);
      exit(EXIT_FAILURE);
	      }
	    NS_ENDHANDLER
    }  
  }
  
  return self;
}

- (void)queryDidStartGathering:(MDKQuery *)query
{

}

- (void)appendRawResults:(NSArray *)lines
{
  if (onlycount == NO) {
    unsigned i;

    for (i = 0; i < [lines count]; i++) {
      NSArray *line = [lines objectAtIndex: i];
      NSString *path = [line objectAtIndex: 0];

      GSPrintf(stdout, @"%@", path);
      
      if (repscore) {
        GSPrintf(stdout, @" %@", [[line objectAtIndex: 1] description]);
      }
      
      GSPrintf(stdout, @"\n");
    }

  } else {
    rescount += [lines count];
  }
}

- (void)queryDidEndGathering:(MDKQuery *)query
{
  if (onlycount) {
    GSPrintf(stdout, @"%i\n", rescount);
  }
    
  exit(EXIT_SUCCESS);
}

- (void)printAttributesList
{
  NSArray *attributes = [MDKQuery attributesNames];
  unsigned i;
  
  for (i = 0; i < [attributes count]; i++) {
    GSPrintf(stderr, @"%@\n", [attributes objectAtIndex: i]);
  }
}

- (void)printAttributeDescription:(NSString *)attribute
{
  NSString *description = [MDKQuery attributeDescription: attribute];

  if (description) {
    GSPrintf(stderr, @"%@\n", description);
  } else {
    GSPrintf(stderr, @"%@: invalid attribute name!\n", attribute);
  }
}

- (void)printHelp
{
  GSPrintf(stderr,
      @"\n"
      @"The 'mdfind' tool finds files matching a given query\n"
      @"\n"
      @"usage: mdfind [arguments] query\n"
      @"\n"
      @"Arguments:\n"
      @"  -onlyin 'directory'    limits the the search to 'directory'.\n"
      @"  -s                     reports also the score for each found path.\n"
      @"  -c                     reports only the count of the found paths.\n"
      @"  -a [attribute]         if 'attribute' is supplied, prints the attribute\n"
      @"                         description, else prints the attributes list.\n"
      @"  -h                     shows this help and exit.\n"
      @"\n"
      @"The query have the format: attribute  operator  value\n"
      @"where 'attribute' is one of the attributes used by the mdextractor\n"
      @"tool when indexing (type 'mdfind -a' for the attribute list),\n"
      @"and 'operator' is one of the following:\n"
      @"  ==   equal\n"
      @"  !=   not equal\n"
      @"  <    less than (only for numeric values and dates)\n"
      @"  <=   less than or equal (only for numeric values and dates)\n"
      @"  >    greater than (only for numeric values and dates)\n"
      @"  >=   greater than or equal (only for numeric values and dates)\n"
      @"\n"
      @"Value comparison modifiers for string values:\n"
      @"Appending the 'c' character to the search value (ex. \"value\"c),\n"
      @"makes the query case insensitive.\n"      
      @"You can use the '*' wildcard to match substrings anywhere in the\n"
      @"search value.\n"
      @"\n"
      @"Combining queries:\n"
      @"Queries can be combined using '&&' for AND and '||' for OR and\n"
      @"parenthesis to define nesting criteria.\n"
      @"\n"
  );
}

@end


int main(int argc, char **argv, char **env)
{
  NSAutoreleasePool	*pool;
  NSProcessInfo *proc;
  MDFind *mdfind;
  
#ifdef GS_PASS_ARGUMENTS
  [NSProcessInfo initializeWithArguments: argv count: argc environment: env];
#endif

  pool = [NSAutoreleasePool new];
  proc = [NSProcessInfo processInfo];
  
  if (proc == nil) {
    GSPrintf(stderr, @"mdfind: unable to get process information!\n");
    RELEASE (pool);
    exit(EXIT_FAILURE);
  }

  mdfind = [[MDFind alloc] initWithArguments: [proc arguments]];
  
  RELEASE (pool);

  if (mdfind != nil) {
	  CREATE_AUTORELEASE_POOL (pool);
    [[NSRunLoop currentRunLoop] run];
  	RELEASE (pool);
  }
  
  exit(EXIT_SUCCESS);
}