Utility.C 19.7 KB
Newer Older
hrchilds's avatar
hrchilds committed
1
2
/*****************************************************************************
*
brugger's avatar
   
brugger committed
3
* Copyright (c) 2000 - 2010, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
   
brugger committed
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6
7
* All rights reserved.
*
brugger's avatar
   
brugger committed
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9
10
11
12
13
14
15
16
17
18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
brugger's avatar
   
brugger committed
19
20
21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22
23
24
25
26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
brugger's avatar
   
brugger committed
27
28
29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30
31
32
33
34
35
36
37
38
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39
40
41
42
43
44
// ************************************************************************* //
//                               Utility.C                                   //
// ************************************************************************* //

#include <Utility.h>

hrchilds's avatar
hrchilds committed
45
#include <visit-config.h>
hrchilds's avatar
hrchilds committed
46
47
#include <stdio.h>
#include <string.h>
hrchilds's avatar
hrchilds committed
48
#include <snprintf.h>
hrchilds's avatar
hrchilds committed
49

hrchilds's avatar
hrchilds committed
50
51
using std::vector;

hrchilds's avatar
hrchilds committed
52
53
54
#if defined(_WIN32)
#include <windows.h>
#else
whitlocb's avatar
whitlocb committed
55
56
57
#if defined(__APPLE__)
#include <malloc/malloc.h> // for mstat
#endif
hrchilds's avatar
hrchilds committed
58
#include <unistd.h>
hrchilds's avatar
hrchilds committed
59
60
61
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
hrchilds's avatar
hrchilds committed
62
#include <pwd.h>
hrchilds's avatar
hrchilds committed
63
64
#endif

brugger's avatar
   
brugger committed
65
#if !defined(_WIN32) && !defined(__APPLE__) && !defined(_AIX)
66
67
68
69
#include <execinfo.h>
#include <cxxabi.h>
#endif

hrchilds's avatar
hrchilds committed
70
71
72
73
74
75
76
77
// ****************************************************************************
//  Function: LongestCommonPrefixLength
//
//  Purpose:
//      Determines what the longest common prefix is to all strings.
//
//  Arguments:
//      list    A list of strings.
whitlocb's avatar
whitlocb committed
78
//      listN   The number of std::string in list.
hrchilds's avatar
hrchilds committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
//  Returns:   The length of the longest common prefix (possibly 0).
//
//  Programmer: Hank Childs
//  Creation:   June 15, 2000
//
// ****************************************************************************

int
LongestCommonPrefixLength(const char * const *list, int listN)
{
    int   i, j;

    //
    // Determine the minimum length over all strings.
    //
    int  min_length = 100000;
    for (i = 0 ; i < listN ; i++)
    {
        int  length = strlen(list[i]);
        min_length = (min_length < length ? min_length : length);
    }

    //
    // Determine the longest prefix common to all strings.
    //
    int  prefix_length = 0;
    for (i = 0 ; i < min_length ; i++)
    {
        bool  same_prefix = true;
        for (j = 1 ; j < listN ; j++)
        {
            if (list[0][i] != list[j][i])
            {
                same_prefix = false;
                break;
            }
        }
        if (same_prefix)
        {
            prefix_length++;
        }
        else
        {
            break;
        }
    }

    return prefix_length;
}


// ****************************************************************************
//  Function: LongestCommonSuffixLength
//
//  Purpose:
//      Determines what the longest common suffix is to all strings.
//
//  Arguments:
//      list    A list of strings.
whitlocb's avatar
whitlocb committed
139
//      listN   The number of strings in list.
hrchilds's avatar
hrchilds committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
//  Returns:   The length of the longest common suffix (possibly 0).
//
//  Programmer: Hank Childs
//  Creation:   August 30, 2000
//
// ****************************************************************************

int
LongestCommonSuffixLength(const char * const *list, int listN)
{
    int   i, j;

    //
    // Take care of degenerative cases.
    //
    if (listN <= 0)
    {
        return -1;
    }

    //
    // Determine the minimum length over all strings.
    //
    int  *lengths    = new int[listN];
    int   min_length = 1000;
    for (i = 0 ; i < listN ; i++)
    {
        lengths[i] = strlen(list[i]);
        if (lengths[i] < min_length)
        {
            min_length = lengths[i];
        }
    }

    //
    // Determine the longest suffix common to all strings.
    //
    int    suffix_length = 0;
    for (i = 0 ; i < min_length ; i++)
    {
        char   c         = list[0][lengths[0]-i-1];
        bool   matches_c = true;
        for (j = 1 ; j < listN ; j++)
        {
            if (c != list[j][lengths[j]-i-1])
            {
                matches_c = false;
                break;
            }
        }
        if (matches_c)
        {
            suffix_length++;
        }
        else
        {
            break;
        }
    }

    delete [] lengths;

    return suffix_length;
}


207
208
209
210
211
212
213
214
215
// ****************************************************************************
//  Function: GetMemorySize
//
//  Purpose:
//      Gets the amount of memory in use and the RSS (resident set size).
//
//  Programmer: Hank Childs (from code from Peter Lindstrom)
//  Creation:   February 28, 2008
//
216
217
218
219
220
//  Modifications:
//    Cyrus Harrison, Wed Apr  1 12:16:19 PDT 2009
//    Modified to use unsigned ints so we can go above 2 gigabytes (up to
//    4 gigabytes).
//
whitlocb's avatar
whitlocb committed
221
222
223
//    Brad Whitlock, Tue Jun 23 17:07:57 PDT 2009
//    I added a Mac implementation.
//
224
225
226
// ****************************************************************************

void
227
GetMemorySize(unsigned int &size, unsigned int &rss)
228
{
229
230
    size = 0;
    rss  = 0;
whitlocb's avatar
whitlocb committed
231
232
233
234
235
236
#if defined(__APPLE__)
    struct mstats m = mstats();
    size = (unsigned int)m.bytes_used; // The bytes used out of the bytes_total.
    rss = (unsigned int)m.bytes_total; // not quite accurate but this should be the total
                                       // amount allocated by malloc.
#elif !defined(_WIN32)
237
238
239
240
241
242
    FILE *file = fopen("/proc/self/statm", "r");
    if (file == NULL)
    {
        return;
    }

243
    int count = fscanf(file, "%u%u", &size, &rss);
244
245
246
247
248
249
250
251
252
253
    if (count != 2)
    {
        return;
    }
    size *= getpagesize();
    rss  *= getpagesize();
    fclose(file);
#endif
}

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// ****************************************************************************
//  Function: PrintCallStack
//
//  Purpose:
//      Prints the call stack.
//
//  Programmer: Dave Pugmire
//  Creation:   Tue Jun 29 15:58:52 EDT 2010
//
//  Modifications:
//
// ****************************************************************************

void
PrintCallStack(ostream &out, const char *file, int line)
{
brugger's avatar
   
brugger committed
270
#if !defined(_WIN32) && !defined(__APPLE__) && !defined(_AIX)
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
    const int N = 100;
    void *stackAddrs[N];
 
    size_t stackDepth = backtrace(stackAddrs, N);
    int nfuncs = backtrace(stackAddrs, N);
    char **stackStrings = backtrace_symbols(stackAddrs, nfuncs);
    
    out<<"Call stack from "<<file<<" "<<line<<endl;
    for (int i = 1; i < nfuncs; i++)
    {
        std::string symbol = stackStrings[i];
        size_t i0 = symbol.find("(");
        size_t i1 = symbol.rfind("+");

        std::string outStr;
        if (i0 == std::string::npos || i1 == std::string::npos)
            outStr = symbol;
        else
        {
            i0 = i0+1;
            std::string funcSymbol = symbol.substr(i0, i1-i0);

            int stat = 0;
            char *demangle = abi::__cxa_demangle(funcSymbol.c_str(), 0,0, &stat);
            if (demangle)
            {
                outStr = demangle;
                free(demangle);
            }
            else
                outStr = funcSymbol;
        }
        
        out<<i<<":  "<<outStr<<endl;
    }
    
    free(stackStrings);
#endif
}

311

hrchilds's avatar
hrchilds committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// ****************************************************************************
//  Function: WaitUntilFile
//
//  Purpose:
//      Does not return until the file exists.  This is useful when debugging
//      and you need to be able to start the debugger with a running process
//      (dbx -p) before it crashes.
//
//  Arguments:
//      filename      The name of the file to wait until.
//
//  Programmer: Hank Childs
//  Creation:   September 25, 2000
//
//  Modifications:
//    Brad Whitlock, Fri Mar 29 08:25:35 PDT 2002
//    Added a windows version of the sleep function.
//
// ****************************************************************************

void
WaitUntilFile(const char *filename)
{
    for (;;)
    {
        ifstream ifile(filename);
        if (! ifile.fail())
        {
             break;
        }
#if defined(_WIN32)
        Sleep(5000);
#else
        sleep(5);
#endif
    }
}


// ****************************************************************************
//  Method: CreateMessageStrings
//
//  Purpose:
//      Makes messages strings of the correct size to go to each processor.
//
//  Arguments:
//      list    A list of strings to put pointers into a bigger string.
//      count   A list of the number of bytes to go to each processor.
//      nl      The number of lists in list.
//
//  Returns:    The char *'s in list will all be offset into a bigger array.
//              Return that bigger array here so it can be properly deleted.
//
//  Programmer: Hank Childs
//  Creation:   January 25, 2001
//
// ****************************************************************************

char *
CreateMessageStrings(char **lists, int *count, int nl)
{
    int   i;

    //
    // Determine how big the big array should be.
    //
    int total = 0;
    for (i = 0 ; i < nl ; i++)
    {
        total += count[i];
    }

    //
    // Make the array of pointers just point into our one big array.
    //
    char *totallist = new char[total];
    char *totaltmp  = totallist;
    for (i = 0 ; i < nl ; i++)
    {
        lists[i] = totaltmp;
        totaltmp += count[i];
    }

    //
    // Return the big array so the top level routine can clean it up later.
    //
    return totallist;
}

// ****************************************************************************
//  Function:  WildcardStringMatch
//
//  Purpose:
//    Match a pattern to a string, using normal '*' and '?' wildcards.
hrchilds's avatar
hrchilds committed
406
//    Also match '#' to single numerical digits.
hrchilds's avatar
hrchilds committed
407
408
409
410
411
412
413
414
415
416
417
418
419
//
//  Notes:
//    Yes, recursive -- but it attempts to do a minimal number of recursion
//    levels, uses no local variables and no data copies, so total overhead
//    is minimal.
//
//  Arguments:
//    p          pattern string possibly including wildcards
//    s          string to compare with
//
//  Programmer:  Jeremy Meredith
//  Creation:    February 12, 2002
//
hrchilds's avatar
hrchilds committed
420
421
422
423
//  Modifications:
//    Jeremy Meredith, Thu Jun 26 10:28:28 PDT 2003
//    Added the '#' wildcard.
//
hrchilds's avatar
hrchilds committed
424
425
426
//    Brad Whitlock, Fri Apr 30 16:13:50 PST 2004
//    I made it case insensitive on Windows.
//
427
428
429
//    Jeremy Meredith, Mon Dec 28 15:40:12 EST 2009
//    I made the case-insensitivity apply to all platforms.
//
hrchilds's avatar
hrchilds committed
430
// ****************************************************************************
hrchilds's avatar
hrchilds committed
431

hrchilds's avatar
hrchilds committed
432
bool
whitlocb's avatar
whitlocb committed
433
WildcardStringMatch(const std::string &p, const std::string &s)
hrchilds's avatar
hrchilds committed
434
435
436
437
438
{
    // wrap around the c-style function
    return WildcardStringMatch(p.c_str(), s.c_str());
}

hrchilds's avatar
hrchilds committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
inline bool
CaseInsensitiveCompare(char p, char s)
{
    bool p_uc = (p >= 'A' && p <= 'Z');
    bool p_lc = (p >= 'a' && p <= 'z');
    bool s_uc = (s >= 'A' && s <= 'Z');
    bool s_lc = (s >= 'a' && s <= 'z');
    bool retval;
    if((p_uc || p_lc) && (s_uc || s_lc))
    {
        char pi = p_uc ? (p - 'A') : (p - 'a');
        char si = s_uc ? (s - 'A') : (s - 'a');
        retval = (pi == si);
    }
    else
        retval = (p == s);

    return retval;
}

hrchilds's avatar
hrchilds committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
bool
WildcardStringMatch(const char *p, const char *s)
{
    // "*" matches any string
    if (p[0] == '*' && p[1] == 0)
        return true;

    // if either pattern or string is empty:
    //    if both are empty, match
    //    if not, no match
    if (*p == 0 || *s == 0)
    {
        if (*p == 0 && *s == 0)
            return true;
        else
            return false;
    }

hrchilds's avatar
hrchilds committed
477
478
479
480
    // first chars match ("?" matches any char, "#" matches a digit),
    // and consume one char
    if ( *p == '?' ||
        (*p == '#' && *s >= '0' && *s <= '9') ||
481
         CaseInsensitiveCompare(*p, *s))
hrchilds's avatar
hrchilds committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    {
        return WildcardStringMatch(&p[1], &s[1]);
    }

    // if first pattern char is "*", consume either the "*" or one string char
    if (*p == '*')
    {
        return
            WildcardStringMatch(&p[1], s) ||
            WildcardStringMatch(p, &s[1]);
    }

    // first characters don't match, so the whole string can't match
    return false;
}

hrchilds's avatar
hrchilds committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// ****************************************************************************
// Function: NumericStringCompare
//
// Purpose: 
//   Compares two strings but treats any numbers contained in the string as
//   numbers so they compare differently than a staight text comparison.
//
// Arguments:
//   str1 : The first string to compare.
//   str2 : The second string to compare.
//
// Returns:    true if str1<str2; false otherwise.
//
// Programmer: Sean Ahern
// Creation:   Tue Aug 26 11:41:54 PDT 2003
//
// Modifications:
//   Brad Whitlock, Tue Aug 26 11:42:12 PDT 2003
//   I stole this function from MeshTV and adapted it to C++.
//
hrchilds's avatar
hrchilds committed
518
519
520
//   Brad Whitlock, Mon Sep 15 14:52:12 PST 2003
//   I fixed it so things sort correctly.
//
521
522
523
//   Brad Whitlock, Fri Nov 21 16:11:43 PST 2008
//   Take the number of digits into account when the numbers are the same.
//
hrchilds's avatar
hrchilds committed
524
525
526
// ****************************************************************************

bool
whitlocb's avatar
whitlocb committed
527
NumericStringCompare(const std::string &str1, const std::string &str2)
hrchilds's avatar
hrchilds committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
{
    const char *p1 = str1.c_str();
    const char *p2 = str2.c_str();

    // Compare the two strings, character by character.
    while ((*p1 != '\0') && (*p2 != '\0'))
    {
        // If we're at some digits, we have to treat it differently.
        if (isdigit(*p1) && isdigit(*p2))
        {
            /* We're in the digits. */
            int num1 = (*p1) - '0';
            int num2 = (*p2) - '0';

            p1++;
            p2++;

            // Walk along until we're out of numbers in each string.
