c80a9e4047a753c440f51172ddc82964bff07112
[reactos.git] / subsys / system / 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 #include "config.h"
27
28 #include <windows.h>
29 #include <tchar.h>
30 #include <string.h>
31 #include <ctype.h>
32
33 #include "cmd.h"
34 #include "batch.h"
35
36
37 /*
38  * Perform GOTO command.
39  *
40  * Only valid if batch file current.
41  *
42  */
43
44 INT cmd_goto (LPTSTR cmd, LPTSTR param)
45 {
46         LPTSTR tmp;
47         LONG   lNewPosHigh;
48
49 #ifdef _DEBUG
50         DebugPrintf ("cmd_goto (\'%s\', \'%s\'\n", cmd, param);
51 #endif
52
53         if (!_tcsncmp (param, _T("/?"), 2))
54         {
55                 ConOutPuts (_T("Directs CMD to a labeled line in a batch script.\n"
56                                            "\n"
57                                            "GOTO label\n"
58                                            "\n"
59                                            "  label  Specifies a text string used in a batch script as a label.\n"
60                                            "\n"
61                                            "You type a label on a line by itself, beginning with a colon."));
62                 return 0;
63         }
64
65         /* if not in batch -- error!! */
66         if (bc == NULL)
67         {
68                 return 1;
69         }
70
71         if (*param == _T('\0'))
72         {
73                 ExitBatch (_T("No label specified for GOTO\n"));
74                 return 1;
75         }
76
77         /* terminate label at first space char */
78         tmp = param;
79         while (*tmp && !_istspace (*tmp))
80                 tmp++;
81         *tmp = _T('\0');
82
83         /* set file pointer to the beginning of the batch file */
84         lNewPosHigh = 0;
85         SetFilePointer (bc->hBatchFile, 0, &lNewPosHigh, FILE_BEGIN);
86
87         while (FileGetString (bc->hBatchFile, textline, sizeof(textline)))
88         {
89                 /* Strip out any trailing spaces or control chars */
90                 tmp = textline + _tcslen (textline) - 1;
91                 while (_istcntrl (*tmp) || _istspace (*tmp))
92                         tmp--;
93                 *(tmp + 1) = _T('\0');
94
95                 /* Then leading spaces... */
96                 tmp = textline;
97                 while (_istspace (*tmp))
98                         tmp++;
99
100                 /* use only 1st 8 chars of label */
101                 if ((*tmp == _T(':')) && (_tcsncmp (++tmp, param, 8) == 0))
102                         return 0;
103         }
104
105         ConErrPrintf (_T("Label '%s' not found\n"), param);
106         ExitBatch (NULL);
107
108         return 1;
109 }