Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

470 lines
11 KiB

  1. /*++
  2. Copyright (c) 1991-1992 Microsoft Corporation
  3. Module Name:
  4. LUI.C
  5. Abstract:
  6. Contains support functions
  7. Author:
  8. Dan Hinsley (danhi) 06-Jun-1991
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. 18-Apr-1991 danhi
  13. 32 bit NT version
  14. 06-Jun-1991 Danhi
  15. Sweep to conform to NT coding style
  16. 23-Oct-1991 W-ShankN
  17. Added Unicode mapping
  18. 01-Oct-1992 JohnRo
  19. RAID 3556: Added NetpSystemTimeToGmtTime() for DosPrint APIs.
  20. 10-Feb-1993 YiHsinS
  21. Moved LUI_GetMsgIns to netlib\luiint.c
  22. --*/
  23. //
  24. // INCLUDES
  25. //
  26. #include <nt.h> // these 3 includes are for RTL
  27. #include <ntrtl.h> // these files are picked up to
  28. #include <nturtl.h> // allow <windows.h> to compile. since we've
  29. // already included NT, and <winnt.h> will not
  30. // be picked up, and <winbase.h> needs these defs.
  31. #include <windows.h> // IN, LPTSTR, etc.
  32. #include <string.h>
  33. #include <lmcons.h>
  34. #include <stdio.h>
  35. #include <process.h>
  36. #include "netlibnt.h"
  37. #include <lui.h>
  38. #include "icanon.h"
  39. #include <lmerr.h>
  40. #include <conio.h>
  41. #include <io.h>
  42. #include <tchar.h>
  43. #include <msystem.h>
  44. #include "apperr.h"
  45. #include "apperr2.h"
  46. #include "netascii.h"
  47. #include "netcmds.h"
  48. //
  49. // Local definitions and macros/function declarations
  50. //
  51. #define LUI_PMODE_DEF 0x00000000
  52. #define LUI_PMODE_EXIT 0x00000002
  53. #define LUI_PMODE_NODEF 0x00000004
  54. #define LUI_PMODE_ERREXT 0x00000008
  55. /* fatal error, just exit */
  56. #define LUIM_ErrMsgExit(E) LUI_PrintMsgIns(NULL, 0, E, NULL, \
  57. LUI_PMODE_ERREXT | LUI_PMODE_DEF | \
  58. LUI_PMODE_EXIT, g_hStdErr)
  59. DWORD
  60. LUI_PrintMsgIns(
  61. LPTSTR *istrings,
  62. DWORD nstrings,
  63. DWORD msgno,
  64. unsigned int *msglen,
  65. DWORD mode,
  66. HANDLE handle
  67. );
  68. /*
  69. * LUI_CanonPassword
  70. *
  71. * This function ensures that the password in the passed buffer is a
  72. * syntactically valid password.
  73. *
  74. * This USED to upper case passwords. No longer so in NT.
  75. *
  76. *
  77. * ENTRY
  78. * buf buffer containing password to be canonicalized
  79. *
  80. * EXIT
  81. * buf canonicalized password, if valid
  82. *
  83. * RETURNS
  84. * 0 password is valid
  85. * otherwise password is invalid
  86. *
  87. */
  88. USHORT LUI_CanonPassword(TCHAR * szPassword)
  89. {
  90. /* check it for validity */
  91. if (I_NetNameValidate(NULL, szPassword, NAMETYPE_PASSWORD, 0L ))
  92. {
  93. return APE_UtilInvalidPass;
  94. }
  95. return 0;
  96. }
  97. /*
  98. * Name: LUI_GetMsg
  99. * This routine is similar to LUI_GetMsgIns,
  100. * except it takes does not accept insert strings &
  101. * takes less arguments.
  102. * Args: msgbuf : buffer to hold message retrieved
  103. * bufsize : size of buffer
  104. * msgno : message number
  105. * Returns: zero if ok, the DOSGETMESSAGE error code otherwise
  106. * Globals: (none)
  107. * Statics: (none)
  108. */
  109. DWORD
  110. LUI_GetMsg(
  111. PTCHAR msgbuf,
  112. USHORT bufsize,
  113. DWORD msgno
  114. )
  115. {
  116. return LUI_GetMsgInsW(NULL, 0, msgbuf, bufsize, msgno, NULL);
  117. }
  118. #define SINGLE_HORIZONTAL '\x02d'
  119. #define SCREEN_WIDTH 79
  120. USHORT
  121. LUI_PrintLine(
  122. VOID
  123. )
  124. {
  125. TCHAR string[SCREEN_WIDTH+1];
  126. USHORT i;
  127. for (i = 0; i < SCREEN_WIDTH; i++) {
  128. string[i] = SINGLE_HORIZONTAL;
  129. }
  130. string[SCREEN_WIDTH] = NULLC;
  131. WriteToCon(TEXT("%s\r\n"), &string);
  132. return(0);
  133. }
  134. /***
  135. * Y o r N
  136. *
  137. * Gets an answer to a Y/N question
  138. *
  139. * Entry: promptMsgNum -- The number of the message to prompt with
  140. * def -- default (TRUE if set, FALSE otherwise)
  141. */
  142. DWORD
  143. LUI_YorN(
  144. USHORT promptMsgNum,
  145. USHORT def
  146. )
  147. {
  148. return LUI_YorNIns(NULL, 0, promptMsgNum, def);
  149. }
  150. /***
  151. * Y o r N Insert
  152. *
  153. * Gets an answer to a Y/N question containing insertions.
  154. *
  155. * !!!!!!!!
  156. * NOTE: istrings[nstrings] will be used to store "Y" or "N",
  157. * depending on default value supplied. Thus this function
  158. * handles one fewer entry in istrings than other LUI Ins
  159. * functions do. Beware!
  160. * !!!!!!!!
  161. *
  162. * Entry: istrings -- Table of insertion strings
  163. * nstrings -- Number of valid insertion strings
  164. * promptMsgNum -- The number of the message to prompt with
  165. * def -- default (TRUE if set, FALSE otherwise)
  166. *
  167. * Returns: TRUE, FALSE, or -1 in case of LUI_PrintMsgIns error.
  168. */
  169. #define PRINT_MODE (LUI_PMODE_ERREXT)
  170. #define STRING_LEN APE2_GEN_MAX_MSG_LEN
  171. #define LUI_LOOP_LIMIT 5
  172. DWORD
  173. LUI_YorNIns(
  174. PTCHAR * istrings,
  175. USHORT nstrings,
  176. USHORT promptMsgNum,
  177. USHORT def
  178. )
  179. {
  180. USHORT count; /* count of times we ask */
  181. DWORD err; /* LUI API return values */
  182. unsigned int dummy; /* length of msg */
  183. /* 10 because max # of insert strings to DosGetMessage is 9, and
  184. we'll leave caller room to mess up and get the error back
  185. from LUI_PrintMsgIns() */
  186. LPTSTR IStrings[10]; /* Insertion strings for LUI */
  187. TCHAR defaultYes[STRING_LEN]; /* (Y/N) [Y] string */
  188. TCHAR defaultNo[STRING_LEN]; /* (Y/N) [N] string */
  189. TCHAR NLSYesChar[STRING_LEN];
  190. TCHAR NLSNoChar[STRING_LEN];
  191. TCHAR strBuf[STRING_LEN]; /* holds input string */
  192. DWORD len; /* length of string input */
  193. TCHAR termChar; /* terminating char */
  194. /* copy istrings to IStrings so we'll have room for Y or N */
  195. for (count=0; count < nstrings; count++)
  196. IStrings[count] = istrings[count];
  197. /* retrieve text we need from message file, bail out if probs */
  198. if (err = LUI_GetMsg(defaultYes, DIMENSION(defaultYes),
  199. APE2_GEN_DEFAULT_YES))
  200. {
  201. LUIM_ErrMsgExit(err);
  202. }
  203. if (err = LUI_GetMsg(defaultNo, DIMENSION(defaultNo),
  204. APE2_GEN_DEFAULT_NO))
  205. LUIM_ErrMsgExit(err);
  206. if (err = LUI_GetMsg(NLSYesChar, DIMENSION(NLSYesChar),
  207. APE2_GEN_NLS_YES_CHAR))
  208. LUIM_ErrMsgExit(err);
  209. if (err = LUI_GetMsg(NLSNoChar, DIMENSION(NLSNoChar),
  210. APE2_GEN_NLS_NO_CHAR))
  211. LUIM_ErrMsgExit(err);
  212. if (def)
  213. IStrings[nstrings] = defaultYes;
  214. else
  215. IStrings[nstrings] = defaultNo;
  216. nstrings++;
  217. for (count = 0; count < LUI_LOOP_LIMIT; count++)
  218. {
  219. if (count)
  220. {
  221. LUI_PrintMsgIns(NULL, 0, APE_UtilInvalidResponse, NULL, PRINT_MODE, g_hStdOut);
  222. }
  223. err = LUI_PrintMsgIns(IStrings, nstrings, promptMsgNum,
  224. &dummy, PRINT_MODE, g_hStdOut);
  225. if ((LONG) err < 0)
  226. return(err);
  227. if (GetString(strBuf, DIMENSION(strBuf), &len, &termChar))
  228. /* overwrote buffer, start again */
  229. continue;
  230. if ((len == 0) && (termChar == (TCHAR)EOF))
  231. {
  232. /* end of file reached */
  233. PrintNL();
  234. LUIM_ErrMsgExit(APE_NoGoodResponse);
  235. }
  236. if (len == 0) /* user hit RETURN */
  237. return def;
  238. else if (!_tcsnicmp(NLSYesChar, strBuf, _tcslen(NLSYesChar)))
  239. return TRUE;
  240. else if (!_tcsnicmp(NLSNoChar, strBuf, _tcslen(NLSNoChar)))
  241. return FALSE;
  242. /* default is handled at top of loop. */
  243. };
  244. LUIM_ErrMsgExit(APE_NoGoodResponse);
  245. return err; // Keep compiler happy.
  246. }
  247. /*
  248. * LUI_CanonMessagename
  249. *
  250. * This function uppercases the contents of the buffer, then checks to
  251. * make sure that it is a syntactically valid messenging name.
  252. *
  253. *
  254. * ENTRY
  255. * buf buffer containing name to be canonicalized
  256. *
  257. * EXIT
  258. * buf canonicalized name, if valid
  259. *
  260. * RETURNS
  261. * 0 name is valid
  262. * otherwise name is invalid
  263. *
  264. */
  265. USHORT
  266. LUI_CanonMessagename(
  267. PTCHAR buf
  268. )
  269. {
  270. /* check it for validity */
  271. if (I_NetNameValidate(NULL, buf, NAMETYPE_MESSAGE, LM2X_COMPATIBLE))
  272. {
  273. return NERR_InvalidComputer;
  274. }
  275. _tcsupr(buf);
  276. return 0;
  277. }
  278. /*
  279. * LUI_CanonMessageDest
  280. *
  281. * This function uppercases the contents of the buffer, then checks to
  282. * make sure that it is a syntactically valid messenging destination.
  283. *
  284. *
  285. * ENTRY
  286. * buf buffer containing name to be canonicalized
  287. *
  288. * EXIT
  289. * buf canonicalized name, if valid
  290. *
  291. * RETURNS
  292. * 0 name is valid
  293. * otherwise name is invalid
  294. *
  295. */
  296. USHORT
  297. LUI_CanonMessageDest(
  298. PTCHAR buf
  299. )
  300. {
  301. /* check it for validity */
  302. if (I_NetNameValidate(NULL, buf, NAMETYPE_MESSAGEDEST, LM2X_COMPATIBLE))
  303. {
  304. return NERR_InvalidComputer;
  305. }
  306. _tcsupr(buf);
  307. return(0);
  308. }
  309. /***
  310. * LUI_CanonForNetBios
  311. * Canonicalizes a name to a NETBIOS canonical form.
  312. *
  313. * Args:
  314. * Destination - Will receive the canonicalized name (Unicode).
  315. * cchDestination - the number of chars Destination can hold
  316. * pszOem - Contains the original name in OEM. Will have
  317. * the canonicalized form put back here.
  318. * Returns:
  319. * 0 if success
  320. * error code otherwise
  321. */
  322. USHORT LUI_CanonForNetBios( WCHAR * Destination, INT cchDestination,
  323. TCHAR * pszOem )
  324. {
  325. _tcscpy(Destination, pszOem);
  326. return NERR_Success;
  327. }
  328. /*
  329. * Name: LUI_PrintMsgIns
  330. * This routine is very similar to LUI_GetmsgIns,
  331. * except it prints the message obtained instead of
  332. * storing it in a buffer.
  333. * Args: istrings : pointer to table of insert strings
  334. * nstrings : number of insert strings
  335. * msgno : message number
  336. * msglen : pointer to variable that will receive message length
  337. * mode : how the message is to be printed.
  338. * handle : file handle to which output goes
  339. * Returns: zero if ok, the DOSGETMESSAGE error code otherwise
  340. * Globals: (none)
  341. * Statics: (none)
  342. * Remarks: (none)
  343. * Updates: (none)
  344. */
  345. DWORD
  346. LUI_PrintMsgIns(
  347. LPTSTR *istrings,
  348. DWORD nstrings,
  349. DWORD msgno,
  350. unsigned int *msglen,
  351. DWORD mode,
  352. HANDLE handle
  353. )
  354. {
  355. TCHAR msgbuf[MSG_BUFF_SIZE];
  356. DWORD result;
  357. unsigned int tmplen;
  358. SHORT exit_on_error, exit_on_completion, no_default_err_msg;
  359. /* check if we have illegal combination */
  360. if ((mode & LUI_PMODE_NODEF)
  361. &&
  362. (mode & (LUI_PMODE_EXIT | LUI_PMODE_ERREXT)))
  363. {
  364. return ERROR_INVALID_PARAMETER;
  365. }
  366. /* setup various flags */
  367. exit_on_error = (SHORT)(mode & LUI_PMODE_ERREXT);
  368. exit_on_completion = (SHORT)(mode & LUI_PMODE_EXIT);
  369. no_default_err_msg = (SHORT)(mode & LUI_PMODE_NODEF);
  370. /* get message and write it */
  371. result = LUI_GetMsgInsW(istrings, nstrings, msgbuf,
  372. DIMENSION(msgbuf),
  373. msgno, (unsigned *) &tmplen);
  374. if (result == 0 || !no_default_err_msg)
  375. {
  376. _tcsncpy(ConBuf, msgbuf, tmplen);
  377. ConBuf[tmplen] = NULLC;
  378. DosPutMessageW(handle, ConBuf, FALSE);
  379. }
  380. if (msglen != NULL) *msglen = tmplen ;
  381. /* different ways of exiting */
  382. if (exit_on_error && result != 0)
  383. {
  384. exit(result) ;
  385. }
  386. if (exit_on_completion)
  387. {
  388. exit(-1) ;
  389. }
  390. return result;
  391. }