Blame view

RIOT/examples/javascript/main.c 1.7 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  /*
   * Copyright (C) 2017 Inria
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     examples
   * @{
   *
   * @file
   * @brief       Showing an example of scripting (javascript) from command line
   *
   * @author      Emmanuel Baccelli <emmanuel.baccelli@inria.fr>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <string.h>
  #include "shell.h"
  #include "jerryscript.h"
  
  int shell_script(int argc, char **argv)
  {
      if (argc < 2) {
          puts("Usage: script <your script here!> \n"
          "For example, try: \n"
          "script var txt = \\'\\'; txt += Math.PI; print (\\'Pi=\\'+txt); \n"
          "Note: you need to substitute usual quotes with \\' \n");
          return -1;
      }
  
      jerry_char_t script[(2 * SHELL_DEFAULT_BUFSIZE + 1)];
      *script = '\0';
      for(int i = 1; i < argc; i++) {
          if (i>1) {
              strcat((char *)script, " ");
          }
          strcat((char *)script, argv[i]);
      }
  
      size_t script_size = strlen((char *) script);
      printf("Executing script: [%s]\n\n", script);
      bool ret_value = jerry_run_simple(script, script_size, JERRY_INIT_EMPTY);
  
      return (ret_value != 0);
  }
  
  const shell_command_t shell_commands[] = {
      { "script", "Shell scripting ", shell_script },
      { NULL, NULL, NULL }
  };
  
  int main(void)
  {
      printf("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
      printf("This board features a(n) %s MCU.\n", RIOT_MCU);
  
      /* start the shell */
      char line_buf[2 * SHELL_DEFAULT_BUFSIZE];
      /* for longer script support shell buffer should be bigger */
      shell_run(shell_commands, line_buf, sizeof(line_buf));
  
      return 0;
  }