546
            int ndigs1 = 1;
hrchilds's avatar
hrchilds committed
547
548
549
550
551
            while ((*p1 != '\0') && isdigit(*p1))
            {
                num1 *= 10;
                num1 += (*p1) - '0';
                p1++;
552
                ndigs1++;
hrchilds's avatar
hrchilds committed
553
            }
554
            int ndigs2 = 1;
hrchilds's avatar
hrchilds committed
555
556
557
558
559
            while ((*p2 != '\0') && isdigit(*p2))
            {
                num2 *= 10;
                num2 += (*p2) - '0';
                p2++;
560
                ndigs2++;
hrchilds's avatar
hrchilds committed
561
562
563
            }

            // Compare the numbers. If they're the same, keep going. If 
564
565
566
            // they're different, return the difference. If the numbers were
            // the same but used a different number of characters then
            // return the lesser number of digits.
hrchilds's avatar
hrchilds committed
567
568
            if (num1 != num2)
                return ((num1 - num2) < 0);
569
570
            else if(ndigs1 != ndigs2)
                return ndigs1 < ndigs2;
hrchilds's avatar
hrchilds committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
        }
        else
        {
            // We're in some normal characters. Just compare them normally.
            if (*p1 == *p2)
            {
                p1++;
                p2++;
            }
            else
            {
                return ((*p1 - *p2) < 0);
            }
        }
    }

    //
    // If we fall out here, the strings are the same up to the point. But 
    // one of the strings is shorter than the other.
    //
    if(*p1)
        return false;
