Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / sources / f2c / mem.c
1 /****************************************************************
2 Copyright 1990 by AT&T Bell Laboratories and Bellcore.
3
4 Permission to use, copy, modify, and distribute this software
5 and its documentation for any purpose and without fee is hereby
6 granted, provided that the above copyright notice appear in all
7 copies and that both that the copyright notice and this
8 permission notice and warranty disclaimer appear in supporting
9 documentation, and that the names of AT&T Bell Laboratories or
10 Bellcore or any of their entities not be used in advertising or
11 publicity pertaining to distribution of the software without
12 specific, written prior permission.
13
14 AT&T and Bellcore disclaim all warranties with regard to this
15 software, including all implied warranties of merchantability
16 and fitness.  In no event shall AT&T or Bellcore be liable for
17 any special, indirect or consequential damages or any damages
18 whatsoever resulting from loss of use, data or profits, whether
19 in an action of contract, negligence or other tortious action,
20 arising out of or in connection with the use or performance of
21 this software.
22 ****************************************************************/
23
24 #include <stdio.h>
25 #include "iob.h"
26 #include "string.h"
27
28 #define MEMBSIZE        32000
29 #define GMEMBSIZE       16000
30
31  extern char *Alloc();
32  extern void exit();
33
34  char *
35 gmem(n, round)
36  int n, round;
37 {
38         static char *last, *next;
39         char *rv;
40         if (round)
41                 next =
42 #ifdef CRAY
43                     (long)next & 0xe000000000000000
44                         ? (char *)(((long)next & 0x1fffffffffffffff) + 1)
45                         : next;
46 #else
47                          (char *)(((long)next + sizeof(char *)-1)
48                                 & ~(sizeof(char *)-1));
49 #endif
50         rv = next;
51         if ((next += n) > last) {
52                 rv = Alloc(n + GMEMBSIZE);
53
54                 next = rv + n;
55                 last = next + GMEMBSIZE;
56                 }
57         return rv;
58         }
59
60  struct memblock {
61         struct memblock *next;
62         char buf[MEMBSIZE];
63         };
64  typedef struct memblock memblock;
65
66  static memblock mem0;
67  memblock *curmemblock = &mem0, *firstmemblock = &mem0;
68
69  char   *mem_first = mem0.buf,
70         *mem_next  = mem0.buf,
71         *mem_last  = mem0.buf + sizeof(mem0.buf),
72         *mem0_last = mem0.buf + sizeof(mem0.buf);
73
74  char *
75 mem(n, round)
76  int n, round;
77 {
78         memblock *b;
79         register char *rv, *s;
80
81         if (round)
82                 mem_next =
83 #ifdef CRAY
84                     (long)mem_next & 0xe000000000000000
85                         ? (char *)(((long)mem_next & 0x1fffffffffffffff) + 1)
86                         : mem_next;
87 #else
88                          (char *)(((long)mem_next + sizeof(char *)-1)
89                                 & ~(sizeof(char *)-1));
90 #endif
91         rv = mem_next;
92         s = rv + n;
93         if (s >= mem_last) {
94                 if (n > sizeof(mem0.buf))  {
95                         fprintf(stderr, "mem(%d) failure!\n", n);
96                         exit(1);
97                         }
98                 if (!(b = curmemblock->next)) {
99                         b = (memblock *)Alloc(sizeof(memblock));
100                         curmemblock->next = b;
101                         b->next = 0;
102                         }
103                 curmemblock = b;
104                 rv = b->buf;
105                 mem_last = rv + sizeof(b->buf);
106                 s = rv + n;
107                 }
108         mem_next = s;
109         return rv;
110         }
111
112  char *
113 tostring(s,n)
114  register char *s;
115  int n;
116 {
117         register char *s1, *se, **sf;
118         extern char *str_fmt[];
119         char *rv, *s0, *s2;
120         register int k = n + 2, t;
121
122         sf = str_fmt;
123         sf['%'] = "%";
124         s0 = s;
125         se = s + n;
126         for(; s < se; s++) {
127                 t = *(unsigned char *)s;
128                 s1 = sf[t < 127 ? t : 127];
129                 while(*++s1)
130                         k++;
131                 }
132         rv = s1 = mem(k,0);
133         *s1++ = '"';
134         for(s = s0; s < se; s++) {
135                 t = *(unsigned char *)s;
136                 if (t < 127)
137                         for(s2 = sf[t]; *s1 = *s2++; s1++);
138                 else {
139                         sprintf(s1, sf[127], t);
140                         s1 += strlen(s1);
141                         }
142                 }
143         *s1 = 0;
144         sf['%'] = "%%";
145         return rv;
146         }
147
148  char *
149 cpstring(s)
150  register char *s;
151 {
152         return strcpy(mem(strlen(s)+1,0), s);
153         }
154
155  void
156 new_iob_data(ios, name)
157  register io_setup *ios;
158  char *name;
159 {
160         register iob_data *iod;
161         register char **s, **se;
162
163         iod = (iob_data *)
164                 mem(sizeof(iob_data) + ios->nelt*sizeof(char *), 1);
165         iod->next = iob_list;
166         iob_list = iod;
167         iod->type = ios->fields[0];
168         iod->name = cpstring(name);
169         s = iod->fields;
170         se = s + ios->nelt;
171         while(s < se)
172                 *s++ = "0";
173         *s = 0;
174         }
175
176  char *
177 string_num(pfx, n)
178  char *pfx;
179  long n;
180 {
181         char buf[32];
182         sprintf(buf, "%s%ld", pfx, n);
183         /* can't trust return type of sprintf -- BSD gets it wrong */
184         return strcpy(mem(strlen(buf)+1,0), buf);
185         }
186
187 static defines *define_list;
188
189  void
190 def_start(outfile, s1, s2, post)
191  FILE *outfile;
192  char *s1, *s2, *post;
193 {
194         defines *d;
195         int n, n1;
196
197         n = n1 = strlen(s1);
198         if (s2)
199                 n += strlen(s2);
200         d = (defines *)mem(sizeof(defines)+n, 1);
201         d->next = define_list;
202         define_list = d;
203         strcpy(d->defname, s1);
204         if (s2)
205                 strcpy(d->defname + n1, s2);
206         nice_printf(outfile, "#define %s %s", d->defname, post);
207         }
208
209  void
210 other_undefs(outfile)
211  FILE *outfile;
212 {
213         defines *d;
214         if (d = define_list) {
215                 define_list = 0;
216                 nice_printf(outfile, "\n");
217                 do
218                         nice_printf(outfile, "#undef %s\n", d->defname);
219                         while(d = d->next);
220                 nice_printf(outfile, "\n");
221                 }
222         }