Exception action when condition isn't met - exception

Im trying to make an except script that will take action only if a string is NOT present on the command execution: example:
send -- "sys set -nd\r"
expect "showdebugcommands" {} "\n$PROMPT" {send -- "sys set showdebugcommands 1\r"}
What i want to do is: Do NOTHING if "showdebugcommands" is present on cmd output but in case it doesn't, execute command "sys set showdebugcommands 1".
How can i accomplish this using expect?

Try this
set seen false
expect {
"showdebugcommands" {set seen true; exp_continue}
"\n$PROMPT"
}
if { ! $seen} {
send -- "sys set showdebugcommands 1\r"
}

One way of doing this is by nesting an expect inside an expect. This is perfectly legal.
send -- "sys set -nd\r"
expect {
"showdebugcommands" {
expect "\n$PROMPT"
}
"\n$PROMPT" {
send -- "sys set showdebugcommands 1\r"
expect "\n$PROMPT"
}
}
The aim is to drain the activity back to the known state (prompt showing) after seeing the thing that you wanted. It's always a good idea to think in terms of code units that take things back to a known state. (Because of that, I also added another expect of the prompt after the inner send; let's get it all back to the state of “I've just seen a prompt” at the end of the outer expect since that's the least crazy option.)

Related

Rerun the program after it ends automatically in octave

Using OCTAVE only...
How can I rerun the code automatically after it ends. Like I want to make a program in which if the input is incorrect value it will end the program and rerun again.
I tried that by writting there a file name and it works but this will only work until I change my file name.
You can wrap your main script in a wrapper script which performs the loop.
% In main.m
disp( 'Hello from main' );
Question = "Do you want to rerun? ";
Response = input( Question, 's');
% in wrapper.m
Response = 'yes';
while strcmp( Response, 'yes' )
main
end

How to use a signal as function parameter in CAPL

I am trying to write a function in CAPL that takes a signal and calculates the physical value with the signal value, the signal factor and the signal offset.
This is how a simple gateway normally works:
message CAN1.myMessage1 myMessage1 = {DIR = RX};//message from the database
message CAN2.myMessage2 myMessage2 = {DIR = TX};//another message from the database
on message CAN1.*
{
if(this.id == myMessage1.id)
{
myMessage1 = this;
myMessage2.mySignalB = myMessage1.mySignalA * myMessage1.mySignalA.factor + myMessage1.mySignalA.offset;
}
}
And this is what I am trying to do:
...
on message CAN1.*
{
if(this.id ==myMessage1.id)
{
myMessage1 = this;
myMessage2.mySignalB = PhysicalValue(myMessage1.mySignalA);
}
}
double PhysicalValue(signal * s)
{
return s*s.factor+s.offset;
}
There are two problems with this code:
Firstly when I pass the signal as the parameter the compiler says that the types don't match. The second problem is that inside the function the attributes (factor and offset) are no longer recognized.
These problems might have something to do with the weird object-oriented-but-not-really nature of CAPL. The value of the signals can be accessed directly but it also has attributes?
int rawValue = myMessage1.mySignalA;
If you are familiar with C you might say that the problem is that I am specifying a pointer in the function but that I am not passing a pointer into it. But in CAPL there are no pointers and the * simply means anything.
Without the * I would have needed to use a specific signal which would have defeated the purpose of the function.
EDIT:
I have found the attribute .phys by now which does exactly what my demo function would have done.
double physValue = myMessage1.mySignalA.phys;
This has already made my code much shorter but there are other operations that I need to perform for multiple signals so being able to use signals as a function parameter would still be useful.
What you can do is this:
double PhysicalValue(signal * s)
{
// access signal by prepending a $
return $s.phys;
}
Call like this
on message CAN1.*
{
if(this.id ==myMessage1.id)
{
myMessage1 = this;
myMessage2.mySignalB = PhysicalValue(CAN1::myMessage1::mySignalA);
}
}
I.e. when you call your function, you have to provide the qualified name of the signal (with colons rather than dots). To my knowledge it is not possible to use myMessage1.mySignalA, since signals itself are not a CAPL datatype.
Apart from this, you might re-think whether you really should be using on message, but rather switch to on signal. Handling the signal values no matter with which message they are sent is done by CANoe's signal server.
Note that CANoe already has a function which does exactly what you're trying to do (multiplying by factor and adding offset). It's called getSignal:
on message CAN1.*
{
if(this.id == myMessage1.id)
{
myMessage2.mySignalB = getSignal(myMessage1::mySignalA);
}
}
Offsets and factors are defined in e.g. the DBC files.

