Source code of Windows XP (NT5)
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.

310 lines
8.5 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. /* File - SYSINICM.C */
  4. extern HWND hwndFrame;
  5. void APIENTRY ConstructSysIniLine(SZ, SZ, SZ);
  6. SZ APIENTRY SzNextConfigLine(SZ);
  7. SZ APIENTRY SzSysIniEdit(SZ, SZ, SZ, SZ);
  8. BOOL APIENTRY FCreateSysIniKeyValue(SZ, SZ, SZ, SZ, CMO);
  9. SZ APIENTRY SzCopyBuf(SZ, SZ, int);
  10. int APIENTRY Linelenl(SZ);
  11. #define ISWHITE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
  12. #define UP_CASE(c) (CHP)(LOBYTE(LOWORD((DWORD)(AnsiUpper((LPSTR)MAKELONG((WORD)(c),0))))))
  13. #define MAX_INF_LINE_LEN (150)
  14. #define ISEOL(c) ((c) == '\n' || (c) == '\r')
  15. #define _CR 0x0D
  16. #define LF 0x0A
  17. /* void ConstructSysIniLine(char*, char*, char*);
  18. *
  19. * ENTRY: szSysIniLine, Address of a buffer large enough to hold the
  20. * constructed system.ini line.
  21. * ProfString, The profile string to be used in constructing
  22. * the system.ini line.
  23. * NameString, The name of the driver, exe, or any string to
  24. * be placed after the profile string in the new
  25. * system.ini line.
  26. *
  27. * EXIT : No exit value, newly constructed system.ini line will reside
  28. * in buffer pointed to by szSysIniLine.
  29. *
  30. ***********************************************************************/
  31. void APIENTRY ConstructSysIniLine(szSysIniLine, szProfString, szNameString)
  32. SZ szSysIniLine;
  33. SZ szProfString;
  34. SZ szNameString;
  35. {
  36. strcpy(szSysIniLine, szProfString);
  37. SzStrCat(szSysIniLine, "=");
  38. if (*(szNameString + 1) == ':')
  39. SzStrCat(szSysIniLine, (szNameString + 2));
  40. else
  41. SzStrCat(szSysIniLine, szNameString);
  42. }
  43. /* LPSTR fnNextConfigLine(LPSTR lpFileOffset);
  44. *
  45. * Advances Far pointer into config.sys files to the first non-white char
  46. * of the next line in the buffer containing the file. Will return null
  47. * in the EOF case.
  48. *
  49. * ENTRY: Far pointer into buffer holding file.
  50. *
  51. * EXIT: Far pointer into buffer holding file. NULL on EOF.
  52. *
  53. ***********************************************************************/
  54. SZ APIENTRY SzNextConfigLine(szFileOffset)
  55. SZ szFileOffset;
  56. {
  57. while (!ISEOL(*szFileOffset))
  58. szFileOffset = SzNextChar(szFileOffset);
  59. while (ISWHITE(*szFileOffset) && *szFileOffset != 0)
  60. szFileOffset = SzNextChar(szFileOffset);
  61. return(szFileOffset);
  62. }
  63. /* BOOL fnSysIniEdit(PSTR szSection,PSTR szSysIniLine);
  64. *
  65. * This function will either add or remove an entire line from the
  66. * system.ini file.
  67. *
  68. * ENTRY: szSection, section string, [boot], [system], or [win386]
  69. * specifies section from which line will be added to. Ignored
  70. * when removing an entry.
  71. *
  72. * szSysIniLine, buffer containing line to be added to system.ini
  73. * file. In the case of RETURN, this buffer will return the line
  74. * from system.ini
  75. *
  76. * EXIT: Returns bool value as to success or failure of function.
  77. *
  78. ***********************************************************************/
  79. SZ APIENTRY SzSysIniEdit(szFile, szSection, szSysIniLine, szIniBuf)
  80. SZ szFile;
  81. SZ szSection;
  82. SZ szSysIniLine;
  83. SZ szIniBuf;
  84. {
  85. BOOL fResult;
  86. SZ szFileOffset;
  87. SZ szTo;
  88. SZ szNewHead;
  89. szFileOffset = szIniBuf;
  90. while (ISWHITE(*szFileOffset) && *szFileOffset != '\0')
  91. szFileOffset = SzNextChar(szFileOffset);
  92. if (*szFileOffset == '\0')
  93. /* REVIEW should create it */
  94. {
  95. EvalAssert(EercErrorHandler(hwndFrame, grcMissingSysIniSectionErr,
  96. fTrue, szSection, szFile, 0) == eercAbort);
  97. return(NULL);
  98. }
  99. while ((szTo = (SZ)SAlloc(strlen(szIniBuf) + strlen(szSysIniLine)
  100. + 3)) == (SZ)NULL)
  101. if (!FHandleOOM(hwndFrame))
  102. return(NULL);
  103. szNewHead = szTo;
  104. fResult = fFalse;
  105. while (*szFileOffset != 0 && fResult == fFalse)
  106. {
  107. if (!strncmp(szSection, SzNextChar(szFileOffset),strlen(szSection)))
  108. {
  109. szTo = SzCopyBuf(szFileOffset, szTo, Linelenl(szFileOffset));
  110. szTo = SzCopyBuf(szSysIniLine, szTo, strlen(szSysIniLine));
  111. szFileOffset = SzNextConfigLine(szFileOffset);
  112. *szTo = _CR;
  113. szTo = SzNextChar(szTo);
  114. *szTo = LF;
  115. szTo = SzNextChar(szTo);
  116. SzCopyBuf(szFileOffset, szTo, 0Xffff);
  117. SFree(szIniBuf);
  118. szIniBuf = szNewHead;
  119. fResult = fTrue;
  120. continue;
  121. }
  122. szTo = SzCopyBuf(szFileOffset, szTo, Linelenl(szFileOffset));
  123. szFileOffset = SzNextConfigLine(szFileOffset);
  124. }
  125. if (fResult)
  126. return(szIniBuf);
  127. else
  128. {
  129. /* REVIEW should create it */
  130. EvalAssert(EercErrorHandler(hwndFrame, grcMissingSysIniSectionErr,
  131. fTrue, szSection, szFile, 0) == eercAbort);
  132. return(NULL);
  133. }
  134. }
  135. /*
  136. ** Purpose:
  137. ** Arguments:
  138. ** szFile: a zero terminated string containing the file name for the
  139. ** ini file.
  140. ** szSect: a zero terminated string containing the name of the section
  141. ** in which the key/value pair will be created.
  142. ** szKey: the name of the key to be created in the section szSect.
  143. ** szValue: the name of the value to be created in conjunction with the
  144. ** key szKey in the section szSect.
  145. ** cmo: valid command options:
  146. ** cmoNone: no effect
  147. ** cmoOverwrite: causes the key/value pair to be removed if it
  148. ** already exists before creating the key.
  149. ** cmoVital: causes the Vital command handler to be called
  150. ** if the function fails for any reason.
  151. ** Returns:
  152. ** fTrue if the function succeeds, fFalse otherwise.
  153. **
  154. ****************************************************************************/
  155. BOOL APIENTRY FCreateSysIniKeyValue(SZ szFile,
  156. SZ szSect,
  157. SZ szKey,
  158. SZ szValue,
  159. CMO cmo)
  160. {
  161. struct _stat FileStatus;
  162. CHL szSysIniLine[MAX_INF_LINE_LEN];
  163. PFH pfh;
  164. SZ szIniBuf;
  165. BOOL fVital = cmo & cmoVital;
  166. EERC eerc;
  167. while ((pfh = PfhOpenFile(szFile, ofmRead)) == (PFH)NULL)
  168. if ((eerc = EercErrorHandler(hwndFrame, grcOpenFileErr, fVital, szFile,
  169. 0, 0)) != eercAbort)
  170. return(eerc == eercIgnore);
  171. while (_fstat(pfh->iDosfh, &FileStatus))
  172. if ((eerc = EercErrorHandler(hwndFrame, grcReadFileErr, fVital, szFile,
  173. 0, 0)) != eercAbort)
  174. {
  175. EvalAssert(FCloseFile(pfh));
  176. return(eerc == eercIgnore);
  177. }
  178. while ((szIniBuf = (SZ)SAlloc((CB)FileStatus.st_size + 1)) == (SZ)NULL)
  179. if (!FHandleOOM(hwndFrame))
  180. {
  181. EvalAssert(FCloseFile(pfh));
  182. return(!fVital);
  183. }
  184. Assert((CB)(FileStatus.st_size) < (CB)(-1));
  185. if ((CB)(FileStatus.st_size) != CbReadFile(pfh, szIniBuf,
  186. (CB)(FileStatus.st_size)))
  187. {
  188. EvalAssert(EercErrorHandler(hwndFrame, grcReadFileErr, fVital, szFile,
  189. 0, 0) == eercAbort);
  190. SFree(szIniBuf);
  191. EvalAssert(FCloseFile(pfh));
  192. return(!fVital);
  193. }
  194. *(szIniBuf + FileStatus.st_size) = '\0';
  195. EvalAssert(FCloseFile(pfh));
  196. ConstructSysIniLine((SZ)szSysIniLine, szKey, szValue);
  197. if ((szIniBuf = SzSysIniEdit(szFile, szSect, szSysIniLine, szIniBuf)) ==
  198. (SZ)NULL)
  199. {
  200. SFree(szIniBuf);
  201. return(!fVital);
  202. }
  203. while (!(pfh = PfhOpenFile(szFile, ofmCreate)))
  204. if ((eerc = EercErrorHandler(hwndFrame, grcOpenFileErr, fVital, szFile,
  205. 0, 0)) != eercAbort)
  206. {
  207. SFree(szIniBuf);
  208. return(eerc == eercIgnore);
  209. }
  210. if (strlen(szIniBuf) != CbWriteFile(pfh, szIniBuf,
  211. strlen(szIniBuf)))
  212. {
  213. EvalAssert(FCloseFile(pfh));
  214. SFree(szIniBuf);
  215. EvalAssert(EercErrorHandler(hwndFrame, grcWriteFileErr, fVital, szFile,
  216. 0, 0) == eercAbort);
  217. return(!fVital);
  218. }
  219. EvalAssert(FCloseFile(pfh));
  220. SFree(szIniBuf);
  221. return(fTrue);
  222. }
  223. /* LPSTR NEAR PASCAL fnCopyBuff(LPSTR pFrom, LPSTR pTo);
  224. *
  225. * Function moves the remaining contents of a text buffer from one location
  226. * within the buffer to a new location within the buffer. This is used to
  227. * either remove or make room for an entry in the file buffer.
  228. *
  229. * ENTRY: pointers To and From designate where the remaining protion of the
  230. * buffer will be moved to. The new buffer will be NULL terminated.
  231. *
  232. * EXIT: Returns pointer to next available char position in the buffer.
  233. *
  234. ***********************************************************************/
  235. SZ APIENTRY SzCopyBuf(szFrom,szTo,iCnt)
  236. SZ szFrom;
  237. SZ szTo;
  238. int iCnt;
  239. {
  240. /* REVIEW won't work for DBCS */
  241. while (*szFrom != 0 && iCnt-- != 0)
  242. {
  243. *szTo = *szFrom;
  244. szTo = SzNextChar(szTo);
  245. szFrom = SzNextChar(szFrom);
  246. }
  247. if (*szFrom == '\0')
  248. *szTo = '\0';
  249. return(szTo);
  250. }
  251. /* int fnLinelenl(LPSTR)
  252. *
  253. * Returns length of buffer line up to and including the LF char.
  254. * (far pointer version).
  255. *
  256. * ENTRY: LPSTR to buffer
  257. * EXIT: length of line.
  258. *
  259. ***********************************************************************/
  260. int APIENTRY Linelenl(szBuf)
  261. SZ szBuf;
  262. {
  263. unsigned i = 1;
  264. while (*szBuf != LF)
  265. {
  266. szBuf = SzNextChar(szBuf);
  267. i++;
  268. }
  269. return(i);
  270. }