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.

229 lines
5.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: dalan.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* idea of a low level data description language. currently only a most
  5. primitive subset exists. */
  6. #include "config.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #if HAVE_STDBOOL_H
  10. #include <stdbool.h>
  11. #endif
  12. #include <ctype.h>
  13. #include "dalan.h"
  14. /* test structure to find maximal alignment */
  15. static struct {
  16. char a;
  17. long double b;
  18. } maxalign;
  19. /* test structure to find minimal alignment */
  20. static struct {
  21. char a;
  22. char b;
  23. } minalign;
  24. /* test union to find kind of byte ordering */
  25. static union {
  26. char a[2];
  27. short b;
  28. } byteorder = { "01" };
  29. struct dalan_opts_s dalan_opts = {
  30. sizeof(int),
  31. sizeof(short),
  32. sizeof(long),
  33. sizeof(char),
  34. sizeof(float),
  35. sizeof(double)
  36. } ;
  37. /* fill the dalan_opts structure with machine dependent defaults values. */
  38. static void _dalan_dflts(struct dalan_opts_s *dlo) {
  39. dlo->c_int = sizeof(int);
  40. dlo->c_short = sizeof(short);
  41. dlo->c_long = sizeof(long);
  42. dlo->c_char = sizeof(char);
  43. dlo->c_float = sizeof(float);
  44. dlo->c_double = sizeof(double);
  45. dlo->maxalign = (char *)&maxalign.b-&maxalign.a;
  46. dlo->minalign = &minalign.b-&minalign.a;
  47. dlo->byteorder = (byteorder.b!=7711);
  48. }
  49. /* allocate a new dalan_opts structure, fills it with machine dependent
  50. defaults values, and returns the pointer. */
  51. struct dalan_opts_s *dalan_props(void) {
  52. struct dalan_opts_s *dlo;
  53. dlo = malloc(sizeof(struct dalan_opts_s));
  54. if (dlo == NULL) {
  55. return NULL;
  56. }
  57. _dalan_dflts(dlo);
  58. return dlo;
  59. }
  60. void dalan_init(void) {
  61. _dalan_dflts(&dalan_opts);
  62. }
  63. /* read data description from line, write result to data; do not write
  64. so much data that *p exceeds n !
  65. p must be initialized to 0.
  66. return 0 on success,
  67. -1 if the data was cut due to n limit,
  68. 1 if a syntax error occurred
  69. *p is a global data counter; especially it must be used when calculating
  70. alignment. On successful return from the function *p must be actual!
  71. */
  72. int dalan(const char *line, char *data, size_t *p, size_t n) {
  73. int align, mask, i, x;
  74. size_t p1 = *p;
  75. char c;
  76. /*fputs(line, stderr); fputc('\n', stderr);*/
  77. while (c = *line++) {
  78. switch (c) {
  79. case ' ':
  80. case '\t':
  81. case '\r':
  82. case '\n':
  83. break;
  84. case ',':
  85. align = 2;
  86. while (*line == ',') {
  87. align <<= 1;
  88. ++line;
  89. }
  90. mask = align - 1; /* create the bitmask */
  91. i = (align - (p1 & mask)) & mask;
  92. while (i && p1<n) data[p1++] = 0, --i;
  93. if (i) { *p = p1; return -1; }
  94. break;
  95. case ';':
  96. align = dalan_opts.c_int;
  97. mask = align - 1;
  98. i = (align - (p1 & mask)) & mask;
  99. while (i && p1<n) data[p1++] = 0, --i;
  100. if (i) { *p = p1; return -1; }
  101. break;
  102. case '"':
  103. while (1) {
  104. switch (c = *line++) {
  105. case '\0': fputs("unterminated string\n", stderr);
  106. return 1;
  107. case '"':
  108. break;
  109. case '\\':
  110. if (!(c = *line++)) {
  111. fputs("continuation line not implemented\n", stderr);
  112. return 1;
  113. }
  114. switch (c) {
  115. case 'n': c = '\n'; break;
  116. case 'r': c = '\r'; break;
  117. case 't': c = '\t'; break;
  118. case 'f': c = '\f'; break;
  119. case 'b': c = '\b'; break;
  120. case 'a': c = '\a'; break;
  121. #if 0
  122. case 'e': c = '\e'; break;
  123. #else
  124. case 'e': c = '\033'; break;
  125. #endif
  126. case '0': c = '\0'; break;
  127. }
  128. /* PASSTHROUGH */
  129. default:
  130. if (p1 >= n) { *p = p1; return -1; }
  131. data[p1++] = c;
  132. continue;
  133. }
  134. if (c == '"')
  135. break;
  136. }
  137. break;
  138. case '\'':
  139. switch (c = *line++) {
  140. case '\0': fputs("unterminated character\n", stderr);
  141. return 1;
  142. case '\'': fputs("error in character\n", stderr);
  143. return 1;
  144. case '\\':
  145. if (!(c = *line++)) {
  146. fputs("continuation line not implemented\n", stderr);
  147. return 1;
  148. }
  149. switch (c) {
  150. case 'n': c = '\n'; break;
  151. case 'r': c = '\r'; break;
  152. case 't': c = '\t'; break;
  153. case 'f': c = '\f'; break;
  154. case 'b': c = '\b'; break;
  155. case 'a': c = '\a'; break;
  156. #if 0
  157. case 'e': c = '\e'; break;
  158. #else
  159. case 'e': c = '\033'; break;
  160. #endif
  161. }
  162. /* PASSTHROUGH */
  163. default:
  164. if (p1 >= n) { *p = p1; return -1; }
  165. data[p1++] = c;
  166. break;
  167. }
  168. if (*line != '\'') {
  169. fputs("error in character termination\n", stderr);
  170. *p = p1; return 1;
  171. }
  172. ++line;
  173. break;
  174. #if LATER
  175. case '0':
  176. c = *line++;
  177. if (c == 'x') {
  178. /* hexadecimal */ ;
  179. } else if (isdigit(c&0xff)) {
  180. /* octal */
  181. } else {
  182. /* it was only 0 */
  183. }
  184. break;
  185. #endif /* LATER */
  186. case 'x':
  187. /* expecting hex data, must be an even number of digits!! */
  188. while (true) {
  189. c = *line;
  190. if (isdigit(c&0xff)) {
  191. x = (c-'0') << 4;
  192. } else if (isxdigit(c&0xff)) {
  193. x = ((c&0x07) + 9) << 4;
  194. } else
  195. break;
  196. ++line;
  197. c = *line;
  198. if (isdigit(c&0xff)) {
  199. x |= (c-'0');
  200. } else if (isxdigit(c&0xff)) {
  201. x |= (c&0x07) + 9;
  202. } else {
  203. fputs("odd number of hexadecimal digits\n", stderr);
  204. *p = p1; return 1;
  205. }
  206. ++line;
  207. if (p1 >= n) { *p = p1; return -1; }
  208. data[p1++] = x;
  209. }
  210. break;
  211. case 'A': case 'a':
  212. case 'C': case 'c':
  213. default: fprintf(stderr, "syntax error in \"%s\"\n", line-1);
  214. return 1;
  215. }
  216. }
  217. *p = p1; return 0;
  218. }