]> code.delx.au - osx-proxyconf/commitdiff
Rewrote proxyconf completely in Objective C
authorJames Bunton <jamesbunton@fastmail.fm>
Sat, 7 Feb 2009 13:26:18 +0000 (00:26 +1100)
committerJames Bunton <jamesbunton@fastmail.fm>
Sat, 7 Feb 2009 13:26:18 +0000 (00:26 +1100)
.hgignore
Makefile
README
proxyconf.m [new file with mode: 0644]
proxyconf.sh [deleted file]
sysconfig.m [deleted file]

index 11c66dc27113207adc7ebdd956b10f2d36a45952..bcdbe3b29529fbb31b0da1dbce9f57f152cd43c0 100644 (file)
--- a/.hgignore
+++ b/.hgignore
@@ -1 +1 @@
-sysconfig
+proxyconf
index 48bdc1126865db4db2d72eaca0be4be03a6b33d5..6df6746c3d858b8f671cf43e36daff854dbe6490 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,11 +1,12 @@
 CFLAGS     := -Wall -W -std=c99 -arch i386 -arch ppc $(CFLAGS)
 LDFLAGS    := $(LDFLAGS)
 FRAMEWORKS := -framework CoreFoundation -framework SystemConfiguration -framework Foundation
-TARGETS    := sysconfig
+TARGETS    := proxyconf
 
 all: $(TARGETS)
 
 %: %.m
+       export MACOSX_DEPLOYMENT_TARGET=10.4; \
        $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) $<
 
 clean:
diff --git a/README b/README
index 65e96c079f0fb1016818b27185c342f9ecf78c42..26e3437ceb2a22153054a548c30fd0080d8df161 100644 (file)
--- a/README
+++ b/README
@@ -35,7 +35,7 @@ $ cd proxyconf
 $ make
 
 Now add the following line to your ~/.bashrc (modify as appropriate):
-eval $(~/Downloads/proxyconf/proxyconf.sh)
+eval $(~/Downloads/proxyconf/proxyconf)
 
 
 -----------
