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.

575 lines
9.9 KiB

  1. //
  2. // query.c
  3. //
  4. // perform database queries
  5. //
  6. #include <stddef.h>
  7. #include <string.h>
  8. #if defined(OS2)
  9. #define INCL_NOCOMMON
  10. #define INCL_DOSPROCESS
  11. #define INCL_DOSSEMAPHORES
  12. #define INCL_DOSFILEMGR
  13. #define INCL_DOSERRORS
  14. #define INCL_DOSMISC
  15. #include <os2.h>
  16. #else
  17. #include <windows.h>
  18. #endif
  19. #include <dos.h>
  20. #include "hungary.h"
  21. #include "bsc.h"
  22. #include "bscsup.h"
  23. // these keep track of the current query, they are globally visible so
  24. // that users can see how the query is progressing
  25. //
  26. // you may not write on these
  27. IDX far idxQyStart;
  28. IDX far idxQyCur;
  29. IDX far idxQyMac;
  30. // this is auxilliary information about the current bob which some
  31. // queries may choose to make available
  32. //
  33. static BOOL fWorking;
  34. static LSZ lszModLast = NULL; // for removing duplicate modules
  35. // prototypes for the query worker functions
  36. //
  37. static BOB BobQyFiles(VOID);
  38. static BOB BobQySymbols (VOID);
  39. static BOB BobQyContains (VOID);
  40. static BOB BobQyCalls (VOID);
  41. static BOB BobQyCalledBy (VOID);
  42. static BOB BobQyUses (VOID);
  43. static BOB BobQyUsedBy (VOID);
  44. static BOB BobQyUsedIn (VOID);
  45. static BOB BobQyDefinedIn(VOID);
  46. static BOB BobQyRefs(VOID);
  47. static BOB BobQyDefs(VOID);
  48. // current bob worker function
  49. static BOB (*bobFn)(VOID) = NULL;
  50. BOOL BSC_API
  51. InitBSCQuery (QY qy, BOB bob)
  52. // do the request query on the given bob
  53. //
  54. {
  55. fWorking = FALSE;
  56. if (lszModLast == NULL)
  57. lszModLast = LpvAllocCb(1024); // REVIEW -- how much to alloc? [rm]
  58. // no memory -- no query
  59. if (lszModLast == NULL)
  60. return FALSE;
  61. strcpy(lszModLast, "");
  62. switch (qy) {
  63. case qyFiles:
  64. bobFn = BobQyFiles;
  65. idxQyStart = (IDX)0;
  66. idxQyMac = (IDX)ImodMac();
  67. break;
  68. case qySymbols:
  69. bobFn = BobQySymbols;
  70. idxQyStart = (IDX)0;
  71. idxQyMac = (IDX)IinstMac();
  72. break;
  73. case qyContains:
  74. {
  75. IMS ims, imsMac;
  76. bobFn = BobQyContains;
  77. if (ClsOfBob(bob) != clsMod) return FALSE;
  78. MsRangeOfMod(ImodFrBob(bob), &ims, &imsMac);
  79. idxQyStart = (IDX)ims;
  80. idxQyMac = (IDX)imsMac;
  81. break;
  82. }
  83. case qyCalls:
  84. {
  85. IUSE iuse, iuseMac;
  86. bobFn = BobQyCalls;
  87. if (ClsOfBob(bob) != clsInst) return FALSE;
  88. UseRangeOfInst(IinstFrBob(bob), &iuse, &iuseMac);
  89. idxQyStart = (IDX)iuse;
  90. idxQyMac = (IDX)iuseMac;
  91. break;
  92. }
  93. case qyUses:
  94. {
  95. IUSE iuse, iuseMac;
  96. bobFn = BobQyUses;
  97. if (ClsOfBob(bob) != clsInst) return FALSE;
  98. UseRangeOfInst(IinstFrBob(bob), &iuse, &iuseMac);
  99. idxQyStart = (IDX)iuse;
  100. idxQyMac = (IDX)iuseMac;
  101. break;
  102. }
  103. case qyCalledBy:
  104. {
  105. IUBY iuby, iubyMac;
  106. bobFn = BobQyCalledBy;
  107. if (ClsOfBob(bob) != clsInst) return FALSE;
  108. UbyRangeOfInst(IinstFrBob(bob), &iuby, &iubyMac);
  109. idxQyStart = (IDX)iuby;
  110. idxQyMac = (IDX)iubyMac;
  111. break;
  112. }
  113. case qyUsedBy:
  114. {
  115. IUBY iuby, iubyMac;
  116. bobFn = BobQyUsedBy;
  117. if (ClsOfBob(bob) != clsInst) return FALSE;
  118. UbyRangeOfInst(IinstFrBob(bob), &iuby, &iubyMac);
  119. idxQyStart = (IDX)iuby;
  120. idxQyMac = (IDX)iubyMac;
  121. break;
  122. }
  123. case qyUsedIn:
  124. {
  125. IREF iref, irefMac;
  126. bobFn = BobQyUsedIn;
  127. if (ClsOfBob(bob) != clsInst) return FALSE;
  128. RefRangeOfInst(IinstFrBob(bob), &iref, &irefMac);
  129. idxQyStart = (IDX)iref;
  130. idxQyMac = (IDX)irefMac;
  131. break;
  132. }
  133. case qyDefinedIn:
  134. {
  135. IDEF idef, idefMac;
  136. bobFn = BobQyDefinedIn;
  137. if (ClsOfBob(bob) != clsInst) return FALSE;
  138. DefRangeOfInst(IinstFrBob(bob), &idef, &idefMac);
  139. idxQyStart = (IDX)idef;
  140. idxQyMac = (IDX)idefMac;
  141. break;
  142. }
  143. case qyRefs:
  144. {
  145. IINST iinst, iinstMac;
  146. bobFn = BobQyRefs;
  147. switch (ClsOfBob(bob)) {
  148. default:
  149. return FALSE;
  150. case clsSym:
  151. InstRangeOfSym(IsymFrBob(bob), &iinst, &iinstMac);
  152. idxQyStart = (IDX)iinst;
  153. idxQyMac = (IDX)iinstMac;
  154. break;
  155. case clsInst:
  156. idxQyStart = (IDX)IinstFrBob(bob);
  157. idxQyMac = idxQyStart+1;
  158. break;
  159. }
  160. break;
  161. }
  162. case qyDefs:
  163. {
  164. IINST iinst, iinstMac;
  165. bobFn = BobQyDefs;
  166. switch (ClsOfBob(bob)) {
  167. default:
  168. return FALSE;
  169. case clsSym:
  170. InstRangeOfSym(IsymFrBob(bob), &iinst, &iinstMac);
  171. idxQyStart = (IDX)iinst;
  172. idxQyMac = (IDX)iinstMac;
  173. break;
  174. case clsInst:
  175. idxQyStart = (IDX)IinstFrBob(bob);
  176. idxQyMac = idxQyStart+1;
  177. break;
  178. }
  179. break;
  180. }
  181. }
  182. idxQyCur = idxQyStart;
  183. return TRUE;
  184. }
  185. BOB BSC_API
  186. BobNext()
  187. // return the next Bob in the query
  188. {
  189. if (idxQyCur < idxQyMac && bobFn != NULL)
  190. return (*bobFn)();
  191. return bobNil;
  192. }
  193. static BOB
  194. BobQyFiles()
  195. // return the next File in a file query
  196. //
  197. {
  198. BOB bob;
  199. while (idxQyCur < idxQyMac) {
  200. IMS ims1, ims2;
  201. MsRangeOfMod((IMOD)idxQyCur, &ims1, &ims2);
  202. if (ims1 != ims2) {
  203. bob = BobFrClsIdx(clsMod, idxQyCur);
  204. idxQyCur++;
  205. return bob;
  206. }
  207. else
  208. idxQyCur++;
  209. }
  210. return bobNil;
  211. }
  212. static BOB
  213. BobQySymbols ()
  214. // get the next symbol in a symbol query
  215. //
  216. {
  217. BOB bob;
  218. bob = BobFrClsIdx(clsInst, idxQyCur);
  219. idxQyCur++;
  220. return bob;
  221. }
  222. static BOB
  223. BobQyContains ()
  224. // get the next symbol in a contains query
  225. //
  226. {
  227. BOB bob;
  228. bob = BobFrClsIdx(clsInst, IinstOfIms((IMS)idxQyCur));
  229. idxQyCur++;
  230. return bob;
  231. }
  232. static BOB
  233. BobQyCalls ()
  234. // get the next symbol which query focus calls
  235. //
  236. {
  237. WORD cuse;
  238. IINST iinst;
  239. ISYM isym;
  240. TYP typ;
  241. ATR atr;
  242. BOB bob;
  243. for (; idxQyCur < idxQyMac; idxQyCur++) {
  244. UseInfo((IUSE)idxQyCur, &iinst, &cuse);
  245. InstInfo(iinst, &isym, &typ, &atr);
  246. if (typ > INST_TYP_LABEL)
  247. continue;
  248. bob = BobFrClsIdx(clsInst, iinst);
  249. idxQyCur++;
  250. return bob;
  251. }
  252. return bobNil;
  253. }
  254. static BOB
  255. BobQyCalledBy ()
  256. // get the next symbol which query focus is called by
  257. //
  258. {
  259. WORD cuse;
  260. IINST iinst;
  261. ISYM isym;
  262. TYP typ;
  263. ATR atr;
  264. BOB bob;
  265. for (; idxQyCur < idxQyMac; idxQyCur++) {
  266. UbyInfo((IUBY)idxQyCur, &iinst, &cuse);
  267. InstInfo(iinst, &isym, &typ, &atr);
  268. if (typ > INST_TYP_LABEL)
  269. continue;
  270. bob = BobFrClsIdx(clsInst, iinst);
  271. idxQyCur++;
  272. return bob;
  273. }
  274. return bobNil;
  275. }
  276. static BOB
  277. BobQyUses ()
  278. // get the next symbol which query focus calls
  279. //
  280. {
  281. WORD cuse;
  282. IINST iinst;
  283. BOB bob;
  284. UseInfo((IUSE)idxQyCur, &iinst, &cuse);
  285. bob = BobFrClsIdx(clsInst, iinst);
  286. idxQyCur++;
  287. return bob;
  288. }
  289. static BOB
  290. BobQyUsedBy ()
  291. // get the next symbol which query focus calls
  292. //
  293. {
  294. WORD cuse;
  295. IINST iinst;
  296. BOB bob;
  297. UbyInfo((IUBY)idxQyCur, &iinst, &cuse);
  298. bob = BobFrClsIdx(clsInst, iinst);
  299. idxQyCur++;
  300. return bob;
  301. }
  302. static BOB
  303. BobQyUsedIn ()
  304. // get the next module which query focus is used in
  305. //
  306. {
  307. WORD wLine;
  308. BOB bob;
  309. LSZ lszMod;
  310. for ( ; idxQyCur < idxQyMac ; idxQyCur++) {
  311. RefInfo((IREF)idxQyCur, &lszMod, &wLine);
  312. if (strcmp(lszMod, lszModLast) == 0)
  313. continue;
  314. strcpy(lszModLast, lszMod);
  315. bob = BobFrClsIdx(clsMod, ImodFrLsz(lszMod));
  316. idxQyCur++;
  317. return bob;
  318. }
  319. return bobNil;
  320. }
  321. static BOB
  322. BobQyDefinedIn ()
  323. // get the next module which query focus is defined in
  324. //
  325. {
  326. WORD wLine;
  327. LSZ lszMod;
  328. BOB bob;
  329. for ( ; idxQyCur < idxQyMac ; idxQyCur++) {
  330. DefInfo((IDEF)idxQyCur, &lszMod, &wLine);
  331. if (strcmp(lszMod, lszModLast) == 0)
  332. continue;
  333. strcpy(lszModLast, lszMod);
  334. bob = BobFrClsIdx(clsMod, ImodFrLsz(lszMod));
  335. idxQyCur++;
  336. return bob;
  337. }
  338. return bobNil;
  339. }
  340. LSZ BSC_API
  341. LszNameFrBob(BOB bob)
  342. // return the name of the given bob
  343. //
  344. {
  345. switch (ClsOfBob(bob)) {
  346. case clsMod:
  347. return LszNameFrMod(ImodFrBob(bob));
  348. case clsSym:
  349. return LszNameFrSym(IsymFrBob(bob));
  350. case clsInst:
  351. {
  352. ISYM isym;
  353. TYP typ;
  354. ATR atr;
  355. InstInfo(IinstFrBob(bob), &isym, &typ, &atr);
  356. return LszNameFrSym(isym);
  357. }
  358. case clsRef:
  359. {
  360. LSZ lsz;
  361. WORD wLine;
  362. RefInfo(IrefFrBob(bob), &lsz, &wLine);
  363. return lsz;
  364. }
  365. case clsDef:
  366. {
  367. LSZ lsz;
  368. WORD wLine;
  369. DefInfo(IdefFrBob(bob), &lsz, &wLine);
  370. return lsz;
  371. }
  372. default:
  373. return "?";
  374. }
  375. }
  376. BOB BSC_API
  377. BobFrName(LSZ lszName)
  378. // return the best bob we can find from the given name
  379. //
  380. {
  381. ISYM isym;
  382. IMOD imod, imodMac;
  383. IINST iinst, iinstMac;
  384. if ((isym = IsymFrLsz(lszName)) != isymNil) {
  385. InstRangeOfSym(isym, &iinst, &iinstMac);
  386. return BobFrClsIdx(clsInst, iinst);
  387. }
  388. if ((imod = ImodFrLsz(lszName)) != imodNil) {
  389. return BobFrClsIdx(clsMod, imod);
  390. }
  391. imodMac = ImodMac();
  392. // no exact match -- try short names
  393. lszName = LszBaseName(lszName);
  394. for (imod = 0; imod < imodMac; imod++)
  395. if (_stricmp(lszName, LszBaseName(LszNameFrMod(imod))) == 0)
  396. return BobFrClsIdx(clsMod, imod);
  397. return bobNil;
  398. }
  399. static BOB
  400. BobQyRefs()
  401. // return the next File in a file query
  402. //
  403. {
  404. BOB bob;
  405. static IREF iref, irefMac;
  406. for (;;) {
  407. if (!fWorking) {
  408. for ( ; idxQyCur < idxQyMac ; idxQyCur++) {
  409. RefRangeOfInst((IINST)idxQyCur, &iref, &irefMac);
  410. if (iref != irefMac)
  411. break;
  412. }
  413. if (idxQyCur >= idxQyMac)
  414. return bobNil;
  415. fWorking = TRUE;
  416. }
  417. if (iref < irefMac) {
  418. bob = BobFrClsIdx(clsRef, iref);
  419. iref++;
  420. return bob;
  421. }
  422. idxQyCur++;
  423. fWorking = FALSE;
  424. }
  425. }
  426. static BOB
  427. BobQyDefs()
  428. // return the next File in a file query
  429. //
  430. {
  431. BOB bob;
  432. static IDEF idef, idefMac;
  433. for (;;) {
  434. if (!fWorking) {
  435. for ( ; idxQyCur < idxQyMac ; idxQyCur++) {
  436. DefRangeOfInst((IINST)idxQyCur, &idef, &idefMac);
  437. if (idef != idefMac)
  438. break;
  439. }
  440. if (idxQyCur >= idxQyMac)
  441. return bobNil;
  442. fWorking = TRUE;
  443. }
  444. if (idef < idefMac) {
  445. bob = BobFrClsIdx(clsDef, idef);
  446. idef++;
  447. return bob;
  448. }
  449. idxQyCur++;
  450. fWorking = FALSE;
  451. }
  452. }