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.

182 lines
5.0 KiB

  1. /* trans.c - transliterate one file into another
  2. *
  3. * Modifications:
  4. * 30-Apr-1987 mz Use fmove ()
  5. * 13-May-1987 mz Check for buffer overflow
  6. * Use stderr for error output
  7. * 14-May-1987 bw Fix stack overflow on fREMatch call
  8. * Make stdin/stdout O_BINARY when used
  9. * Use return message from fmove()
  10. * Send debug output to stderr
  11. * 01-Mar-1988 mz Add parameter to RECompile for Z syntax
  12. * 15-Sep-1988 bw fREMatch became REMatch
  13. *
  14. */
  15. #include <malloc.h>
  16. #include <stdio.h>
  17. #include <fcntl.h>
  18. #include <io.h>
  19. #include <string.h>
  20. #include <process.h>
  21. #include <windows.h>
  22. #include <tools.h>
  23. #include <remi.h>
  24. #define BUFFERSIZE 512
  25. #define MAXRESTACK 1024
  26. flagType fCase = FALSE; /* TRUE => case is significant */
  27. flagType fTrans = FALSE; /* TRUE => transform file */
  28. flagType fDebug = FALSE;
  29. // Forward Function Declarations...
  30. void usage( void );
  31. void fatal( char * );
  32. flagType fDoTrans( FILE *, FILE *, char * );
  33. extern flagType RETranslate( struct patType *, char *, char * );
  34. extern int RETranslateLength( struct patType *, char * );
  35. struct patType *pbuf;
  36. RE_OPCODE * REStack[MAXRESTACK];
  37. void usage ()
  38. {
  39. fatal ("Usage: trans [/c] [/t] [files]\n");
  40. }
  41. void fatal( p1 )
  42. char *p1;
  43. {
  44. fprintf (stderr, p1 );
  45. exit (1);
  46. }
  47. flagType fDoTrans (fhin, fhout, rbuf)
  48. FILE *fhin, *fhout;
  49. char *rbuf;
  50. {
  51. flagType fChanged;
  52. static char buf[BUFFERSIZE], rpl[BUFFERSIZE];
  53. char * p, * np;
  54. int line = 0;
  55. fChanged = FALSE;
  56. if (fDebug)
  57. fprintf (stderr, "Replacement '%s'\n", rbuf);
  58. while (fgetl (buf, BUFFERSIZE, fhin) != 0) {
  59. line++;
  60. p = buf;
  61. if (fDebug)
  62. fprintf (stderr, "Input: '%s'\n", buf);
  63. while (!REMatch (pbuf, buf, p, REStack, MAXRESTACK, TRUE)) {
  64. fChanged = TRUE;
  65. if (fDebug)
  66. fprintf (stderr, " Match at %x length %x\n",
  67. REStart (pbuf)-buf,
  68. RELength (pbuf, 0));
  69. /* Make sure translation will fit in temp buffers
  70. */
  71. if (RETranslateLength (pbuf, rbuf) >= BUFFERSIZE) {
  72. fprintf (stderr, "After translation, line %d too long", line);
  73. exit (1);
  74. }
  75. if (!RETranslate (pbuf, rbuf, rpl))
  76. fatal ("Invalid replacement pattern\n");
  77. if (fDebug)
  78. fprintf (stderr, " Replacement: '%s'\n", rpl);
  79. /* Make sure body - match + translation still fits in buffer
  80. */
  81. if (strlen (buf) - RELength (pbuf, 0) + strlen (rpl) >= BUFFERSIZE) {
  82. fprintf (stderr, "After translation, line %d too long", line);
  83. exit (1);
  84. }
  85. np = (p = REStart (pbuf)) + strlen (rpl);
  86. strcat (rpl, p + RELength (pbuf, 0));
  87. strcpy (p, rpl);
  88. p = np;
  89. if (fDebug)
  90. fprintf (stderr, " Match start %x in '%s'\n", p-buf, buf);
  91. }
  92. if (!fTrans || p != buf) {
  93. if (fDebug)
  94. fprintf (stderr, " Outputting '%s'\n", buf);
  95. fputl (buf, strlen(buf), fhout);
  96. }
  97. }
  98. return fChanged;
  99. }
  100. __cdecl
  101. main (
  102. int c,
  103. char *v[]
  104. )
  105. {
  106. FILE *fhin, *fhout;
  107. char *p, *p1;
  108. flagType fChanged;
  109. ConvertAppToOem( c, v );
  110. if (c < 3)
  111. usage ();
  112. while (fSwitChr (*v[1])) {
  113. switch (v[1][1]) {
  114. case 'c':
  115. fCase = TRUE;
  116. break;
  117. case 'd':
  118. fDebug = TRUE;
  119. break;
  120. case 't':
  121. fTrans = TRUE;
  122. break;
  123. default:
  124. usage ();
  125. }
  126. SHIFT(c,v);
  127. }
  128. if ((pbuf = RECompile (v[1], fCase, TRUE)) == NULL)
  129. fatal ("Invalid pattern\n");
  130. p = v[2];
  131. if (c == 3) {
  132. _setmode(0, O_BINARY);
  133. _setmode(1, O_BINARY);
  134. fDoTrans (stdin, stdout, p);
  135. }
  136. else
  137. while (c != 3) {
  138. if ((fhin = fopen (v[3], "rb")) == NULL)
  139. fprintf (stderr, "trans: Cannot open %s - %s\n", v[3], error ());
  140. else
  141. if ((fhout = fopen ("trans$$$.$$$", "wb")) == NULL) {
  142. fprintf (stderr, "trans: Cannot create temp file - %s\n", error ());
  143. fclose (fhin);
  144. }
  145. else {
  146. printf ("%s ", v[3]);
  147. fChanged = fDoTrans (fhin, fhout, p);
  148. fclose (fhin);
  149. fclose (fhout);
  150. if (fChanged) {
  151. if (p1 = fmove ("trans$$$.$$$", v[3]))
  152. printf ("[%s]\n", p1);
  153. else
  154. printf ("[OK]\n");
  155. }
  156. else {
  157. _unlink ("trans$$$.$$$");
  158. printf ("[No change]\n");
  159. }
  160. }
  161. SHIFT(c,v);
  162. }
  163. return( 0 );
  164. }