How can I prepare arguments for NSTask from UI items

92 views Asked by At

I have a Cocoa app which user is allowed to execute shell commands (just like using Terminal) by entering commands in UI items like NSTextBox. The app allows has a few predetermined parameters which will be converted to certain characters inside the app.

The app also allows multithreads in which a few commands can be run at the same time.

The challenge I am facing now is to let the user enter the commands as it is in Terminal with all the spaces and etc. I have difficulty breaking down what user enters to be NSArray element to be the arguments to be fed to NSTask. For example, there are some paths or parameters which has spaces in between, for e.g. /users/Application Support/testFolder/ or 'test - b'

What I am doing now is I have fixed number of NSTextBoxes and each NSTextBox allows only 1 parameter. 1 other issue is if certain command has number of arguments less than my total NSTextBoxes, I have to give "" to the argument(s) and hence, the command may not be executed correctly. I want to make it more flexible for user to enter as it is. Then, my app is able to handle the characters and run the user-entered command using NSTask.

....
paramArray = [cmdParam componentsSeparatedByString:@","];

NSString *launchPath = [paramArray objectAtIndex:0];

if ([launchPath rangeOfString:@"\""].location == NSNotFound) {
    int nLineNo = [launchPath integerValue];
    launchPath = [self cmdReading:nLineNo uutIndex:nUut];
}
else {
    launchPath = [launchPath stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}

NSString *param1 = [paramArray objectAtIndex:1];

if ([param1 rangeOfString:@"\""].location == NSNotFound) {
    if ([param1 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
        param1 = [NSString stringWithFormat:@"UUT%d", nUut+1];
    }
    else if ([param1 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
        param1 = [NSString stringWithFormat:@"%d", nUut+1];
    }
    else if ([param1 length] == 0) {
        param1 = @"";
    }
    else {
        int nLineNo = [param1 integerValue];
        param1 = [self cmdReading:nLineNo uutIndex:nUut];
    }
}
else {
    param1 = [param1 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}

NSString *param2 = [paramArray objectAtIndex:2];

if ([param2 rangeOfString:@"\""].location == NSNotFound) {
    if ([param2 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
        param2 = [NSString stringWithFormat:@"UUT%d", nUut+1];
    }
    else if ([param2 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
        param2 = [NSString stringWithFormat:@"%d", nUut+1];
    }
    else if ([param2 length] == 0) {
        param2 = @"";
    }
    else {
        int nLineNo = [param2 integerValue];
        param2 = [self cmdReading:nLineNo uutIndex:nUut];
 }
    }
    else {
        param2 = [param2 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    }

    NSString *param3 = [paramArray objectAtIndex:3];

    if ([param3 rangeOfString:@"\""].location == NSNotFound) {
        if ([param3 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
       param3 = [NSString stringWithFormat:@"UUT%d", nUut+1];

    }
    else if ([param3 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
        param3 = [NSString stringWithFormat:@"%d", nUut+1];
    }
    else if ([param3 length] == 0) {
        param3 = @"";
    }
    else {
        int nLineNo = [param3 integerValue];
        param3 = [self cmdReading:nLineNo uutIndex:nUut];
    }
}
else {
    param3 = [param3 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}

NSString *param4 = [paramArray objectAtIndex:4];

if ([param4 rangeOfString:@"\""].location == NSNotFound) {
    if ([param4 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
        param4 = [NSString stringWithFormat:@"UUT%d", nUut+1];
    }
    else if ([param4 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
        param4 = [NSString stringWithFormat:@"%d", nUut+1];
    }
    else if ([param4 length] == 0) {
        param4 = @"";
    }
    else {
        int nLineNo = [param4 integerValue];
        param4 = [self cmdReading:nLineNo uutIndex:nUut];
    }
}
else {
    param4 = [param4 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}

NSString *param5 = [paramArray objectAtIndex:5];

if ([param5 rangeOfString:@"\""].location == NSNotFound) {
    if ([param5 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
        param5 = [NSString stringWithFormat:@"UUT%d", nUut+1];
    }
    else if ([param5 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
        param5 = [NSString stringWithFormat:@"%d", nUut+1];
    }
    else if ([param5 length] == 0) {
        param5 = @"";
    }
    else {
        int nLineNo = [param5 integerValue];
        param5 = [self cmdReading:nLineNo uutIndex:nUut];
    }
}
else {
    param5 = [param5 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}

NSString *param6 = [paramArray objectAtIndex:6];

if ([param6 rangeOfString:@"\""].location == NSNotFound) {
    if ([param6 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
        param6 = [NSString stringWithFormat:@"UUT%d", nUut+1];
    }
    else if ([param6 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
        param6 = [NSString stringWithFormat:@"%d", nUut+1];
    }
    else if ([param6 length] == 0) {
        param6 = @"";
    }
    else {
        int nLineNo = [param6 integerValue];
        param6 = [self cmdReading:nLineNo uutIndex:nUut];

    }
}
else {
    param6 = [param6 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}

NSString *param7 = [paramArray objectAtIndex:7];

if ([param7 rangeOfString:@"\""].location == NSNotFound) {
    if ([param7 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
        param7 = [NSString stringWithFormat:@"UUT%d", nUut+1];
    }
    else if ([param7 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
        param7 = [NSString stringWithFormat:@"%d", nUut+1];
    }
    else if ([param7 length] == 0) {
        param7 = @"";
    }
    else {
        int nLineNo = [param7 integerValue];
        param7 = [self cmdReading:nLineNo uutIndex:nUut];
    }
}
else {
    param7 = [param7 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}


    NSLog(@"_Terminal_Command for UUT:%d, launchPath=%@", nUut+1, launchPath);
    NSLog(@"_Terminal_Command for UUT:%d, param1=%@", nUut+1, param1);
    NSLog(@"_Terminal_Command for UUT:%d, param2=%@", nUut+1, param2);
    NSLog(@"_Terminal_Command for UUT:%d, param3=%@", nUut+1, param3);
    NSLog(@"_Terminal_Command for UUT:%d, param4=%@", nUut+1, param4);
    NSLog(@"_Terminal_Command for UUT:%d, param5=%@", nUut+1, param5);
    NSLog(@"_Terminal_Command for UUT:%d, param6=%@", nUut+1, param6);
    NSLog(@"_Terminal_Command for UUT:%d, param7=%@", nUut+1, param7);

    NSTask* task = [NSTask new];

    [task setLaunchPath:launchPath];
    [task setArguments:[NSArray arrayWithObjects:param1,
                                param2, param3,
                                param4, param5,
                                param6, param7, nil]];

...
0

There are 0 answers