[CMD] Code formatting; don't hardcode the string buffer sizes in function calls.
[reactos.git] / base / shell / 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)
30 * Added help text ("/?").
31 *
32 * 14-Dec-1998 (Eric Kohl)
33 * Added "$+" option.
34 *
35 * 09-Jan-1999 (Eric Kohl)
36 * Added "$A", "$C" and "$F" option.
37 * Added locale support.
38 * Fixed "$V" option.
39 *
40 * 20-Jan-1999 (Eric Kohl)
41 * Unicode and redirection safe!
42 *
43 * 24-Jan-1999 (Eric Kohl)
44 * Fixed Win32 environment handling.
45 *
46 * 30-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
47 * Remove all hardcoded strings in En.rc
48 */
49 #include "precomp.h"
50
51 /* The default prompt */
52 static TCHAR DefaultPrompt[] = _T("$P$G");
53
54 /*
55 * Initialize prompt support.
56 */
57 VOID InitPrompt(VOID)
58 {
59 TCHAR Buffer[2];
60
61 /*
62 * Set the PROMPT environment variable if it doesn't exist already.
63 * You can change the PROMPT environment variable before cmd starts.
64 */
65 if (GetEnvironmentVariable(_T("PROMPT"), Buffer, _countof(Buffer)) == 0)
66 SetEnvironmentVariable(_T("PROMPT"), DefaultPrompt);
67 }
68
69 /*
70 * Print an information line on top of the screen.
71 */
72 VOID PrintInfoLine(VOID)
73 {
74 #define FOREGROUND_WHITE (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY)
75
76 HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
77 CONSOLE_SCREEN_BUFFER_INFO csbi;
78 COORD coPos;
79 DWORD dwWritten;
80
81 PTSTR pszInfoLine = NULL;
82 INT iInfoLineLen;
83
84 /* Return directly if the output handle is not a console handle */
85 if (!GetConsoleScreenBufferInfo(hOutput, &csbi))
86 return;
87
88 iInfoLineLen = LoadString(CMD_ModuleHandle, STRING_CMD_INFOLINE, (PTSTR)&pszInfoLine, 0);
89 if (!pszInfoLine || iInfoLineLen == 0)
90 return;
91
92 /* Display the localized information line */
93 coPos.X = 0;
94 coPos.Y = 0;
95 FillConsoleOutputAttribute(hOutput, BACKGROUND_BLUE | FOREGROUND_WHITE,
96 csbi.dwSize.X,
97 coPos, &dwWritten);
98 FillConsoleOutputCharacter(hOutput, _T(' '),
99 csbi.dwSize.X,
100 coPos, &dwWritten);
101
102 WriteConsoleOutputCharacter(hOutput, pszInfoLine, iInfoLineLen,
103 coPos, &dwWritten);
104 }
105
106 /*
107 * Print the command-line prompt.
108 */
109 VOID PrintPrompt(VOID)
110 {
111 LPTSTR pr;
112 TCHAR szPrompt[256];
113 TCHAR szPath[MAX_PATH];
114
115 if (GetEnvironmentVariable(_T("PROMPT"), szPrompt, _countof(szPrompt)))
116 pr = szPrompt;
117 else
118 pr = DefaultPrompt;
119
120 while (*pr)
121 {
122 if (*pr != _T('$'))
123 {
124 ConOutChar(*pr);
125 }
126 else
127 {
128 pr++;
129
130 switch (_totupper(*pr))
131 {
132 case _T('A'):
133 ConOutChar(_T('&'));
134 break;
135
136 case _T('B'):
137 ConOutChar(_T('|'));
138 break;
139
140 case _T('C'):
141 ConOutChar(_T('('));
142 break;
143
144 case _T('D'):
145 ConOutPrintf(_T("%s"), GetDateString());
146 break;
147
148 case _T('E'):
149 ConOutChar(_T('\x1B'));
150 break;
151
152 case _T('F'):
153 ConOutChar(_T(')'));
154 break;
155
156 case _T('G'):
157 ConOutChar(_T('>'));
158 break;
159
160 case _T('H'):
161 ConOutChar(_T('\x08'));
162 ConOutChar(_T(' '));
163 ConOutChar(_T('\x08'));
164 break;
165
166 case _T('I'):
167 PrintInfoLine();
168 break;
169
170 case _T('L'):
171 ConOutChar(_T('<'));
172 break;
173
174 case _T('N'):
175 {
176 GetCurrentDirectory(_countof(szPath), szPath);
177 ConOutChar(szPath[0]);
178 break;
179 }
180
181 case _T('P'):
182 {
183 GetCurrentDirectory(_countof(szPath), szPath);
184 ConOutPrintf(_T("%s"), szPath);
185 break;
186 }
187
188 case _T('Q'):
189 ConOutChar(_T('='));
190 break;
191
192 case _T('S'):
193 ConOutChar(_T(' '));
194 break;
195
196 case _T('T'):
197 ConOutPrintf(_T("%s"), GetTimeString());
198 break;
199
200 case _T('V'):
201 PrintOSVersion();
202 break;
203
204 case _T('_'):
205 ConOutChar(_T('\n'));
206 break;
207
208 case '$':
209 ConOutChar(_T('$'));
210 break;
211
212 #ifdef FEATURE_DIRECTORY_STACK
213 case '+':
214 {
215 INT i;
216 for (i = 0; i < GetDirectoryStackDepth(); i++)
217 ConOutChar(_T('+'));
218 break;
219 }
220 #endif
221 }
222 }
223 pr++;
224 }
225 }
226
227
228 #ifdef INCLUDE_CMD_PROMPT
229
230 INT cmd_prompt(LPTSTR param)
231 {
232 if (!_tcsncmp(param, _T("/?"), 2))
233 {
234 ConOutResPaging(TRUE, STRING_PROMPT_HELP1);
235
236 #ifdef FEATURE_DIRECTORY_STACK
237 ConOutResPaging(FALSE, STRING_PROMPT_HELP2);
238 #endif
239 ConOutResPaging(FALSE, STRING_PROMPT_HELP3);
240 return 0;
241 }
242
243 /*
244 * Set the PROMPT environment variable. If 'param' is NULL or is
245 * an empty string (the user entered "prompt" only), then remove
246 * the environment variable and therefore use the default prompt.
247 * Otherwise, use the new prompt.
248 */
249 if (!SetEnvironmentVariable(_T("PROMPT"),
250 (param && param[0] != _T('\0') ? param : NULL)))
251 {
252 return 1;
253 }
254
255 return 0;
256 }
257 #endif
258
259 /* EOF */