-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathse-show.c
More file actions
94 lines (71 loc) · 1.38 KB
/
se-show.c
File metadata and controls
94 lines (71 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* S-Expressions: Pretty Printer
*
* Copyright (c) 2019-2023 Alexei A. Smekalkine
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <ctype.h>
#include "se-int.h"
static int str_is_simple (const char *s)
{
for (; *s != '\0'; ++s)
if (*s == '"' || *s == '\\' || !isgraph (*s))
return 0;
return 1;
}
static void show_escaped (const char *s, FILE *to)
{
if (str_is_simple (s)) {
fprintf (to, "%s", s);
return;
}
fputc ('"', to);
for (; *s != '\0'; ++s)
switch (*s) {
case '"':
case '\\':
fprintf (to, "\\%c", *s);
break;
default:
if (isprint (*s))
fputc (*s, to);
else
fprintf (to, "\\%03o", *s);
}
fputc ('"', to);
}
static int se_show_list (struct se *o, FILE *to)
{
struct se *p;
for (p = o; !se_is_atom (p); p = se_to_pair (p)->tail) {}
if (p != NULL)
return 0;
fputs ("(", to);
if (o != NULL)
for (;;) {
se_show (se_to_pair (o)->head, to);
if ((o = se_to_pair (o)->tail) == NULL)
break;
fputc (' ', to);
}
fputs (")", to);
return 1;
}
static int se_show_atom (struct se *o, FILE *to)
{
if (!se_is_atom (o))
return 0;
show_escaped (se_atom_name (o), to);
return 1;
}
void se_show (struct se *o, FILE *to)
{
if (se_show_list (o, to) || se_show_atom (o, to))
return;
fputs ("(", to);
se_show (se_to_pair (o)->head, to);
fputs (" . ", to);
se_show (se_to_pair (o)->tail, to);
fputs (")", to);
}