Leaked source code of windows server 2003
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.

526 lines
17 KiB

  1. //+------------------------------------------------------------------
  2. //
  3. // File: cmdlinew.hxx
  4. //
  5. // Contents: class definitions for command line parsing
  6. //
  7. // Classes: CBaseCmdline
  8. // CBaseCmdlineObj
  9. // CIntCmdlineObj
  10. // CUlongCmdlineObj
  11. // CBoolCmdlineObj
  12. // CStrLengthCmdlineObj
  13. // CCmdlineArg
  14. // CCmdline
  15. //
  16. // History: 23 Dec 91 Lizch Created.
  17. // 28 Jul 92 Davey Added changes to support a
  18. // formatted usage display.
  19. // 28 Aug 92 GeordiS Changed nlsNULL to nlsNULLSTR
  20. // 09 Sep 92 Lizch Removed definition of SUCCESS
  21. // 10 Sep 92 DonCl removed extern "C" from around
  22. // windows.h and commnot.h
  23. // 21 Sep 92 Davey Added ifdef of ANYSTRICT around
  24. // debug macros.
  25. // 13 Oct 93 DeanE Converted to WCHAR
  26. //
  27. //-------------------------------------------------------------------
  28. #ifndef __CMDLINEW_HXX__
  29. #define __CMDLINEW_HXX__
  30. #include <windows.h>
  31. #include <nchar.h>
  32. #ifdef WIN16
  33. #include <ptypes16.h>
  34. #include <types16.h>
  35. #endif
  36. #include <wstrlist.hxx>
  37. #define MAX_ERROR_STRING 100
  38. // Cmdline Success/Error values
  39. #define CMDLINE_NO_ERROR 0
  40. #define CMDLINE_ERROR_BASE 10000
  41. #define CMDLINE_ERROR_OUT_OF_MEMORY CMDLINE_ERROR_BASE+2
  42. #define CMDLINE_ERROR_ARGUMENT_MISSING CMDLINE_ERROR_BASE+3
  43. #define CMDLINE_ERROR_INVALID_VALUE CMDLINE_ERROR_BASE+4
  44. #define CMDLINE_ERROR_TOO_BIG CMDLINE_ERROR_BASE+5
  45. #define CMDLINE_ERROR_UNRECOGNISED_ARG CMDLINE_ERROR_BASE+6
  46. #define CMDLINE_ERROR_DISPLAY_PARAMS CMDLINE_ERROR_BASE+7
  47. #define CMDLINE_ERROR_USAGE_FOUND CMDLINE_ERROR_BASE+8
  48. // Typedef for a void function pointer
  49. typedef void (__cdecl *PFVOID) (VOID);
  50. //+------------------------------------------------------------------
  51. //
  52. // Class: CBaseCmdline
  53. //
  54. // Purpose: implementation for base class for all command line
  55. // classes, both the individual command line object
  56. // classes and the overall parsing class
  57. //
  58. // It contains the print function pointer
  59. // the default print implementation and the constructor
  60. // error value.
  61. //
  62. // History: 05/17/92 Lizch Created
  63. // 08/11/92 Davey Added declaration of constructor
  64. //
  65. //-------------------------------------------------------------------
  66. class CBaseCmdline
  67. {
  68. public:
  69. CBaseCmdline();
  70. static void SetDisplayMethod(
  71. void (* pfnNewDisplayMethod)(LPCNSTR nszMessage));
  72. INT QueryError(void);
  73. static void (* _pfnDisplay)(LPCNSTR nszMessage);
  74. protected:
  75. void SetError(INT iLastError);
  76. static NCHAR _nszErrorBuf[MAX_ERROR_STRING+1];
  77. private:
  78. INT _iLastError;
  79. };
  80. //+------------------------------------------------------------------
  81. //
  82. // Class: CBaseCmdlineObj
  83. //
  84. // Purpose: Provides the basic structure for a command line argument
  85. //
  86. // History: 12/27/91 Lizch Created.
  87. // 07/31/92 Davey Added QueryCmdlineType, QueryLineArgType
  88. // Added linearg parameter to constructors.
  89. // Added Display... functions to support
  90. // usage output.
  91. // Removed DisplayCmdlineObjType()
  92. // 06/13/97 MariusB ResetValue added.
  93. //
  94. //-------------------------------------------------------------------
  95. class CBaseCmdlineObj : public CBaseCmdline
  96. {
  97. public:
  98. CBaseCmdlineObj(LPCNSTR nszSwitch,
  99. LPCNSTR nszUsage,
  100. LPCNSTR nszDefault,
  101. LPCNSTR nszLineArg = NULL);
  102. CBaseCmdlineObj (LPCNSTR nszSwitch,
  103. LPCNSTR nszUsage,
  104. BOOL fMustHave = FALSE,
  105. LPCNSTR nszLineArg = NULL);
  106. ~CBaseCmdlineObj ();
  107. virtual INT SetValue (LPCNSTR nszArg);
  108. virtual void ResetValue ();
  109. virtual INT SetValueToDefault(void);
  110. virtual void DisplayValue (void);
  111. LPCNSTR GetValue (void);
  112. BOOL IsFound (void);
  113. BOOL IsRequired (void);
  114. BOOL IsDefaultSpecified(void);
  115. LPCNSTR QuerySwitchString (void);
  116. void SetFoundFlag (BOOL fFound);
  117. INT DisplayUsageLine(USHORT *pusWidth,
  118. USHORT usDisplayWidth,
  119. USHORT usIndent);
  120. INT DisplayUsageDescr(USHORT usSwitchIndent,
  121. USHORT usDisplayWidth,
  122. USHORT usIndent);
  123. NCHAR SetSeparator(NCHAR nchSeparator);
  124. NCHAR SetEquater (NCHAR nchEquater);
  125. NCHAR GetSeparator(void);
  126. NCHAR GetEquater (void);
  127. BOOL SecondArg (void) { return(_fSecondArg); };
  128. protected:
  129. void DisplayNoValue(void);
  130. virtual INT DisplaySpecialUsage(USHORT usDisplayWidth,
  131. USHORT usIndent,
  132. USHORT *pusWidth);
  133. virtual LPCNSTR QueryCmdlineType(void) const;
  134. virtual LPCNSTR QueryLineArgType(void) const;
  135. INT DisplayStringByWords(LPCNSTR nszString,
  136. USHORT usDisplayWidth,
  137. USHORT usIndent,
  138. USHORT *pusWidth);
  139. INT CopyWord(LPCNSTR pnchWord,
  140. ULONG cchWord,
  141. LPNSTR *ppnszWord);
  142. INT DisplayWord(LPCNSTR nszWord,
  143. USHORT usDisplayWidth,
  144. USHORT usIndent,
  145. USHORT *pusWidth);
  146. void *_pValue;
  147. NCHAR _nchSeparator;
  148. NCHAR _nchEquater;
  149. LPNSTR _pnszUsageString;
  150. LPNSTR _pnszSwitch; // the switch on the command line.
  151. LPNSTR _pnszDefaultValue; // default value if none specified.
  152. LPNSTR _pnszLineArgType;
  153. BOOL _fDefaultSpecified; // TRUE if user specified a default
  154. BOOL _fMandatory;
  155. BOOL _fSecondArg; // TRUE if should parse for second arg.
  156. // mainly needed for boolean type.
  157. private:
  158. void Init(LPCNSTR nszSwitch,
  159. LPCNSTR nszUsage,
  160. BOOL fMustHave,
  161. LPCNSTR nszDefault,
  162. LPCNSTR nszLineArg);
  163. BOOL _fFoundSwitch;
  164. };
  165. //+------------------------------------------------------------------
  166. //
  167. // Class: CIntCmdlineObj
  168. //
  169. // Purpose: Provides the basic structure for an integer
  170. // command line argument
  171. //
  172. // Derivation: CBaseCmdlineObj
  173. //
  174. // History: 12/27/91 Lizch Created.
  175. // 07/31/92 Davey Added QueryCmdlineType, QueryLineArgType
  176. // Added linearg parameter to constructors.
  177. //
  178. //-------------------------------------------------------------------
  179. class CIntCmdlineObj : public CBaseCmdlineObj
  180. {
  181. public:
  182. CIntCmdlineObj(LPCNSTR nszSwitch,
  183. LPCNSTR nszUsage,
  184. BOOL fMustHave = FALSE,
  185. LPCNSTR nszLineArg = NULL) :
  186. CBaseCmdlineObj(nszSwitch, nszUsage, fMustHave, nszLineArg)
  187. {
  188. };
  189. CIntCmdlineObj(LPCNSTR nszSwitch,
  190. LPCNSTR nszUsage,
  191. LPCNSTR nszDefault,
  192. LPCNSTR nszLineArg = NULL) :
  193. CBaseCmdlineObj(nszSwitch, nszUsage, nszDefault, nszLineArg)
  194. {
  195. };
  196. ~CIntCmdlineObj(void);
  197. const INT * GetValue (void);
  198. virtual INT SetValue (LPCNSTR nszArg);
  199. virtual void DisplayValue(void);
  200. protected:
  201. virtual LPCNSTR QueryCmdlineType(void) const;
  202. virtual LPCNSTR QueryLineArgType(void) const;
  203. };
  204. //+------------------------------------------------------------------
  205. //
  206. // Class: CUlongCmdlineObj
  207. //
  208. // Purpose: Provides the basic structure for an unsigned long
  209. // command line argument
  210. //
  211. // Derivation: CBaseCmdlineObj
  212. //
  213. // History: 6/15/92 DeanE Created: Cut and paste from CIntCmdLine
  214. // 07/31/92 Davey Added QueryCmdlineType, QueryLineArgType
  215. // Added linearg parameter to constructors.
  216. //
  217. //-------------------------------------------------------------------
  218. class CUlongCmdlineObj : public CBaseCmdlineObj
  219. {
  220. public:
  221. CUlongCmdlineObj(LPCNSTR nszSwitch,
  222. LPCNSTR nszUsage,
  223. BOOL fMustHave = FALSE,
  224. LPCNSTR nszLineArg = NULL) :
  225. CBaseCmdlineObj(nszSwitch, nszUsage, fMustHave, nszLineArg)
  226. {
  227. };
  228. CUlongCmdlineObj(LPCNSTR nszSwitch,
  229. LPCNSTR nszUsage,
  230. LPCNSTR nszDefault,
  231. LPCNSTR nszLineArg = NULL) :
  232. CBaseCmdlineObj(nszSwitch, nszUsage, nszDefault, nszLineArg)
  233. {
  234. };
  235. ~CUlongCmdlineObj(void);
  236. const ULONG *GetValue (void);
  237. virtual INT SetValue (LPCNSTR nszArg);
  238. virtual void DisplayValue(void);
  239. protected:
  240. virtual LPCNSTR QueryCmdlineType(void) const;
  241. virtual LPCNSTR QueryLineArgType(void) const;
  242. };
  243. //+------------------------------------------------------------------
  244. //
  245. // Class: CBoolCmdlineObj
  246. //
  247. // Purpose: Provides the basic structure for a Boolean
  248. // command line argument
  249. //
  250. // Derivation: CBaseCmdlineObj
  251. //
  252. // History: 6/15/92 DeanE Created: Obtained from security project
  253. // 07/31/92 Davey Added QueryCmdlineType, QueryLineArgType
  254. // Modified constructor.
  255. //
  256. //-------------------------------------------------------------------
  257. class CBoolCmdlineObj : public CBaseCmdlineObj
  258. {
  259. public :
  260. CBoolCmdlineObj(LPCNSTR nszSwitch,
  261. LPCNSTR nszUsage,
  262. LPCNSTR nszDefault = _TEXTN("FALSE"));
  263. ~CBoolCmdlineObj (void);
  264. virtual INT SetValue (LPCNSTR nszString);
  265. virtual void DisplayValue (void);
  266. const BOOL * GetValue (void);
  267. protected :
  268. virtual LPCNSTR QueryCmdlineType(void) const;
  269. virtual LPCNSTR QueryLineArgType(void) const;
  270. };
  271. //+------------------------------------------------------------------
  272. //
  273. // Class: CStrLengthCmdlineObj
  274. //
  275. // Purpose: Provides the basic structure for an string
  276. // command line argument whose length lies within
  277. // a specified range.
  278. //
  279. // Derivation: CBaseCmdlineObj
  280. //
  281. // History: 12/27/91 Lizch Created.
  282. // 07/31/92 Davey Added QueryCmdlineType, QueryLineArgType
  283. // Added linearg parameter to constructors.
  284. //
  285. //-------------------------------------------------------------------
  286. class CStrLengthCmdlineObj : public CBaseCmdlineObj
  287. {
  288. public:
  289. CStrLengthCmdlineObj(LPCNSTR nszSwitch,
  290. LPCNSTR nszUsage,
  291. UINT uiMin,
  292. UINT uiMax,
  293. BOOL fMustHave = FALSE,
  294. LPCNSTR nszLineArg = NULL) :
  295. CBaseCmdlineObj(nszSwitch, nszUsage, fMustHave, nszLineArg)
  296. {
  297. _uiMinLength = uiMin;
  298. _uiMaxLength = uiMax;
  299. };
  300. CStrLengthCmdlineObj(LPCNSTR nszSwitch,
  301. LPCNSTR nszUsage,
  302. UINT uiMin,
  303. UINT uiMax,
  304. LPCNSTR nszDefault,
  305. LPCNSTR nszLineArg = NULL) :
  306. CBaseCmdlineObj(nszSwitch, nszUsage, nszDefault, nszLineArg)
  307. {
  308. _uiMinLength = uiMin;
  309. _uiMaxLength = uiMax;
  310. };
  311. virtual INT SetValue(LPCNSTR nszArg);
  312. protected:
  313. virtual INT DisplaySpecialUsage(USHORT usDisplayWidth,
  314. USHORT usIndent,
  315. USHORT *pusWidth);
  316. virtual LPCNSTR QueryCmdlineType(void) const;
  317. virtual LPCNSTR QueryLineArgType(void) const;
  318. private:
  319. UINT _uiMinLength;
  320. UINT _uiMaxLength;
  321. };
  322. //+------------------------------------------------------------------
  323. //
  324. // Class: CStrListCmdlineObj
  325. //
  326. // Purpose: Provides the basic structure for a
  327. // command line argument that takes a list of strings
  328. //
  329. // Derivation: CBaseCmdlineObj
  330. //
  331. // History: 12/31/91 Lizch Created.
  332. // 07/31/92 Davey Added QueryCmdlineType, QueryLineArgType
  333. // Added linearg parameter to constructors.
  334. // 12/23/93 XimingZ Converted to CNStrList
  335. // 06/13/97 MariusB ResetValue added.
  336. //
  337. //-------------------------------------------------------------------
  338. class CStrListCmdlineObj:public CBaseCmdlineObj
  339. {
  340. public:
  341. CStrListCmdlineObj (LPCNSTR pnszSwitch,
  342. LPCNSTR pnszUsage,
  343. LPCNSTR pnszDefault,
  344. LPCNSTR pnszLineArg = NULL);
  345. CStrListCmdlineObj (LPCNSTR pnszSwitch,
  346. LPCNSTR pnszUsage,
  347. BOOL fMustHave,
  348. LPCNSTR pnszLineArg = NULL);
  349. ~CStrListCmdlineObj(void);
  350. virtual INT SetValue(LPCNSTR pnszArg);
  351. virtual void ResetValue();
  352. VOID Reset(void);
  353. LPCNSTR GetValue(void);
  354. virtual void DisplayValue();
  355. INT SetDelims(LPCNSTR pnszDelims);
  356. protected:
  357. virtual INT DisplaySpecialUsage(USHORT usDisplayWidth,
  358. USHORT usIndent,
  359. USHORT *pusWidth);
  360. virtual LPCNSTR QueryCmdlineType() const;
  361. virtual LPCNSTR QueryLineArgType() const;
  362. private:
  363. LPNSTR _pnszListDelims;
  364. CnStrList *_pNStrList;
  365. };
  366. //+------------------------------------------------------------------
  367. //
  368. // Class: CCmdlineArg
  369. //
  370. // Purpose: Encapsulates one command line argument and indicates
  371. // found/not found
  372. //
  373. //-------------------------------------------------------------------
  374. class CCmdlineArg : public CBaseCmdline
  375. {
  376. public:
  377. CCmdlineArg(LPCNSTR nszArg);
  378. ~CCmdlineArg(void);
  379. const BOOL IsProcessed (void);
  380. void SetProcessedFlag(BOOL fProcessed);
  381. LPCNSTR QueryArg (void);
  382. private:
  383. LPNSTR _pnszArgument;
  384. BOOL _fProcessed;
  385. };
  386. //+------------------------------------------------------------------
  387. //
  388. // Class: CCmdLine
  389. //
  390. // Purpose: Parses command line arguments
  391. //
  392. // Created: 12/23/91 Lizch
  393. // 07/28/92 Davey Added extra usage function pointer.
  394. // Also added _usIndent, _usDisplayUsage.
  395. // Also changed SetProgName. Also added
  396. // QueryDisplayParamerters, SetIndent,
  397. // SetDisplayWidth, and SetSwitchIndent
  398. // Changed SetSeparators/SetEquators to
  399. // only take one character. Changed the
  400. // names to singular instead of plural
  401. // Added fCheckForExtras to FindSwitch.
  402. // 02/15/95 jesussp
  403. // Added contructor for Windows programs.
  404. //
  405. //-------------------------------------------------------------------
  406. class CCmdline : public CBaseCmdline
  407. {
  408. public:
  409. CCmdline (BOOL fInternalUsage = FALSE);
  410. CCmdline (int argc, char *argv[], BOOL fInternalUsage = FALSE);
  411. ~CCmdline(void);
  412. INT Parse(CBaseCmdlineObj * apExpectedArgs[],
  413. UINT uiMaxArgs,
  414. BOOL fCheckForExtras = TRUE);
  415. INT DisplayUsage(CBaseCmdlineObj * const apExpectedArgs[],
  416. UINT uiMaxArgs);
  417. INT SetProgName (LPNSTR pnszProgName);
  418. LPCNSTR GetProgName (void);
  419. void SetExtraUsage (PFVOID pfUsage);
  420. INT SetIndent (USHORT usIndent);
  421. INT SetSwitchIndent (USHORT usSwitchIndent);
  422. INT SetDisplayWidth (USHORT usDisplayWidth);
  423. void QueryDisplayParameters (USHORT *pusDisplayWidth,
  424. USHORT *pusSwitchIndent,
  425. USHORT *pusIndent) const ;
  426. private:
  427. INT CheckParameterConsistency(void) const;
  428. INT FindSwitch(CBaseCmdlineObj * const pArg, BOOL fCheckForExtras);
  429. INT ConfirmArgs(CBaseCmdlineObj *apExpectedArgs[], UINT cMaxArgs);
  430. INT SetInternalUsage(void);
  431. CCmdlineArg **_apArgs;
  432. UINT _uiNumArgs;
  433. LPNSTR _pnszProgName;
  434. CBoolCmdlineObj *_pbcInternalUsage;
  435. PFVOID _pfExtraUsage;
  436. USHORT _usIndent; // indention of usage display
  437. USHORT _usSwitchIndent; // indention of switch
  438. USHORT _usDisplayWidth; // width for usage display
  439. };
  440. #endif // __CMDLINEW_HXX__