hrchilds's avatar
hrchilds committed
593
594
    else
        return true;
hrchilds's avatar
hrchilds committed
595
}
hrchilds's avatar
hrchilds committed
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611


// ****************************************************************************
//  Function:  SplitValues
//
//  Purpose:
//    Separate a string into a vector of strings using a single char delimiter.
//
//  Arguments:
//    buff       the string to split
//    delim      the single-character delimiter
//
//  Programmer:  Jeremy Meredith
//  Creation:    March 23, 2004
//
// ****************************************************************************
whitlocb's avatar
whitlocb committed
612
613
vector<std::string>
SplitValues(const std::string &buff, char delim)
hrchilds's avatar
hrchilds committed
614
{
whitlocb's avatar
whitlocb committed
615
    vector<std::string> output;
hrchilds's avatar
hrchilds committed
616
    
whitlocb's avatar
whitlocb committed
617
    std::string tmp="";
618
    for (size_t i=0; i<buff.length(); i++)
hrchilds's avatar
hrchilds committed
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
    {
        if (buff[i] == delim)
        {
            if (!tmp.empty())
                output.push_back(tmp);
            tmp = "";
        }
        else
        {
            tmp += buff[i];
        }
    }
    if (!tmp.empty())
        output.push_back(tmp);

    return output;
}
hrchilds's avatar
hrchilds committed
636
637

