def1cbf57f228064d99b4814decf25b583a027cc
[reactos.git] / reactos / apps / utils / cmd / goto.c
1 /*
2 * GOTO.C - goto internal batch command.
3 *
4 * History:
5 *
6 * 16 Jul 1998 (Hans B Pufal)
7 * started.
8 *
9 * 16 Jul 1998 (John P Price)
10 * Seperated commands into individual files.
11 *
12 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
13 * added config.h include
14 *
15 * 28 Jul 1998 (Hans B Pufal) [HBP_003]
16 * Terminate label on first space character, use only first 8 chars of
17 * label string
18 *
19 * 24-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
20 * Unicode and redirection safe!
21 *
22 * 27-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
23 * Added help text ("/?").
24 */
25
26 #define WIN32_LEAN_AND_MEAN
27
28 #include "config.h"
29
30 #include <windows.h>
31 #include <tchar.h>
32 #include <string.h>
33
34 #include "cmd.h"
35 #include "batch.h"
36
37
38 /*
39 * Perform GOTO command.
40 *
41 * Only valid if batch file current.
42 *
43 */
44
45 INT cmd_goto (LPTSTR cmd, LPTSTR param)
46 {
47 LPTSTR tmp;
48 LONG lNewPosHigh;
49
50 #ifdef _DEBUG
51 DebugPrintf ("cmd_goto (\'%s\', \'%s\'\n", cmd, param);
52 #endif
53
54 if (!_tcsncmp (param, _T("/?"), 2))
55 {
56 ConOutPuts (_T("Directs CMD to a labeled line in a batch script.\n"
57 "\n"
58 "GOTO label\n"
59 "\n"
60 " label Specifies a text string used in a batch script as a label.\n"
61 "\n"
62 "You type a label on a line by itself, beginning with a colon."));
63 return 0;
64 }
65
66 /* if not in batch -- error!! */
67 if (bc == NULL)
68 {
69 return 1;
70 }
71
72 if (*param == _T('\0'))
73 {
74 ExitBatch (_T("No label specified for GOTO\n"));
75 return 1;
76 }
77
78 /* terminate label at first space char */
79 tmp = param;
80 while (*tmp && !_istspace (*tmp))
81 tmp++;
82 *tmp = _T('\0');
83
84 /* set file pointer to the beginning of the batch file */
85 lNewPosHigh = 0;
86 SetFilePointer (bc->hBatchFile, 0, &lNewPosHigh, FILE_BEGIN);
87
88 while (FileGetString (bc->hBatchFile, textline, sizeof(textline)))
89 {
90 /* Strip out any trailing spaces or control chars */
91 tmp = textline + _tcslen (textline) - 1;
92 while (_istcntrl (*tmp) || _istspace (*tmp))
93 tmp--;
94 *(tmp + 1) = _T('\0');
95
96 /* Then leading spaces... */
97 tmp = textline;
98 while (_istspace (*tmp))
99 tmp++;
100
101 /* use only 1st 8 chars of label */
102 if ((*tmp == _T(':')) && (_tcsncmp (++tmp, param, 8) == 0))
103 return 0;
104 }
105
106 ConErrPrintf (_T("Label '%s' not found\n"), param);
107 ExitBatch (NULL);
108
109 return 1;
110 }