Windows NT 4.0 source code leak
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.

351 lines
11 KiB

4 years ago
  1. /*** INIT.C -- routines to handle TOOLS.INI ************************************
  2. *
  3. * Copyright (c) 1988-1990, Microsoft Corporation. All rights reserved.
  4. *
  5. * Purpose:
  6. * Module contains routines to deal with TOOLS.INI file. Functions in TOOLS.LIB
  7. * have not been used because NMAKE needs to be small and the overhead is too
  8. * much.
  9. *
  10. * Revision History:
  11. * 15-Oct-1993 HV Use tchar.h instead of mbstring.h directly, change STR*() to _ftcs*()
  12. * 10-May-1993 HV Add include file mbstring.h
  13. * Change the str* functions to STR*
  14. * 10-May-1993 HV Revise SearchFileInEnv to take care of cases when path
  15. * characters (\,/,:) are used. This fixed the recursive
  16. * problem.
  17. * 22-Apr-1993 HV Rewrite SearchRunPath() to use _makepath(), _searchenv()
  18. * Add SearchFileInEnv() helper for SearchRunPath()
  19. * 08-Jun-1992 SS Port to DOSX32
  20. * 02-Feb-1990 SB Replace fopen() by FILEOPEN
  21. * 22-Nov-1989 SB Changed free() to FREE()
  22. * 19-Oct-1989 SB searchHandle passed around as extra param
  23. * 16-Aug-1989 SB error check for fclose() added
  24. * 24-Apr-1989 SB made FILEINFO as void * for OS/2 1.2 support
  25. * 05-Apr-1989 SB made all funcs NEAR; Reqd to make all function calls NEAR
  26. * 20-Sep-1988 RB Add SearchRunPath().
  27. * Remove TOOLS.INI warning.
  28. * 17-Aug-1988 RB Clean up.
  29. * 10-May-1988 rb Find tools.ini in current directory first.
  30. * 27-May-1988 rb Remove NO_INIT_ENTRY warning because of built-ins.
  31. *
  32. *******************************************************************************/
  33. #include "nmake.h"
  34. #include "nmmsg.h"
  35. #include "proto.h"
  36. #include "globals.h"
  37. #include "grammar.h"
  38. LOCAL BOOL NEAR findTag(char*);
  39. LOCAL char * NEAR SearchFileInEnv(const char *, const char *, const char *, char *);
  40. /* ----------------------------------------------------------------------------
  41. * tagOpen()
  42. *
  43. * arguments: where pointer to name of environment variable
  44. * containing path to search
  45. * name pointer to name of initialization file
  46. * tag pointer to name of tag to find in file
  47. *
  48. * actions: looks for file in current directory
  49. * if not found, looks in each dir in path (semicolons
  50. * separate each path from the next in the string)
  51. * if file is found and opened, looks for the given tag
  52. *
  53. * (if ported to xenix, tagOpen() and searchPath()
  54. * should probably use access() and not findFirst().)
  55. *
  56. * returns: if file and tag are found, returns pointer to file,
  57. * opened for reading and positioned at the line
  58. * following the tag line
  59. * else returns NULL
  60. */
  61. BOOL NEAR
  62. tagOpen(where, name, tag)
  63. char *where;
  64. char *name;
  65. char *tag;
  66. {
  67. //CONSIDER: make one exit point to generate one free call
  68. //CONSIDER: This should reduce code size -Sundeep-
  69. register char *p;
  70. void *findBuf = _alloca(resultbuf_size);
  71. NMHANDLE searchHandle;
  72. extern char *makeStr;
  73. /*
  74. * Look for 'name' in current directory then path. searchPath does all
  75. * the work.
  76. */
  77. if (!(p = searchPath(getenv(where),name,findBuf, &searchHandle))) {
  78. return(FALSE);
  79. }
  80. #ifdef DEBUG_ALL
  81. printf ("found file %s\n", p);
  82. #endif
  83. if (!(file = FILEOPEN(p,"rt")))
  84. makeError(0,CANT_READ_FILE,p); /* p now pts to pathname */
  85. FREE(p);
  86. #ifdef DEBUG_ALL
  87. printf ("findTag %s\n", tag);
  88. #endif
  89. if (findTag(tag)) {
  90. #ifdef DEBUG_ALL
  91. printf ("foundTag %s\n", tag);
  92. #endif
  93. return(TRUE); /* look for tag in file */
  94. }
  95. #ifdef DEBUG_ALL
  96. printf ("no found Tag %s\n", tag);
  97. #endif
  98. if (fclose(file) == EOF) /* if tag not found, close*/
  99. makeError(0, ERROR_CLOSING_FILE, p);
  100. return(FALSE); /* file and pretend file */
  101. } /* not found */
  102. /* ----------------------------------------------------------------------------
  103. * searchPath()
  104. *
  105. * arguments: p pointer to string of paths to be searched
  106. * name name of file being searched for
  107. *
  108. * actions: looks for name in current directory, then each
  109. * directory listed in string.
  110. *
  111. * returns: pointer to path spec of file found, else NULL
  112. *
  113. * I don't use _ftcstok() here because that modifies the string that it "token-
  114. * izes" and we cannot modify the environment-variable string. I'd have to
  115. * make a local copy of the whole string, and then make another copy of each
  116. * directory to which I concatenate the filename to in order to test for the
  117. * file's existence.
  118. */
  119. //Added parameters findBuf and searchHandle to work for OS/2 longnames
  120. // and OS/2 1.1, 1.2 and DOS
  121. char * NEAR
  122. searchPath(p,name,findBuf, searchHandle)
  123. char *p;
  124. char *name;
  125. void *findBuf;
  126. NMHANDLE *searchHandle;
  127. {
  128. register char *s; /* since it's not in use */
  129. /* CONSIDER: Why aren't we using access() here? FindFirst has problems
  130. * CONSIDER: with networks and DOS 2.x. Also maybe cheaper. [RLB]. */
  131. // We use FindFirst() because the dateTime of file matters to us
  132. // We don't need it always but then access() probably uses findFirst()
  133. // -Sundeep-
  134. if (findFirst(name,&findBuf, searchHandle)) /* check current dir first*/
  135. return(makeString(name));
  136. /*
  137. * Check if environment string is NULL. Unnecessary if check is done
  138. * elsewhere, but it's more convenient and safer to do it here.
  139. */
  140. if (p == NULL)
  141. return(NULL);
  142. for (s = buf; ;) {
  143. if (!*p || (*s = *p++) == ';') { /* found a dir separator */
  144. if (s == buf) { /* ignore ; w/out name */
  145. if (*p) continue;
  146. return(NULL); /* list exhausted ... */
  147. }
  148. if (*(s-1) != '\\' && *(s-1) != '/') /* append path separator */
  149. *s++ = '\\';
  150. *s = '\0';
  151. if (_ftcspbrk(buf,"*?")) { /* wildcards not allowed */
  152. s = buf;
  153. continue;
  154. }
  155. _ftcscpy(s,name); /* append file name, zap ;*/
  156. if (findFirst(buf,&findBuf, searchHandle))
  157. return(makeString(buf));
  158. #ifdef DEBUG_ALL
  159. heapdump(__FILE__, __LINE__);
  160. #endif
  161. s = buf; /* reset ptr to begin of */
  162. } /* buf and check next dir*/
  163. else ++s; /* we keep copying chars */
  164. } /* until find ';' or '\0'*/
  165. }
  166. /* ----------------------------------------------------------------------------
  167. * findTag()
  168. *
  169. * arguments: tag pointer to tag name to be searched for
  170. *
  171. * actions: reads tokens from file
  172. * whenever it sees a newline, checks the next token
  173. * to see if 1st char is opening paren
  174. * if no, reads and discards rest of line and
  175. * checks next token to see if it's newline or EOF
  176. * and if newline loops to check next token . . .
  177. * if yes ('[' found), looks on line for tag
  178. * if tag found, looks for closing paren
  179. * if ']' found, discards rest of line and returns
  180. * else keeps looking until end of file or error
  181. *
  182. * returns: if successful, returns TRUE
  183. * if tag never found, returns FALSE
  184. */
  185. LOCAL BOOL NEAR
  186. findTag(tag)
  187. char *tag;
  188. {
  189. register BOOL endTag; /* TRUE when find [...] */
  190. register unsigned n;
  191. register char *s;
  192. for (line = 0; fgets(buf,MAXBUF,file); ++line) {
  193. if (*buf == '[') {
  194. endTag = FALSE;
  195. for (s = _ftcstok(buf+1," \t\n"); s && !endTag;
  196. s = _ftcstok(NULL," \t\n")) {
  197. n = _ftcslen(s) - 1;
  198. if (s[n] == ']') {
  199. endTag = TRUE;
  200. s[n] = '\0';
  201. }
  202. if (!_ftcsicmp(s,tag)) return(TRUE);
  203. }
  204. }
  205. }
  206. if (!feof(file)) {
  207. currentLine = line;
  208. makeError(0,CANT_READ_FILE);
  209. }
  210. return(FALSE);
  211. }
  212. #if defined(DOS)
  213. /*** SearchFileInEnv -- search for a file in the environment variable ************
  214. *
  215. * Scope:
  216. * Local
  217. *
  218. * Purpose:
  219. * Given a filename, an extension, and an environment variable, construct
  220. * The pathname, and search for that pathname in the current directory, then
  221. * in each directories specified in the environment variable. If found, copy
  222. * the full pathname to the resulting string, and return a pointer to the
  223. * beginning of the extension.
  224. *
  225. * Input:
  226. * pszName -- The name (without extension) of the file to search for
  227. * pszExtension -- The extension to append to the filename
  228. * pszEnvVar -- The name of the env var (PATH, INCLUDE, ...)
  229. * pszResultPath -- The fully qualified name when found.
  230. *
  231. * Output:
  232. * Return a pointer to the extension (the dot to be precise) in the buffer
  233. * if found, NULL if not.
  234. *
  235. * Errors/Warnings:
  236. *
  237. * Assumes:
  238. * - Assumes that filenames are not quoted.
  239. *
  240. * Modifies Globals:
  241. * None.
  242. *
  243. * Uses Globals:
  244. * None.
  245. *
  246. * Notes:
  247. *
  248. * History:
  249. * 10-May-1993 HV Revise SearchFileInEnv to take care of cases when path
  250. * characters (\,/,:) are used. This fixed the recursive
  251. * problem.
  252. *
  253. *******************************************************************************/
  254. LOCAL char * NEAR
  255. SearchFileInEnv(
  256. const char * pszName,
  257. const char * pszExtension,
  258. const char * pszEnvVar,
  259. char * pszResultPath)
  260. {
  261. char szNameAndExtension[_MAX_PATH];
  262. _makepath(szNameAndExtension, NULL, NULL, pszName, pszExtension);
  263. _searchenv(szNameAndExtension, pszEnvVar, pszResultPath);
  264. if (*pszResultPath && _ftcspbrk(pszName, "/\\:"))
  265. _ftcscpy(pszResultPath, szNameAndExtension);
  266. return (_ftcsrchr(pszResultPath, '.'));
  267. }
  268. /*** SearchRunPath -- search PATH list for foo.COM, foo.EXE, foo.BAT ***********
  269. *
  270. * Scope:
  271. * Global.
  272. *
  273. * Purpose:
  274. * Given a program name FOO with no extention:
  275. * Look for FOO.COM, FOO.EXE, and FOO.BAT as given. If not found
  276. * and FOO does not contain any path separators, search the path
  277. * looking for FOO.COM, FOO.EXE, and FOO.BAT in each directory.
  278. * Order of search is as given.
  279. * Only called in real mode; else CMD does the searching.
  280. *
  281. * Input:
  282. * pszFilename -- The program name to search for.
  283. * pszResultPath -- The fully qualified name when found.
  284. *
  285. * Output:
  286. * Return a pointer to the extension (the dot to be precise) in the buffer
  287. * if found, NULL if not.
  288. *
  289. * Errors/Warnings:
  290. *
  291. * Assumes:
  292. *
  293. * Modifies Globals:
  294. * None.
  295. *
  296. * Uses Globals:
  297. * None.
  298. *
  299. * Notes:
  300. *
  301. * History:
  302. * 22-Apr-1993 HV Rewrite SearchRunPath() to use _makepath(), _searchenv()
  303. *
  304. *******************************************************************************/
  305. char * NEAR
  306. SearchRunPath(const char *pszFilename, char *pszResultPath)
  307. {
  308. const char * pszDOSPathVar = "PATH";
  309. char * pszExtensionStart;
  310. // Search .com file
  311. pszExtensionStart = SearchFileInEnv(pszFilename, ".com", pszDOSPathVar, pszResultPath);
  312. if (NULL != pszExtensionStart)
  313. return (pszExtensionStart);
  314. // Search .exe file
  315. pszExtensionStart = SearchFileInEnv(pszFilename, ".exe", pszDOSPathVar, pszResultPath);
  316. if (NULL != pszExtensionStart)
  317. return (pszExtensionStart);
  318. // Search .bat file
  319. pszExtensionStart = SearchFileInEnv(pszFilename, ".bat", pszDOSPathVar, pszResultPath);
  320. if (NULL != pszExtensionStart)
  321. return (pszExtensionStart);
  322. // Still not found, we're out of luck.
  323. return NULL;
  324. }
  325. #endif