// ****************************************************************************
638
//  Method:  ConvertArgsToTunneledValues
hrchilds's avatar
hrchilds committed
639
//
640
641
642
643
//  Purpose:
//    When we're doing SSH tunneling, we  need to replace client:oldport
//    with remote:tunnelend port (called "localhost":tunneledport because
//    localhost is determined relative to the remote machine).
hrchilds's avatar
hrchilds committed
644
//
645
646
647
//  Arguments:
//    portTunnelMap        the map of local-to-remote port numbers
//    args                 the argv stringVector to modify in-place
hrchilds's avatar
hrchilds committed
648
//
649
//  Returns:  true on success, false on failure.
650
//
651
652
//  Programmer:  Jeremy Meredith
//  Creation:    May 24, 2007
653
//
654
655
656
657
658
659
660
661
//  Modifications:
//    Jeremy Meredith, Thu Jun 28 13:32:26 EDT 2007
//    If we're tunneling through SSH, this should override any selection of
//    how the client host name should otherwise be determined.  We had partly
//    solved this already by overriding the value of the -host argument,
//    and this adds support for the case where they specify -guesshost
//    instead of -host <hostname>.  I also added the -sshtunneling argument
//    just incase downstream calls need to know this.
662
//
663
664
//    Thomas R. Treadway, Mon Oct  8 13:27:42 PDT 2007
//    Backing out SSH tunneling on Panther (MacOS X 10.3)
665
//
hrchilds's avatar
hrchilds committed
666
// ****************************************************************************
667
668
669
670
671
672
#if defined(PANTHERHACK)
// Broken on Panther
#else
bool
ConvertArgsToTunneledValues(const std::map<int,int> &portTunnelMap,
                            std::vector<std::string> &args)
