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.

380 lines
9.8 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include "list.h"
  6. static char ReMap [256];
  7. #define FOUND 0
  8. #define NOT_FOUND 1
  9. #define ABORT 2
  10. int
  11. GetNewFile ()
  12. {
  13. char FileName [160];
  14. struct Flist *pOrig;
  15. SyncReader ();
  16. GetInput ("File...> ", FileName, 160);
  17. if (FileName[0] == 0) {
  18. SetUpdate (U_HEAD);
  19. SetEvent (vSemReader);
  20. return (0);
  21. }
  22. pOrig = vpFlCur;
  23. AddFileToList (FileName);
  24. vpFlCur = pOrig;
  25. SetEvent (vSemReader);
  26. return (1);
  27. }
  28. void
  29. GetSearchString ()
  30. {
  31. UpdateHighClear ();
  32. GetInput ("String.> ", vSearchString, 40);
  33. InitSearchReMap ();
  34. }
  35. void
  36. InitSearchReMap ()
  37. {
  38. unsigned i;
  39. if (vStatCode & S_NOCASE)
  40. _strupr (vSearchString);
  41. /*
  42. * Build ReMap
  43. */
  44. for (i=0; i < 256; i++)
  45. ReMap[i] = (char)i;
  46. if (vStatCode & S_NOCASE)
  47. for (i='a'; i <= 'z'; i++)
  48. ReMap[i] = (char)(i - ('a' - 'A'));
  49. }
  50. void
  51. FindString()
  52. {
  53. char eof, dir_next;
  54. long offset, lrange, line, l, hTopLine;
  55. struct Flist *phCurFile, *pFile;
  56. if (vSearchString[0] == 0) {
  57. SetUpdate (U_HEAD);
  58. return ;
  59. }
  60. SetUpdate (U_NONE);
  61. DisLn (CMDPOS, (Uchar)(vLines+1), "Searching");
  62. vStatCode |= S_INSEARCH;
  63. dir_next = (char)(vStatCode & S_NEXT);
  64. /*
  65. * Get starting point for search in the current file.
  66. * Save current file, and location.
  67. */
  68. hTopLine = vTopLine;
  69. phCurFile = vpFlCur;
  70. if (vHighTop >= 0L)
  71. vTopLine = vHighTop;
  72. if (vStatCode & S_NEXT)
  73. vTopLine++;
  74. if (vTopLine >= vNLine)
  75. vTopLine = vNLine-1;
  76. QuickRestore (); /* Jump to starting line */
  77. for (; ;) {
  78. /*
  79. * Make sure starting point is in memory
  80. */
  81. while (InfoReady () == 0) { /* Set extern values */
  82. ResetEvent (vSemMoreData);
  83. SetEvent (vSemReader);
  84. WaitForSingleObject(vSemMoreData, WAITFOREVER);
  85. ResetEvent(vSemMoreData);
  86. }
  87. if (! dir_next) {
  88. if (vOffTop)
  89. vOffTop--;
  90. else if (vpBlockTop->prev) {
  91. vpBlockTop = vpBlockTop->prev;
  92. vOffTop = vpBlockTop->size;
  93. }
  94. }
  95. vTopLine = 1L;
  96. /*
  97. * Do the search.
  98. * Use 2 different routines, for speed.
  99. *
  100. * Uses vpBlockTop & vOffTop. They are set up by setting TopLine
  101. * then calling InfoReady.
  102. */
  103. eof = (char)SearchText (dir_next);
  104. if (eof != FOUND)
  105. vTopLine = hTopLine;
  106. /* Multi-file search? Yes, go onto next file */
  107. if (eof == NOT_FOUND && (vStatCode & S_MFILE)) {
  108. if (vStatCode & S_NEXT) {
  109. if ( (pFile = vpFlCur->next) == NULL)
  110. break;
  111. NextFile (0, pFile); /* Get file */
  112. hTopLine = vTopLine; /* Save position */
  113. vTopLine = line = 0; /* Set search position */
  114. } else {
  115. if ( (pFile = vpFlCur->prev) == NULL)
  116. break;
  117. NextFile (0, pFile);
  118. hTopLine = vTopLine;
  119. if (vLastLine == NOLASTLINE) { /* HACK. if EOF is unkown */
  120. dir_next = S_NEXT; /* goto prev file, but scan */
  121. vTopLine = line = 0; /* from TOF to EOF */
  122. } else {
  123. vTopLine = (line = vLastLine) - vLines;
  124. dir_next = 0; /* else, scan from EOF to */
  125. if (vTopLine < 0) /* TOF. */
  126. vTopLine = 0;
  127. }
  128. }
  129. QuickRestore (); /* Display 1 page of new */
  130. SetUpdate (U_ALL); /* new file. then set scan */
  131. SetUpdate (U_NONE); /* position */
  132. vTopLine = line;
  133. continue;
  134. }
  135. break; /* Done searching */
  136. }
  137. /*
  138. * If not found (or abort), then resotre position
  139. */
  140. vStatCode &= ~S_INSEARCH;
  141. if (eof) {
  142. if (phCurFile != vpFlCur) /* Restore file & position */
  143. NextFile (0, phCurFile);
  144. QuickRestore ();
  145. SetUpdate (U_ALL); /* Force screen update, to fix */
  146. SetUpdate (U_NONE); /* scroll bar position. */
  147. DisLn (CMDPOS, (Uchar)(vLines+1), eof == 1 ? "* Text not found *" : "* Aborting Search *");
  148. if (eof == 1)
  149. beep ();
  150. return ;
  151. }
  152. // Search routine adjusts vpBlockTop & vOffTop to next(prev)
  153. // occurance of string. Now the line # must be set.
  154. offset = vpBlockTop->offset + vOffTop;
  155. lrange = vNLine/4 + 2;
  156. line = vNLine/2;
  157. while (lrange > 4L) {
  158. l = vprgLineTable[line/PLINES][line%PLINES];
  159. if (l < offset) {
  160. if ( (line += lrange) > vNLine)
  161. line = vNLine;
  162. } else {
  163. if ( (line -= lrange) < 0L)
  164. line = 0L;
  165. }
  166. /* lrange >>= 1; */
  167. lrange = (lrange>>1) + 1;
  168. }
  169. line += 7;
  170. while (vprgLineTable[line/PLINES][line%PLINES] > offset)
  171. line--;
  172. vHighTop = line;
  173. vHighLen = 0;
  174. /*
  175. * Was found. Adjust to be in center of CRT
  176. */
  177. GoToMark ();
  178. }
  179. int
  180. SearchText (
  181. char dir
  182. )
  183. {
  184. char *data;
  185. char *data1;
  186. int i;
  187. Uchar c, d;
  188. for (; ;) {
  189. data = vpBlockTop->Data;
  190. data += vOffTop;
  191. if (ReMap [(unsigned char)*data] == vSearchString[0]) {
  192. data1 = data;
  193. i = vOffTop;
  194. d = 1;
  195. for (; ;) {
  196. c = vSearchString[d++];
  197. if (c == 0)
  198. return (FOUND);
  199. if (++i >= BLOCKSIZE) {
  200. while (vpBlockTop->next == NULL) {
  201. vpCur = vpBlockTop;
  202. vReaderFlag = F_DOWN;
  203. SetEvent (vSemReader);
  204. WaitForSingleObject(vSemMoreData, WAITFOREVER);
  205. ResetEvent(vSemMoreData);
  206. }
  207. i = 0;
  208. data1 = vpBlockTop->next->Data;
  209. } else {
  210. data1++;
  211. }
  212. if (ReMap [(unsigned char)*data1] != (char)c)
  213. break;
  214. }
  215. }
  216. if (dir) {
  217. vOffTop++;
  218. if (vOffTop >= BLOCKSIZE) {
  219. if (vpBlockTop->flag == F_EOF)
  220. return (NOT_FOUND);
  221. fancy_percent ();
  222. if (_abort ())
  223. return (ABORT);
  224. while (vpBlockTop->next == NULL) {
  225. vpCur = vpBlockTop;
  226. vReaderFlag = F_DOWN;
  227. SetEvent (vSemReader);
  228. WaitForSingleObject(vSemMoreData, WAITFOREVER);
  229. ResetEvent(vSemMoreData);
  230. }
  231. vOffTop = 0;
  232. vpBlockTop = vpBlockTop->next;
  233. }
  234. } else {
  235. vOffTop--;
  236. if (vOffTop < 0) {
  237. if (vpBlockTop->offset == 0L)
  238. return (NOT_FOUND);
  239. fancy_percent ();
  240. if (_abort ())
  241. return (ABORT);
  242. while (vpBlockTop->prev == NULL) {
  243. vpCur = vpBlockTop;
  244. vReaderFlag = F_UP;
  245. SetEvent (vSemReader);
  246. WaitForSingleObject(vSemMoreData, WAITFOREVER);
  247. ResetEvent(vSemMoreData);
  248. }
  249. vOffTop = BLOCKSIZE - 1;
  250. vpBlockTop = vpBlockTop->prev;
  251. }
  252. }
  253. }
  254. }
  255. void
  256. GoToMark ()
  257. {
  258. long line;
  259. if (vHighTop < 0L)
  260. return ;
  261. line = vHighTop;
  262. UpdateHighClear ();
  263. vTopLine = 1;
  264. vHighTop = line;
  265. line = vHighTop - vLines / 2;
  266. while (line >= vNLine) {
  267. if (! (vLastLine == NOLASTLINE)) { /* Mark is past EOF? */
  268. vHighTop = vLastLine - 1; /* Then set it to EOF. */
  269. break;
  270. }
  271. if (_abort()) {
  272. line = vNLine-1;
  273. break;
  274. }
  275. fancy_percent (); /* Wait for marked line */
  276. vpBlockTop = vpCur = vpTail; /* to be processed */
  277. vReaderFlag = F_DOWN;
  278. ResetEvent (vSemMoreData);
  279. SetEvent (vSemReader);
  280. WaitForSingleObject(vSemMoreData, WAITFOREVER);
  281. ResetEvent(vSemMoreData);
  282. }
  283. if (line > vLastLine - vLines)
  284. line = vLastLine - vLines;
  285. if (line < 0L)
  286. line = 0L;
  287. vTopLine = line;
  288. vHLBot = vHLTop = 0;
  289. QuickRestore ();
  290. SetUpdate (U_ALL);
  291. }
  292. void
  293. GoToLine ()
  294. {
  295. char LineNum [10];
  296. long line;
  297. GetInput ("Line #.> ", LineNum, 10);
  298. if (LineNum[0] == 0)
  299. return;
  300. line = atol (LineNum);
  301. vHighTop = line;
  302. vHighLen = 0;
  303. GoToMark ();
  304. }
  305. void
  306. SlimeTOF ()
  307. {
  308. char Text [10];
  309. long KOff;
  310. SyncReader ();
  311. GetInput ("K Off..> ", Text, 40);
  312. KOff = atol (Text) * 1024;
  313. KOff -= KOff % BLOCKSIZE;
  314. if (Text[0] == 0 || KOff == vpFlCur->SlimeTOF) {
  315. SetEvent (vSemReader);
  316. return;
  317. }
  318. vpFlCur->SlimeTOF = KOff;
  319. vpFlCur->FileTime.dwLowDateTime = (unsigned)-1; /* Cause info to be invalid */
  320. vpFlCur->FileTime.dwHighDateTime = (unsigned)-1; /* Cause info to be invalid */
  321. FreePages (vpFlCur);
  322. NextFile (0, NULL);
  323. }