summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Demo/FreeRTOS_Plus_Reliance_Edge_and_CLI_Windows_Simulator/File-Related-CLI-commands.c
blob: 8659e59647af35ebaef5094f0f17385bb94751fb (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
/*
 * FreeRTOS V202112.00
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */

/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"

/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

/* FreeRTOS+CLI includes. */
#include "FreeRTOS_CLI.h"

/* File system includes. */
#include <redposix.h>
#include <redtests.h>

#ifdef _WINDOWS_
	#define snprintf _snprintf
#endif

#define cliNEW_LINE		"\r\n"

/*******************************************************************************
 * See the URL in the comments within main.c for the location of the online
 * documentation.
 ******************************************************************************/

/*
 * Print out information on a single file.
 */
static void prvCreateFileInfoString( char *pcBuffer, REDDIRENT *pxDirent );

/*
 * Copies an existing file into a newly created file.
 */
static BaseType_t prvPerformCopy( int32_t lSourceFildes,
							int32_t lDestinationFiledes,
							char *pxWriteBuffer,
							size_t xWriteBufferLen );

/*
 * Implements the DIR command.
 */
static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the DEL command.
 */
static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the TYPE command.
 */
static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the APPEND command.
 */
static BaseType_t prvAPPENDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the COPY command.
 */
static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the CREATE command.
 */
static BaseType_t prvCREATECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the MKDIR command.
 */
static BaseType_t prvMKDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the RENAME command.
 */
static BaseType_t prvRENAMECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the LINK command.
 */
static BaseType_t prvLINKCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the STAT command.
 */
static BaseType_t prvSTATCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the STATFS command.
 */
static BaseType_t prvSTATFSCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the FORMAT command.
 */
static BaseType_t prvFORMATCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the TRANSACT command.
 */
static BaseType_t prvTRANSACTCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the TRANSMASKGET command.
 */
static BaseType_t prvTRANSMASKGETCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the TRANSMASKSET command.
 */
static BaseType_t prvTRANSMASKSETCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the ABORT command.
 */
static BaseType_t prvABORTCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/*
 * Implements the TEST command.
 */
static BaseType_t prvTESTFSCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );


/* Structure that defines the DIR command line command, which lists all the
files in the current directory. */
static const CLI_Command_Definition_t xDIR =
{
	"dir", /* The command string to type. */
	"\r\ndir <filename>:\r\n Lists the files in the named directory\r\n",
	prvDIRCommand, /* The function to run. */
	1 /* One parameter is expected. */
};

/* Structure that defines the TYPE command line command, which prints the
contents of a file to the console. */
static const CLI_Command_Definition_t xTYPE =
{
	"type", /* The command string to type. */
	"\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",
	prvTYPECommand, /* The function to run. */
	1 /* One parameter is expected. */
};

/* Structure that defines the APPEND command line command, which appends data
to a file. */
static const CLI_Command_Definition_t xAPPEND =
{
	"append", /* The command string to type. */
	"\r\nappend <filename> <character> <length>:\r\n Appends data to a file (created if it does not exist)\r\n",
	prvAPPENDCommand, /* The function to run. */
	3 /* Three parameters are expected. */
};

/* Structure that defines the DEL command line command, which deletes a file. */
static const CLI_Command_Definition_t xDEL =
{
	"del", /* The command string to type. */
	"\r\ndel <filename>:\r\n deletes a file or directory\r\n",
	prvDELCommand, /* The function to run. */
	1 /* One parameter is expected. */
};

/* Structure that defines the COPY command line command, which copies a file. */
static const CLI_Command_Definition_t xCOPY =
{
	"copy", /* The command string to type. */
	"\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",
	prvCOPYCommand, /* The function to run. */
	2 /* Two parameters are expected. */
};

/* Structure that defines the CREATE command line command, which creates an
empty file. */
static const CLI_Command_Definition_t xCREATE =
{
	"create", /* The command string to type. */
	"\r\ncreate <filename>:\r\n Creates an empty file\r\n",
	prvCREATECommand, /* The function to run. */
	1 /* One parameter is expected. */
};

/* Structure that defines the MKDIR command line command, which creates an
empty directory. */
static const CLI_Command_Definition_t xMKDIR =
{
	"mkdir", /* The command string to type. */
	"\r\nmkdir <filename>:\r\n Creates an empty directory\r\n",
	prvMKDIRCommand, /* The function to run. */
	1 /* One parameter is expected. */
};

/* Structure that defines the RENAME command line command, which renames a file. */
static const CLI_Command_Definition_t xRENAME =
{
	"rename", /* The command string to type. */
	"\r\nrename <source file> <dest file>:\r\n Rename <source file> to <dest file>\r\n",
	prvRENAMECommand, /* The function to run. */
	2 /* Two parameters are expected. */
};

/* Structure that defines the LINK command line command, which creates a hard
link. */
static const CLI_Command_Definition_t xLINK =
{
	"link", /* The command string to type. */
	"\r\nlink <source file> <dest file>:\r\n Create hard link <dest file> pointing at <source file>\r\n",
	prvLINKCommand, /* The function to run. */
	2 /* Two parameters are expected. */
};

