taste.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
  3. * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
  4. * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
  5. */
  6. /*$Header: /tmp_amd/presto/export/kbs/jutta/src/gsm/RCS/taste.c,v 1.1 1992/10/28 00:28:39 jutta Exp $*/
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <memory.h>
  10. #include "config.h"
  11. #ifdef HAS_STDLIB_H
  12. # include <stdlib.h>
  13. #else
  14. #include "proto.h"
  15. # ifdef HAS_MALLOC_H
  16. # include <malloc.h>
  17. # else
  18. extern char * malloc P((char *)), * realloc P((char *,int));
  19. # endif
  20. extern int exit P((int));
  21. #endif
  22. #include "proto.h"
  23. /*
  24. * common code to sweet.c and bitter.c: read the name:#bits description.
  25. */
  26. #include "taste.h"
  27. static struct spex * s_spex;
  28. static int n_spex, m_spex;
  29. extern void write_code P((struct spex *, int));
  30. char * strsave P1((str), char * str) /* strdup() + errors */
  31. {
  32. int n = strlen(str) + 1;
  33. char * s = malloc(n);
  34. if (!s) {
  35. fprintf(stderr, "Failed to malloc %d bytes, abort\n",
  36. strlen(str) + 1);
  37. exit(1);
  38. }
  39. return memcpy(s, str, n);
  40. }
  41. struct spex * new_spex P0()
  42. {
  43. if (n_spex >= m_spex) {
  44. m_spex += 500;
  45. if (!(s_spex = (struct spex *)(n_spex
  46. ? realloc((char *)s_spex, m_spex * sizeof(*s_spex))
  47. : malloc( m_spex * sizeof(*s_spex))))) {
  48. fprintf(stderr, "Failed to malloc %d bytes, abort\n",
  49. m_spex * sizeof(*s_spex));
  50. exit(1);
  51. }
  52. }
  53. return s_spex + n_spex;
  54. }
  55. char * strtek P2((str, sep), char * str, char * sep) {
  56. static char * S = (char *)0;
  57. char * c, * base;
  58. if (str) S = str;
  59. if (!S || !*S) return (char *)0;
  60. /* Skip delimiters.
  61. */
  62. while (*S) {
  63. for (c = sep; *c && *c != *S; c++) ;
  64. if (*c) *S++ = 0;
  65. else break;
  66. }
  67. base = S;
  68. /* Skip non-delimiters.
  69. */
  70. for (base = S; *S; S++) {
  71. for (c = sep; *c; c++)
  72. if (*c == *S) {
  73. *S++ = 0;
  74. return base;
  75. }
  76. }
  77. return base == S ? (char *)0 : base;
  78. }
  79. int read_spex P0()
  80. {
  81. char buf[200];
  82. char * s, *t;
  83. struct spex * sp = s_spex;
  84. while (fgets(buf, sizeof buf, stdin)) {
  85. char * nl;
  86. if (nl = strchr(buf, '\n'))
  87. *nl = '\0';
  88. if (!*buf || *buf == ';') continue;
  89. s = strtek(buf, " \t");
  90. if (!s) {
  91. fprintf(stderr, "? %s\n", buf);
  92. continue;
  93. }
  94. sp = new_spex();
  95. sp->var = strsave(s);
  96. s = strtek((char*)0, " \t");
  97. if (!s) {
  98. fprintf(stderr, "varsize?\n");
  99. continue;
  100. }
  101. sp->varsize = strtol(s, (char **)0, 0);
  102. n_spex++;
  103. }
  104. return sp - s_spex;
  105. }
  106. int main P0()
  107. {
  108. read_spex();
  109. write_code(s_spex, n_spex);
  110. exit(0);
  111. }