hrchilds's avatar
hrchilds committed
673
{
674
675
676
    bool foundHost = false;
    // strip off -guesshost
    for (size_t i=0; i<args.size(); i++)
hrchilds's avatar
hrchilds committed
677
    {
678
679
680
681
682
683
684
685
686
        if (args[i] == "-guesshost")
        {
            for (size_t j=i+1; j<args.size(); j++)
            {
                args[j-1] = args[j];
            }
            args.resize(args.size()-1);
            break;
        }
hrchilds's avatar
hrchilds committed
687
    }
688
689
    // replace host and port
    for (size_t i=0; i<args.size(); i++)
hrchilds's avatar
hrchilds committed
690
    {
691
        if (i<args.size()-1 && args[i] == "-host")
hrchilds's avatar
hrchilds committed
692
        {
693
694
            args[i+1] = "localhost";
            foundHost = true;
hrchilds's avatar
hrchilds committed
695
        }
696
        if (i<args.size()-1 && args[i] == "-port")
hrchilds's avatar
hrchilds committed
697
        {
698
699
700
701
702
703
704
            int oldport = atoi(args[i+1].c_str());
            if (portTunnelMap.count(oldport) <= 0)
                return false;
            int newport = portTunnelMap.find(oldport)->second;
            char newportstr[10];
            sprintf(newportstr,"%d",newport);
            args[i+1] = newportstr;
hrchilds's avatar
hrchilds committed
705
706
        }
    }
707
708
    // add "localhost" if it there wasn't already a host specified
    if (!foundHost)
hrchilds's avatar
hrchilds committed
709
    {
710
711
        args.push_back("-host");
        args.push_back("localhost");
hrchilds's avatar
hrchilds committed
712
    }
713
714
715
    // add -sshtunneling for anyone that wants to know
    args.push_back("-sshtunneling");
    return true;
hrchilds's avatar
hrchilds committed
716
717
}
#endif