/* Structure that defines the STAT command line command, which shows various
information about a file. */
static const CLI_Command_Definition_t xSTAT =
{
	"stat", /* The command string to type. */
	"\r\nstat <filename>:\r\n Show file information\r\n",
	prvSTATCommand, /* The function to run. */
	1 /* One parameter is expected. */
};

/* Structure that defines the STATFS command line command, which shows various
file system information. */
static const CLI_Command_Definition_t xSTATFS =
{
	"statfs", /* The command string to type. */
	"\r\nstatfs:\r\n Show file system information.\r\n",
	prvSTATFSCommand, /* The function to run. */
	0 /* No parameters are expected. */
};

/* Structure that defines the FORMAT command line command, which re-formats the
file system. */
static const CLI_Command_Definition_t xFORMAT =
{
	"format", /* The command string to type. */
	"\r\nformat:\r\n Re-formats the file system volume.  ALL FILES WILL BE DELETED!\r\n",
	prvFORMATCommand, /* The function to run. */
	0 /* No parameters are expected. */
};

/* Structure that defines the TRANSACT command line command, which commits a
transaction point. */
static const CLI_Command_Definition_t xTRANSACT =
{
	"transact", /* The command string to type. */
	"\r\ntransact:\r\n Commit a Reliance Edge transaction point\r\n",
	prvTRANSACTCommand, /* The function to run. */
	0 /* No parameters are expected. */
};

/* Structure that defines the TRANSMASKGET command line command, which retrieves
the current automatic transaction event mask. */
static const CLI_Command_Definition_t xTRANSMASKGET =
{
	"transmaskget", /* The command string to type. */
	"\r\ntransmaskget:\r\n Retrieve the Reliance Edge automatic transaction mask\r\n",
	prvTRANSMASKGETCommand, /* The function to run. */
	0 /* No parameters are expected. */
};

/* Structure that defines the TRANSMASKSET command line command, which sets the
automatic transaction event mask. */
static const CLI_Command_Definition_t xTRANSMASKSET =
{
	"transmaskset", /* The command string to type. */
	"\r\ntransmaskset <hex mask>:\r\n Set the Reliance Edge automatic transaction mask\r\n",
	prvTRANSMASKSETCommand, /* The function to run. */
	1 /* One parameter is expected. */
};

/* Structure that defines the ABORT command line command, which rolls back
changes which have not been transacted. */
static const CLI_Command_Definition_t xABORT =
{
	"abort", /* The command string to type. */
	"\r\nabort:\r\n Roll back all changes not part of the last transaction point\r\n",
	prvABORTCommand, /* The function to run. */
	0 /* No parameters are expected. */
};

/* Structure that defines the TEST-FS command line command, which executes some
file system driver tests. */
static const CLI_Command_Definition_t xTEST_FS =
{
	"test-fs", /* The command string to type. */
	"\r\ntest-fs:\r\n Executes file system tests.  ALL FILES WILL BE DELETED!\r\n",
	prvTESTFSCommand, /* The function to run. */
	0 /* No parameters are expected. */
};

/*-----------------------------------------------------------*/

void vRegisterFileSystemCLICommands( void )
{
	/* Register all the command line commands defined immediately above. */
	FreeRTOS_CLIRegisterCommand( &xDIR );
	FreeRTOS_CLIRegisterCommand( &xTYPE );
	FreeRTOS_CLIRegisterCommand( &xAPPEND );
	FreeRTOS_CLIRegisterCommand( &xDEL );
	FreeRTOS_CLIRegisterCommand( &xCOPY );
	FreeRTOS_CLIRegisterCommand( &xCREATE );
	FreeRTOS_CLIRegisterCommand( &xMKDIR );
	FreeRTOS_CLIRegisterCommand( &xRENAME );
	FreeRTOS_CLIRegisterCommand( &xLINK );
	FreeRTOS_CLIRegisterCommand( &xSTAT );
	FreeRTOS_CLIRegisterCommand( &xSTATFS );
	FreeRTOS_CLIRegisterCommand( &xFORMAT );
	FreeRTOS_CLIRegisterCommand( &xTRANSACT );
	FreeRTOS_CLIRegisterCommand( &xTRANSMASKGET );
	FreeRTOS_CLIRegisterCommand( &xTRANSMASKSET );
	FreeRTOS_CLIRegisterCommand( &xABORT );
	FreeRTOS_CLIRegisterCommand( &xTEST_FS );
}
/*-----------------------------------------------------------*/