diff --git a/proxyconf.m b/proxyconf.m
new file mode 100644 (file)
index 0000000..a161886
--- /dev/null
@@ -0,0 +1,95 @@
+#include <stdio.h>
+
+#import <Foundation/Foundation.h>
+#import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
+
+void
+DXPrint(NSString* format, ...)
+{
+       va_list args;
+       va_start(args, format);
+       NSString* string = [[NSString alloc] initWithFormat:format arguments:args];
+       va_end(args);
+       fputs([string UTF8String], stdout);
+       [string release];
+}
+
+
+
+
+NSDictionary* proxies;
+BOOL proxyFound;
+
+void
+printUnset(NSString* env)
+{
+       DXPrint(@"unset %@\n", env);
+}
+
+void
+printProxy(NSDictionary* proxies, NSString* proto)
+{
+       NSString* key = nil;
+       NSString* env = [[proto lowercaseString] stringByAppendingString:@"_proxy"];
+
+       key = [proto stringByAppendingString:@"Enable"];
+       NSNumber* enabled = [proxies objectForKey:key];
+       if(enabled == nil || [enabled intValue] != 1) {
+               printUnset(env);
+               return;
+       }
+
+       key = [proto stringByAppendingString:@"Proxy"];
+       id host = [proxies objectForKey:key];
+       if(host == nil) {
+               printUnset(env);
+               return;
+       }
+
+       key = [proto stringByAppendingString:@"Port"];
+       id port = [proxies objectForKey:key];
+       if(port == nil) {
+               printUnset(env);
+               return;
+       }
+
+       NSString* uriPrefix = nil;
+       if(![proto isEqual:@"SOCKS"]) {
+               uriPrefix = @"http://";
+       } else {
+               uriPrefix = @"socks://";
+       }
+
+       DXPrint(@"export %@='%@%@:%@'\n", env, uriPrefix, host, port);
+       proxyFound = YES;
+}
+
+void
+printExceptions(NSDictionary* proxies)
+{
+       id exceptions = [proxies objectForKey:@"ExceptionsList"];
+       if(exceptions == nil || ![exceptions isKindOfClass:[NSArray class]] || !proxyFound) {
+               printUnset(@"no_proxy");
+               return;
+       }
+
+       DXPrint(@"export no_proxy='%@'\n", [exceptions componentsJoinedByString:@","]);
+}
+
+int
+main()
+{
+       NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+
+       proxies  = (NSDictionary*)SCDynamicStoreCopyProxies(nil);
+       proxyFound = NO;
+       printProxy(proxies, @"HTTP");
+       printProxy(proxies, @"HTTPS");
+       printProxy(proxies, @"FTP");
+       printProxy(proxies, @"SOCKS");
+       printExceptions(proxies);
+       
+       [pool release];
+       return 0;
+}
+
diff --git a/proxyconf.sh b/proxyconf.sh
deleted file mode 100755 (executable)
index a6b7d23..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/bin/bash
-
-PROXY_FOUND=0
-function printEnvironment() {
-       local proxy_type="$1"
-       local environment_variable="$2"
-       local uri_prefix="${3:-http}"
-       local host port
-
-       if [ "$(sysconfig -q "${proxy_type}Enable")" = "1" ]; then
-               host="$(sysconfig -q "${proxy_type}Proxy")"
-               port="$(sysconfig -q "${proxy_type}Port")"
-               echo "export ${environment_variable}='${uri_prefix}://${host}:${port}'"
-
-               PROXY_FOUND=1
-       else
-               echo "unset ${environment_variable}"
-       fi
-
-}
-
-
-PATH="$(dirname "$0"):${PATH}"
-
-printEnvironment "HTTP" "http_proxy"
-printEnvironment "HTTPS" "https_proxy"
-printEnvironment "FTP" "ftp_proxy"
-printEnvironment "SOCKS" "socks_proxy" "socks"
-
-no_proxy="$(sysconfig -q ExceptionsList)"
-if [ -n "${no_proxy}" -a $PROXY_FOUND -eq 1 ]; then
-       echo "export no_proxy=\"${no_proxy}\""
-else
-       echo "unset no_proxy"
-fi
diff --git a/sysconfig.m b/sysconfig.m
deleted file mode 100644 (file)
index c28ce59..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-#import <Foundation/Foundation.h>
-#import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
-
-void
-NSPrintF(NSString* file, NSString* format, ...)
-{
-       va_list args;
-       va_start(args, format);
-       NSString* string = [[NSString alloc] initWithFormat:format arguments:args];
-       va_end(args);
-       [string writeToFile:file atomically:NO encoding:NSUTF8StringEncoding error:nil];
-       [string release];
-}
-
-void
-NSPrint(NSString* format, ...)
-{
-       va_list args;
-       va_start(args, format);
-       NSString* string = [[NSString alloc] initWithFormat:format arguments:args];
-       va_end(args);
-       [string writeToFile:@"/dev/stdout" atomically:NO encoding:NSUTF8StringEncoding error:nil];
-       [string release];
-}
-
-
-bool
-printSystemConfiguration(bool quiet, NSString* keyName, NSDictionary* proxies)
-{
-       id value = [proxies objectForKey:keyName];
-       if(nil != value) {
-               NSArray* items;
-               if([value isKindOfClass:[NSArray class]]) {
-                       items = value;
-               } else {
-                       items = [NSArray arrayWithObjects:value, nil];
-               }
-
-               NSPrint([items componentsJoinedByString:@","]);
-               NSPrint(@"\n");
-
-       } else {
-               if(!quiet) {
-                       NSPrintF(@"/dev/stderr", @"Value does not exist\n");
-               }
-               return FALSE; // Signal a fail condition
-       }
-       return TRUE;
-}
-
-void
-printUsage(void)
-{
-       NSPrintF(@"/dev/stderr", @"sysconfig [-q] SystemConfigurationKeyName\n");
-       exit(1);
-}
-
-int
-main(int argc, const char* argv[])
-{
-       NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
-       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();
-               } else {
-                       quiet = TRUE;
-               }
-
-               keyName = [NSString stringWithCString:argv[2]];
-       } else {
-               printUsage();
-       }
-
-       if(!printSystemConfiguration(quiet, keyName, proxies)) {
-               return 2;
-       }
-       
-       [pool release];
-       return 0;
-}
-