git @ Cat's Eye Technologies Maentwrog / f994afb
I'm a bit sick of the strdup() nonsense, so let's do this. Chris Pressey 8 years ago
3 changed file(s) with 32 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
99 -Winline -Wnested-externs -Wredundant-decls
1010
1111 ifdef ANSI
12 CFLAGS+= -ansi -pedantic -D_BSD_SOURCE
12 CFLAGS+= -ansi -pedantic
1313 else
1414 CFLAGS+= -std=c99 -D_POSIX_C_SOURCE=200809L
1515 endif
4747 /* prototypes */
4848
4949 /* word-handling */
50 struct word *addword(const char *name, const char *macro, int fcn);
51 struct word *lookup(char *name);
50 struct word *addword(const char *, const char *, int);
51 struct word *lookup(const char *);
5252 void initwords(void);
5353 void makeword(void);
5454
5555 /* variable-handling */
56 struct vari *addvari(char *name);
57 struct vari *getvari(char *name);
58 void setvari(char *name, signed long value);
56 struct vari *addvari(char *);
57 struct vari *getvari(char *);
58 void setvari(char *, signed long);
5959
6060 /* stack-handling */
61 void push(signed long val);
61 void push(signed long);
6262 signed long pop(void);
6363
6464 /* functions */
65 void dofunc(struct word * w);
65 void dofunc(struct word *);
6666 void words(void);
6767 void vars(void);
6868 signed long sizestack(void);
6969
7070 /* parsing and interpreting */
71 void process(char *s);
72 void procstr(char *s);
71 char *strdupe(const char *);
72 void process(char *);
73 void procstr(char *);
7374
7475 /* entry point */
7576 int main(int, char **);
171172 printf("unknown command '%s'\n", s);
172173 }
173174
175 char *strdupe(const char *s)
176 {
177 char *t = malloc(strlen(s) + 1);
178 strcpy(t, s);
179 return t;
180 }
181
174182 /*
175183 * processes each word in the string s.
176184 * strtok doesn't work with recursion :-(
177185 */
178186 void procstr(char *s)
179187 {
180 char *h=(char *)strdup(s);
188 char *h=strdupe(s);
181189 char *g, *gg;
182190 g = h;
183191
198206 g++;
199207 }
200208
201 free(h); /* called with strdup(), so we must free */
209 free(h); /* called with strdupe(), so we must free */
202210 }
203211
204212 /*
215223 }
216224 new = (struct word *) malloc(sizeof(struct word));
217225 strcpy(new->name, name);
218 new->macro = (char *)strdup(macro);
226 new->macro = strdupe(macro);
219227 new->fcn = fcn;
220228
221229 new->next = whead;
227235 * attempts to find the word 'name' in the words list. returns NULL if it
228236 * could not be found.
229237 */
230 struct word *lookup(char *name)
238 struct word *lookup(const char *name)
231239 {
232240 struct word *l = whead;
233241 struct word *k = NULL;
6767 void vars(void);
6868
6969 /* parsing and interpreting */
70 char *strdupe(const char *);
7071 void process(char *s);
7172 void procstr(char *s);
7273
134135 printf("unknown command '%s'\n", s);
135136 }
136137
138 char *strdupe(const char *s)
139 {
140 char *t = malloc(strlen(s) + 1);
141 strcpy(t, s);
142 return t;
143 }
144
137145 /*
138146 * processes each word in the string s.
139147 */
140148 void procstr(char *s)
141149 {
142 char *h = (char *)strdup(s);
150 char *h = strdupe(s);
143151 char *g;
144152 g = strtok(h, " ");
145153 while (g)
147155 process(g);
148156 g = strtok(NULL, " ");
149157 }
150 free(h); /* called with strdup(), so we must free */
158 free(h); /* called with strdupe(), so we must free */
151159 }
152160
153161 /*