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:
$ make
Now add the following line to your ~/.bashrc (modify as appropriate):
-eval $(~/Downloads/proxyconf/proxyconf.sh)
+eval $(~/Downloads/proxyconf/proxyconf)
-----------
--- /dev/null
+#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;
+}
+
+++ /dev/null
-#!/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
+++ /dev/null
-#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;
-}
-