]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/rtems-shell/orte_rtems_shell.c
Reformat the sources with orte/uncrustify script
[orte.git] / orte / examples / rtems-shell / orte_rtems_shell.c
1 #include "rtems_shell_local_config.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <malloc.h>
5 #include <pthread.h>
6 #include <string.h>
7 #include <getopt.h>
8 #include <rtems/error.h>
9 #include <rtems/monitor.h>
10 #include <rtems/shell.h>
11
12 #include <orte_rtems_shell.h>
13
14 typedef struct orte_spawn_args {
15   int argc;
16   char *argv[];
17 } orte_spawn_args_t;
18
19 void *
20 orte_spawn_task_func(void *arg_pack)
21 {
22   rtems_shell_cmd_t *shell_cmd;
23   int                errorlevel = 0;
24   orte_spawn_args_t  *spawn_args = (orte_spawn_args_t *)arg_pack;
25   int                argc = spawn_args->argc;
26   char               **argv = &(spawn_args->argv[0]);
27
28   if (optind)
29     optind = 1;
30
31   if (argc) {
32     shell_cmd = rtems_shell_lookup_cmd(argv[0]);
33     if (argv[1] == NULL) {
34       errorlevel = -1;
35     } else if (shell_cmd == NULL) {
36       errorlevel = rtems_shell_script_file(argc, &argv[0]);
37     } else {
38       errorlevel = shell_cmd->command(argc, &argv[0]);
39     }
40   }
41
42   free(arg_pack);
43   return (void *)errorlevel;
44 }
45
46 int
47 orte_spawn_main(int argc, char **argv)
48 {
49   orte_spawn_args_t  *spawn_args;
50   int       sparg_size;
51   int       sparg_str_offs;
52   int       status;
53   pthread_t task_id;
54   int       i;
55   char      *p;
56
57   argv++;
58   argc--;
59
60   sparg_str_offs = sizeof(orte_spawn_args_t) + (argc + 1) *sizeof(char *);
61   sparg_size = sparg_str_offs;
62
63   for (i = 0; i < argc; i++)
64     sparg_size += strlen(argv[i]) + 1;
65
66   spawn_args = malloc(sparg_size);
67   if (spawn_args == NULL)
68     return 1;
69
70   spawn_args->argc = argc;
71
72   p = (char *)spawn_args + sparg_str_offs;
73   for (i = 0; i < argc; i++) {
74     int len = strlen(argv[i]);
75     spawn_args->argv[i] = p;
76     memcpy(p, argv[i], len);
77     p += len;
78     *(p++) = 0;
79   }
80   spawn_args->argv[argc] = NULL;
81
82   status = pthread_create(&task_id, NULL, orte_spawn_task_func, spawn_args);
83   if (status == 0)
84     pthread_detach(task_id);
85
86   return status ? 1 : 0;
87 }