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.

398 lines
12 KiB

  1. /*****************************************************************************
  2. *
  3. * m4.h
  4. *
  5. *****************************************************************************/
  6. #ifdef POSIX
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <fcntl.h>
  10. #include <limits.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. typedef char TCHAR, *PTCH;
  14. typedef const char *PCSTR;
  15. typedef unsigned char TBYTE, BYTE, *PBYTE;
  16. typedef unsigned int UINT;
  17. typedef unsigned long DWORD;
  18. typedef int BOOL, HFILE;
  19. typedef void *PVOID;
  20. #define VOID void
  21. #define CONST const
  22. #define TEXT(lit) lit
  23. #define MAX_PATH PATH_MAX
  24. #define max(a,b) (((a) > (b)) ? (a) : (b))
  25. #define min(a,b) (((a) < (b)) ? (a) : (b))
  26. #define EOL TEXT("\n")
  27. #define cbEol 1
  28. #else
  29. #include <windows.h>
  30. #define EOL TEXT("\r\n")
  31. #define cbEol 2
  32. #endif
  33. #include <stddef.h> /* offsetof */
  34. /*****************************************************************************
  35. *
  36. * Dialectical variation
  37. *
  38. *****************************************************************************/
  39. #ifdef DBG
  40. #define DEBUG
  41. #endif
  42. /*****************************************************************************
  43. *
  44. * Baggage - Stuff I carry everywhere.
  45. *
  46. * Stuff that begin with underscores are bottom-level gizmos which tend
  47. * to get wrapped by functions with the same name.
  48. *
  49. *****************************************************************************/
  50. #if defined(_MSC_VER)
  51. #define STDCALL __stdcall
  52. #undef CDECL /* <windows.h> defines it wrong */
  53. #define CDECL __cdecl
  54. #define INLINE static __inline /* Inlines are always static */
  55. #define NORETURN
  56. #define PURE
  57. #define _pvAllocCb(cb) LocalAlloc(LMEM_FIXED, cb)
  58. #define _pvZAllocCb(cb) LocalAlloc(LMEM_FIXED + LMEM_ZEROINIT, cb)
  59. #define _pvReallocPvCb(pv, cb) LocalReAlloc(pv, cb, LMEM_MOVEABLE)
  60. #define _FreePv(pv) LocalFree(pv)
  61. #define PrintPtchPtchVa wvsprintf
  62. #define PrintPtchPtchV wsprintf
  63. #define exit ExitProcess
  64. #define strlen lstrlen
  65. #define strcmp lstrcmp
  66. #define bzero ZeroMemory
  67. #elif defined(__GNUC__)
  68. #define STDCALL
  69. #define CDECL
  70. #define INLINE static __inline__ /* Inlines are always static */
  71. #define NORETURN __NORETURN
  72. #define PURE __CONSTVALUE
  73. #define _pvAllocCb(cb) malloc(cb)
  74. #define _pvZAllocCb(cb) calloc(cb, 1)
  75. #define _pvReallocPvCb(pv, cb) realloc(pv, cb)
  76. #define _FreePv(pv) free(pv)
  77. #define PrintPtchPtchVa vsprintf
  78. #define PrintPtchPtchV sprintf
  79. #endif
  80. typedef TCHAR TCH, *PTSTR; /* More basic types */
  81. typedef UINT ITCH;
  82. typedef UINT CTCH;
  83. typedef UINT CB;
  84. typedef BOOL F;
  85. typedef PVOID PV;
  86. typedef CONST VOID *PCVOID;
  87. typedef CONST TCH *PCTCH, *PCTSTR;
  88. #define cbCtch(ctch) ((ctch) * sizeof(TCHAR))
  89. #define ctchCb(cb) ((cb) / sizeof(TCHAR))
  90. #define ctchMax ((CTCH)~0)
  91. #define CopyPtchPtchCtch(ptchDst, ptchSrc, ctch) \
  92. memcpy(ptchDst, ptchSrc, cbCtch(ctch))
  93. #define MovePtchPtchCtch(ptchDst, ptchSrc, ctch) \
  94. memmove(ptchDst, ptchSrc, cbCtch(ctch))
  95. #define fEqPtchPtchCtch(ptchDst, ptchSrc, ctch) \
  96. !memcmp(ptchDst, ptchSrc, cbCtch(ctch))
  97. #define pvSubPvCb(pv, cb) ((PV)((PBYTE)pv - (cb)))
  98. /*
  99. * Round cb up to the nearest multiple of cbAlign. cbAlign must be
  100. * a power of 2 whose evaluation entails no side-effects.
  101. */
  102. #define ROUNDUP(cb, cbAlign) ((((cb) + (cbAlign) - 1) / (cbAlign)) * (cbAlign))
  103. /*
  104. * Returns the number of elements in an array.
  105. */
  106. #define cA(a) (sizeof(a)/sizeof(a[0]))
  107. /*****************************************************************************
  108. *
  109. * assert.c
  110. *
  111. *****************************************************************************/
  112. void NORETURN CDECL Die(PCTSTR pszFormat, ...);
  113. int NORETURN STDCALL AssertPszPszLn(PCSTR pszExpr, PCSTR pszFile, int iLine);
  114. #ifdef DEBUG
  115. #define AssertFPsz(c, psz) ((c) ? 0 : AssertPszPszLn(psz, __FILE__, __LINE__))
  116. #define Validate(c) ((c) ? 0 : AssertPszPszLn(#c, __FILE__, __LINE__))
  117. #define D(x) x
  118. #else
  119. #define AssertFPsz(c, psz)
  120. #define Validate(c) (c)
  121. #define D(x)
  122. #endif
  123. #define Assert(c) AssertFPsz(c, #c)
  124. typedef unsigned long SIG; /* Signatures */
  125. #define sigABCD(a,b,c,d) ((a) + ((b)<<8) + ((c)<<16) + ((d)<<24))
  126. #define AssertPNm(p, nm) AssertFPsz((p)->sig == (sig##nm), "Assert"#nm)
  127. /*****************************************************************************
  128. *
  129. * tchMagic - Super-secret value used to signal out-of-band info
  130. *
  131. *****************************************************************************/
  132. #define tchMagic '\0' /* Out-of-band marker */
  133. #include "io.h" /* File I/O stuff */
  134. #include "m4ctype.h" /* Character types */
  135. #include "tok.h" /* Tokens */
  136. #include "mem.h" /* Memory and GC */
  137. #include "divert.h" /* Diversions */
  138. #include "stream.h" /* Files, streams */
  139. /*****************************************************************************
  140. *
  141. * A VAL records a macro's value, either the current value or a pushed
  142. * value.
  143. *
  144. * tok - text value (HeapAllocate'd)
  145. * fTrace - nonzero if this instance should be traced
  146. * pvalPrev - link to previous value
  147. *
  148. * A MACRO records an active macro.
  149. *
  150. * tokName - macro name (HeapAllocate'd)
  151. * pval - macro value
  152. *
  153. * A TSFL records the state of a token (token state flags).
  154. *
  155. *****************************************************************************/
  156. typedef struct VALUE VAL, *PVAL;
  157. struct VALUE {
  158. D(SIG sig;)
  159. TOK tok;
  160. BOOL fTrace;
  161. PVAL pvalPrev;
  162. };
  163. #define sigPval sigABCD('V', 'a', 'l', 'u')
  164. #define AssertPval(pval) AssertPNm(pval, Pval)
  165. typedef struct MACRO MAC, *PMAC, **PPMAC;
  166. struct MACRO {
  167. D(SIG sig;)
  168. PMAC pmacNext;
  169. TOK tokName;
  170. PVAL pval;
  171. };
  172. #define sigPmac sigABCD('M', 'a', 'c', 'r')
  173. #define AssertPmac(pmac) AssertPNm(pmac, Pmac)
  174. extern PPMAC mphashpmac;
  175. /*****************************************************************************
  176. *
  177. * operators
  178. *
  179. * Each operator is called as op(argv), where argv is the magic
  180. * cookie for accessing argument vector.
  181. *
  182. * To access the parameters, use the following macros:
  183. *
  184. * ctokArgv -- Number of arguments provided, not including $0.
  185. *
  186. * ptokArgv(i) -- Access the i'th parameter
  187. *
  188. * Note that it is safe to pass a pptok because the call stack does
  189. * not grow during macro expansion. Therefore, the token array
  190. * cannot get reallocated.
  191. *
  192. * For convenience, ptokArgv(ctokArgv+1) is always ptokNil.
  193. *
  194. *****************************************************************************/
  195. typedef PTOK ARGV; /* Argument vector cookie */
  196. #define ptokArgv(i) (&argv[i])
  197. #define ptchArgv(i) ptchPtok(ptokArgv(i))
  198. #define ctchArgv(i) ctchSPtok(ptokArgv(i))
  199. #define ctokArgv ((ITOK)ctchUPtok(ptokArgv(-1)))
  200. #define SetArgvCtok(ctok) SetPtokCtch(ptokArgv(-1), ctok)
  201. #define DeclareOp(op) void STDCALL op(ARGV argv)
  202. #define DeclareOpc(opc) void STDCALL opc(PTOK ptok, ITOK itok, DWORD dw)
  203. typedef void (STDCALL *OP)(ARGV argv);
  204. typedef void (STDCALL *OPC)(PTOK ptok, ITOK itok, DWORD dw);
  205. typedef void (STDCALL *MOP)(PMAC pmac);
  206. void STDCALL EachOpcArgvDw(OPC opc, ARGV argv, DWORD dw);
  207. void STDCALL EachReverseOpcArgvDw(OPC opc, ARGV argv, DWORD dw);
  208. void STDCALL EachMacroOp(MOP mop);
  209. extern OP rgop[];
  210. /*****************************************************************************
  211. *
  212. * hash.c - Hashing
  213. *
  214. *****************************************************************************/
  215. typedef unsigned long HASH;
  216. extern HASH g_hashMod;
  217. HASH STDCALL hashPtok(PCTOK ptok);
  218. void STDCALL InitHash(void);
  219. /*****************************************************************************
  220. *
  221. * obj.c - Basic object methods
  222. *
  223. *****************************************************************************/
  224. void STDCALL PopdefPmac(PMAC pmac);
  225. void STDCALL PushdefPmacPtok(PMAC pmac, PCTOK ptok);
  226. void STDCALL FreePmac(PMAC pmac);
  227. PMAC STDCALL pmacFindPtok(PCTOK ptok);
  228. PMAC STDCALL pmacGetPtok(PCTOK ptok);
  229. F STDCALL PURE fEqPtokPtok(PCTOK ptok1, PCTOK ptok2);
  230. F STDCALL PURE fIdentPtok(PCTOK ptok);
  231. PTCH STDCALL ptchDupPtch(PCTCH ptch);
  232. PTCH STDCALL ptchDupPtok(PCTOK ptok);
  233. /*****************************************************************************
  234. *
  235. * at.c - Arithmetic types
  236. *
  237. *****************************************************************************/
  238. typedef int AT; /* AT = arithmetic type */
  239. typedef AT *PAT; /* Pointer to AT */
  240. typedef int DAT; /* Delta to AT */
  241. void STDCALL SkipWhitePtok(PTOK ptok);
  242. void STDCALL AddExpAt(AT at);
  243. void STDCALL PushAtRadixCtch(AT atConvert, unsigned radix, CTCH ctch);
  244. void STDCALL PushAt(AT at);
  245. F STDCALL PURE fEvalPtokPat(PTOK ptok, PAT at);
  246. AT STDCALL PURE atTraditionalPtok(PCTOK ptok);
  247. /*****************************************************************************
  248. *
  249. * eval.c - Arithmetic evaluation
  250. *
  251. *****************************************************************************/
  252. extern struct CELL *rgcellEstack;
  253. /*****************************************************************************
  254. *
  255. * crackle.c - Macro expansion
  256. *
  257. *****************************************************************************/
  258. void STDCALL PushSubstPtokArgv(PTOK ptok, ARGV argv);
  259. void STDCALL TraceArgv(ARGV argv);
  260. /*****************************************************************************
  261. *
  262. * main.c - Boring stuff
  263. *
  264. *****************************************************************************/
  265. HF STDCALL hfInputPtchF(PTCH ptch, F fFatal);
  266. /*****************************************************************************
  267. *
  268. * predef.c - Predefined (a.k.a. built-in) macros
  269. *
  270. *****************************************************************************/
  271. void STDCALL InitPredefs(void);
  272. /*****************************************************************************
  273. *
  274. * EachOp
  275. *
  276. * Before calling this macro, define the macro `x' to do whatever
  277. * you want.
  278. *
  279. * EachOpX
  280. *
  281. * Same as EachOp, except that it also includes the Eof magic.
  282. *
  283. *****************************************************************************/
  284. #define EachOp() \
  285. x(Define, define) \
  286. x(Undefine, undefine) \
  287. x(Defn, defn) \
  288. x(Pushdef, pushdef) \
  289. x(Popdef, popdef) \
  290. x(Ifdef, ifdef) \
  291. x(Shift, shift) \
  292. /* x(Changequote, changequote) */ \
  293. /* x(Changecom, changecom) */ \
  294. x(Divert, divert) \
  295. /* x(Undivert, undivert) */ \
  296. x(Divnum, divnum) \
  297. x(Dnl, dnl) \
  298. x(Ifelse, ifelse) \
  299. x(Incr, incr) \
  300. x(Decr, decr) \
  301. x(Eval, eval) \
  302. x(Len, len) \
  303. x(Index, index) \
  304. x(Substr, substr) \
  305. x(Translit, translit) \
  306. x(Include, include) \
  307. x(Sinclude, sinclude) \
  308. /* x(Syscmd, syscmd) */ \
  309. /* x(Sysval, sysval) */ \
  310. /* x(Maketemp, maketemp) */ \
  311. /* x(M4exit, m4exit) */ \
  312. /* x(M4wrap, m4wrap) */ \
  313. x(Errprint, errprint) \
  314. x(Dumpdef, dumpdef) \
  315. x(Traceon, traceon) \
  316. x(Traceoff, traceoff) \
  317. x(Patsubst, patsubst) /* GNU extension that the d3d guys rely on */ \
  318. #define EachOpX() EachOp() x(Eof, eof) x(Eoi, eoi)
  319. #define x(cop, lop) DeclareOp(op##cop);
  320. EachOpX()
  321. #undef x
  322. enum MAGIC {
  323. #define x(cop, lop) tch##cop,
  324. EachOpX()
  325. #undef x
  326. tchMax,
  327. };