Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (c) 2002 Julien Jalon <jalon@julien-jalon.org>
- // See license info at the end of this file.
- #import <Foundation/Foundation.h>
- #include <CoreFoundation/CoreFoundation.h>
- #include <SystemConfiguration/SystemConfiguration.h>
- #define PRINT(X) printf("%s\n", [[NSString stringWithFormat:@"%@", (X)] ISOLatin1EncodedCString])
- // Encoding String additions
- @interface NSString (EncodedCStringAdditions)
- + (NSString *)stringWithCString:(const char *)cString encoding:(NSStringEncoding)encoding;
- - (const char *)cStringUsingEncoding:(NSStringEncoding)encoding;
- + (NSString *)stringWithISOLatin1EncodedCString:(const char *)cString;
- - (const char *)ISOLatin1EncodedCString;
- @end
- @implementation NSString (EncodedCStringAdditions)
- + (NSString *)stringWithCString:(const char *)cString encoding:(NSStringEncoding)encoding {
- return [[[NSString alloc] initWithData:[NSData dataWithBytes:cString length:strlen(cString)] encoding:encoding] autorelease];
- }
- - (const char *)cStringUsingEncoding:(NSStringEncoding)encoding {
- char *returnValue;
- NSData *encodedData=[self dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:YES];
- unsigned dataLength=[encodedData length];
- returnValue = [[NSMutableData dataWithLength:dataLength+1] mutableBytes];
- memcpy(returnValue,(char *)[encodedData bytes],dataLength);
- returnValue[dataLength]='\0';
- return returnValue;
- }
- + (NSString *)stringWithISOLatin1EncodedCString:(const char *)cString {
- return [NSString stringWithCString:cString encoding:NSISOLatin1StringEncoding];
- }
- - (const char *)ISOLatin1EncodedCString {
- return [self cStringUsingEncoding:NSISOLatin1StringEncoding];
- }
- @end
- SCPreferencesRef prefRef;
- void usage() {
- NSString *commandpath = [[NSProcessInfo processInfo] processName];
- NSString *licence = @"This program is free software; you can redistribute it and/or modify\nit under the terms of the GNU General Public License version 2 as published\nby the Free Software Foundation.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program; if not, write to the Free Software\nFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA\n02111-1307, USA.";
- NSString *usageString = [NSString stringWithFormat:
- @"%@ view (sets|current|<setid>)\n%@ viewservice <serviceid> [<property>]\n%@ change <serviceid> <property> <key> (string|array) <value1> [<value2> ...]\n\n%@", commandpath, commandpath, commandpath, licence];
- PRINT(usageString);
- exit(0);
- }
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- NSString *output;
- NSArray *arguments;
- NSString *command, *objId;
- arguments = [[NSProcessInfo processInfo] arguments];
- if([arguments count]<2) {
- usage();
- }
- command = [arguments objectAtIndex:1];
- objId = [arguments objectAtIndex:2];
- if(!(command && objId)) {
- usage();
- }
- prefRef = SCPreferencesCreate(kCFAllocatorSystemDefault, (CFStringRef)@"XXX", NULL);
- if([command isEqualToString:@"view"]) {
- NSString *set = objId;
- if([set isEqualToString:@"sets"]) {
- NSDictionary *allSets = (NSDictionary *)SCPreferencesGetValue(prefRef, kSCPrefSets);
- NSEnumerator *setEnumerator = [allSets keyEnumerator];
- NSString *setId;
- while(setId = [setEnumerator nextObject]) {
- NSString *setStatus = [NSString stringWithFormat:@"%@ - %@", setId,
- [[allSets objectForKey:setId] objectForKey:(NSString *)kSCPropUserDefinedName]];
- PRINT(setStatus);
- }
- } else {
- NSString *setPath;
- NSDictionary *setDict;
- NSString *setName;
- NSArray *setServices;
- if([set isEqualToString:@"current"]) {
- setPath = (NSString *)SCPreferencesGetValue(prefRef, kSCPrefCurrentSet);
- } else {
- setPath = [NSString stringWithFormat:@"/%@/%@", kSCPrefSets, set];
- }
- setDict = (NSDictionary *)SCPreferencesPathGetValue(prefRef, (CFStringRef)setPath);
- if(!setDict) {
- PRINT(@"Set not found");
- exit(1);
- }
- setName = [setDict objectForKey:(NSString *)kSCPropUserDefinedName];
- setServices = [[[setDict objectForKey:(NSString *)kSCCompNetwork] objectForKey:(NSString *)kSCCompService] allKeys];
- {
- NSEnumerator *serviceEnumerator = [setServices objectEnumerator];
- NSString *serviceId;
- output = [NSString stringWithFormat:@"Set %@:", setName];
- PRINT(output);
- while(serviceId = [serviceEnumerator nextObject]) {
- NSString *servicePath = [NSString stringWithFormat:@"/%@/%@", (NSString *)kSCPrefNetworkServices, serviceId];
- NSDictionary *service = (NSDictionary *)SCPreferencesPathGetValue(prefRef, (CFStringRef)servicePath);
- NSString *serviceName = [service objectForKey:(NSString *)kSCPropUserDefinedName];
- BOOL isActive = ![service objectForKey:(NSString *)kSCResvInactive];
- NSString *output = [NSString stringWithFormat:@"%@ - %@ (%@)", serviceId, serviceName, (isActive?@"Active":@"Inactive")];
- PRINT(output);
- }
- }
- }
- } else if([command isEqualToString:@"viewservice"]) {
- NSString *serviceId = objId;
- NSString *servicePath = [NSString stringWithFormat:@"/%@/%@", (NSString *)kSCPrefNetworkServices, serviceId];
- NSDictionary *service = (NSDictionary *)SCPreferencesPathGetValue(prefRef, (CFStringRef)servicePath);
- NSString *serviceName;
- BOOL isActive;
- NSString *output;
- NSEnumerator *servicePropsEnumerator;
- NSString *serviceProp;
- id serviceProperty;
- if(!service) {
- PRINT(@"Service not found");
- exit(1);
- }
- serviceName = [service objectForKey:(NSString *)kSCPropUserDefinedName];
- isActive = ![service objectForKey:(NSString *)kSCResvInactive];
- output = [NSString stringWithFormat:@"Service %@ (%@):", serviceName, (isActive?@"Active":@"Inactive")];
- PRINT(output);
- if([arguments count] == 4) {
- serviceProp = [arguments objectAtIndex:3];
- serviceProperty = [service objectForKey:serviceProp];
- if(!serviceProperty) {
- PRINT(@"Property not found");
- exit(1);
- } else {
- output = [NSString stringWithFormat:@"%@ = %@", serviceProp, serviceProperty];
- PRINT(output);
- }
- } else {
- servicePropsEnumerator = [service keyEnumerator];
- while(serviceProp = [servicePropsEnumerator nextObject]) {
- serviceProperty = [service objectForKey:serviceProp];
- if([serviceProperty respondsToSelector:@selector(objectForKey:)]) {
- output = [NSString stringWithFormat:@"%@ = %@", serviceProp, serviceProperty];
- PRINT(output);
- }
- }
- }
- } else if([command isEqualToString:@"change"]) {
- int argcount = [arguments count];
- if(argcount<7) {
- usage();
- }
- {
- NSString *serviceId = [arguments objectAtIndex:2];
- NSString *propId = [arguments objectAtIndex:3];
- NSString *key = [arguments objectAtIndex:4];
- NSString *type = [arguments objectAtIndex:5];
- NSString *servicePath = [NSString stringWithFormat:@"/%@/%@", (NSString *)kSCPrefNetworkServices, serviceId];
- NSDictionary *service = (NSDictionary *)SCPreferencesPathGetValue(prefRef, (CFStringRef)servicePath);
- NSDictionary *propDict;
- id newValue = nil;
- if(!service) {
- PRINT(@"Service not found");
- exit(1);
- }
- propDict = [service objectForKey:propId];
- if(!propDict) {
- PRINT(@"Key not found in service properties");
- exit(1);
- }
- if([type isEqualToString:@"string"]) {
- if(argcount!=7) {
- usage();
- }
- newValue = [arguments objectAtIndex:6];
- } else if([type isEqualToString:@"array"]) {
- newValue = [arguments subarrayWithRange:NSMakeRange(6, argcount-6)];
- } else {
- usage();
- }
- {
- NSString *propDictPath = [NSString stringWithFormat:@"%@/%@", servicePath, propId];
- NSMutableDictionary *newPropDict = [propDict mutableCopy];
- BOOL ok;
- [newPropDict setObject: newValue forKey: key];
- ok = SCPreferencesPathSetValue(prefRef, (CFStringRef)propDictPath, (CFDictionaryRef)newPropDict);
- if(ok) {
- PRINT(@"Change succeeded");
- } else {
- PRINT(@"Change failed!");
- exit(1);
- }
- ok = SCPreferencesCommitChanges(prefRef);
- if(ok) {
- PRINT(@"Commit succeeded");
- } else {
- PRINT(@"Commit failed!");
- exit(1);
- }
- ok = SCPreferencesApplyChanges(prefRef);
- if(ok) {
- PRINT(@"Apply succeeded");
- } else {
- PRINT(@"Apply failed!");
- exit(1);
- }
- }
- }
- } else {
- usage();
- }
- [pool release];
- return 0;
- }
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License version 2 as published
- // by the Free Software Foundation.
- //
- // 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., 59 Temple Place - Suite 330, Boston, MA
- // 02111-1307, USA.
- Choose download locationDownload
Add Comment
Please, Sign In to add comment