9 struct list
*list_cons(void *el
, struct list
*tail
)
11 struct list
*res
= safe_malloc(sizeof(struct list
));
17 void list_free(struct list
*head
, void (*freefun
)(void *))
19 while (head
!= NULL
) {
25 void **list_to_array(struct list
*list
, int *num
, bool reverse
)
27 int i
= list_length(list
);
29 void **ptr
= safe_malloc(i
*sizeof(void *));
31 struct list
*r
= list
;
36 ptr
[*num
-(--i
)] = r
->el
;
44 int list_length(struct list
*r
)
56 if (c
>= '0' && c
<= '9')
58 if (c
>= 'a' && c
<= 'f')
60 if (c
>= 'A' && c
<= 'F')
65 char *escape_char(char c
, char *buf
, bool str
)
67 buf
= buf
== NULL
? safe_malloc(10) : buf
;
69 case '\0': strcpy(buf
, "\\0"); break;
70 case '\a': strcpy(buf
, "\\a"); break;
71 case '\b': strcpy(buf
, "\\b"); break;
72 case '\t': strcpy(buf
, "\\t"); break;
73 case '\n': strcpy(buf
, "\\n"); break;
74 case '\v': strcpy(buf
, "\\v"); break;
75 case '\f': strcpy(buf
, "\\f"); break;
76 case '\r': strcpy(buf
, "\\r"); break;
77 case '\'': strcpy(buf
, str
? "'" : "\\'"); break;
78 case '"': strcpy(buf
, str
? "\\\"" : "\""); break;
80 if (c
>= ' ' && c
< 127)
81 sprintf(buf
, "%c", c
);
83 sprintf(buf
, "\\x%02x", (unsigned char)c
);
91 return c
>= '0' && c
<= '7';
101 char *unescape_char(char *c
)
104 if (c
[1] == 'x' && isxdigit(c
[2])) {
106 if (isxdigit(c
[3])) {
107 c
[3] = (fromHex(c
[2])*16)+fromHex(c
[3]);
111 c
[2] = fromHex(c
[2]);
114 } else if (c
[1] == '0' && isodigit(c
[2])) {
115 if (isodigit(c
[3])) {
117 if (isodigit(c
[4])) {
118 c
[4] = fromOctal(c
[2])*64
124 c
[3] = fromOctal(c
[2])*8
130 c
[2] = fromOctal(c
[2]);
135 case '0': c
[1] = '\0'; break;
136 case '\'': c
[1] = '\''; break;
137 case '\\': c
[1] = '\\'; break;
138 case '"': c
[1] = '"'; break;
139 case 'a': c
[1] = '\a'; break;
140 case 'b': c
[1] = '\b'; break;
141 case 't': c
[1] = '\t'; break;
142 case 'v': c
[1] = '\v'; break;
143 case 'f': c
[1] = '\f'; break;
144 case 'r': c
[1] = '\r'; break;
152 char *trimquotes(char *c
)
155 r
[strlen(r
)-1] = '\0';
159 void pdie(const char *msg
)
165 void die(const char *msg
, ...)
169 vfprintf(stderr
, msg
, ap
);
174 void pindent(int indent
, FILE *out
)
176 for (int i
= 0; i
<indent
; i
++)
177 if (fputc('\t', out
) == EOF
)
181 void safe_fprintf(FILE *out
, const char *msg
, ...)
185 int r
= vfprintf(out
, msg
, ap
);
191 void *safe_malloc(size_t size
)
193 void *res
= malloc(size
);
199 void *safe_strdup(const char *c
)
201 char *res
= strdup(c
);