-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescapec.c
153 lines (133 loc) · 3.66 KB
/
escapec.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* escapec
* A tool to escape text for use as C/C++ string literals
*
* Copyright (C) 2025 Sau P
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#define program_name escapec
int print_usage(const char *program) {
fprintf(stderr,
"Usage: %s [OPTIONS] [FILE]\n"
"Escapes text so that it can be used for string literals (C / C++).\n"
"Options:\n"
" -s, --split-lines Break output on newlines.\n"
" -p, --percent Do not escape %% \n"
" -n, --null End output with null terminator, not a "
"new-line (the default).\n"
" -h, --help Display this help message.\n"
"\nIf FILE is not specified, data is read from standard input.\n",
program);
return 0;
}
void convert_to_cstr(FILE *input, FILE *output, bool split_lines,
bool escape_percent) {
int c;
bool first_line = true;
fprintf(output, "\"");
while ((c = fgetc(input)) != EOF) {
switch (c) {
case '\f':
fprintf(output, "\\f");
break;
case '\n':
if (split_lines) {
fprintf(output, "\\n\"\n\"");
first_line = false;
} else {
fprintf(output, "\\n");
}
break;
case '\r':
fprintf(output, "\\r");
break;
case '\t':
fprintf(output, "\\t");
break;
case '\"':
fprintf(output, "\\\"");
break;
case '\\':
fprintf(output, "\\\\");
break;
case '%':
if (!escape_percent) {
fprintf(output, "%%");
} else {
fprintf(output, "%%%%");
}
break;
default:
fputc(c, output);
break;
}
}
fprintf(output, "\"");
}
/* Main */
int main(int argc, char *argv[]) {
bool split_lines = false;
bool percent = true;
bool null_term = false;
FILE *input = stdin;
struct option long_options[] = {{"split-lines", no_argument, 0, 's'},
{"percent", no_argument, 0, 'p'},
{"null", no_argument, 0, 'n'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}};
int opt;
int option_index = 0;
while ((opt = getopt_long(argc, argv, "spnh", long_options, &option_index)) !=
-1) {
switch (opt) {
case 's':
split_lines = true;
break;
case 'p':
percent = false;
break;
case 'n':
null_term = true;
break;
case 'h':
print_usage(argv[0]);
return 0;
default:
print_usage(argv[0]);
return 1;
}
}
/* Check if we have a filename as an argument */
if (optind < argc) {
input = fopen(argv[optind], "r");
if (!input) {
fprintf(stderr, "Error: Could not open file '%s'\n", argv[optind]);
return 1;
}
}
convert_to_cstr(input, stdout, split_lines, percent);
if (input != stdin) {
fclose(input);
}
if (!null_term) {
fputc('\n', stdout);
}
fputc('\0', stdout);
fflush(stdout);
return 0;
}