]> code.delx.au - osx-proxyconf/commitdiff
Moved from a C implementation of the sysconfig program to a cocoa version
authorGreg Darke <greg+laptop@tsukasa.net.au>
Thu, 7 Feb 2008 12:55:29 +0000 (23:55 +1100)
committerGreg Darke <greg+laptop@tsukasa.net.au>
Thu, 7 Feb 2008 12:55:29 +0000 (23:55 +1100)
Makefile
sysconfig.c [deleted file]
sysconfig.m [new file with mode: 0644]

index 3295fda721775b7474ead13ea241674425d77149..06c71bccce4d07a80e94bde162d34db7c889f240 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,10 @@
-CFLAGS     := --std=c99 -Wall -W $(CFLAGS)
-FRAMEWORKS := -framework CoreFoundation -framework SystemConfiguration 
+CFLAGS     := -Wall -W $(CFLAGS)
+FRAMEWORKS := -framework CoreFoundation -framework SystemConfiguration -framework Foundation
 
-sysconfig: sysconfig.c
-       gcc $(CFLAGS) $(FRAMEWORKS) -o sysconfig sysconfig.c
+sysconfig: sysconfig.m
+       $(CC) $< -o $@ $(CFLAGS) $(FRAMEWORKS)
 
 clean:
        rm -f sysconfig
 