Ignore "Variable might have not been defined" in specific place

I have code like this
<?php
$first_condition = time() % 2 == 0;
$second_condition = time() % 3 == 0;
if ($first_condition) {
if ($second_condition) {
$param1 = 'param1_1_1';
} else {
$param1 = 'param1_2_1';
$param2 = 'param2_2_1';
}
} else {
if ($second_condition) {
$param1 = 'param1_1_2';
} else {
$param1 = 'param1_2_2';
$param2 = 'param2_2_2';
}
}
if ($second_condition) {
$param2 = $param1;
}
$total = array(
'param1' => $param2,
'param2' => $param1,
);
I really know that $param2 would be defined anyway, but PhpStorm say that it's wrong.
Is exist there any way to mark this place as ignored of this inspection? Only this place, not global settings, and only this inspection, not all.
Sure -- you can suppress such warning for that statement.
Standard procedure:
Place caret on such error/warning.
Invoke Alt + Enter to bring quick fix menu (or via light bulb icon).
Find the right inspection.
Expand submenu (e.g. Arrow Right using keyboard or using mouse -- note: click area can be quite small -- depends on GUI theme used).
Choose Suppress for statement option.
The above will add special PHPDoc-like comment (/** #noinspection PhpUndefinedVariableInspection */) just before that statement -- it tells IDE to ignore that particular issue here.
https://www.jetbrains.com/help/phpstorm/suppressing-inspections.html?search=suppress
On another hand (especially if it's your code/code that you cane edit): why not go a bit safer route and just declare those $paramX variables with default values (e.g. empty string) before the conditionals ... so the variable will be indeed defined? This will prevent such false complains from IDE (when it tries to statically analyse such rather complex logic).
New subquestion: is it possible to disable inspection only for param1 but not for param2 using /** #noinspection PhpUndefinedVariableInspection */ ?
Yes and No.
Without making changes to the code -- No. Those variables are both used in one statement (array definition) and suppression comment is applied to the whole statement.
Yes -- split it into 2 statements if you need such separate suppression.

Apache2 Perl vHosts Error

I just worked through this tutorial and modified the table by adding another column. I want to check the value before adding the template script. It didn't work and the script includes the template-ssl every time. It is important that this script works with MySQL, mass vhosts is not possible.
$My::dir = #row[3];
$My::encrypted = #row[4];
if ($My::encrypted == 'ssl') {
$s->add_config(["Include /etc/apache2/sites-available/template-ssl"]);
}
else {
$s->add_config(["Include /etc/apache2/sites-available/template-def"]);
}
I think the variables doesn't work but if(#row[4] == "ssl") also fire as true every time. Even when the DataRow contains "def".
Ok, it was too simple. The error was that you compare stings with "xx" eq "yy" and numbers with 1 == 2.

How to suppress the warning "Assignment within conditional. Did you mean == instead of =?"

With the new ASC 2.0 compiler I get warnings when I code like below:
// (_achievementsFromServer is an Array)
while(item=_achievementsFromServer.pop())
{
// do something with item here
}
The warning reads: "Assignment within conditional. Did you mean == instead of =?"
While in general I appreciate all warnings from the compiler, I'd like to suppress this one in this case because I did not mean == here. I want to pop all items in the array and do something with it until the array is empty.
while( (item=_achievementsFromServer.pop())==true )
seems to work but looks a bit confusing. Any other ideas?
This may seem better.
while(_achievementsFromServer.length > 0) {
var item:Object = _achievementsFromServer.pop();
}
Just like removeChild
var d:DisplayObjectContainer;
while(d.numChildren > 0) {
d.removeChildAt(0);
}
While I was hoping for some other way, I think #AmyBlankenship improved my own suggestion:
while((item=_achievementsFromServer.pop())!=null)
{
//....
}
It's clear and understandable what's going on, and doesn't rely on checking the length of the Array on every iteration.
Googling some more I found a compiler option -compiler.warn-assignment-within-conditional that could be set to false but then you won't be warned anywhere in your project anymore. And I'm not so confident that I never accidently type = instead of ==, so that's not a good solution I think.