tagged release 0.7.1
[parrot.git] / compilers / pirc / src / pirutil.c
bloba29b0a24a0c7f59c2f92daeab84a4c9f874e6e34
1 /*
2 * $Id$
3 * Copyright (C) 2007, The Perl Foundation.
4 */
6 /*
8 =head1 NAME
10 pirutil.c - various utility functions
12 =cut
15 #include "pirutil.h"
16 #include <string.h>
17 #include <stdio.h>
18 #include <stdlib.h>
20 #include "parrot/parrot.h"
24 =head1 FUNCTIONS
26 =over 4
28 =item C<char * clone_string(NOTNULL(char const * src))>
30 clone a string. Copy the characters of src into dest
31 and return dest. Memory allocation is done by this function, keeping
32 this function's client code simple. Please free() the memory after usage!
34 =cut
37 char *
38 clone_string(NOTNULL(char const * src))
40 int srclen;
41 char * dest, *ptr;
43 srclen = strlen(src);
44 /* dest is used as an iterator, ptr - still pointing to the beginning
45 * of the string - is returned
47 dest = ptr = (char *)calloc(srclen + 1, sizeof (char));
48 while (*src) {
49 *dest++ = *src++;
51 return ptr;
56 =item C<FILE * open_file(char const *filename, char const *mode)>
58 Open the file C<filename> in mode C<mode>. If this fails, an error message
59 is printed, and the program is terminated.
61 =cut
64 FILE *
65 open_file(char const *filename, char const *mode)
67 FILE *file = fopen(filename, mode);
69 if (file == NULL) {
70 fprintf(stderr, "Failed to open file '%s' in mode '%s'\nTerminating\n", filename, mode);
71 exit(EXIT_FAILURE);
74 return file;
79 =item C<void printverbose(char *message)>
81 Prints the specified message if the verbose flag was set
83 =cut
86 void
87 printverbose(char *message)
89 fprintf(stdout, message);
94 =item C<void printdebug(char *message)>
96 Prints the specified message if the debug flag was set.
98 =cut
101 void
102 printdebug(char *message)
104 fprintf(stdout, message);
109 /* Array holding all parrot ops */
110 static char const *parrot_ops[] = {
111 "yield",
112 "xor",
113 "warningson",
114 "warningsoff",
115 "valid_type",
116 "upcase",
117 "unshift",
118 "unregister",
119 "unpin",
120 "unless_null",
121 "unless",
122 "typeof",
123 "trans_encoding",
124 "trans_charset",
125 "trace",
126 "titlecase",
127 "time",
128 "throw",
129 "thaw",
130 "tell",
131 "tanh",
132 "tan",
133 "tailcallmethod",
134 "tailcall",
135 "sysinfo",
136 "sweepon",
137 "sweepoff",
138 "sweep",
139 "substr",
140 "subclass",
141 "sub",
142 "stringinfo",
143 "store_lex",
144 "stat",
145 "sqrt",
146 "sprintf",
147 "split",
148 "spawnw",
149 "socket",
150 "sockaddr",
151 "sleep",
152 "sizeof",
153 "sinh",
154 "sin",
155 "shr",
156 "shl",
157 "shift",
158 "setstdout",
159 "setstderr",
160 "sets_ind",
161 "setref",
162 "setprop",
163 "setp_ind",
164 "setn_ind",
165 "seti_ind",
166 "setattribute",
167 "set_root_global",
168 "set_returns",
169 "set_hll_global",
170 "set_global",
171 "set_args",
172 "set_addr",
173 "set",
174 "send",
175 "seek",
176 "sech",
177 "sec",
178 "savec",
179 "saveall",
180 "save",
181 "runinterp",
182 "rotate_up",
183 "rot",
184 "returncc",
185 "rethrow",
186 "ret",
187 "result_info",
188 "restoreall",
189 "restore",
190 "reserved",
191 "repeat",
192 "removeparent",
193 "removeattribute",
194 "register",
195 "recv",
196 "readline",
197 "read",
198 "pushmark",
199 "pushaction",
200 "push_eh",
201 "push",
202 "prophash",
203 "profile",
204 "printerr",
205 "print",
206 "pow",
207 "popmark",
208 "pop",
209 "poll",
210 "pioctl",
211 "pin",
212 "peek",
213 "ord"
214 "or",
215 "open",
216 "null",
217 "not",
218 "noop",
219 "newclosure",
220 "newclass",
221 "new_callback",
222 "new",
223 "new",
224 "neg",
225 "needs_destroy",
226 "ne_str",
227 "ne_num",
228 "ne_addr",
229 "ne",
230 "n_repeat",
231 "n_not",
232 "n_neg",
233 "n_infix",
234 "n_concat",
235 "n_bnots"
236 "n_bnot"
237 "n_abs",
238 "mul",
239 "mod",
240 "mmdvtregister",
241 "mmdvtfind",
242 "lt_str",
243 "lt_num",
244 "lt_addr",
245 "lt",
246 "lsr",
247 "lookback",
248 "log2",
249 "log10",
250 "localtime",
251 "loadlib",
252 "load_bytecode",
253 "ln",
254 "listen",
255 "length",
256 "le_str",
257 "le_num",
258 "le_addr",
259 "le",
260 "lcm",
261 "jsr",
262 "join",
263 "istrue",
264 "issame",
265 "isnull",
266 "isntsame",
267 "isne",
268 "islt",
269 "isle",
270 "isgt",
271 "isge",
272 "isfalse",
273 "iseq",
274 "isa",
275 "is_cclass",
276 "invokecc",
277 "invoke",
278 "interpinfo",
279 "infix",
280 "index",
281 "inc",
282 "if_null",
283 "if",
284 "hash",
285 "gt_str",
286 "gt_num",
287 "gt_addr",
288 "gt",
289 "gmtime",
290 "getstdout",
291 "getstdin",
292 "getstderr",
293 "getprop",
294 "getline",
295 "getinterp",
296 "getfile",
297 "getattribute",
298 "get_root_namespace",
299 "get_root_global",
300 "get_results",
301 "get_repr",
302 "get_params",
303 "get_namespace",
304 "get_mro",
305 "get_hll_namespace",
306 "get_hll_global",
307 "get_global",
308 "get_addr",
309 "ge_str",
310 "ge_num",
311 "ge_addr",
312 "ge",
313 "gcd",
314 "gc_debug",
315 "freeze",
316 "floor",
317 "find_type",
318 "find_not_cclass",
319 "find_name",
320 "find_method",
321 "find_lex",
322 "find_encoding",
323 "find_charset",
324 "find_cclass",
325 "fdopen",
326 "fdiv",
327 "fact",
328 "exp",
329 "exit",
330 "exists",
331 "exchange",
332 "escape",
333 "errorson",
334 "errorsoff",
335 "err",
336 "eq_str",
337 "eq_num",
338 "eq_addr",
339 "eq",
340 "entrytype",
341 "enternative",
342 "end",
343 "encoding",
344 "elements",
345 "downcase",
346 "does",
347 "dlvar",
348 "dlfunc",
349 "div",
350 "die",
351 "deref",
352 "depth",
353 "delprop",
354 "delete",
355 "defined",
356 "decodetime",
357 "decodelocaltime",
358 "dec",
359 "debug_print",
360 "debug_load",
361 "debug_init",
362 "debug_break",
363 "debug",
364 "cosh",
365 "cos",
366 "connect",
367 "concat",
368 "compreg",
369 "compose",
370 "collecton",
371 "collectoff",
372 "collect",
373 "cmp_str",
374 "cmp_num",
375 "cmp",
376 "cmod",
377 "close",
378 "clone",
379 "clears",
380 "clearp",
381 "clearn",
382 "cleari",
383 "pop_eh",
384 "classname",
385 "class",
386 "chr",
387 "chopn",
388 "charsetname",
389 "charset",
390 "ceil",
391 "can",
392 "callmethodcc",
393 "callmethod",
394 "bytelength",
395 "bxors",
396 "bxor",
397 "bsr",
398 "branch_cs",
399 "branch",
400 "bounds",
401 "bors",
402 "bor",
403 "bnots",
404 "bnot",
405 "bind",
406 "bands",
407 "band",
408 "backtrace",
409 "atan",
410 "assign",
411 "asin",
412 "asec",
413 "and",
414 "addparent",
415 "addmethod",
416 "addattribute",
417 "add",
418 "acos",
419 "accept",
420 "abs",
421 NULL
428 =item C<int is_op(char *id)>
430 Return 1 if the specified id is a Parrot op, 0 otherwise.
432 =cut
436 is_op(char *id)
438 char const *iter = parrot_ops[0];
439 int index = 0;
441 /* very inefficient implementation, but for now it works */
442 /* suggestions: hashtable, binary search */
444 while (iter != NULL) {
445 if (strcmp(iter, id) == 0)
446 return 1;
448 iter = parrot_ops[++index];
452 return 0;
458 =back
460 =cut
465 * Local variables:
466 * c-file-style: "parrot"
467 * End:
468 * vim: expandtab shiftwidth=4: