Skip to content

Commit d19261d

Browse files
committed
Replace a macro with a block (C > preprocessor) and reformat some code
1 parent 85d65c3 commit d19261d

File tree

1 file changed

+101
-89
lines changed

1 file changed

+101
-89
lines changed

Source/SPSSHTunnel.m

+101-89
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ - (void)connect
246246
[debugMessagesLock unlock];
247247
taskExitedUnexpectedly = NO;
248248

249-
[NSThread detachNewThreadWithName:@"SPSSHTunnel SSH binary communication task" target:self selector:@selector(launchTask:) object:nil];
249+
[NSThread detachNewThreadWithName:@"SPSSHTunnel SSH binary communication task"
250+
target:self
251+
selector:@selector(launchTask:)
252+
object:nil];
250253
}
251254

252255
/*
@@ -317,7 +320,9 @@ - (void)launchTask:(id) dummy
317320

318321
// Prepare to set up the arguments for the task
319322
taskArguments = [[NSMutableArray alloc] init];
320-
#define TA(_name,_value) [taskArguments addObjectsFromArray:@[_name,_value]]
323+
void (^TA)(NSString *, NSString *) = ^(NSString *_name, NSString *_value) {
324+
[taskArguments addObjectsFromArray:@[_name,_value]];
325+
};
321326

322327
// Enable verbose mode for message parsing
323328
[taskArguments addObject:@"-v"];
@@ -328,7 +333,6 @@ - (void)launchTask:(id) dummy
328333
// If explicitly enabled, activate connection multiplexing - note that this can cause connection
329334
// instability on some setups, so is currently disabled by default.
330335
if (connectionMuxingEnabled) {
331-
332336
// Enable automatic connection muxing/sharing, for faster connections
333337
TA(@"-o",@"ControlMaster=auto");
334338

@@ -340,8 +344,8 @@ - (void)launchTask:(id) dummy
340344
CC_MD5([pathString UTF8String], (unsigned int)strlen([pathString UTF8String]), hashedPathResult);
341345
NSString *hashedString = [[[NSData dataWithBytes:hashedPathResult length:16] dataToHexString] substringToIndex:8];
342346
TA(@"-o",([NSString stringWithFormat:@"ControlPath=%@/SPSSH-%@", [NSFileManager temporaryDirectory], hashedString]));
343-
} else {
344-
347+
}
348+
else {
345349
// Disable muxing if requested
346350
TA(@"-S", @"none");
347351
TA(@"-o", @"ControlMaster=no");
@@ -374,16 +378,17 @@ - (void)launchTask:(id) dummy
374378
}
375379
if ([sshLogin length]) {
376380
[taskArguments addObject:[NSString stringWithFormat:@"%@@%@", sshLogin, sshHost]];
377-
} else {
381+
}
382+
else {
378383
[taskArguments addObject:sshHost];
379384
}
380385
if (useHostFallback) {
381386
TA(@"-L",([NSString stringWithFormat:@"%ld:127.0.0.1:%ld", (long)localPort, (long)remotePort]));
382387
TA(@"-L",([NSString stringWithFormat:@"%ld:%@:%ld", (long)localPortFallback, remoteHost, (long)remotePort]));
383-
} else {
388+
}
389+
else {
384390
TA(@"-L", ([NSString stringWithFormat:@"%ld:%@:%ld", (long)localPort, remoteHost, (long)remotePort]));
385391
}
386-
#undef TA
387392

388393
[task setArguments:taskArguments];
389394

@@ -412,11 +417,11 @@ - (void)launchTask:(id) dummy
412417

413418
// Set up the standard error pipe
414419
standardError = [[NSPipe alloc] init];
415-
[task setStandardError:standardError];
416-
[[ NSNotificationCenter defaultCenter] addObserver:self
417-
selector:@selector(standardErrorHandler:)
418-
name:NSFileHandleDataAvailableNotification
419-
object:[standardError fileHandleForReading]];
420+
[task setStandardError:standardError];
421+
[[NSNotificationCenter defaultCenter] addObserver:self
422+
selector:@selector(standardErrorHandler:)
423+
name:NSFileHandleDataAvailableNotification
424+
object:[standardError fileHandleForReading]];
420425
[[standardError fileHandleForReading] waitForDataInBackgroundAndNotify];
421426

422427
{
@@ -481,9 +486,9 @@ - (void)launchTask:(id) dummy
481486
// On tunnel close, clean up, ready for re-use if the delegate reconnects.
482487
SPClear(task);
483488
SPClear(standardError);
484-
[[NSNotificationCenter defaultCenter] removeObserver:self
485-
name:@"NSFileHandleDataAvailableNotification"
486-
object:nil];
489+
[[NSNotificationCenter defaultCenter] removeObserver:self
490+
name:NSFileHandleDataAvailableNotification
491+
object:nil];
487492

488493
// If the task closed unexpectedly, alert appropriately
489494
if (connectionState != SPMySQLProxyIdle) {
@@ -497,7 +502,6 @@ - (void)launchTask:(id) dummy
497502
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
498503

499504
SPClear(taskEnvironment);
500-
#undef TA
501505
SPClear(taskArguments);
502506

503507
[pool release];
@@ -508,7 +512,7 @@ - (void)launchTask:(id) dummy
508512
*/
509513
- (void)disconnect
510514
{
511-
if (connectionState == SPMySQLProxyIdle) return;
515+
if (connectionState == SPMySQLProxyIdle) return;
512516

513517
// If there's a delegate set, clear it to prevent unexpected state change messaging
514518
if (delegate) {
@@ -518,9 +522,9 @@ - (void)disconnect
518522

519523
// Before terminating the tunnel, check that it's actually running. This is to accommodate tunnels which
520524
// suddenly disappear as a result of network disconnections.
521-
if ([task isRunning]) [task terminate];
525+
if ([task isRunning]) [task terminate];
522526
}
523-
527+
524528
/*
525529
* Processes messages recieved from the SSH task. These may be received singly
526530
* or several stuck together.
@@ -537,7 +541,7 @@ - (void)standardErrorHandler:(NSNotification*)aNotification
537541
if ([notificationText length]) {
538542
messages = [notificationText componentsSeparatedByString:@"\n"];
539543
enumerator = [messages objectEnumerator];
540-
while ((message = [[enumerator nextObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]])) {
544+
while ((message = [[enumerator nextObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]])) {
541545
if (![message length]) continue;
542546
[debugMessagesLock lock];
543547
[debugMessages addObject:[NSString stringWithString:message]];
@@ -590,7 +594,7 @@ - (void)standardErrorHandler:(NSNotification*)aNotification
590594
if (connectionState != SPMySQLProxyIdle) {
591595
[[standardError fileHandleForReading] waitForDataInBackgroundAndNotify];
592596
}
593-
597+
594598
[notificationText release];
595599
}
596600

@@ -631,27 +635,27 @@ - (NSString *)getPasswordWithVerificationHash:(NSString *)theHash
631635
*/
632636
- (BOOL)getResponseForQuestion:(NSString *)theQuestion
633637
{
634-
// Lock the answer available lock
635-
[[answerAvailableLock onMainThread] lock];
636-
637-
// Request an answer on the main thread (UI stuff must be done on main thread)
638+
// Lock the answer available lock
639+
[[answerAvailableLock onMainThread] lock];
640+
641+
// Request an answer on the main thread (UI stuff must be done on main thread)
638642
[self performSelectorOnMainThread:@selector(workerGetResponseForQuestion:) withObject:theQuestion waitUntilDone:YES];
639-
640-
// Wait for closeSSHQuestionSheet: to unlock the lock, indicating an answer is available
643+
644+
// Wait for closeSSHQuestionSheet: to unlock the lock, indicating an answer is available
641645
while (![answerAvailableLock tryLock]) usleep(25000);
642-
643-
// Save the answer
644-
BOOL response = requestedResponse;
645-
646-
// Unlock the lock again
647-
[answerAvailableLock unlock];
648-
649-
// Return the answer
646+
647+
// Save the answer
648+
BOOL response = requestedResponse;
649+
650+
// Unlock the lock again
651+
[answerAvailableLock unlock];
652+
653+
// Return the answer
650654
return response;
651655
}
652656

653657
- (void)workerGetResponseForQuestion:(NSString *)theQuestion
654-
{
658+
{
655659
NSSize questionTextSize;
656660
NSRect windowFrameRect;
657661

@@ -661,9 +665,13 @@ - (void)workerGetResponseForQuestion:(NSString *)theQuestion
661665
windowFrameRect = [sshQuestionDialog frame];
662666
windowFrameRect.size.height = ((questionTextSize.height < 100)?100:questionTextSize.height) + 70 + ([sshPasswordDialog isSheet]?0:22);
663667
[sshQuestionDialog setFrame:windowFrameRect display:NO];
664-
665-
//show the question window
666-
[NSApp beginSheet:sshQuestionDialog modalForWindow:parentWindow modalDelegate:self didEndSelector:nil contextInfo:nil];
668+
669+
//show the question window
670+
[NSApp beginSheet:sshQuestionDialog
671+
modalForWindow:parentWindow
672+
modalDelegate:nil
673+
didEndSelector:NULL
674+
contextInfo:NULL];
667675
[parentWindow makeKeyAndOrderFront:self];
668676
}
669677

@@ -672,10 +680,10 @@ - (void)workerGetResponseForQuestion:(NSString *)theQuestion
672680
*/
673681
- (IBAction)closeSSHQuestionSheet:(id)sender
674682
{
675-
requestedResponse = [sender tag]==1 ? YES : NO;
676-
[NSApp endSheet:sshQuestionDialog];
683+
requestedResponse = [sender tag] == 1 ? YES : NO;
684+
[NSApp endSheet:sshQuestionDialog];
677685
[sshQuestionDialog orderOut:nil];
678-
[[answerAvailableLock onMainThread] unlock];
686+
[[answerAvailableLock onMainThread] unlock];
679687
}
680688

681689
/*
@@ -688,26 +696,26 @@ - (NSString *)getPasswordForQuery:(NSString *)theQuery verificationHash:(NSStrin
688696

689697
if (passwordPromptCancelled) return nil;
690698

691-
// Lock the answer available lock
692-
[[answerAvailableLock onMainThread] lock];
693-
694-
// Request password on the main thread (UI stuff must be done on main thread)
699+
// Lock the answer available lock
700+
[[answerAvailableLock onMainThread] lock];
701+
702+
// Request password on the main thread (UI stuff must be done on main thread)
695703
[self performSelectorOnMainThread:@selector(workerGetPasswordForQuery:) withObject:theQuery waitUntilDone:YES];
696704

697-
// Wait for closeSSHPasswordSheet: to unlock the lock, indicating an answer is available
705+
// Wait for closeSSHPasswordSheet: to unlock the lock, indicating an answer is available
698706
while (![answerAvailableLock tryLock]) usleep(25000);
699707

700-
// Save the answer
708+
// Save the answer
701709
NSString *thePassword = nil;
702-
if (requestedPassphrase) {
703-
thePassword = [NSString stringWithString:requestedPassphrase];
704-
SPClear(requestedPassphrase);
705-
}
706-
707-
// Unlock the lock again
708-
[answerAvailableLock unlock];
709-
710-
// Return the answer
710+
if (requestedPassphrase) {
711+
thePassword = [NSString stringWithString:requestedPassphrase];
712+
SPClear(requestedPassphrase);
713+
}
714+
715+
// Unlock the lock again
716+
[answerAvailableLock unlock];
717+
718+
// Return the answer
711719
return thePassword;
712720
}
713721

@@ -722,12 +730,12 @@ - (void)workerGetPasswordForQuery:(NSString *)theQuery
722730
if (keyName) {
723731
[sshPasswordText setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Enter your password for the SSH key\n\"%@\"", @"SSH key password prompt"), keyName]];
724732
[sshPasswordKeychainCheckbox setHidden:NO];
725-
currentKeyName = [keyName retain];
733+
currentKeyName = [keyName retain];
726734
}
727735
else {
728736
[sshPasswordText setStringValue:theQuery];
729737
[sshPasswordKeychainCheckbox setHidden:YES];
730-
currentKeyName = nil;
738+
currentKeyName = nil;
731739
}
732740

733741
// Request the password, sizing the window appropriately to fit the query
@@ -736,7 +744,11 @@ - (void)workerGetPasswordForQuery:(NSString *)theQuery
736744
windowFrameRect.size.height = ((queryTextSize.height < 40)?40:queryTextSize.height) + 140 + ([sshPasswordDialog isSheet]?0:22);
737745

738746
[sshPasswordDialog setFrame:windowFrameRect display:NO];
739-
[NSApp beginSheet:sshPasswordDialog modalForWindow:parentWindow modalDelegate:self didEndSelector:nil contextInfo:nil];
747+
[NSApp beginSheet:sshPasswordDialog
748+
modalForWindow:parentWindow
749+
modalDelegate:nil
750+
didEndSelector:NULL
751+
contextInfo:NULL];
740752
[parentWindow makeKeyAndOrderFront:self];
741753
}
742754

@@ -745,33 +757,33 @@ - (void)workerGetPasswordForQuery:(NSString *)theQuery
745757
*/
746758
- (IBAction)closeSSHPasswordSheet:(id)sender
747759
{
748-
requestedResponse = [sender tag]==1 ? YES : NO;
760+
requestedResponse = [sender tag]==1 ? YES : NO;
749761

750762
[NSApp endSheet:sshPasswordDialog];
751763
[sshPasswordDialog orderOut:nil];
752-
753-
if (requestedResponse) {
754-
NSString *thePassword = [NSString stringWithString:[sshPasswordField stringValue]];
755-
[sshPasswordField setStringValue:@""];
756-
if ([delegate respondsToSelector:@selector(undoManager)] && [delegate undoManager]) {
757-
[[delegate undoManager] removeAllActionsWithTarget:sshPasswordField];
758-
} else if ([[parentWindow windowController] document] && [[[parentWindow windowController] document] undoManager]) {
759-
[[[[parentWindow windowController] document] undoManager] removeAllActionsWithTarget:sshPasswordField];
760-
}
761-
requestedPassphrase = [[NSString alloc] initWithString:thePassword];
762-
763-
// Add to keychain if appropriate
764-
if (currentKeyName && [sshPasswordKeychainCheckbox state] == NSOnState) {
765-
SPKeychain *keychain = [[SPKeychain alloc] init];
766-
[keychain addPassword:thePassword forName:@"SSH" account:currentKeyName withLabel:[NSString stringWithFormat:@"SSH: %@", currentKeyName]];
767-
[keychain release];
768-
SPClear(currentKeyName);
769-
}
770-
}
764+
765+
if (requestedResponse) {
766+
NSString *thePassword = [NSString stringWithString:[sshPasswordField stringValue]];
767+
[sshPasswordField setStringValue:@""];
768+
if ([delegate respondsToSelector:@selector(undoManager)] && [delegate undoManager]) {
769+
[[delegate undoManager] removeAllActionsWithTarget:sshPasswordField];
770+
} else if ([[parentWindow windowController] document] && [[[parentWindow windowController] document] undoManager]) {
771+
[[[[parentWindow windowController] document] undoManager] removeAllActionsWithTarget:sshPasswordField];
772+
}
773+
requestedPassphrase = [[NSString alloc] initWithString:thePassword];
774+
775+
// Add to keychain if appropriate
776+
if (currentKeyName && [sshPasswordKeychainCheckbox state] == NSOnState) {
777+
SPKeychain *keychain = [[SPKeychain alloc] init];
778+
[keychain addPassword:thePassword forName:@"SSH" account:currentKeyName withLabel:[NSString stringWithFormat:@"SSH: %@", currentKeyName]];
779+
[keychain release];
780+
SPClear(currentKeyName);
781+
}
782+
}
771783

772784
if (!requestedPassphrase) passwordPromptCancelled = YES;
773-
774-
[[answerAvailableLock onMainThread] unlock];
785+
786+
[[answerAvailableLock onMainThread] unlock];
775787
}
776788

777789
#pragma mark -
@@ -796,14 +808,14 @@ - (void)dealloc
796808
[answerAvailableLock tryLock];
797809
[answerAvailableLock unlock];
798810
SPClear(answerAvailableLock);
799-
if (password) SPClear(password);
800-
if (keychainName) SPClear(keychainName);
801-
if (keychainAccount) SPClear(keychainAccount);
802-
if (identityFilePath) SPClear(identityFilePath);
811+
SPClear(password);
812+
SPClear(keychainName);
813+
SPClear(keychainAccount);
814+
SPClear(identityFilePath);
803815

804816
// As this object is not a NSWindowController, use manual top-level nib item management
805-
if (sshQuestionDialog) SPClear(sshQuestionDialog);
806-
if (sshPasswordDialog) SPClear(sshPasswordDialog);
817+
SPClear(sshQuestionDialog);
818+
SPClear(sshPasswordDialog);
807819

808820
[super dealloc];
809821
}

0 commit comments

Comments
 (0)