-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsh_parse.c
2747 lines (2201 loc) · 73.9 KB
/
nsh_parse.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* apps/nshlib/nsh_parse.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#ifdef CONFIG_NSH_CMDPARMS
# include <sys/stat.h>
#endif
#include <nuttx/version.h>
#include "nshlib/nshlib.h"
#include "nsh.h"
#include "nsh_console.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* If CONFIG_NSH_CMDPARMS or CONFIG_NSH_ARGCAT is enabled, then we will need
* retain a list of memory allocations to be freed at the completion of
* command processing.
*/
#undef HAVE_MEMLIST
#if defined(CONFIG_NSH_CMDPARMS) || defined(CONFIG_NSH_ARGCAT)
# define HAVE_MEMLIST 1
#endif
#if defined(HAVE_MEMLIST) && !defined(CONFIG_NSH_MAXALLOCS)
# ifdef CONFIG_NSH_ARGCAT
# define CONFIG_NSH_MAXALLOCS (2*CONFIG_NSH_MAXARGUMENTS)
# else
# define CONFIG_NSH_MAXALLOCS CONFIG_NSH_MAXARGUMENTS
# endif
#endif
/* Allocation list helper macros */
#ifdef HAVE_MEMLIST
# define NSH_MEMLIST_TYPE struct nsh_memlist_s
# define NSH_MEMLIST_INIT(m) memset(&(m), 0, sizeof(struct nsh_memlist_s));
# define NSH_MEMLIST_ADD(m,a) nsh_memlist_add(m,a)
# define NSH_MEMLIST_FREE(m) nsh_memlist_free(m)
#else
# define NSH_MEMLIST_TYPE uint8_t
# define NSH_MEMLIST_INIT(m) do { (m) = 0; } while (0)
# define NSH_MEMLIST_ADD(m,a)
# define NSH_MEMLIST_FREE(m)
#endif
/* Do we need g_nullstring[]? */
#undef NEED_NULLSTRING
#if defined(NSH_HAVE_VARS) || defined(CONFIG_NSH_CMDPARMS)
# define NEED_NULLSTRING 1
#elif !defined(CONFIG_NSH_ARGCAT) || !defined(HAVE_MEMLIST)
# define NEED_NULLSTRING 1
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/* These structure describes the parsed command line */
#ifndef CONFIG_NSH_DISABLEBG
struct cmdarg_s
{
FAR struct nsh_vtbl_s *vtbl; /* For front-end interaction */
int fd; /* FD for output redirection */
int argc; /* Number of arguments in argv */
FAR char *argv[MAX_ARGV_ENTRIES]; /* Argument list */
};
#endif
/* This structure describes the allocation list */
#ifdef HAVE_MEMLIST
struct nsh_memlist_s
{
int nallocs; /* Number of allocations */
FAR char *allocations[CONFIG_NSH_MAXALLOCS];
};
#endif
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
#ifdef HAVE_MEMLIST
static void nsh_memlist_add(FAR struct nsh_memlist_s *memlist,
FAR char *allocation);
static void nsh_memlist_free(FAR struct nsh_memlist_s *memlist);
#endif
#ifndef CONFIG_NSH_DISABLEBG
static void nsh_releaseargs(struct cmdarg_s *arg);
static pthread_addr_t nsh_child(pthread_addr_t arg);
static struct cmdarg_s *nsh_cloneargs(FAR struct nsh_vtbl_s *vtbl,
int fd, int argc, char *argv[]);
#endif
static int nsh_saveresult(FAR struct nsh_vtbl_s *vtbl, bool result);
static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
int argc, FAR char *argv[], FAR const char *redirfile,
int oflags);
#ifdef CONFIG_NSH_CMDPARMS
static FAR char *nsh_filecat(FAR struct nsh_vtbl_s *vtbl, FAR char *s1,
FAR const char *filename);
static FAR char *nsh_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
FAR char **allocation);
#endif
#ifdef CONFIG_NSH_ARGCAT
static FAR char *nsh_strcat(FAR struct nsh_vtbl_s *vtbl, FAR char *s1,
FAR const char *s2);
#endif
#if defined(CONFIG_NSH_QUOTE) && defined(CONFIG_NSH_ARGCAT)
static FAR char *nsh_strchr(FAR const char *str, int ch);
#else
# define nsh_strchr(s,c) strchr(s,c)
#endif
#ifdef NSH_HAVE_VARS
static FAR char *nsh_envexpand(FAR struct nsh_vtbl_s *vtbl,
FAR char *varname);
#endif
#if defined(CONFIG_NSH_QUOTE) && defined(CONFIG_NSH_ARGCAT)
static void nsh_dequote(FAR char *cmdline);
#else
# define nsh_dequote(c)
#endif
static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl,
FAR char *cmdline, FAR char **allocation, FAR int *isenvvar);
static FAR char *nsh_argument(FAR struct nsh_vtbl_s *vtbl, char **saveptr,
FAR NSH_MEMLIST_TYPE *memlist, FAR int *isenvvar);
#ifndef CONFIG_NSH_DISABLESCRIPT
#ifndef CONFIG_NSH_DISABLE_LOOPS
static bool nsh_loop_enabled(FAR struct nsh_vtbl_s *vtbl);
#endif
#ifndef CONFIG_NSH_DISABLE_ITEF
static bool nsh_itef_enabled(FAR struct nsh_vtbl_s *vtbl);
#endif
static bool nsh_cmdenabled(FAR struct nsh_vtbl_s *vtbl);
#ifndef CONFIG_NSH_DISABLE_LOOPS
static int nsh_loop(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
FAR char **saveptr, FAR NSH_MEMLIST_TYPE *memlist);
#endif
#ifndef CONFIG_NSH_DISABLE_ITEF
static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
FAR char **saveptr, FAR NSH_MEMLIST_TYPE *memlist);
#endif
#endif
#ifndef CONFIG_NSH_DISABLEBG
static int nsh_nice(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
FAR char **saveptr, FAR NSH_MEMLIST_TYPE *memlist);
#endif
#ifdef CONFIG_NSH_CMDPARMS
static int nsh_parse_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
FAR const char *redirfile);
#endif
static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline);
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_token_separator[] = " \t\n";
#ifndef NSH_DISABLE_SEMICOLON
static const char g_line_separator[] = "\"#;\n";
#endif
#ifdef CONFIG_NSH_ARGCAT
static const char g_arg_separator[] = "`$";
#endif
static const char g_redirect1[] = ">";
static const char g_redirect2[] = ">>";
#ifdef NSH_HAVE_VARS
static const char g_exitstatus[] = "?";
static const char g_success[] = "0";
static const char g_failure[] = "1";
#endif
#ifdef NEED_NULLSTRING
static const char g_nullstring[] = "";
#endif
/****************************************************************************
* Public Data
****************************************************************************/
/* If NuttX versioning information is available, Include that information
* in the NSH greeting.
*/
#if CONFIG_VERSION_MAJOR != 0 || CONFIG_VERSION_MINOR != 0
const char g_nshgreeting[] =
"\nNuttShell (NSH) NuttX-" CONFIG_VERSION_STRING "\n";
#else
const char g_nshgreeting[] = "\nNuttShell (NSH)\n";
#endif
/* Fixed Message of the Day (MOTD) */
#if defined(CONFIG_NSH_MOTD) && !defined(CONFIG_NSH_PLATFORM_MOTD)
const char g_nshmotd[] = CONFIG_NSH_MOTD_STRING;
#endif
/* Telnet login prompts */
#ifdef CONFIG_NSH_LOGIN
#if defined(CONFIG_NSH_TELNET_LOGIN) && defined(CONFIG_NSH_TELNET)
const char g_telnetgreeting[] =
"\nWelcome to NuttShell(NSH) Telnet Server...\n";
#endif
const char g_userprompt[] = "login: ";
const char g_passwordprompt[] = "password: ";
const char g_loginsuccess[] = "\nUser Logged-in!\n";
const char g_badcredentials[] = "\nInvalid username or password\n";
const char g_loginfailure[] = "Login failed!\n";
#endif
/* The NSH prompt */
const char g_nshprompt[] = CONFIG_NSH_PROMPT_STRING;
/* Common, message formats */
const char g_fmtsyntax[] = "nsh: %s: syntax error\n";
const char g_fmtargrequired[] = "nsh: %s: missing required argument(s)\n";
const char g_fmtnomatching[] = "nsh: %s: no matching %s\n";
const char g_fmtarginvalid[] = "nsh: %s: argument invalid\n";
const char g_fmtargrange[] = "nsh: %s: value out of range\n";
const char g_fmtcmdnotfound[] = "nsh: %s: command not found\n";
const char g_fmtnosuch[] = "nsh: %s: no such %s: %s\n";
const char g_fmttoomanyargs[] = "nsh: %s: too many arguments\n";
const char g_fmtdeepnesting[] = "nsh: %s: nesting too deep\n";
const char g_fmtcontext[] = "nsh: %s: not valid in this context\n";
#ifdef CONFIG_NSH_STRERROR
const char g_fmtcmdfailed[] = "nsh: %s: %s failed: %s\n";
#else
const char g_fmtcmdfailed[] = "nsh: %s: %s failed: %d\n";
#endif
const char g_fmtcmdoutofmemory[] = "nsh: %s: out of memory\n";
const char g_fmtinternalerror[] = "nsh: %s: Internal error\n";
const char g_fmtsignalrecvd[] = "nsh: %s: Interrupted by signal\n";
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nsh_memlist_add
****************************************************************************/
#ifdef HAVE_MEMLIST
static void nsh_memlist_add(FAR struct nsh_memlist_s *memlist,
FAR char *allocation)
{
if (memlist && allocation)
{
int index = memlist->nallocs;
if (index < CONFIG_NSH_MAXALLOCS)
{
memlist->allocations[index] = allocation;
memlist->nallocs = index + 1;
}
}
}
#endif
/****************************************************************************
* Name: nsh_memlist_free
****************************************************************************/
#ifdef HAVE_MEMLIST
static void nsh_memlist_free(FAR struct nsh_memlist_s *memlist)
{
if (memlist)
{
int index;
for (index = 0; index < memlist->nallocs; index++)
{
free(memlist->allocations[index]);
memlist->allocations[index] = NULL;
}
memlist->nallocs = 0;
}
}
#endif
/****************************************************************************
* Name: nsh_releaseargs
****************************************************************************/
#ifndef CONFIG_NSH_DISABLEBG
static void nsh_releaseargs(struct cmdarg_s *arg)
{
FAR struct nsh_vtbl_s *vtbl = arg->vtbl;
int i;
#if CONFIG_NFILE_STREAMS > 0
/* If the output was redirected, then file descriptor should
* be closed. The created task has its one, independent copy of
* the file descriptor
*/
if (vtbl->np.np_redirect)
{
close(arg->fd);
}
#endif
/* Released the cloned vtbl instance */
nsh_release(vtbl);
/* Release the cloned args */
for (i = 0; i < arg->argc; i++)
{
free(arg->argv[i]);
}
free(arg);
}
#endif
/****************************************************************************
* Name: nsh_child
****************************************************************************/
#ifndef CONFIG_NSH_DISABLEBG
static pthread_addr_t nsh_child(pthread_addr_t arg)
{
struct cmdarg_s *carg = (struct cmdarg_s *)arg;
int ret;
_info("BG %s\n", carg->argv[0]);
/* Execute the specified command on the child thread */
ret = nsh_command(carg->vtbl, carg->argc, carg->argv);
/* Released the cloned arguments */
_info("BG %s complete\n", carg->argv[0]);
nsh_releaseargs(carg);
return (pthread_addr_t)((uintptr_t)ret);
}
#endif
/****************************************************************************
* Name: nsh_cloneargs
****************************************************************************/
#ifndef CONFIG_NSH_DISABLEBG
static struct cmdarg_s *nsh_cloneargs(FAR struct nsh_vtbl_s *vtbl,
int fd, int argc, char *argv[])
{
struct cmdarg_s *ret = (struct cmdarg_s *)zalloc(sizeof(struct cmdarg_s));
int i;
if (ret)
{
ret->vtbl = vtbl;
ret->fd = fd;
ret->argc = argc;
for (i = 0; i < argc; i++)
{
ret->argv[i] = strdup(argv[i]);
}
}
return ret;
}
#endif
/****************************************************************************
* Name: nsh_saveresult
****************************************************************************/
static int nsh_saveresult(FAR struct nsh_vtbl_s *vtbl, bool result)
{
struct nsh_parser_s *np = &vtbl->np;
#ifndef CONFIG_NSH_DISABLESCRIPT
#ifndef CONFIG_NSH_DISABLE_LOOPS
/* Check if we are waiting for the condition associated with a while
* token.
*
* while <test-cmd>; do <cmd-sequence>; done
*
* Execute <cmd-sequence> as long as <test-cmd> has an exit status of
* zero.
*/
if (np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_WHILE)
{
np->np_fail = false;
np->np_lpstate[np->np_lpndx].lp_enable = (result == OK);
return OK;
}
/* Check if we are waiting for the condition associated with an until
* token.
*
* until <test-cmd>; do <cmd-sequence>; done
*
* Execute <cmd-sequence> as long as <test-cmd> has a non-zero exit
* status.
*/
else if (np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_UNTIL)
{
np->np_fail = false;
np->np_lpstate[np->np_lpndx].lp_enable = (result != OK);
return OK;
}
else
#endif
#ifndef CONFIG_NSH_DISABLE_ITEF
/* Check if we are waiting for the condition associated with an if token */
if (np->np_iestate[np->np_iendx].ie_state == NSH_ITEF_IF)
{
np->np_fail = false;
np->np_iestate[np->np_iendx].ie_ifcond =
np->np_iestate[np->np_iendx].ie_inverted ^ result;
return OK;
}
else
#endif
#endif
{
np->np_fail = result;
return result ? ERROR : OK;
}
}
/****************************************************************************
* Name: nsh_execute
****************************************************************************/
static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
int argc, FAR char *argv[],
FAR const char *redirfile, int oflags)
{
#if CONFIG_NFILE_STREAMS > 0 || !defined(CONFIG_NSH_DISABLEBG)
int fd = -1;
#endif
int ret;
/* DO NOT CHANGE THE ORDERING OF THE FOLLOWING STEPS
*
* They must work as follows:
*
* 1. Load a file from file system if possible. An external command on a
* file system with the provided name (and on the defined PATH) takes
* precendence over any other source of a command by that name. This
* allows the user to replace a built-in command with a command on a`
* file system
*
* 2. If not, run a built-in application of that name if possible. A
* built-in application will take precendence over any NSH command.
*
* 3. If not, run an NSH command line command if possible.
*
* 4. If not, report that the command was not found.
*/
/* Does this command correspond to a built-in command?
* nsh_builtin() returns:
*
* -1 (ERROR) if the application task corresponding to 'argv[0]' could
* not be started (possibly because it does not exist).
* 0 (OK) if the application task corresponding to 'argv[0]' was
* and successfully started. If CONFIG_SCHED_WAITPID is
* defined, this return value also indicates that the
* application returned successful status (EXIT_SUCCESS)
* 1 If CONFIG_SCHED_WAITPID is defined, then this return value
* indicates that the application task was spawned
* successfully but returned failure exit status.
*
* Note the priority is not effected by nice-ness.
*/
#ifdef CONFIG_NSH_BUILTIN_APPS
#if CONFIG_NFILE_STREAMS > 0
ret = nsh_builtin(vtbl, argv[0], argv, redirfile, oflags);
#else
ret = nsh_builtin(vtbl, argv[0], argv, NULL, 0);
#endif
if (ret >= 0)
{
/* nsh_builtin() returned 0 or 1. This means that the built-in
* command was successfully started (although it may not have ran
* successfully). So certainly it is not an NSH command.
*/
/* Save the result: success if 0; failure if 1 */
return nsh_saveresult(vtbl, ret != OK);
}
/* No, not a built in command (or, at least, we were unable to start a
* built-in command of that name). Maybe it is a built-in application
* or an NSH command.
*/
#endif
/* Does this command correspond to an application filename?
* nsh_fileapp() returns:
*
* -1 (ERROR) if the application task corresponding to 'argv[0]' could
* not be started (possibly because it doesn not exist).
* 0 (OK) if the application task corresponding to 'argv[0]' was
* and successfully started. If CONFIG_SCHED_WAITPID is
* defined, this return value also indicates that the
* application returned successful status (EXIT_SUCCESS)
* 1 If CONFIG_SCHED_WAITPID is defined, then this return value
* indicates that the application task was spawned
* successfully but returned failure exit status.
*
* Note the priority is not effected by nice-ness.
*/
#ifdef CONFIG_NSH_FILE_APPS
ret = nsh_fileapp(vtbl, argv[0], argv, redirfile, oflags);
if (ret >= 0)
{
/* nsh_fileapp() returned 0 or 1. This means that the built-in
* command was successfully started (although it may not have ran
* successfully). So certainly it is not an NSH command.
*/
/* Save the result: success if 0; failure if 1 */
return nsh_saveresult(vtbl, ret != OK);
}
/* No, not a file name command (or, at least, we were unable to start a
* program of that name). Treat it like an NSH command.
*/
#endif
#if CONFIG_NFILE_STREAMS > 0
/* Redirected output? */
if (vtbl->np.np_redirect)
{
/* Open the redirection file. This file will eventually
* be closed by a call to either nsh_release (if the command
* is executed in the background) or by nsh_undirect if the
* command is executed in the foreground.
*/
fd = open(redirfile, oflags, 0666);
if (fd < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
goto errout;
}
}
#endif
/* Handle the case where the command is executed in background.
* However is app is to be started as built-in new process will
* be created anyway, so skip this step.
*/
#ifndef CONFIG_NSH_DISABLEBG
if (vtbl->np.np_bg)
{
struct sched_param param;
struct nsh_vtbl_s *bkgvtbl;
struct cmdarg_s *args;
pthread_attr_t attr;
pthread_t thread;
/* Get a cloned copy of the vtbl with reference count=1.
* after the command has been processed, the nsh_release() call
* at the end of nsh_child() will destroy the clone.
*/
bkgvtbl = nsh_clone(vtbl);
if (!bkgvtbl)
{
goto errout_with_redirect;
}
/* Create a container for the command arguments */
args = nsh_cloneargs(bkgvtbl, fd, argc, argv);
if (!args)
{
nsh_release(bkgvtbl);
goto errout_with_redirect;
}
#if CONFIG_NFILE_STREAMS > 0
/* Handle redirection of output via a file descriptor */
if (vtbl->np.np_redirect)
{
nsh_redirect(bkgvtbl, fd, NULL);
}
#endif
/* Get the execution priority of this task */
ret = sched_getparam(0, ¶m);
if (ret != 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "sched_getparm",
NSH_ERRNO);
nsh_releaseargs(args);
nsh_release(bkgvtbl);
goto errout;
}
/* Determine the priority to execute the command */
if (vtbl->np.np_nice != 0)
{
int priority = param.sched_priority - vtbl->np.np_nice;
if (vtbl->np.np_nice < 0)
{
int max_priority = sched_get_priority_max(SCHED_NSH);
if (priority > max_priority)
{
priority = max_priority;
}
}
else
{
int min_priority = sched_get_priority_min(SCHED_NSH);
if (priority < min_priority)
{
priority = min_priority;
}
}
param.sched_priority = priority;
}
/* Set up the thread attributes */
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr, SCHED_NSH);
pthread_attr_setschedparam(&attr, ¶m);
/* Execute the command as a separate thread at the appropriate
* priority.
*/
ret = pthread_create(&thread, &attr, nsh_child, (pthread_addr_t)args);
if (ret != 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "pthread_create",
NSH_ERRNO_OF(ret));
nsh_releaseargs(args);
nsh_release(bkgvtbl);
goto errout;
}
/* Detach from the pthread since we are not going to join with it.
* Otherwise, we would have a memory leak.
*/
pthread_detach(thread);
nsh_output(vtbl, "%s [%d:%d]\n", argv[0], thread,
param.sched_priority);
}
else
#endif
{
#if CONFIG_NFILE_STREAMS > 0
uint8_t save[SAVE_SIZE];
/* Handle redirection of output via a file descriptor */
if (vtbl->np.np_redirect)
{
nsh_redirect(vtbl, fd, save);
}
#endif
/* Then execute the command in "foreground" -- i.e., while the user
* waits for the next prompt. nsh_command will return:
*
* -1 (ERROR) if the command was unsuccessful
* 0 (OK) if the command was successful
*/
ret = nsh_command(vtbl, argc, argv);
#if CONFIG_NFILE_STREAMS > 0
/* Restore the original output. Undirect will close the redirection
* file descriptor.
*/
if (vtbl->np.np_redirect)
{
nsh_undirect(vtbl, save);
}
#endif
/* Mark errors so that it is possible to test for non-zero return
* values in nsh scripts.
*/
if (ret < 0)
{
goto errout;
}
}
/* Return success if the command succeeded (or at least, starting of the
* command task succeeded).
*/
return nsh_saveresult(vtbl, false);
#ifndef CONFIG_NSH_DISABLEBG
errout_with_redirect:
#if CONFIG_NFILE_STREAMS > 0
if (vtbl->np.np_redirect)
{
close(fd);
}
#endif
#endif
errout:
return nsh_saveresult(vtbl, true);
}
/****************************************************************************
* Name: nsh_filecat
****************************************************************************/
#ifdef CONFIG_NSH_CMDPARMS
static FAR char *nsh_filecat(FAR struct nsh_vtbl_s *vtbl, FAR char *s1,
FAR const char *filename)
{
struct stat buf;
size_t s1size = 0;
size_t allocsize;
ssize_t nbytesread;
FAR char *argument;
int index;
int fd;
int ret;
/* Get the size of the string */
if (s1)
{
s1size = (size_t)strlen(s1);
}
/* Get the size of file */
ret = stat(filename, &buf);
if (ret != 0)
{
nsh_error(vtbl, g_fmtcmdfailed, "``", "stat", NSH_ERRNO);
return NULL;
}
/* Get the total allocation size */
allocsize = s1size + (size_t)buf.st_size + 1;
argument = (FAR char *)realloc(s1, allocsize);
if (!argument)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, "``");
return NULL;
}
/* Open the source file for reading */
fd = open(filename, O_RDONLY);
if (fd < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, "``", "open", NSH_ERRNO);
goto errout_with_alloc;
}
/* Now copy the file. Loop until the entire file has been transferred to
* the allocated string (after the original contents of s1size bytes.
*/
for (index = s1size; index < allocsize - 1; )
{
/* Loop until we successfully read something , we encounter the
* end-of-file, or until a read error occurs
*/
do
{
nbytesread = read(fd, &argument[index], IOBUFFERSIZE);
if (nbytesread == 0)
{
/* Unexpected end of file -- Break out of the loop */
break;
}
else if (nbytesread < 0)
{
/* EINTR is not an error (but will still stop the copy) */
if (errno == EINTR)
{
nsh_error(vtbl, g_fmtsignalrecvd, "``");
}
else
{
/* Read error */
nsh_error(vtbl, g_fmtcmdfailed, "``", "read", NSH_ERRNO);
}
goto errout_with_fd;
}
}
while (nbytesread <= 0);
/* Update the index based upon the number of bytes read */
index += nbytesread;
}
/* Make sure that the new string is null terminated */
argument[index] = '\0';
/* Close the temporary file and return the concatenated value */
close (fd);
return argument;
errout_with_fd:
close(fd);
errout_with_alloc:
free(argument);
return NULL;
}
#endif
/****************************************************************************
* Name: nsh_cmdparm
****************************************************************************/
#ifdef CONFIG_NSH_CMDPARMS
static FAR char *nsh_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
FAR char **allocation)
{
FAR char *tmpfile;
FAR char *argument;
int ret;
/* We cannot process the command argument if there is no allocation
* pointer.
*/
if (!allocation)
{
return (FAR char *)g_nullstring;
}
/* Create a unique file name using the task ID */
tmpfile = NULL;
ret = asprintf(&tmpfile, "%s/TMP%d.dat", CONFIG_LIBC_TMPDIR, getpid());
if (ret < 0 || !tmpfile)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, "``");
return (FAR char *)g_nullstring;
}
/* Execute the command that will re-direct the output of the command to
* the temporary file. This is a simple command that can't handle most
* options.
*/
ret = nsh_parse_cmdparm(vtbl, cmdline, tmpfile);
if (ret != OK)
{
/* Report the failure */
nsh_error(vtbl, g_fmtcmdfailed, "``", "exec", NSH_ERRNO);
free(tmpfile);
return (FAR char *)g_nullstring;
}
/* Concatenate the file contents with the current allocation */
argument = nsh_filecat(vtbl, *allocation, tmpfile);
*allocation = argument;
/* We can now unlink the tmpfile and free the tmpfile string */
ret = unlink(tmpfile);
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, "``", "unlink", NSH_ERRNO);
}
free(tmpfile);
return argument;
}
#endif
/****************************************************************************
* Name: nsh_strcat
****************************************************************************/
#ifdef CONFIG_NSH_ARGCAT
static FAR char *nsh_strcat(FAR struct nsh_vtbl_s *vtbl, FAR char *s1,
FAR const char *s2)
{
FAR char *argument;
int s1size = 0;
int allocsize;
/* Get the size of the first string... it might be NULL */
if (s1)
{
s1size = strlen(s1);
}
/* Then reallocate the first string so that it is large enough to hold
* both (including the NUL terminator).
*/
allocsize = s1size + strlen(s2) + 1;
argument = (FAR char *)realloc(s1, allocsize);
if (!argument)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, "$");
argument = s1;
}
else
{
argument[s1size] = '\0'; /* (In case s1 was NULL) */
strcat(argument, s2);
}
return argument;
}
#endif
/****************************************************************************
* Name: nsh_strchr
****************************************************************************/