2004-08-16 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / subsys / system / cmd / prompt.c
1 /*
2 * PROMPT.C - prompt handling.
3 *
4 *
5 * History:
6 *
7 * 14/01/95 (Tim Normal)
8 * started.
9 *
10 * 08/08/95 (Matt Rains)
11 * i have cleaned up the source code. changes now bring this source
12 * into guidelines for recommended programming practice.
13 *
14 * 01/06/96 (Tim Norman)
15 * added day of the week printing (oops, forgot about that!)
16 *
17 * 08/07/96 (Steffan Kaiser)
18 * small changes for speed
19 *
20 * 20-Jul-1998 (John P Price <linux-guru@gcfl.net>)
21 * removed redundant day strings. Use ones defined in date.c.
22 *
23 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
24 * added config.h include
25 *
26 * 28-Jul-1998 (John P Price <linux-guru@gcfl.net>)
27 * moved cmd_prompt from internal.c to here
28 *
29 * 09-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
30 * Added help text ("/?").
31 *
32 * 14-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
33 * Added "$+" option.
34 *
35 * 09-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
36 * Added "$A", "$C" and "$F" option.
37 * Added locale support.
38 * Fixed "$V" option.
39 *
40 * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
41 * Unicode and redirection safe!
42 *
43 * 24-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
44 * Fixed Win32 environment handling.
45 */
46
47 #include "config.h"
48
49 #include "precomp.h"
50 #include <tchar.h>
51 #include <string.h>
52 #include <ctype.h>
53
54
55 /*
56 * print the command-line prompt
57 *
58 */
59 VOID PrintPrompt(VOID)
60 {
61 static TCHAR default_pr[] = _T("$P$G");
62 TCHAR szPrompt[256];
63 LPTSTR pr;
64
65 if (GetEnvironmentVariable (_T("PROMPT"), szPrompt, 256))
66 pr = szPrompt;
67 else
68 pr = default_pr;
69
70 while (*pr)
71 {
72 if (*pr != _T('$'))
73 {
74 ConOutChar (*pr);
75 }
76 else
77 {
78 pr++;
79
80 switch (_totupper (*pr))
81 {
82 case _T('A'):
83 ConOutChar (_T('&'));
84 break;
85
86 case _T('B'):
87 ConOutChar (_T('|'));
88 break;
89
90 case _T('C'):
91 ConOutChar (_T('('));
92 break;
93
94 case _T('D'):
95 PrintDate ();
96 break;
97
98 case _T('E'):
99 ConOutChar (_T('\x1B'));
100 break;
101
102 case _T('F'):
103 ConOutChar (_T(')'));
104 break;
105
106 case _T('G'):
107 ConOutChar (_T('>'));
108 break;
109
110 case _T('H'):
111 ConOutChar (_T('\x08'));
112 break;
113
114 case _T('L'):
115 ConOutChar (_T('<'));
116 break;
117
118 case _T('N'):
119 {
120 TCHAR szPath[MAX_PATH];
121 GetCurrentDirectory (MAX_PATH, szPath);
122 ConOutChar (szPath[0]);
123 }
124 break;
125
126 case _T('P'):
127 {
128 TCHAR szPath[MAX_PATH];
129 GetCurrentDirectory (MAX_PATH, szPath);
130 ConOutPrintf (_T("%s"), szPath);
131 }
132 break;
133
134 case _T('Q'):
135 ConOutChar (_T('='));
136 break;
137
138 case _T('T'):
139 PrintTime ();
140 break;
141
142 case _T('V'):
143 switch (osvi.dwPlatformId)
144 {
145 case VER_PLATFORM_WIN32_WINDOWS:
146 if (osvi.dwMajorVersion == 4 &&
147 osvi.dwMinorVersion == 1)
148 ConOutPrintf (_T("Windows 98"));
149 else
150 ConOutPrintf (_T("Windows 95"));
151 break;
152
153 case VER_PLATFORM_WIN32_NT:
154 ConOutPrintf (_T("Windows NT Version %lu.%lu"),
155 osvi.dwMajorVersion, osvi.dwMinorVersion);
156 break;
157 }
158 break;
159
160 case _T('_'):
161 ConOutChar (_T('\n'));
162 break;
163
164 case '$':
165 ConOutChar (_T('$'));
166 break;
167
168 #ifdef FEATURE_DIRECTORY_STACK
169 case '+':
170 {
171 INT i;
172 for (i = 0; i < GetDirectoryStackDepth (); i++)
173 ConOutChar (_T('+'));
174 }
175 break;
176 #endif
177 }
178 }
179 pr++;
180 }
181 }
182
183
184 #ifdef INCLUDE_CMD_PROMPT
185
186 INT cmd_prompt (LPTSTR cmd, LPTSTR param)
187 {
188 if (!_tcsncmp (param, _T("/?"), 2))
189 {
190 ConOutPuts (_T("Changes the command prompt.\n\n"
191 "PROMPT [text]\n\n"
192 " text Specifies a new command prompt.\n\n"
193 "Prompt can be made up of normal characters and the following special codes:\n\n"
194 " $A & (Ampersand)\n"
195 " $B | (pipe)\n"
196 " $C ( (Left parenthesis)\n"
197 " $D Current date\n"
198 " $E Escape code (ASCII code 27)\n"
199 " $F ) (Right parenthesis)\n"
200 " $G > (greater-than sign)\n"
201 " $H Backspace (erases previous character)\n"
202 " $L < (less-than sign)\n"
203 " $N Current drive\n"
204 " $P Current drive and path\n"
205 " $Q = (equal sign)\n"
206 " $T Current time\n"
207 " $V OS version number\n"
208 " $_ Carriage return and linefeed\n"
209 " $$ $ (dollar sign)"));
210 #ifdef FEATURE_DIRECTORY_STACK
211 ConOutPuts (_T(" $+ Displays the current depth of the directory stack"));
212 #endif
213 ConOutPuts (_T("\nType PROMPT without parameters to reset the prompt to the default setting."));
214 return 0;
215 }
216
217 /* set PROMPT environment variable */
218 if (!SetEnvironmentVariable (_T("PROMPT"), param))
219 return 1;
220
221 return 0;
222 }
223 #endif