[FSUTIL]
[reactos.git] / reactos / base / applications / cmdutils / fsutil / common.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS FS utility tool
4 * FILE: base/applications/cmdutils/common.c
5 * PURPOSE: FSutil common functions
6 * PROGRAMMERS: Pierre Schweitzer <pierre@reactos.org>
7 */
8
9 #include "fsutil.h"
10
11 int FindHandler(int argc,
12 const TCHAR *argv[],
13 HandlerItem * HandlersList,
14 int HandlerListCount,
15 void (*UsageHelper)(const TCHAR *))
16 {
17 int i;
18 int ret;
19 const TCHAR * Command;
20
21 ret = 1;
22 Command = NULL;
23 i = HandlerListCount;
24
25 /* If we have a command, does it match a known one? */
26 if (argc > 1)
27 {
28 /* Browse all the known commands finding the right one */
29 Command = argv[1];
30 for (i = 0; i < HandlerListCount; ++i)
31 {
32 if (_tcsicmp(Command, HandlersList[i].Command) == 0)
33 {
34 ret = HandlersList[i].Handler(argc - 1, &argv[1]);
35 break;
36 }
37 }
38 }
39
40 /* We failed finding someone to handle the caller's needs, print out */
41 if (i == HandlerListCount)
42 {
43 UsageHelper(Command);
44 }
45
46 return ret;
47 }
48
49 void PrintDefaultUsage(const TCHAR * Command,
50 const TCHAR * SubCommand,
51 HandlerItem * HandlersList,
52 int HandlerListCount)
53 {
54 int i;
55
56 /* If we were given a command, print it's not supported */
57 if (SubCommand != NULL)
58 {
59 _ftprintf(stderr, _T("Unhandled%scommand: %s\n"), Command, SubCommand);
60 }
61
62 /* And dump any available command */
63 _ftprintf(stderr, _T("---- Handled%scommands ----\n\n"), Command);
64 for (i = 0; i < HandlerListCount; ++i)
65 {
66 _ftprintf(stderr, _T("%s\t%s\n"), HandlersList[i].Command, HandlersList[i].Desc);
67 }
68 }