static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
static REDDIR *pxDir = NULL;
REDDIRENT *pxDirent;
const char *pcParameter;
BaseType_t xParameterStringLength, xReturn = pdFALSE;

	/* This assumes pcWriteBuffer is long enough. */
	( void ) pcCommandString;

	/* Ensure the buffer leaves space for the \r\n. */
	configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
	xWriteBufferLen -= strlen( cliNEW_LINE );

	if( pxDir == NULL )
	{
		/* Retrieve the directory to DIR. */
		pcParameter = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							1,						/* Return the first parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

		/* Sanity check something was returned. */
		configASSERT( pcParameter );

		/* This is the first time this function has been executed since the Dir
		command was run.  Open the directory. */
		pxDir = red_opendir( pcParameter );
	}

	if( pxDir )
	{
		/* red_readdir() returns NULL either on error or upon reaching the
		end of the directory.  Clear errno so these conditions can be
		distinguished. */
		red_errno = 0;
		pxDirent = red_readdir( pxDir );

		if( pxDirent )
		{
			prvCreateFileInfoString( pcWriteBuffer, pxDirent );
			xReturn = pdPASS;
		}
		else if( red_errno == 0 )
		{
			/* There are no more files.  Close the directory. */
			red_closedir( pxDir );
			pxDir = NULL;

			/* No string to return. */
			pcWriteBuffer[ 0 ] = 0x00;
		}
		else
		{
			snprintf( pcWriteBuffer, xWriteBufferLen, "Error %d reading directory.", ( int ) red_errno );
		}
	}
	else
	{
		/* User-friendly messages for common errors. */
		switch( red_errno )
		{
			case RED_ENOENT :
				snprintf( pcWriteBuffer, xWriteBufferLen, "Directory not found." );
				break;

			case RED_ENOTDIR :
				snprintf( pcWriteBuffer, xWriteBufferLen, "Directory not found or not a directory." );
				break;

			default :
				snprintf( pcWriteBuffer, xWriteBufferLen, "Error %d opening directory.", ( int ) red_errno );
				break;
		}
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return xReturn;
}
/*-----------------------------------------------------------*/

static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength, xReturn = pdTRUE;
static int32_t lFildes = -1;
REDSTAT finfo;
int32_t lStatus, lBytesRead;
size_t xColumns = 50U;

	/* Ensure there is always a null terminator after each character written. */
	memset( pcWriteBuffer, 0x00, xWriteBufferLen );

	/* Ensure the buffer leaves space for the \r\n. */
	configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
	xWriteBufferLen -= strlen( cliNEW_LINE );

	if( xWriteBufferLen < xColumns )
	{
		/* Ensure the loop that uses xColumns as an end condition does not
		write off the end of the buffer. */
		xColumns = xWriteBufferLen;
	}

	if( lFildes == -1 )
	{
		/* The file has not been opened yet.  Find the file name. */
		pcParameter = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							1,						/* Return the first parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

		/* Sanity check something was returned. */
		configASSERT( pcParameter );

		/* Attempt to open the requested file. */
		lFildes = red_open( pcParameter, RED_O_RDONLY );
		if( lFildes == -1 )
		{
			/* User-friendly messages for common errors. */
			switch( red_errno )
			{
				case RED_ENOENT :
				case RED_ENOTDIR :
					snprintf( pcWriteBuffer, xWriteBufferLen, "File not found." );
					break;

				default :
					snprintf( pcWriteBuffer, xWriteBufferLen, "Error %d opening file.", ( int ) red_errno );
					break;
			}
		}
		else
		{
			/* Make sure this is a file, not a directory. */
			lStatus = red_fstat( lFildes, &finfo );
			if( lStatus == 0 )
			{
				if( RED_S_ISDIR( finfo.st_mode ) )
				{
					snprintf( pcWriteBuffer, xWriteBufferLen, "Cannot TYPE a directory." );
					red_close( lFildes );
					lFildes = -1;
				}
			}
			else
			{
				snprintf( pcWriteBuffer, xWriteBufferLen, "Error %d querying file.", ( int ) red_errno );
				red_close( lFildes );
				lFildes = -1;
			}
		}
	}

	if( lFildes != -1 )
	{
		/* Read the next chunk of data from the file. */
		lBytesRead = red_read( lFildes, pcWriteBuffer, xColumns );

		if( lBytesRead < ( int32_t ) xColumns )
		{
			/* Error or no more characters to return. */
			red_close( lFildes );
			lFildes = -1;
		}
	}

	if( lFildes == -1 )
	{
		/* Either the file was not opened, or all the data from the file has
		been returned and the file is now closed. */
		xReturn = pdFALSE;
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return xReturn;
}
/*-----------------------------------------------------------*/

