sedutil.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. sedutil.c
  5. Abstract:
  6. This module implements utility functions for the sed utility.
  7. Author:
  8. Evan Green 11-Jul-2013
  9. Environment:
  10. POSIX
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include "sed.h"
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "../swlib.h"
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. #define SED_READ_BLOCK_SIZE 1024
  26. //
  27. // ------------------------------------------------------ Data Type Definitions
  28. //
  29. //
  30. // ----------------------------------------------- Internal Function Prototypes
  31. //
  32. //
  33. // -------------------------------------------------------------------- Globals
  34. //
  35. //
  36. // ------------------------------------------------------------------ Functions
  37. //
  38. PSED_STRING
  39. SedReadFileIn (
  40. PSTR Path,
  41. BOOL MustSucceed
  42. )
  43. /*++
  44. Routine Description:
  45. This routine reads a file into a null terminated sed string.
  46. Arguments:
  47. Path - Supplies a pointer to a string containing the path of the file to
  48. read in.
  49. MustSucceed - Supplies a boolean indicating whether or not the open and
  50. read calls must succeed, or should return a partial or empty string
  51. on failure.
  52. Return Value:
  53. Returns a pointer to the string containing the contents of the file on
  54. success.
  55. NULL on failure.
  56. --*/
  57. {
  58. PCHAR Buffer;
  59. ssize_t BytesRead;
  60. FILE *File;
  61. BOOL Result;
  62. PSED_STRING String;
  63. Buffer = NULL;
  64. File = NULL;
  65. Result = FALSE;
  66. String = SedCreateString(NULL, 0, TRUE);
  67. if (String == NULL) {
  68. goto ReadFileInEnd;
  69. }
  70. Buffer = malloc(SED_READ_BLOCK_SIZE);
  71. if (Buffer == NULL) {
  72. goto ReadFileInEnd;
  73. }
  74. File = fopen(Path, "r");
  75. if (File == NULL) {
  76. if (MustSucceed == FALSE) {
  77. Result = TRUE;
  78. }
  79. goto ReadFileInEnd;
  80. }
  81. while (TRUE) {
  82. do {
  83. BytesRead = fread(Buffer, 1, SED_READ_BLOCK_SIZE, File);
  84. } while ((BytesRead == 0) && (errno == EINTR));
  85. if (BytesRead == 0) {
  86. if (ferror(File) != 0) {
  87. Result = FALSE;
  88. } else if (feof(File) == 0) {
  89. Result = FALSE;
  90. } else {
  91. Result = TRUE;
  92. }
  93. break;
  94. }
  95. Result = SedAppendString(String, Buffer, BytesRead);
  96. if (Result == FALSE) {
  97. goto ReadFileInEnd;
  98. }
  99. }
  100. Result = TRUE;
  101. ReadFileInEnd:
  102. if (File != NULL) {
  103. fclose(File);
  104. }
  105. if (Buffer != NULL) {
  106. free(Buffer);
  107. }
  108. if (Result == FALSE) {
  109. if (String == NULL) {
  110. SedDestroyString(String);
  111. String = NULL;
  112. }
  113. }
  114. return String;
  115. }
  116. PSED_STRING
  117. SedCreateString (
  118. PSTR Data,
  119. UINTN Size,
  120. BOOL NullTerminate
  121. )
  122. /*++
  123. Routine Description:
  124. This routine creates a sed string.
  125. Arguments:
  126. Data - Supplies an optional pointer to the initial data. This data will be
  127. copied.
  128. Size - Supplies the size of the initial data. Supply 0 if no data was
  129. supplied.
  130. NullTerminate - Supplies a boolean indicating if the string should be
  131. null terminated if it is not already.
  132. Return Value:
  133. Returns a pointer to the allocated string structure on success. The caller
  134. is responsible for destroying this string structure.
  135. NULL on allocation failure.
  136. --*/
  137. {
  138. UINTN Capacity;
  139. UINTN NeededSize;
  140. BOOL Result;
  141. PSED_STRING String;
  142. Result = FALSE;
  143. String = malloc(sizeof(SED_STRING));
  144. if (String == NULL) {
  145. goto CreateStringEnd;
  146. }
  147. memset(String, 0, sizeof(SED_STRING));
  148. if ((NullTerminate != FALSE) && (Size == 0)) {
  149. String->Data = malloc(SED_INITIAL_STRING_SIZE);
  150. if (String->Data == NULL) {
  151. goto CreateStringEnd;
  152. }
  153. String->Capacity = SED_INITIAL_STRING_SIZE;
  154. String->Size = 1;
  155. String->Data[0] = '\0';
  156. } else if ((Data != NULL) && (Size != 0)) {
  157. NeededSize = Size;
  158. if ((NullTerminate != FALSE) && (Data[Size - 1] != '\0')) {
  159. NeededSize += 1;
  160. }
  161. Capacity = SED_INITIAL_STRING_SIZE;
  162. while (Capacity < NeededSize) {
  163. Capacity *= 2;
  164. }
  165. String->Data = malloc(Capacity);
  166. if (String->Data == NULL) {
  167. goto CreateStringEnd;
  168. }
  169. memcpy(String->Data, Data, Size);
  170. if ((NullTerminate != FALSE) && (Data[Size - 1] != '\0')) {
  171. String->Data[Size] = '\0';
  172. }
  173. String->Size = NeededSize;
  174. String->Capacity = Capacity;
  175. }
  176. Result = TRUE;
  177. CreateStringEnd:
  178. if (Result == FALSE) {
  179. if (String != NULL) {
  180. if (String->Data != NULL) {
  181. free(String->Data);
  182. }
  183. free(String);
  184. String = NULL;
  185. }
  186. }
  187. return String;
  188. }
  189. BOOL
  190. SedAppendString (
  191. PSED_STRING String,
  192. PSTR Data,
  193. UINTN Size
  194. )
  195. /*++
  196. Routine Description:
  197. This routine appends a string of characters to the given string. If the
  198. original string was null terminated, the resulting string will also be
  199. null terminated on success.
  200. Arguments:
  201. String - Supplies a pointer to the string to append to.
  202. Data - Supplies a pointer to the bytes to append.
  203. Size - Supplies the number of bytes to append.
  204. Return Value:
  205. TRUE on success.
  206. FALSE on failure.
  207. --*/
  208. {
  209. UINTN Index;
  210. BOOL NullTerminated;
  211. BOOL Result;
  212. NullTerminated = FALSE;
  213. if ((String->Size != 0) && (String->Data[String->Size - 1] == '\0')) {
  214. NullTerminated = TRUE;
  215. String->Size -= 1;
  216. }
  217. //
  218. // Reallocate the buffer if needed.
  219. //
  220. if (String->Size + Size + 1 >= String->Capacity) {
  221. if (String->Capacity == 0) {
  222. String->Capacity = SED_INITIAL_STRING_SIZE;
  223. }
  224. while (String->Size + Size >= String->Capacity) {
  225. String->Capacity *= 2;
  226. }
  227. String->Data = realloc(String->Data, String->Capacity);
  228. if (String->Data == NULL) {
  229. String->Size = 0;
  230. String->Capacity = 0;
  231. Result = FALSE;
  232. goto AppendStringEnd;
  233. }
  234. }
  235. for (Index = 0; Index < Size; Index += 1) {
  236. String->Data[String->Size] = Data[Index];
  237. String->Size += 1;
  238. }
  239. if (NullTerminated != FALSE) {
  240. if (String->Data[String->Size - 1] != '\0') {
  241. String->Data[String->Size] = '\0';
  242. String->Size += 1;
  243. }
  244. }
  245. Result = TRUE;
  246. AppendStringEnd:
  247. return Result;
  248. }
  249. VOID
  250. SedDestroyString (
  251. PSED_STRING String
  252. )
  253. /*++
  254. Routine Description:
  255. This routine destroys a sed string structure.
  256. Arguments:
  257. String - Supplies a pointer to the string to destroy.
  258. Return Value:
  259. None.
  260. --*/
  261. {
  262. if (String->Data != NULL) {
  263. free(String->Data);
  264. }
  265. String->Data = NULL;
  266. String->Capacity = 0;
  267. String->Size = 0;
  268. free(String);
  269. return;
  270. }
  271. INT
  272. SedOpenWriteFile (
  273. PSED_CONTEXT Context,
  274. PSED_STRING Path,
  275. PSED_WRITE_FILE *WriteFile
  276. )
  277. /*++
  278. Routine Description:
  279. This routine opens up a write file, sharing descriptors between
  280. duplicate write file names.
  281. Arguments:
  282. Context - Supplies a pointer to the application context.
  283. Path - Supplies a pointer to the string containing the path of the write
  284. file.
  285. WriteFile - Supplies a pointer where the pointer to the write file will
  286. be returned on success.
  287. Return Value:
  288. 0 on success.
  289. Error code on failure.
  290. --*/
  291. {
  292. PLIST_ENTRY CurrentEntry;
  293. PSED_WRITE_FILE CurrentFile;
  294. PSED_WRITE_FILE NewFile;
  295. INT Result;
  296. *WriteFile = NULL;
  297. //
  298. // Look to see if this file is already opened, and return it if so.
  299. //
  300. CurrentEntry = Context->WriteFileList.Next;
  301. while (CurrentEntry != &(Context->WriteFileList)) {
  302. CurrentFile = LIST_VALUE(CurrentEntry, SED_WRITE_FILE, ListEntry);
  303. CurrentEntry = CurrentEntry->Next;
  304. if (strcmp(CurrentFile->Name->Data, Path->Data) == 0) {
  305. *WriteFile = CurrentFile;
  306. return 0;
  307. }
  308. }
  309. //
  310. // Allocate a new structure.
  311. //
  312. NewFile = malloc(sizeof(SED_WRITE_FILE));
  313. if (NewFile == NULL) {
  314. Result = ENOMEM;
  315. goto OpenWriteFileEnd;
  316. }
  317. memset(NewFile, 0, sizeof(SED_WRITE_FILE));
  318. NewFile->LineTerminated = TRUE;
  319. NewFile->Name = SedCreateString(Path->Data, Path->Size, TRUE);
  320. if (NewFile->Name == NULL) {
  321. Result = ENOMEM;
  322. goto OpenWriteFileEnd;
  323. }
  324. //
  325. // Open up the write file.
  326. //
  327. NewFile->File = fopen(NewFile->Name->Data, "w");
  328. if (NewFile->File == NULL) {
  329. Result = errno;
  330. SwPrintError(Result, NewFile->Name->Data, "Unable to open write file");
  331. goto OpenWriteFileEnd;
  332. }
  333. //
  334. // Add it to the global list.
  335. //
  336. INSERT_BEFORE(&(NewFile->ListEntry), &(Context->WriteFileList));
  337. Result = 0;
  338. OpenWriteFileEnd:
  339. if (Result != 0) {
  340. if (NewFile != NULL) {
  341. if (NewFile->File != NULL) {
  342. fclose(NewFile->File);
  343. }
  344. if (NewFile->Name != NULL) {
  345. SedDestroyString(NewFile->Name);
  346. }
  347. free(NewFile);
  348. NewFile = NULL;
  349. }
  350. }
  351. *WriteFile = NewFile;
  352. return Result;
  353. }
  354. INT
  355. SedPrint (
  356. PSED_CONTEXT Context,
  357. PSTR String,
  358. INT LineTerminator
  359. )
  360. /*++
  361. Routine Description:
  362. This routine prints a null terminated string to standard out.
  363. Arguments:
  364. Context - Supplies a pointer to the application context.
  365. String - Supplies the null terminated string to print.
  366. LineTerminator - Supplies the character that terminates this line. If this
  367. is EOF, then that tells this routine the line is not terminated.
  368. Return Value:
  369. 0 on success.
  370. Non-zero error code on failure.
  371. --*/
  372. {
  373. size_t Size;
  374. Size = strlen(String);
  375. return SedWrite(&(Context->StandardOut), String, Size, LineTerminator);
  376. }
  377. INT
  378. SedWrite (
  379. PSED_WRITE_FILE WriteFile,
  380. PVOID Buffer,
  381. UINTN Size,
  382. INT LineTerminator
  383. )
  384. /*++
  385. Routine Description:
  386. This routine write the given buffer out to the given file descriptor.
  387. Arguments:
  388. WriteFile - Supplies a pointer to the file to write to.
  389. Buffer - Supplies the buffer to write.
  390. Size - Supplies the number of characters in the buffer.
  391. LineTerminator - Supplies the character that terminates this line. If this
  392. is EOF, then that tells this routine the line is not terminated.
  393. Return Value:
  394. 0 on success.
  395. Non-zero error code on failure.
  396. --*/
  397. {
  398. size_t BytesThisRound;
  399. ssize_t BytesWritten;
  400. INT Result;
  401. UINTN TotalBytesWritten;
  402. assert(WriteFile->File != NULL);
  403. //
  404. // If the previous line written wasn't terminated, terminate it now.
  405. //
  406. if (WriteFile->LineTerminated == FALSE) {
  407. fputc('\n', WriteFile->File);
  408. WriteFile->LineTerminated = TRUE;
  409. }
  410. //
  411. // Writing anything resets the terminator status.
  412. //
  413. if (Size != 0) {
  414. WriteFile->LineTerminated = FALSE;
  415. }
  416. //
  417. // Write the stuff.
  418. //
  419. Result = 0;
  420. TotalBytesWritten = 0;
  421. while (TotalBytesWritten < Size) {
  422. if (Size - TotalBytesWritten > MAX_LONG) {
  423. BytesThisRound = MAX_LONG;
  424. } else {
  425. BytesThisRound = Size - TotalBytesWritten;
  426. }
  427. do {
  428. BytesWritten = fwrite(Buffer + TotalBytesWritten,
  429. 1,
  430. BytesThisRound,
  431. WriteFile->File);
  432. } while ((BytesWritten <= 0) && (errno == EINTR));
  433. if (BytesWritten <= 0) {
  434. Result = errno;
  435. SwPrintError(Result, NULL, "Could not write to file");
  436. return Result;
  437. }
  438. TotalBytesWritten += BytesWritten;
  439. }
  440. //
  441. // If there is a terminating character, write it out. But only mark the
  442. // line as terminated if it's a newline so if anything else comes in a
  443. // newline will get written.
  444. //
  445. if (LineTerminator != EOF) {
  446. fputc(LineTerminator, WriteFile->File);
  447. if (LineTerminator == '\n') {
  448. WriteFile->LineTerminated = TRUE;
  449. }
  450. }
  451. return 0;
  452. }