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.

492 lines
14 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1999
  5. //
  6. // File: output.h
  7. //
  8. // Contents: Header file for classes and function used for display
  9. //
  10. // History: 3-oct-2000 hiteshr Created
  11. //
  12. //--------------------------------------------------------------------------
  13. extern bool g_bQuiet;
  14. extern int g_iQueryLimit;
  15. extern bool g_bDeafultLimit;
  16. HRESULT LocalCopyString(LPTSTR* ppResult, LPCTSTR pString);
  17. //+--------------------------------------------------------------------------
  18. //
  19. // Class: CDisplay
  20. //
  21. // Purpose: This class is used for displaying a column
  22. //
  23. // History: 3-oct-2000 hiteshr Created
  24. //
  25. //---------------------------------------------------------------------------
  26. class CDisplay
  27. {
  28. #define MAXPAD 80
  29. public:
  30. //
  31. //Initialize the Pad
  32. //
  33. CDisplay()
  34. {
  35. PadChar = L' ';
  36. //Initialize the pad.
  37. for( int i = 0; i < MAXPAD; ++i)
  38. Pad[i] = PadChar;
  39. }
  40. //
  41. //Display width number of Pad Charachter
  42. //
  43. VOID DisplayPad(LONG width)
  44. {
  45. if(width <= 0 )
  46. return;
  47. if(width >= MAXPAD)
  48. width = MAXPAD -1;
  49. Pad[width] = 0;
  50. DisplayOutputNoNewline(Pad);
  51. Pad[width] = PadChar;
  52. }
  53. //
  54. //Dispaly a column with two starting pad,
  55. //column value and two ending pad
  56. //
  57. VOID DisplayColumn(LONG width, LPWSTR lpszValue)
  58. {
  59. //Display Two PadChar in the begining
  60. DisplayPad(2);
  61. if(lpszValue)
  62. {
  63. DisplayOutputNoNewline(lpszValue);
  64. //Security Review:This is fine.
  65. DisplayPad(width- static_cast<LONG>(wcslen(lpszValue)));
  66. }
  67. else
  68. DisplayPad(width);
  69. //Display Two Trailing Padchar
  70. DisplayPad(2);
  71. }
  72. //
  73. //Display Newline
  74. //
  75. VOID DisplayNewLine()
  76. {
  77. DisplayOutputNoNewline(L"\r\n");
  78. }
  79. private:
  80. WCHAR Pad[MAXPAD];
  81. WCHAR PadChar;
  82. };
  83. //+--------------------------------------------------------------------------
  84. //
  85. // Class: CFormaInfo
  86. //
  87. // Purpose: Used to format table columns and display table
  88. //
  89. // History: 3-oct-2000 hiteshr Created
  90. //
  91. //---------------------------------------------------------------------------
  92. class CFormatInfo
  93. {
  94. public:
  95. CFormatInfo():m_cCol(0),
  96. m_ppszOutPutVal(NULL),
  97. m_pColWidth(NULL),
  98. m_bInit(FALSE),
  99. m_cTotalRow(-1)
  100. {};
  101. ~CFormatInfo()
  102. {
  103. if(m_ppszOutPutVal)
  104. {
  105. for(LONG i = 0; i < m_SampleSize*m_cCol; ++i)
  106. LocalFree(m_ppszOutPutVal[i]);
  107. }
  108. LocalFree(m_ppszOutPutVal);
  109. LocalFree(m_pColWidth);
  110. }
  111. //
  112. //Do the initialization
  113. //
  114. HRESULT Init(LONG sampleSize, LONG cCol, LPWSTR * ppszColHeaders)
  115. {
  116. if(!sampleSize || !cCol || !ppszColHeaders)
  117. {
  118. ASSERT(FALSE);
  119. return E_INVALIDARG;
  120. }
  121. m_SampleSize = sampleSize;
  122. m_cCol = cCol;
  123. m_ppszColHeaders = ppszColHeaders;
  124. m_ppszOutPutVal = (LPWSTR*)LocalAlloc(LPTR,m_SampleSize*cCol*sizeof(LPWSTR));
  125. if(!m_ppszOutPutVal)
  126. return E_OUTOFMEMORY;
  127. m_pColWidth = (LONG*)LocalAlloc(LPTR, cCol*sizeof(LONG));
  128. if(!m_pColWidth)
  129. return E_OUTOFMEMORY;
  130. //
  131. //Initialize the minimum column width to width of column heading
  132. //
  133. for(LONG i = 0; i < m_cCol; ++i)
  134. m_pColWidth[i] = static_cast<LONG>(wcslen(m_ppszColHeaders[i]));
  135. m_bInit = TRUE;
  136. return S_OK;
  137. };
  138. //
  139. //Get the Column Width
  140. //
  141. inline
  142. LONG GetColWidth(LONG col)
  143. {
  144. if(!m_bInit)
  145. {
  146. ASSERT(m_bInit);
  147. return 0;
  148. }
  149. if(col >= m_cCol)
  150. {
  151. ASSERT(FALSE);
  152. return 0;
  153. }
  154. return m_pColWidth[col];
  155. }
  156. //
  157. //Set the column Width
  158. //
  159. inline
  160. VOID SetColWidth(LONG col, LONG width)
  161. {
  162. if(!m_bInit)
  163. {
  164. ASSERT(m_bInit);
  165. return;
  166. }
  167. if(col >= m_cCol)
  168. {
  169. ASSERT(FALSE);
  170. return;
  171. }
  172. if(width > m_pColWidth[col])
  173. m_pColWidth[col] = width;
  174. }
  175. //
  176. //Cache the value and update column width
  177. //
  178. BOOL Set(LONG row, LONG col, LPWSTR pszValue)
  179. {
  180. if(!m_bInit)
  181. {
  182. ASSERT(m_bInit);
  183. return FALSE;
  184. }
  185. if(row >= m_SampleSize || col >= m_cCol)
  186. {
  187. ASSERT(FALSE);
  188. return FALSE;
  189. }
  190. if(pszValue)
  191. {
  192. SetColWidth(col, static_cast<LONG>(wcslen(pszValue)));
  193. LocalCopyString((LPWSTR*)(m_ppszOutPutVal + (row*m_cCol) + col),pszValue);
  194. }
  195. if(row>= m_cTotalRow)
  196. m_cTotalRow = row +1;
  197. return TRUE;
  198. }
  199. //
  200. //Total number of rows in cache
  201. //
  202. LONG GetRowCount()
  203. {
  204. return m_cTotalRow;
  205. }
  206. //
  207. //Get the value
  208. //
  209. inline
  210. LPWSTR Get(LONG row, LONG col)
  211. {
  212. if(!m_bInit)
  213. {
  214. ASSERT(m_bInit);
  215. NULL;
  216. }
  217. if(row >= m_cTotalRow || col >= m_cCol)
  218. {
  219. ASSERT(FALSE);
  220. return NULL;
  221. }
  222. return (LPWSTR)(*(m_ppszOutPutVal + row*m_cCol +col));
  223. }
  224. //
  225. //Display headers
  226. //
  227. VOID DisplayHeaders()
  228. {
  229. if(!m_bInit)
  230. {
  231. ASSERT(m_bInit);
  232. return;
  233. }
  234. if (g_bQuiet)
  235. {
  236. return;
  237. }
  238. if(!m_ppszColHeaders)
  239. {
  240. ASSERT(m_ppszColHeaders);
  241. return;
  242. }
  243. for( long i = 0; i < m_cCol; ++i)
  244. {
  245. m_display.DisplayColumn(GetColWidth(i),m_ppszColHeaders[i]);
  246. }
  247. NewLine();
  248. }
  249. //
  250. //Display a coulmn which is in cache
  251. //
  252. VOID DisplayColumn(LONG row,LONG col)
  253. {
  254. ASSERT(m_bInit);
  255. if(row >= m_cTotalRow || col >= m_cCol)
  256. {
  257. ASSERT(FALSE);
  258. return ;
  259. }
  260. m_display.DisplayColumn(GetColWidth(col),Get(row,col));
  261. }
  262. //
  263. //Display the value using column width for col
  264. //
  265. VOID DisplayColumn(LONG col, LPWSTR pszValue)
  266. {
  267. if(col >= m_cCol)
  268. {
  269. ASSERT(FALSE);
  270. return;
  271. }
  272. m_display.DisplayColumn(GetColWidth(col),pszValue);
  273. }
  274. //
  275. //Display all rows in cache
  276. //
  277. VOID DisplayAllRows()
  278. {
  279. for(long i = 0; i < m_cTotalRow; ++i)
  280. {
  281. for(long j = 0; j < m_cCol; ++j)
  282. DisplayColumn(i,j);
  283. NewLine();
  284. }
  285. }
  286. //
  287. //Display a newline
  288. //
  289. VOID NewLine(){m_display.DisplayNewLine();}
  290. private:
  291. //
  292. //True if Init is called
  293. //
  294. BOOL m_bInit;
  295. //
  296. //Number of rows to be used for calulating
  297. //column width. This is also the size of the table.
  298. //
  299. LONG m_SampleSize;
  300. //
  301. //Count of rows in cache
  302. //
  303. LONG m_cTotalRow;
  304. //
  305. //Number of columns
  306. //
  307. LONG m_cCol;
  308. LPWSTR *m_ppszOutPutVal;
  309. LONG * m_pColWidth;
  310. //
  311. // Array of column headers. Its assumed that its length is same as m_cCol
  312. //
  313. LPWSTR *m_ppszColHeaders;
  314. CDisplay m_display;
  315. };
  316. //+--------------------------------------------------------------------------
  317. //
  318. // Synopsis: Defines the scopes that a search can be run against when
  319. // looking for a server object
  320. //
  321. // NOTE: If SERVER_QUERY_SCOPE_FOREST is not set then we are scoped
  322. // against a site.
  323. //
  324. //---------------------------------------------------------------------------
  325. #define SERVER_QUERY_SCOPE_SITE 0x00000001
  326. #define SERVER_QUERY_SCOPE_FOREST 0x00000002
  327. #define SERVER_QUERY_SCOPE_DOMAIN 0x00000004
  328. //+--------------------------------------------------------------------------
  329. //
  330. // Function: GetServerSearchRoot
  331. //
  332. // Synopsis: Builds the path to the root of the search as determined by
  333. // the parameters passed in from the command line.
  334. //
  335. // Arguments: [pCommandArgs IN] : the table of the command line input
  336. // [refBasePathsInfo IN] : reference to the base paths info
  337. // [refsbstrDN OUT] : reference to a CComBSTR that will
  338. // receive the DN at which to start
  339. // the search
  340. //
  341. // Returns: DWORD : one of: SERVER_QUERY_SCOPE_FOREST,
  342. // SERVER_QUERY_SCOPE_DOMAIN,
  343. // SERVER_QUERY_SCOPE_SITE
  344. // which define the scope being used
  345. //
  346. // History: 11-Dec-2000 JeffJon Created
  347. //
  348. //---------------------------------------------------------------------------
  349. DWORD GetServerSearchRoot(IN PARG_RECORD pCommandArgs,
  350. IN CDSCmdBasePathsInfo& refBasePathsInfo,
  351. OUT CComBSTR& refsbstrDN);
  352. //+--------------------------------------------------------------------------
  353. //
  354. // Function: GetSubnetSearchRoot
  355. //
  356. // Synopsis: Builds search root path for Subnet. Its always
  357. // cn=subnet,cn=site in configuration container
  358. //
  359. // Arguments: [refBasePathsInfo IN] : reference to the base paths info
  360. // [refsbstrDN OUT] : reference to a CComBSTR that will
  361. // receive the DN at which to start
  362. // the search
  363. //
  364. // Returns: HRESULT
  365. //
  366. // History: 11-Dec-2000 JeffJon Created
  367. //
  368. //---------------------------------------------------------------------------
  369. VOID GetSubnetSearchRoot(IN CDSCmdBasePathsInfo& refBasePathsInfo,
  370. OUT CComBSTR& refsbstrDN);
  371. //+--------------------------------------------------------------------------
  372. //
  373. // Function: GetSiteContainerPath
  374. //
  375. // Synopsis: Returns the DN for site container in Configuration
  376. // container
  377. //
  378. // Arguments: [refBasePathsInfo IN] : reference to the base paths info
  379. // [refsbstrDN OUT] : reference to a CComBSTR that will
  380. // receive the DN
  381. //
  382. // Returns: HRESULT
  383. //
  384. // History: 24-April-2001 hiteshr Created
  385. //
  386. //---------------------------------------------------------------------------
  387. VOID GetSiteContainerPath(IN CDSCmdBasePathsInfo& refBasePathsInfo,
  388. OUT CComBSTR& refSubSiteSuffix);
  389. //+--------------------------------------------------------------------------
  390. //
  391. // Function: DsQueryServerOutput
  392. //
  393. // Synopsis: This functions outputs the query results for server object.
  394. //
  395. // Arguments: [outputFormat IN] Output format specified at commandline.
  396. // [ppszAttributes IN] List of attributes fetched by query
  397. // [cAttributes,IN] Number of arributes in above array
  398. // [refServerSearch,IN]reference to the search Object
  399. // [refCredObject IN] reference to the credential object
  400. // [refBasePathsInfo IN] reference to the base paths info
  401. // [pCommandArgs,IN] The pointer to the commands table
  402. //
  403. // Returns: HRESULT : S_OK if everything succeeded
  404. // E_INVALIDARG
  405. // Anything else is a failure code from an ADSI call
  406. //
  407. // History: 08-Dec-2000 JeffJon Created
  408. //
  409. //---------------------------------------------------------------------------
  410. HRESULT DsQueryServerOutput( IN DSQUERY_OUTPUT_FORMAT outputFormat,
  411. IN LPWSTR* ppszAttributes,
  412. IN DWORD cAttributes,
  413. IN CDSSearch& refServerSearch,
  414. IN const CDSCmdCredentialObject& refCredObject,
  415. IN CDSCmdBasePathsInfo& refBasePathsInfo,
  416. IN PARG_RECORD pCommandArgs);
  417. //+--------------------------------------------------------------------------
  418. //
  419. // Function: DsQueryOutput
  420. //
  421. // Synopsis: This functions outputs the query results.
  422. //
  423. // Arguments: [outputFormat IN] Output format specified at commandline.
  424. // [ppszAttributes IN] List of attributes fetched by query
  425. // [cAttributes,IN] Number of arributes in above array
  426. // [*pSeach,IN] Search Object which has queryhandle
  427. // [bListFormat IN] Is Output to shown in List Format.
  428. // This is valid for "dsquery *" only.
  429. // Returns: HRESULT : S_OK if everything succeeded
  430. // E_INVALIDARG
  431. // Anything else is a failure code from an ADSI call
  432. //
  433. // History: 25-Sep-2000 hiteshr Created
  434. //
  435. //---------------------------------------------------------------------------
  436. HRESULT DsQueryOutput( IN DSQUERY_OUTPUT_FORMAT outputFormat,
  437. IN LPWSTR * ppszAttributes,
  438. IN DWORD cAttributes,
  439. IN CDSSearch *pSearch,
  440. IN BOOL bListFormat );