static BaseType_t prvAPPENDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
char *pcFileName = NULL;
const char *pcCharacter = NULL, *pcLength;
BaseType_t xParameterStringLength, xGoodParameters = pdTRUE;
int32_t lFildes, lAppendLength = -1, lThisWrite, lTotalWritten, lBytesWritten;

	/* This function assumes xWriteBufferLen is large enough! */
	( void ) xWriteBufferLen;

	/* Find the length to write. */
	pcLength = FreeRTOS_CLIGetParameter
					(
						pcCommandString,		/* The command string itself. */
						3,						/* Return the third parameter. */
						&xParameterStringLength	/* Store the parameter string length. */
					);
	configASSERT( pcLength );

	/* Convert the string into a number. */
	lAppendLength = RedAtoI( pcLength );
	if( lAppendLength < 0 )
	{
		strcpy( pcWriteBuffer, "Third parameter cannot be a negative number." );
		xGoodParameters = pdFALSE;
	}

	if( xGoodParameters )
	{
		/* Find the character to write. */
		pcCharacter = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							2,						/* Return the second parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);
		configASSERT( pcCharacter );

		if( xParameterStringLength != 1 )
		{
			strcpy( pcWriteBuffer, "Second parameter must be a single character." );
			xGoodParameters = pdFALSE;
		}
	}

	if( xGoodParameters )
	{
		/* Find the file name. */
		pcFileName = ( char * ) FreeRTOS_CLIGetParameter
								(
									pcCommandString,		/* The command string itself. */
									1,						/* Return the first parameter. */
									&xParameterStringLength	/* Store the parameter string length. */
								);
		configASSERT( pcFileName );

		/* Terminate the string. */
		pcFileName[ xParameterStringLength ] = 0x00;
	}

	if( xGoodParameters )
	{
		/* Attempt to open the requested file. */
		lFildes = red_open( pcFileName, RED_O_WRONLY|RED_O_APPEND|RED_O_CREAT );

		if( lFildes == -1 )
		{
			/* User-friendly messages for common errors. */
			switch( red_errno )
			{
				case RED_ENOENT :
				case RED_ENOTDIR :
					strcpy( pcWriteBuffer, "Bad file path." );
					break;

				case RED_EISDIR :
					strcpy( pcWriteBuffer, "Cannot append to a directory." );
					break;

				default :
					sprintf( pcWriteBuffer, "Error %d opening file.", ( int ) red_errno );
					break;
			}
		}
		else
		{
			/* Put the requested character into the buffer. */
			memset( pcWriteBuffer, pcCharacter[0], xWriteBufferLen );

			/* Append the data. */
			for( lTotalWritten = 0; lTotalWritten < lAppendLength; lTotalWritten += lThisWrite )
			{
				lThisWrite = lAppendLength - lTotalWritten;
				if( lThisWrite > ( int32_t ) xWriteBufferLen )
				{
					lThisWrite = ( int32_t ) xWriteBufferLen;
				}

				lBytesWritten = red_write( lFildes, pcWriteBuffer, lThisWrite );
				if( lBytesWritten == -1 )
				{
					/* User-friendly messages for common errors. */
					switch( red_errno )
					{
						case RED_ENOSPC :
							strcpy( pcWriteBuffer, "Out of disk space." );
							break;

						default :
							sprintf( pcWriteBuffer, "Error %d writing to file.", ( int ) red_errno );
							break;
					}

					break;
				}
				else if( lBytesWritten != lThisWrite )
				{
					/*	Some data was written, but not all of it.  This only
					happens when the disk is full or the file reached its
					maximum size.  That latter is unlikely in this demo. */
					strcpy( pcWriteBuffer, "Out of disk space." );
					break;
				}
			}

			if( lTotalWritten == lAppendLength )
			{
				strcpy( pcWriteBuffer, "Append successful." );
			}

			red_close( lFildes );
		}
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength;
int32_t lStatus;

	/* This function assumes xWriteBufferLen is large enough! */
	( void ) xWriteBufferLen;

	/* Obtain the parameter string. */
	pcParameter = FreeRTOS_CLIGetParameter
					(
						pcCommandString,		/* The command string itself. */
						1,						/* Return the first parameter. */
						&xParameterStringLength	/* Store the parameter string length. */
					);

	/* Sanity check something was returned. */
	configASSERT( pcParameter );

	/* Attempt to delete the file or directory. */
	lStatus = red_unlink( pcParameter );

	if( lStatus == 0 )
	{
		sprintf( pcWriteBuffer, "%s was deleted", pcParameter );
	}
	else
	{
		/* User-friendly messages for common errors. */
		switch( red_errno )
		{
			case RED_ENOTDIR :
			case RED_ENOENT :
				sprintf( pcWriteBuffer, "File not found." );
				break;

			case RED_ENOTEMPTY :
				sprintf( pcWriteBuffer, "Cannot remove directory: not empty." );
				break;

			default :
				sprintf( pcWriteBuffer, "Error %d deleting file.", ( int ) red_errno );
				break;
		}
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
char *pcSourceFile;
const char *pcDestinationFile;
BaseType_t xParameterStringLength;
int32_t lSourceFildes, lDestinationFildes;

	/* Obtain the name of the destination file. */
	pcDestinationFile = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							2,						/* Return the second parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

	/* Sanity check something was returned. */
	configASSERT( pcDestinationFile );

	/* Obtain the name of the source file. */
	pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter
								(
									pcCommandString,		/* The command string itself. */
									1,						/* Return the first parameter. */
									&xParameterStringLength	/* Store the parameter string length. */
								);

	/* Sanity check something was returned. */
	configASSERT( pcSourceFile );

	/* Terminate the string. */
	pcSourceFile[ xParameterStringLength ] = 0x00;

	/* See if the source file exists, openm it if it does. */
	lSourceFildes = red_open( pcSourceFile, RED_O_RDONLY );

	if( lSourceFildes == -1 )
	{
		sprintf( pcWriteBuffer, "Source file does not exist" );
	}
	else
	{
		/* Create the destination file, error if it already exists. */
		lDestinationFildes = red_open( pcDestinationFile, RED_O_CREAT|RED_O_EXCL|RED_O_WRONLY );

		if( lDestinationFildes == -1 )
		{
			sprintf( pcWriteBuffer, "Error: Destination file already exists" );
		}
		else
		{
			if( prvPerformCopy( lSourceFildes, lDestinationFildes, pcWriteBuffer, xWriteBufferLen ) == pdPASS )
			{
				sprintf( pcWriteBuffer, "Copy made" );
			}
			else
			{
				sprintf( pcWriteBuffer, "Error during copy" );
			}

			red_close( lDestinationFildes );
		}

		red_close( lSourceFildes );
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvCREATECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength;
int32_t lFildes;

	/* This function assumes xWriteBufferLen is large enough! */
	( void ) xWriteBufferLen;

	/* Obtain the parameter string. */
	pcParameter = FreeRTOS_CLIGetParameter
					(
						pcCommandString,		/* The command string itself. */
						1,						/* Return the first parameter. */
						&xParameterStringLength	/* Store the parameter string length. */
					);

	/* Sanity check something was returned. */
	configASSERT( pcParameter );

	/* Attempt to create the file. */
	lFildes = red_open( pcParameter, RED_O_CREAT|RED_O_EXCL|RED_O_RDWR );

	if( lFildes != -1 )
	{
		sprintf( pcWriteBuffer, "%s was created", pcParameter );
		red_close( lFildes );
	}
	else
	{
		/* User-friendly messages for common errors. */
		switch( red_errno )
		{
			case RED_ENOTDIR :
			case RED_ENOENT :
				sprintf( pcWriteBuffer, "Bad file path." );
				break;

			case RED_EEXIST :
				sprintf( pcWriteBuffer, "File already exists." );
				break;

			default :
				sprintf( pcWriteBuffer, "Error %d creating file.", ( int ) red_errno );
				break;
		}
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvMKDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength;
int32_t lStatus;

	/* This function assumes xWriteBufferLen is large enough! */
	( void ) xWriteBufferLen;

	/* Obtain the parameter string. */
	pcParameter = FreeRTOS_CLIGetParameter
					(
						pcCommandString,		/* The command string itself. */
						1,						/* Return the first parameter. */
						&xParameterStringLength	/* Store the parameter string length. */
					);

	/* Sanity check something was returned. */
	configASSERT( pcParameter );

	/* Attempt to create the file. */
	lStatus = red_mkdir( pcParameter );

	if( lStatus == 0 )
	{
		sprintf( pcWriteBuffer, "%s was created", pcParameter );
	}
	else
	{
		/* User-friendly messages for common errors. */
		switch( red_errno )
		{
			case RED_ENOTDIR :
			case RED_ENOENT :
				sprintf( pcWriteBuffer, "Bad file path." );
				break;

			case RED_EEXIST :
				sprintf( pcWriteBuffer, "Directory already exists." );
				break;

			default :
				sprintf( pcWriteBuffer, "Error %d creating directory.", ( int ) red_errno );
				break;
		}
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvRENAMECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcDestinationFile;
char *pcSourceFile;
BaseType_t xParameterStringLength;
int32_t lStatus;

	/* This function assumes xWriteBufferLen is large enough! */
	( void ) xWriteBufferLen;

	/* Obtain the name of the destination file. */
	pcDestinationFile = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							2,						/* Return the second parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

	/* Sanity check something was returned. */
	configASSERT( pcDestinationFile );

	/* Obtain the name of the source file. */
	pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter
								(
									pcCommandString,		/* The command string itself. */
									1,						/* Return the first parameter. */
									&xParameterStringLength	/* Store the parameter string length. */
								);

	/* Sanity check something was returned. */
	configASSERT( pcSourceFile );

	/* Terminate the string. */
	pcSourceFile[ xParameterStringLength ] = 0x00;

	/* Attempt to rename the file. */
	lStatus = red_rename( pcSourceFile, pcDestinationFile );

	if( lStatus == 0 )
	{
		sprintf( pcWriteBuffer, "%s was renamed to %s", pcSourceFile, pcDestinationFile );
	}
	else
	{
		/* User-friendly messages for common errors. */
		switch( red_errno )
		{
			case RED_ENOTDIR :
			case RED_ENOENT :
			case RED_EISDIR :
				sprintf( pcWriteBuffer, "Bad file path." );
				break;

			/* This will only be seen if POSIX rename is disabled. */
			case RED_EEXIST :
				sprintf( pcWriteBuffer, "Destination already exists." );
				break;

			default :
				sprintf( pcWriteBuffer, "Error %d renaming file.", ( int ) red_errno );
				break;
		}
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvLINKCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcDestinationFile;
char *pcSourceFile;
BaseType_t xParameterStringLength;
int32_t lStatus;

	/* This function assumes xWriteBufferLen is large enough! */
	( void ) xWriteBufferLen;

	/* Obtain the name of the destination file. */
	pcDestinationFile = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							2,						/* Return the second parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

	/* Sanity check something was returned. */
	configASSERT( pcDestinationFile );

	/* Obtain the name of the source file. */
	pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter
								(
									pcCommandString,		/* The command string itself. */
									1,						/* Return the first parameter. */
									&xParameterStringLength	/* Store the parameter string length. */
								);

	/* Sanity check something was returned. */
	configASSERT( pcSourceFile );

	/* Terminate the string. */
	pcSourceFile[ xParameterStringLength ] = 0x00;

	/* Attempt to create the hard link. */
	lStatus = red_link( pcSourceFile, pcDestinationFile );

	if( lStatus == 0 )
	{
		sprintf( pcWriteBuffer, "%s was linked to %s", pcDestinationFile, pcSourceFile );
	}
	else
	{
		/* User-friendly messages for common errors. */
		switch( red_errno )
		{
			case RED_ENOTDIR :
			case RED_ENOENT :
				sprintf( pcWriteBuffer, "Bad file path." );
				break;

			case RED_EPERM :
				sprintf( pcWriteBuffer, "Cannot link a directory." );
				break;

			case RED_EMLINK :
				sprintf( pcWriteBuffer, "Too many hard links." );
				break;

			default :
				sprintf( pcWriteBuffer, "Error %d linking file.", ( int ) red_errno );
				break;
		}
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvSTATCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter, *pcModeString;
BaseType_t xParameterStringLength;
REDSTAT finfo;
int32_t lFildes, lStatus;

	/* Ensure the buffer leaves space for the \r\n. */
	configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
	xWriteBufferLen -= strlen( cliNEW_LINE );

	/* Find the file name. */
	pcParameter = FreeRTOS_CLIGetParameter
					(
						pcCommandString,		/* The command string itself. */
						1,						/* Return the first parameter. */
						&xParameterStringLength	/* Store the parameter string length. */
					);

	/* Sanity check something was returned. */
	configASSERT( pcParameter );

	/* Attempt to open the requested file. */
	lFildes = red_open( pcParameter, RED_O_RDONLY );
	if( lFildes == -1 )
	{
		/* User-friendly messages for common errors. */
		switch( red_errno )
		{
			case RED_ENOENT :
			case RED_ENOTDIR :
				snprintf( pcWriteBuffer, xWriteBufferLen, "File not found." );
				break;

			default :
				snprintf( pcWriteBuffer, xWriteBufferLen, "Error %d opening file.", ( int ) red_errno );
				break;
		}
	}
	else
	{
		lStatus = red_fstat( lFildes, &finfo );
		if( lStatus == 0 )
		{
			if( RED_S_ISDIR( finfo.st_mode ) )
			{
				pcModeString = "dir";
			}
			else
			{
				pcModeString = "file";
			}

			snprintf( pcWriteBuffer, xWriteBufferLen, "ino=%lu mode=0x%04x(%s) nlink=%x size=%lu blocks=%lu",
				( unsigned long ) finfo.st_ino, ( unsigned ) finfo.st_mode, pcModeString,
				( unsigned ) finfo.st_nlink, (unsigned long) finfo.st_size, (unsigned long) finfo.st_blocks );
		}
		else
		{
			snprintf( pcWriteBuffer, xWriteBufferLen, "Error %d querying file.", ( int ) red_errno );
		}

		red_close( lFildes );
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvSTATFSCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
REDSTATFS fsinfo;
int32_t lStatus;

	/* Avoid compiler warnings. */
	( void ) pcCommandString;

	/* Ensure the buffer leaves space for the \r\n. */
	configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
	xWriteBufferLen -= strlen( cliNEW_LINE );

	lStatus = red_statvfs( "", &fsinfo );

	if( lStatus == -1 )
	{
		snprintf( pcWriteBuffer, xWriteBufferLen, "Error %d querying file system.", ( int ) red_errno );
	}
	else
	{
		snprintf( pcWriteBuffer, xWriteBufferLen,
			"Block size: %lu\r\n"
			"Block count: %lu\r\n"
			"Free blocks: %lu\r\n"
			"Inode count: %lu\r\n"
			"Free inodes: %lu\r\n",
			( unsigned long ) fsinfo.f_bsize, ( unsigned long ) fsinfo.f_blocks,
			( unsigned long ) fsinfo.f_bfree, ( unsigned long ) fsinfo.f_files,
			( unsigned long ) fsinfo.f_ffree );
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvFORMATCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
int32_t lStatus;

	/* Avoid compiler warnings. */
	( void ) pcCommandString;

	/* This function assumes xWriteBufferLen is large enough! */
	( void ) xWriteBufferLen;

	/* File system volumes cannot be formatted while mounted. */
	lStatus = red_umount( "" );
	if( lStatus == -1 )
	{
		sprintf( pcWriteBuffer, "Error %d during unmount.",  ( int ) red_errno );
	}
	else
	{
		/* Re-format the file system volume. */
		lStatus = red_format( "" );

		if( lStatus == -1 )
		{
			sprintf( pcWriteBuffer, "Error %d during format.",  ( int ) red_errno );
		}
		else
		{
			/* Mount again so that other commands will work properly. */
			lStatus = red_mount( "" );

			if( lStatus == -1 )
			{
				sprintf( pcWriteBuffer, "Error %d during mount.",  ( int ) red_errno );
			}
			else
			{
				strcpy( pcWriteBuffer, "Format successful." );
			}
		}
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvTRANSACTCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
int32_t lStatus;

	/* Avoid compiler warnings. */
	( void ) pcCommandString;

	/* This function assumes xWriteBufferLen is large enough! */
	( void ) xWriteBufferLen;

	/* Save the original transaction mask settings. */
	lStatus = red_transact( "" );

	if( lStatus == -1 )
	{
		sprintf( pcWriteBuffer, "Error %d during transaction point.",  ( int ) red_errno );
	}
	else
	{
		strcpy( pcWriteBuffer, "Transaction point successful." );
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvTRANSMASKGETCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
uint32_t ulEventMask;
int32_t lStatus;

	/* Avoid compiler warnings. */
	( void ) pcCommandString;

	/* Ensure the buffer leaves space for the \r\n. */
	configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
	xWriteBufferLen -= strlen( cliNEW_LINE );

	lStatus = red_gettransmask( "", &ulEventMask );
	if( lStatus == -1 )
	{
		snprintf( pcWriteBuffer, xWriteBufferLen, "Error %d retrieving automatic transaction event mask.",  ( int ) red_errno );
	}
	else
	{
		snprintf( pcWriteBuffer, xWriteBufferLen,
			"Current automatic transaction event mask: 0x%04lx\r\n"
			"RED_TRANSACT_UMOUNT   (0x0001): %s\r\n"
			"RED_TRANSACT_CREAT    (0x0002): %s\r\n"
			"RED_TRANSACT_UNLINK   (0x0004): %s\r\n"
			"RED_TRANSACT_MKDIR    (0x0008): %s\r\n"
			"RED_TRANSACT_RENAME   (0x0010): %s\r\n"
			"RED_TRANSACT_LINK     (0x0020): %s\r\n"
			"RED_TRANSACT_CLOSE    (0x0040): %s\r\n"
			"RED_TRANSACT_WRITE    (0x0080): %s\r\n"
			"RED_TRANSACT_FSYNC    (0x0100): %s\r\n"
			"RED_TRANSACT_TRUNCATE (0x0200): %s\r\n"
			"RED_TRANSACT_VOLFULL  (0x0400): %s\r\n",
			( unsigned long ) ulEventMask,
			( ulEventMask & RED_TRANSACT_UMOUNT )   ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_CREAT )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_UNLINK )   ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_MKDIR )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_RENAME )   ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_LINK )     ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_CLOSE )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_WRITE )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_FSYNC )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_TRUNCATE ) ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_VOLFULL )  ? "Enabled" : "Disabled" );
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvTRANSMASKSETCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength;
uint32_t ulEventMask;
int32_t lStatus;

	/* Ensure the buffer leaves space for the \r\n. */
	configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
	xWriteBufferLen -= strlen( cliNEW_LINE );

	/* Obtain the parameter string. */
	pcParameter = FreeRTOS_CLIGetParameter
					(
						pcCommandString,		/* The command string itself. */
						1,						/* Return the first parameter. */
						&xParameterStringLength	/* Store the parameter string length. */
					);

	/* Sanity check something was returned. */
	configASSERT( pcParameter );

	if( ( pcParameter[0] == '0' ) && ( ( pcParameter[1] == 'x' ) || ( pcParameter[1] == 'X' ) ) )
	{
		pcParameter += 2;
	}

	/* Convert the argument into a value. */
	RedHtoUL( pcParameter, &ulEventMask );

	/* Set the new transaction mask. */
	lStatus = red_settransmask( "", ulEventMask );
	if( lStatus == -1 )
	{
		/* User-friendly messages for common errors. */
		switch( red_errno )
		{
			case RED_EINVAL:
				snprintf( pcWriteBuffer, xWriteBufferLen, "Invalid bits in transaction mask." );
				break;

			default :
				snprintf( pcWriteBuffer, xWriteBufferLen, "Error %d setting transaction mask.", ( int ) red_errno );
				break;
		}
	}
	else
	{
		snprintf( pcWriteBuffer, xWriteBufferLen,
			"Successfully set automatic transaction mask.  Enabled events:\r\n"
			"RED_TRANSACT_UMOUNT   (0x0001): %s\r\n"
			"RED_TRANSACT_CREAT    (0x0002): %s\r\n"
			"RED_TRANSACT_UNLINK   (0x0004): %s\r\n"
			"RED_TRANSACT_MKDIR    (0x0008): %s\r\n"
			"RED_TRANSACT_RENAME   (0x0010): %s\r\n"
			"RED_TRANSACT_LINK     (0x0020): %s\r\n"
			"RED_TRANSACT_CLOSE    (0x0040): %s\r\n"
			"RED_TRANSACT_WRITE    (0x0080): %s\r\n"
			"RED_TRANSACT_FSYNC    (0x0100): %s\r\n"
			"RED_TRANSACT_TRUNCATE (0x0200): %s\r\n"
			"RED_TRANSACT_VOLFULL  (0x0400): %s\r\n",
			( ulEventMask & RED_TRANSACT_UMOUNT )   ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_CREAT )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_UNLINK )   ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_MKDIR )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_RENAME )   ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_LINK )     ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_CLOSE )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_WRITE )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_FSYNC )    ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_TRUNCATE ) ? "Enabled" : "Disabled",
			( ulEventMask & RED_TRANSACT_VOLFULL )  ? "Enabled" : "Disabled" );
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvABORTCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
uint32_t ulEventMask;
int32_t lStatus;

	/* Avoid compiler warnings. */
	( void ) pcCommandString;

	/* This function assumes xWriteBufferLen is large enough! */
	( void ) xWriteBufferLen;

	/* Save the original transaction mask settings. */
	lStatus = red_gettransmask( "", &ulEventMask );

	if( lStatus == -1 )
	{
		sprintf( pcWriteBuffer, "Error %d querying transaction mask.",  ( int ) red_errno );
	}
	else
	{
		/* Make it so that red_umount() will not automatically commit a new
		transaction point. */
		lStatus = red_settransmask( "", ulEventMask & ~( ( uint32_t ) RED_TRANSACT_UMOUNT ) );

		if( lStatus == -1 )
		{
			sprintf( pcWriteBuffer, "Error %d setting transaction mask.",  ( int ) red_errno );
		}
		else
		{
			/* Unmount.  Since red_umount() will not transact, all changes which
			were not already transacted are rolled back. */
			lStatus = red_umount( "" );

			if( lStatus == -1 )
			{
				sprintf( pcWriteBuffer, "Error %d during unmount.",  ( int ) red_errno );
			}
			else
			{
				/* Mount.  Mount always starts from the last transaction point. */
				lStatus = red_mount( "" );

				if( lStatus == -1 )
				{
					sprintf( pcWriteBuffer, "Error %d during mount.",  ( int ) red_errno );
				}
				else
				{
					strcpy( pcWriteBuffer, "Working state changes successfully aborted." );
				}
			}

			/* Restore the original transaction mask settings. */
			red_settransmask( "", ulEventMask );
		}
	}

	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvTESTFSCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
