vector.com|Contact
Search tips

apple banana
Find rows that contain at least one of the two words.

+apple +juice
Find rows that contain both words.

+apple macintosh
Find rows that contain the word 'apple', but rank rows higher if they also contain 'macintosh'.

+apple -macintosh
Find rows that contain the word 'apple' but not 'macintosh'.

+apple ~macintosh
Find rows that contain the word 'apple', but if the row also contains the word 'macintosh', rate it lower than if row does not. This is "softer" than a search for '+apple -macintosh', for which the presence of 'macintosh' causes the row not to be returned at all.

+apple +(>turnover >strudel)
Find rows that contain the words 'apple' and 'turnover', or 'apple' and 'strudel' (in any order), but rank 'apple turnover' higher than 'apple strudel'.

apple*
Find rows that contain words such as 'apple', 'apples', 'applesauce', or 'applet'.

"some words"
Find rows that contain the exact phrase 'some words' (for example, rows that contain 'some words of wisdom' but not "some noise words").

By continuing to use this site you agree to the use of cookies. For more information and to find out how to change this click here. Accept Cookies
Please enable cookies in your browser for this website.
Search:
Advanced search

Collecting Return Values of an External Command Run Via sysExec

Last updated: 2020-02-25
Question:

How can I collect return value(s) of an external command that’s run through sysExecCmd (as sysExecCmd doesn’t wait for the execution to complete and grabs the results)?

Answer:

Apparently, invocation of system calls from CANoe (viz. Windows applications, batch files, plug-in command-line interfaces, etc.) could be realized through the below functions. But, it is to be noted that CANoe does not wait for the command to finish executing. This is not to have any indefinite outcome/waiting as a result of external calls. This also means that the results generated from external system calls are not collected.

long sysExec(char cmd[], char params[]);
long sysExec(char cmd[], char params[], char directory[]); // form 2
long sysExecCmd(char cmd[], char params[]);
long sysExecCmd(char cmd[], char params[], char directory[]); // form 2

The solution at a higher picture is to write the results generated from system calls to a text file and read the content back in CANoe. The desired command is to be appended with a file output operator or modifier (typically, > in Windows command consoles) to a permitted location. The content of the file could be read using various CAPL file functions (for example: fileGetString) and be evaluated using CAPL string functions.

With this workaround, CAPL system calls could be applied to various use-cases, namely,

  • Reading Windows Environment Variables
  • Running Linux cygwin interface commands (to rip tar files/tarball)
  • Running any external utilities, scripts, plug-in interfaces that could be run in a Windows console

The following sections explain the general design notes and working examples on some use-cases.

General Design Considerations

  • Correctness of the target commands and parameters supplied to sysExec(Cmd) has to be ensured by validating the retun value of the very function itself:1 if the command was successfully started, else 0
  • Destination Path: In computers managed by admin, C: drive might not allow write-access due to which the file write permissions would be denied. It could be better to write the results of system calls to D: drive to be read back
  • Working Directory: Some external commands cannot be recognized by Windows console engine. This could be because the command-line interface is not run from the installation directory. This is to be handled by supplying the right working directory as parameter in sysExec(Cmd) (form 2)
  • Appending/Overwriting to file: When multiple system calls yield different results at each time, then the requirement can be to store the results of each call. This can be ensured by appending the results to the previous file contents (typically, >> in Windows consoles) and a line-marker shall count the calls and link the results to it. At any point of time, the contents could be read & validated with the below format
    • Write & Append:  <TargetCommand> >> <D:\FileName>
    • Read: fileGetStringSZ(<BufferName>, elcount(readStringFromFile), glbHandle) // Calling fileGetString repeatedly returns the content line-by-line
    In calls where multiple results need not be stored, then it is safe to overwrite the file by using > each time a system call is made and the whole contents be read and validated with the below format
    • Overwrite: <TargetCommand> > <D:\FileName>
    • Read: fileGetString(<BufferName>, elcount(readStringFromFile), glbHandle)
  • Validating Results: All possible outcomes of each system call (from a suite/utility, referring the command-list documentation) shall be collated and unique string literals on each case be defined for comparison with the actual results read from the destination file
  • Time Responsiveness: Some commands might take some time to get the results and write them into destination file. In such cases, reading the content shall be performed after a calculated time-out as CANoe does not wait for the execution to finish
Example 1:

Reading Windows Environment Variables

CANalyzer_InstallDir is to be read

With the sample code below:

on key 't'
{
  sysExecCmd("echo", "%CANalyzer_InstallDir% > test.txt", "d:\\");
  glbHandle = OpenFileRead ("d:\\test.txt", 0);
}

on key 'u'
{
  fileGetString(readStringFromFile, elcount(readStringFromFile), glbHandle);
  write("CANalyzer_InstallDir = %s", readStringFromFile);
}

Example 2:

Dealing with cygwin commands

Software files are to be tarred/untarred (for example: untar & flash)

  • Case 1: Sample code to tar/untar test folder and no content is expected from log3.txt
    on key 'o'
    {
      sysExecCmd("start c:\\cygwin64\\bin\\tar", " -cf d:\\test3.tar * > d:\\log3.txt 2>&1 --force-local", "d:\\test");
      glbHandle = OpenFileRead ("d:\\log3.txt", 0);  
    }

    on key 'h'
    {
      // Create a new folder where all the files are to be extracted
      sysExecCmd("md test3", "", "d:\\");
    }

    on key 'm'
    {  
      sysExecCmd("start c:\\cygwin64\\bin\\tar", " -xf d:\\test3.tar > d:\\log3.txt 2>&1 --force-local", "d:\\test3"); // Correct command ‘tar’
      glbHandle = OpenFileRead ("d:\\log3.txt", 0);
    }

    on key 'l'
    {
      fileGetString(readStringFromFile, elcount(readStringFromFile), glbHandle);
      write("tar result = %s", readStringFromFile); // Validate empty content
    }

  • Case 2: Sample code that does not work and error content is to be validated from log3.txt
    on key 'o'
    {
      sysExecCmd("start c:\\cygwin64\\bin\\tar", " -cf d:\\test3.tar * > d:\\log3.txt 2>&1 --force-local", "d:\\test");
      glbHandle = OpenFileRead ("d:\\log3.txt", 0);  
    }

    on key 'h'
    {
      // Create a new folder where all the files are to be extracted
      sysExecCmd("md test3", "", "d:\\"); }

    on key 'm'
    {  
      sysExecCmd("start c:\\cygwin64\\bin\\tar88", " -xf d:\\test3.tar > d:\\log3.txt 2>&1 --force-local", "d:\\test3"); // Wrong command ‘tar88’
      glbHandle = OpenFileRead ("d:\\log3.txt", 0);
    }

    on key 'l'
    {
      fileGetString(readStringFromFile, elcount(readStringFromFile), glbHandle);
      write("tar result = %s", readStringFromFile); // Validate non-empty content
    }

 
Article Options
2020-02-25
Views: 232