~ubuntu-branches/ubuntu/karmic/gnustep-base/karmic

« back to all changes in this revision

Viewing changes to Tools/dread.m

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This is a simple tool to read and display defaults information
2
 
   Copyright (C) 1997 Free Software Foundation, Inc.
3
 
 
4
 
   Written by:  Richard Frith-Macdonald <richard@brainstorm.co.uk>
5
 
   Created: October 1997
6
 
 
7
 
   This file is part of the GNUstep Project
8
 
 
9
 
   This library is free software; you can redistribute it and/or
10
 
   modify it under the terms of the GNU General Public License
11
 
   as published by the Free Software Foundation; either version 2
12
 
   of the License, or (at your option) any later version.
13
 
    
14
 
   You should have received a copy of the GNU General Public  
15
 
   License along with this library; see the file COPYING.LIB.
16
 
   If not, write to the Free Software Foundation,
17
 
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
 
 
19
 
   */
20
 
 
21
 
#include "config.h"
22
 
#include        <Foundation/NSArray.h>
23
 
#include        <Foundation/NSDictionary.h>
24
 
#include        <Foundation/NSString.h>
25
 
#include        <Foundation/NSProcessInfo.h>
26
 
#include        <Foundation/NSUserDefaults.h>
27
 
#include        <Foundation/NSAutoreleasePool.h>
28
 
 
29
 
 
30
 
int
31
 
main(int argc, char** argv)
32
 