UBaseType_t uxOriginalPriority;
FSSTRESSPARAM param;

	/* Avoid compiler warnings. */
	( void ) xWriteBufferLen;
	( void ) pcCommandString;

	/* Limitations in the interaction with the Windows TCP/IP stack require
	the command console to run at the idle priority.  Raise the priority for
	the duration of the tests to ensure there are not multiple switches to the
	idle task as in the simulated environment the idle task hook function may
	include a (relatively) long delay. */
	uxOriginalPriority = uxTaskPriorityGet( NULL );
	vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 );

	/* Delete all files to avoid interfering with the test. */
	red_umount( "" );
	red_format( "" );
	red_mount( "" );

	FsstressDefaultParams(&param);
	param.fVerbose = pdTRUE;
	param.ulNops = 10000;
	param.ulSeed = 1;
	FsstressStart(&param);

	/* Clean up after the test. */
	red_umount( "" );
	red_format( "" );
	red_mount( "" );

	/* Reset back to the original priority. */
	vTaskPrioritySet( NULL, uxOriginalPriority );

	sprintf( pcWriteBuffer, "%s", "Test results were sent to Windows console" );
	strcat( pcWriteBuffer, cliNEW_LINE );

	return pdFALSE;
}
/*-----------------------------------------------------------*/

