spack.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * GTools C compiler
  3. * =================
  4. * source file :
  5. * sPack header packing/unpacking
  6. */
  7. #include "define.h"
  8. #define SPK_START (char)0x81
  9. #ifdef STATIC_SPACK_TAB
  10. #ifdef ARRAY_VER
  11. #define C(al) al
  12. char *sPk_tab[]={
  13. #else
  14. #define C(al) al "\0"
  15. char *sPk_table=
  16. "\0"
  17. #endif
  18. C(" *)")
  19. C("*)")
  20. C("*,")
  21. C("int x")
  22. C("int y")
  23. C("int z")
  24. C("int a")
  25. C("int b")
  26. C("int c")
  27. C("register ")
  28. C("unsigned ")
  29. C("signed ")
  30. C("volatile ")
  31. C("void _(")
  32. C("int _(")
  33. C("short _(")
  34. C("BOOL _(")
  35. C("long _(")
  36. C(" *_(")
  37. C("*_(")
  38. C("const ")
  39. C("void,(")
  40. C("char,(")
  41. C("int,(")
  42. C("short,(")
  43. C("long,(")
  44. C("float,(")
  45. C("void")
  46. C("char")
  47. C("int")
  48. C("short")
  49. C("long")
  50. C("float")
  51. C("typedef struct")
  52. C("typedef ")
  53. #ifdef GEN
  54. C("FIXED ")
  55. C("HANDLE ")
  56. C("BGS *")
  57. C("SCREEN *")
  58. C("POINT *")
  59. C("FIXED *")
  60. C("PLANE *p")
  61. #endif
  62. C("Gray")
  63. C("_RR(")
  64. C(",15")
  65. C(",(")
  66. C("),")
  67. C("({")
  68. C("})")
  69. C("_rom_call(")
  70. C("_rom_call_addr(")
  71. C("FILE*")
  72. C("__ATTR_LIB_ASM__")
  73. C("__ATTR_LIB_C__")
  74. C("__ATTR_TIOS__")
  75. C("__ATTR_")
  76. C(" __")
  77. C("__")
  78. C(" *")
  79. #ifdef ARRAY_VER
  80. C(0)
  81. };
  82. #else
  83. C("")
  84. - 1
  85. ;
  86. #endif
  87. #else
  88. char *sPk_table=0;
  89. #endif
  90. #undef C
  91. /*#define sPk_num sizeof(sPk_tab)*/
  92. #ifdef PC
  93. extern int sPk_usetab[128];
  94. #endif
  95. #ifndef isidch
  96. #ifdef PC
  97. static int
  98. isidch(c)
  99. char c;
  100. {
  101. return (c>='0'&&c<='9') || (c>='A'&&c<='Z') || (c>='a'&&c<='z')
  102. || c == '_' || c == '$';
  103. }
  104. static int
  105. isbegidch(c)
  106. char c;
  107. {
  108. return (c>='A'&&c<='Z') || (c>='a'&&c<='z') || c == '_' || c == '$';
  109. }
  110. #else
  111. #define isidch(___c) ({register short __c=(___c); \
  112. (__c>='0'&&__c<='9') || (__c>='A'&&__c<='Z') || (__c>='a'&&__c<='z') || __c=='_' || __c=='$';})
  113. #define isbegidch(___c) ({register short __c=(___c); \
  114. (__c>='A'&&__c<='Z') || (__c>='a'&&__c<='z') || __c=='_' || __c=='$';})
  115. #endif
  116. #endif
  117. #ifdef PC
  118. char sPackRet[10000];
  119. char sPackTemp[10000];
  120. #else
  121. char sPackRet[1000];
  122. char sPackTemp[1000];
  123. #endif
  124. int strbeg(char *S,char *s) {
  125. char c;
  126. while ((c=*s++))
  127. if (c!=*S++)
  128. return 1;
  129. return 0;
  130. }
  131. char *sPack(char *s,char *name,char *defn) {
  132. int nl=strlen(name),dl=strlen(defn);
  133. char *s0=s;
  134. char c,*d=sPackTemp;
  135. while ((c=*s)) {
  136. if (nl && !strbeg(s,name) && (s==s0 || (!isidch(s[-1]) && !isidch(s[nl]))))
  137. s+=nl,*d++='_';
  138. else if (dl && !strbeg(s,defn) && (s==s0 || (!isidch(s[-1]) && !isidch(s[nl]))))
  139. s+=dl,*d++='_';
  140. else *d++=*s++;
  141. }
  142. *d++=0;
  143. s=sPackTemp; d=sPackRet;
  144. //return s;
  145. while ((c=*s)) {
  146. #ifdef ARRAY_VER
  147. char pk=SPK_START; char *str,**p=sPk_tab;
  148. while ((str=*p++)) {
  149. if (!strbeg(s,str)) {
  150. s+=strlen(str),*d++=pk;
  151. goto cont;
  152. }
  153. pk++;
  154. }
  155. *d++=*s++;
  156. #else
  157. char pk=SPK_START; char c,*p=sPk_table;
  158. while ((c=*p++)) {
  159. char *S=s;
  160. do
  161. if (c!=*S++) goto skip;
  162. while ((c=*p++));
  163. #ifdef PC
  164. sPk_usetab[pk-SPK_START]++;
  165. #endif
  166. p--; while (*--p); p++;
  167. s+=strlen(p),*d++=pk;
  168. goto cont;
  169. /*pk++;
  170. continue;*/
  171. skip:
  172. while (*p++);
  173. pk++;
  174. }
  175. if ((char)*s<0) *d++=(char)0x80;
  176. *d++=*s++;
  177. #endif
  178. cont:
  179. (void)0;
  180. }
  181. *d++=0;
  182. return sPackRet;
  183. }
  184. #ifdef PC
  185. char defPackRet[10000];
  186. #else
  187. char defPackRet[1000];
  188. #endif
  189. #ifdef PC
  190. #define w(p,v) (*p++=(unsigned char)((v)>>8),*p++=(unsigned char)(v))
  191. #else
  192. #define w(p,_v) ({short v=_v; *p++=(unsigned char)((v)>>8); *p++=(unsigned char)(v);})
  193. #endif
  194. int def_is_packed=0;
  195. char *defPack(char *s) {
  196. char c=0; int na=0;
  197. def_is_packed=0;
  198. //return s;
  199. // if (!strncmp("_rom_call(",s,10)) c=(char)0xFF,s+=10,na=2;
  200. if (c) {
  201. int l=0; char *prv=s;
  202. char *o=defPackRet;
  203. *o++=c;
  204. while ((c=*s++)) {
  205. if (c=='(') l++;
  206. else if (c==')') { if (--l<0) s[-1]=0; w(o,atoi(prv)); }
  207. else if (c==',' && !l && !--na) { s[-1]=0; strcpy(o,sPack(s,"","")); while (*o++); }
  208. }
  209. return defPackRet;
  210. } else if (strlen(s)>4) {
  211. char *packed=sPack(s,"","");
  212. if (strcmp(packed,s)) {
  213. def_is_packed=1;
  214. strcpy(defPackRet,packed);
  215. return defPackRet;
  216. }
  217. }
  218. return s;
  219. }