+.PHONY: clean
diff --git a/sysconfig.c b/sysconfig.c
deleted file mode 100644 (file)
index 77ad88f..0000000
+++ /dev/null
@@ -1,157 +0,0 @@
-#include <CoreFoundation/CoreFoundation.h>
-#include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
-
-const char* KEYFILE = "/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCSchemaDefinitions.h";
-
-Boolean
-printCFTypeRef(CFTypeRef value);
-
-
-Boolean
-getDictValue(CFTypeRef* value, CFStringRef key)
-{
-       assert(value != NULL);
-       assert(key != NULL);
-
-       Boolean result;
-       CFDictionaryRef dictRef;
-       dictRef = SCDynamicStoreCopyProxies((SCDynamicStoreRef)NULL);
-       result = (dictRef != NULL);
-       if(result) {
-               *value = (CFNumberRef)CFDictionaryGetValue(dictRef, key);
-       }
-       if(*value != NULL) {
-               CFRetain(*value);
-       }
-
-       if(dictRef != NULL) {
-               CFRelease(dictRef);
-       }
-       if(!result) {
-               *value = NULL;
-       }
-       return result;
-}
-
-Boolean
-printNumber(CFNumberRef numberRef)
-{
-       assert(numberRef != NULL);
-
-       int numberVal = 0;
-       if(CFNumberGetValue(numberRef, kCFNumberIntType, &numberVal)) {
-               printf("%d\n", numberVal);
-               return TRUE;
-       }
-       return FALSE;
-}
-
-Boolean
-printString(CFStringRef strRef)
-{
-       assert(strRef != NULL);
-
-       char strVal[1024];
-       if(CFStringGetCString(strRef, strVal, (CFIndex)1024,
-                             kCFStringEncodingASCII))
-       {
-               printf("%s\n", strVal);
-               return TRUE;
-       }
-       return FALSE;
-}
-
-Boolean
-printArray(CFArrayRef arrayRef)
-{
-       assert(arrayRef != NULL);
-
-       int length = CFArrayGetCount(arrayRef);
-       for(int i = 0; i < length; ++i) {
-               if(!printCFTypeRef(CFArrayGetValueAtIndex(arrayRef, i))) {
-                       return FALSE;
-               }
-       }
-
-       return TRUE;
-}
-
-Boolean
-printCFTypeRef(CFTypeRef value)
-{
-       if(value == NULL) {
-               return FALSE;
-       }
-       else if(CFStringGetTypeID() == CFGetTypeID(value)) {
-               return printString((CFStringRef)value);
-       }
-       else if(CFNumberGetTypeID() == CFGetTypeID(value)) {
-               return printNumber((CFNumberRef)value);
-       }
-       else if(CFArrayGetTypeID()  == CFGetTypeID(value)) {
-               return printArray((CFArrayRef)value);
-       }
-       else {
-               return FALSE;
-       }
-}
-
-CFStringRef
-createCFString(const char* str)
-{
-       return CFStringCreateWithCStringNoCopy(NULL, str,
-                                              kCFStringEncodingASCII,
-                                              kCFAllocatorNull);
-}
-
-void
-usage(const char* program)
-{
-       fprintf(stderr, "Usage: %s [-q] key\n", program);
-       fprintf(stderr, "Look in %s for keys. Eg, HTTPProxy\n\n", KEYFILE);
-}
-
-int
-main(int argc, char** argv)
-{
-       int quiet = 0;
-       const char* key;
-       if(argc == 2) {
-               key = argv[1];
-       } else if(argc == 3 && strcmp("-q", argv[1]) == 0) {
-               quiet = 1;
-               key = argv[2];
-       } else {
-               usage(argv[0]);
-               return 1;
-       }
-
-
-       CFStringRef keyRef = createCFString(key);
-       if(keyRef == NULL) {
-               fprintf(stderr, "Fatal error: Couldn't create CFStringRef from arg2\n");
-               return 1;
-       }
-
-       CFTypeRef valueRef = NULL;
-       if(!getDictValue(&valueRef, keyRef)) {
-               fprintf(stderr, "Fatal error: Error accessing dictionary\n");
-               return 1;
-       }
-
-       if(valueRef == NULL) {
-               if(!quiet) {
-                       fprintf(stderr, "No value for that key\n");
-               }
-               return 0;
-       }
-       if(!printCFTypeRef(valueRef)) {
-               fprintf(stderr, "Fatal error: Unsupported value type in dictionary\n");
-               CFRelease(valueRef);
-               return 1;
-       }
-
-       CFRelease(valueRef);
-       return 0;
-}
-
diff --git a/sysconfig.m b/sysconfig.m
new file mode 100644 (file)
index 0000000..37360d7
--- /dev/null
@@ -0,0 +1,83 @@
+#import <Foundation/Foundation.h>
+#import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
+
+// Begin the nasty hack so that I can easily print out NSStrings to stdout
+void fNSPrint(NSString * outputFilename, NSString * str)
+{
+       NSString * output = [NSString stringWithFormat:@"%@\n", str];
+       [output writeToFile:outputFilename atomically:NO 
+                               encoding:NSUTF8StringEncoding error:nil];
+}
+
+void NSPrint(NSString *str)
+{
+       fNSPrint(@"/dev/stdout", str);
+}
+
+bool printSystemConfiguration(bool quiet, NSString * keyName, NSDictionary * proxies)
+{
+       id value = [proxies objectForKey:keyName];
+       if (nil != value) {
+               NSArray * itemsArray;
+               if ([value isKindOfClass:[NSArray class]]) {
+                       itemsArray = value;
+               } else {
+                       itemsArray = [NSArray arrayWithObjects:value, nil];
+               }
+
+               NSEnumerator * enumerator = [itemsArray objectEnumerator];
+               id item;
+               while ((item = [enumerator nextObject]))
+                       NSPrint([NSString stringWithFormat:@"%@", item]);
+
+       } else {
+               if (!quiet)
+                       fNSPrint(@"/dev/stderr", @"Value does not exist");
+               return FALSE; // Signal a fail condition
+       }
+       return TRUE;
+}
+
+void printUsage(void)
+{
+       fNSPrint(@"/dev/stderr", @"proxyconf [-q] SystemConfigurationKeyName");
+}
+
+int resolveSystemConfiguration(int argc, const char * argv[])
+{
+       NSDictionary * proxies = (NSDictionary *)SCDynamicStoreCopyProxies(nil);
+       
+       bool quiet = FALSE;
+       NSString * keyName;
+
+       if (2 == argc) {
+               keyName = [NSString stringWithCString:argv[1]];
+       } else if (3 == argc) {
+               if (! [[NSString stringWithCString:argv[1]] isEqualTo:@"-q"]) {
+                       printUsage();
+                       return 1;
+               } else {
+                       quiet = TRUE;
+               }
+
+               keyName = [NSString stringWithCString:argv[2]];
+       } else {
+               printUsage();
+               return 1;
+       }
+
+       if (! printSystemConfiguration(quiet, keyName, proxies))
+               return 2;
+
+       return 0;
+}
+
+int main (int argc, const char * argv[])
+{
+       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+
+       int ret = resolveSystemConfiguration(argc, argv);
+       
+    [pool drain];
+    return ret;
+}