8 struct list
*list_cons(void *el
, struct list
*tail
)
10 struct list
*res
= safe_malloc(sizeof(struct list
));
16 void list_free(struct list
*head
, void (*freefun
)(void *))
18 while (head
!= NULL
) {
24 void **list_to_array(struct list
*list
, int *num
, bool reverse
)
26 int i
= list_length(list
);
28 void **ptr
= safe_malloc(i
*sizeof(void *));
30 struct list
*r
= list
;
35 ptr
[*num
-(--i
)] = r
->el
;
43 int list_length(struct list
*r
)
55 if (c
>= '0' && c
<= '9')
57 if (c
>= 'a' && c
<= 'f')
59 if (c
>= 'A' && c
<= 'F')
64 char *escape_char(char c
, char *buf
, bool str
)
66 buf
= buf
== NULL
? safe_malloc(10) : buf
;
68 case '\0': strcpy(buf
, "\\0"); break;
69 case '\a': strcpy(buf
, "\\a"); break;
70 case '\b': strcpy(buf
, "\\b"); break;
71 case '\t': strcpy(buf
, "\\t"); break;
72 case '\n': strcpy(buf
, "\\n"); break;
73 case '\v': strcpy(buf
, "\\v"); break;
74 case '\f': strcpy(buf
, "\\f"); break;
75 case '\r': strcpy(buf
, "\\r"); break;
76 case '\'': strcpy(buf
, str
? "'" : "\\'"); break;
77 case '"': strcpy(buf
, str
? "\\\"" : "\""); break;
79 if (c
>= ' ' && c
< 127) {
80 sprintf(buf
, "%c", c
);
82 sprintf(buf
, "\\x%02x", (unsigned char)c
);
89 char *unescape_char(char *c
)
94 case '0': c
[1] = '\0'; break;
95 case '\'': c
[1] = '\''; break;
96 case '\\': c
[1] = '\\'; break;
97 case '"': c
[1] = '"'; break;
98 case 'a': c
[1] = '\a'; break;
99 case 'b': c
[1] = '\b'; break;
100 case 't': c
[1] = '\t'; break;
101 case 'v': c
[1] = '\v'; break;
102 case 'f': c
[1] = '\f'; break;
103 case 'r': c
[1] = '\r'; break;
104 case 'x': c
[3] = (fromHex(c
[2])<<4)+fromHex(c
[3]); c
+=2; break;
111 char *trimquotes(char *c
)
114 r
[strlen(r
)-1] = '\0';
118 void pdie(const char *msg
)
124 void die(const char *msg
, ...)
128 vfprintf(stderr
, msg
, ap
);
133 void pindent(int indent
, FILE *out
)
135 for (int i
= 0; i
<indent
; i
++)
136 if (fputc('\t', out
) == EOF
)
140 void safe_fprintf(FILE *out
, const char *msg
, ...)
144 int r
= vfprintf(out
, msg
, ap
);
150 void *safe_malloc(size_t size
)
152 void *res
= malloc(size
);
158 void *safe_strdup(const char *c
)
160 char *res
= strdup(c
);