|
1 # |
|
2 # Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 #!/usr/bin/perl |
|
18 # |
|
19 |
|
20 my $NISTNET="/usr/local/bin/cnistnet"; |
|
21 my $EFILE="1.txt"; |
|
22 my $NFILE="2.txt"; |
|
23 |
|
24 ################################################################################################# |
|
25 # |
|
26 # Main() |
|
27 # |
|
28 ################################################################################################# |
|
29 |
|
30 # DEBUG: Show the passed line |
|
31 my $argc = scalar(@ARGV); |
|
32 my $dstr = "ArgC: $argc\nArgV: @ARGV\n"; |
|
33 #print $dstr; |
|
34 |
|
35 # CASE 1 -- REMOVE ENTRIES |
|
36 if( $ARGV[0] =~ /-r/ ) { |
|
37 system( "$NISTNET -r $ARGV[1] $ARGV[2]" ); |
|
38 exit( 0 ); |
|
39 } |
|
40 |
|
41 # CASE 2 -- NISTNET DOWN |
|
42 if( $ARGV[0] =~ /-d/ ) { |
|
43 system( "$NISTNET -d" ); |
|
44 exit( 0 ); |
|
45 } |
|
46 |
|
47 |
|
48 # CASE 3 -- ADD ENTRY |
|
49 |
|
50 # Make sure that NISTNET is running and start it if it isn't |
|
51 my $result = system( "$NISTNET -G | grep \"Emulator is: On\" > /dev/null" ); |
|
52 if( $result != 0 ) { |
|
53 system( "$NISTNET -u" ); |
|
54 my $result = system( "$NISTNET -G | grep \"Emulator is: On\" > /dev/null" ); |
|
55 if( $result != 0 ) { |
|
56 print( "ERROR: Nistnet is not running.\n" ); |
|
57 exit( -1 ); |
|
58 } |
|
59 } |
|
60 |
|
61 |
|
62 # Remove the temporary files created previously |
|
63 system( "rm -f 1.txt 2.txt" ); |
|
64 |
|
65 # Now get the nistnet output and put into file 1.txt |
|
66 my $result = system( "$NISTNET -R > $EFILE" ); |
|
67 |
|
68 |
|
69 # Grep the nistnet output for our channel |
|
70 my $gstr = "grep -- \"$ARGV[2] $ARGV[3]\" $EFILE"; |
|
71 my $result = system( "$gstr > $NFILE" ); |
|
72 |
|
73 # DEBUG: print the grep string |
|
74 #print "GREP STRING: $gstr\n"; |
|
75 |
|
76 # If the output is <> 0 then no match was found so just call nistnet |
|
77 if( $result != 0 ) { |
|
78 # print "cmd: $NISTNET @ARGV\n"; |
|
79 $result = system( "$NISTNET @ARGV" ); |
|
80 exit( $result ); |
|
81 } |
|
82 |
|
83 |
|
84 # Open the file containing the previous command line and read it into an array. |
|
85 open( CFILE, "$NFILE" ) or die "ERROR: can't open intermediate file $NFILE.\n"; |
|
86 my $cmd = <CFILE>; |
|
87 my @cmdarray = split( " ", $cmd); |
|
88 |
|
89 |
|
90 # DEBUG: print the current configuration |
|
91 #print "Current Configuration: @cmdarray\n"; |
|
92 |
|
93 |
|
94 # Look for the passed option in the existing string. If it is found then blank out these entries |
|
95 my $option_to_set = $ARGV[4]; |
|
96 my $in_option = 0; |
|
97 my $len = scalar(@cmdarray); |
|
98 for( $i = 0; $i < $len; $i++ ) { |
|
99 my $current_config_token = $cmdarray[$i]; |
|
100 if( $current_config_token =~ /^--/ ) { |
|
101 $in_option = 0; |
|
102 } |
|
103 if( $current_config_token eq $option_to_set ) { |
|
104 $in_option = 1; |
|
105 } |
|
106 if( $in_option == 1 ) { |
|
107 $cmdarray[$i] = ""; |
|
108 } |
|
109 } |
|
110 |
|
111 # Now append the argument array to the current array |
|
112 $len = scalar(@ARGV); |
|
113 for( $i = 4; $i < $len; $i++ ) { |
|
114 push( @cmdarray, $ARGV[$i] ); |
|
115 } |
|
116 |
|
117 # Clear the nistnet cmd as we add this here |
|
118 $cmdarray[0] = ""; |
|
119 |
|
120 # DEBUG: We now have the complete string |
|
121 #print "cmda: @cmdarray\n"; |
|
122 |
|
123 # Now run the command |
|
124 my $result = system( "$NISTNET @cmdarray" ); |
|
125 |
|
126 # Success |
|
127 exit( $result ); |