static BaseType_t prvPerformCopy( int32_t lSourceFildes,
									int32_t lDestinationFiledes,
									char *pxWriteBuffer,
									size_t xWriteBufferLen )
{
int32_t lBytesRead;
BaseType_t xReturn = pdPASS;

	/* Assuming both files are at offset zero. */

	for( ;; )
	{
		/* Read the next block of data. */
		lBytesRead = red_read( lSourceFildes, pxWriteBuffer, xWriteBufferLen );
		if( lBytesRead <= 0 )
		{
			if( lBytesRead == -1)
			{
				/* Error reading from file. */
				xReturn = pdFAIL;
			}
			else
			{
				/* No error: reached end of file, time to stop. */
			}

			break;
		}

		/* Write the block of data to the end of the file. */
		if( red_write( lDestinationFiledes, pxWriteBuffer, lBytesRead ) != lBytesRead )
		{
			xReturn = pdFAIL;
			break;
		}
	}

	return xReturn;
}
/*-----------------------------------------------------------*/

static void prvCreateFileInfoString( char *pcBuffer, REDDIRENT *pxDirent )
{
const char *pcFile = "file", *pcDirectory = "directory";
const char *pcAttrib;

	/* Point pcAttrib to a string that describes the file. */
	if( RED_S_ISDIR(pxDirent->d_stat.st_mode) )
	{
		pcAttrib = pcDirectory;
	}
	else
	{
		pcAttrib = pcFile;
	}

	/* Create a string that includes the file name, the file size and the
	attributes string. */
	sprintf( pcBuffer, "%s [%s] [size=%lld]", pxDirent->d_name, pcAttrib, pxDirent->d_stat.st_size );
}