{
33
 
    NSAutoreleasePool   *pool = [NSAutoreleasePool new];
34
 
    NSUserDefaults      *defs;
35
 
    NSProcessInfo       *proc;
36
 
    NSArray             *args;
37
 
    NSArray             *domains;
38
 
    NSString            *owner = nil;
39
 
    NSString            *name = nil;
40
 
    NSString            *user = nil;
41
 
    BOOL                found = NO;
42
 
    NSDictionary        *locale;
43
 
    int                 i;
44
 
 
45
 
    proc = [NSProcessInfo processInfo];
46
 
    if (proc == nil) {
47
 
        NSLog(@"unable to get process information!\n");
48
 
        [pool release];
49
 
        exit(0);
50
 
    }
51
 
 
52
 
    args = [proc arguments];
53
 
 
54
 
    for (i = 1; i < [args count]; i++) {
55
 
        if ([[args objectAtIndex: i] isEqual: @"--help"]) {
56
 
            printf(
57
 
"\nThe 'dread' command lets you to read a user's defaults database.\n"
58
 
"WARNING - this program is obsolete - please use 'defaults read' instead.\n\n"
59
 
"Results are printed on standard output in a format suitable for input to\n"
60
 
"the 'dwrite' command.  The value of each default is quoted with \"'\" and\n"
61
 
"may wrap over line boundaries.\n"
62
 
"Single quotes used within a default value are repeated.\n\n"
63
 
"If you have read access to another user's defaults database, you may include\n"
64
 
"the '-u' flag to read that user's database rather than your own.\n\n");
65
 
            printf(
66
 
"dread [-u uname] -g key\n"
67
 
"    read the named default from the global domain.\n\n");
68
 
            printf(
69
 
"dread [-u uname] -l\n"
70
 
"    read all defaults from all domains.\n\n");
71
 
            printf(
72
 
"dread [-u uname] -n key\n"
73
 
"    read values named 'key' from all domains.\n\n");
74
 
            printf(
75
 
"dread [-u uname] -o domain\n"
76
 
"    read all defaults from the specified domain.\n\n");
77
 
            printf(
78
 
"dread [-u uname] domain key\n"
79
 
"    read default with name 'key' from domain 'domain'.\n\n");
80
 
            printf(
81
 
"dread [-u uname] key\n"
82
 
"    read default named 'key' from the global domain.\n");
83
 
            [pool release];
84
 
            exit(0);
85
 
        }
86
 
    }
87
 
 
88
 
    i = 1;
89
 
    if ([args count] <= i) {
90
 
        NSLog(@"too few arguments supplied!\n");
91
 
        [pool release];
92
 
        exit(0);
93
 
    }
94
 
    
95
 
    if ([[args objectAtIndex: i] isEqual: @"-u"]) {
96
 
        if ([args count] > ++i) {
97
 
            user = [args objectAtIndex: i++];
98
 
        }
99
 
        else {
100
 
            NSLog(@"no name supplied for -u option!\n");
101
 
            [pool release];
102
 
            exit(0);
103
 
        }
104
 
    }
105
 
 
106
 
    if ([args count] <= i) {
107
 
        NSLog(@"too few arguments supplied!\n");
108
 
        [pool release];
109
 
        exit(0);
110
 
    }
111
 
 
112
 
    if ([[args objectAtIndex: i] isEqual: @"-g"]) {
113
 
        owner = NSGlobalDomain;
114
 
        if ([args count] > ++i) {
115
 
            name = [args objectAtIndex: i];
116
 
        }
117
 
        else {
118
 
            NSLog(@"no key supplied for -g option!\n");
119
 
            [pool release];
120
 
            exit(0);
121
 
        }
122
 
    }
123
 
    else if ([[args objectAtIndex: i] isEqual: @"-n"]) {
124
 
        owner = nil;
125
 
        if ([args count] > ++i) {
126
 
            name = [args objectAtIndex: i];
127
 
        }
128
 
        else {
129
 
            NSLog(@"no key supplied for -n option!\n");
130
 
            [pool release];
131
 
            exit(0);
132
 
        }
133
 
    }
134
 
    else if ([[args objectAtIndex: i] isEqual: @"-o"]) {
135
 
        name = nil;
136
 
        if ([args count] > ++i) {
137
 
            owner = [args objectAtIndex: i];
138
 
        }
139
 
        else {
140
 
            NSLog(@"no domain name supplied for -o option!\n");
141
 
            [pool release];
142
 
            exit(0);
143
 
        }
144
 
    }
145
 
    else if ([[args objectAtIndex: i] isEqual: @"-l"]) {
146
 
        owner = nil;
147
 
        name = nil;
148
 
    }
149
 
    else {
150
 
        if ([args count] > i+1) {
151
 
            owner = [args objectAtIndex: i];
152
 
            name = [args objectAtIndex: ++i];
153
 
        }
154
 
        else {
155
 
            owner = NSGlobalDomain;
156
 
            name = [args objectAtIndex: i];
157
 
        }
158
 
    }
159
 
 
160
 
    if (user) {
161
 
        defs = [[NSUserDefaults alloc] initWithUser: user];
162
 
    }
163
 
    else {
164
 
        defs = [NSUserDefaults standardUserDefaults];
165
 
    }
166
 
    if (defs == nil) {
167
 
        NSLog(@"unable to access defaults database!\n");
168
 
        [pool release];
169
 
        exit(0);
170
 
    }
171
 
    /* We don't want dwrite in the defaults database - so remove it. */
172
 
    [defs removePersistentDomainForName: [proc processName]];
173
 
 
174
 
    locale = [defs dictionaryRepresentation];
175
 
    domains = [defs persistentDomainNames];
176
 
    for (i = 0; i < [domains count]; i++) {
177
 
        NSString        *domainName = [domains objectAtIndex: i];
178
 
 
179
 
        if (owner == nil || [owner isEqual: domainName]) {
180
 
            NSDictionary        *dom;
181
 
 
182
 
            dom = [defs persistentDomainForName: domainName];
183
 
            if (dom) {
184
 
                if (name == nil) {
185
 
                    NSEnumerator        *enumerator;
186
 
                    NSString            *key;
187
 
 
188
 
                    enumerator = [dom keyEnumerator];
189
 
                    while ((key = [enumerator nextObject]) != nil) {
190
 
                        id              obj = [dom objectForKey: key];
191
 
                        const char      *ptr;
192
 
 
193
 
                        printf("%s %s '", [domainName cString], [key cString]);
194
 
                        ptr = [[obj descriptionWithLocale: locale indent: 0]
195
 
                          cString];
196
 
                        while (*ptr) {
197
 
                            if (*ptr == '\'') {
198
 
                                putchar('\'');
199
 
                            }
200
 
                            putchar(*ptr);
201
 
                            ptr++;
202
 
                        }
203
 
                        printf("'\n");
204
 
                    }
205
 
                }
206
 
                else {
207
 
                    id          obj = [dom objectForKey: name];
208
 
 
209
 
                    if (obj) {
210
 
                        const char      *ptr;
211
 
 
212
 
                        printf("%s %s '", [domainName cString], [name cString]);
213
 
                        ptr = [[obj descriptionWithLocale: locale indent: 0]
214
 
                          cString];
215
 
                        while (*ptr) {
216
 
                            if (*ptr == '\'') {
217
 
                                putchar('\'');
218
 
                            }
219
 
                            putchar(*ptr);
220
 
                            ptr++;
221
 
                        }
222
 
                        printf("'\n");
223
 
                        found = YES;
224
 
                    }
225
 
                }
226
 
            }
227
 
        }
228
 
    }
229
 
 
230
 
    if (found == NO && name != nil) {
231
 
        printf("dread: couldn't read default\n");
232
 
    }
233
 
 
234
 
    [pool release];
235
 
    exit(0);
236
 
}
237
 
 
238