-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstr.c
More file actions
167 lines (137 loc) · 4.15 KB
/
cstr.c
File metadata and controls
167 lines (137 loc) · 4.15 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Part of liwc, a collection of tools for manipulating C source code
* Copyright (c) 1994-2003 Lars Wirzenius
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
/*
* ccstr.c -- print out C string literals
* Lars Wirzenius
*
* Usage: cstr [-hv] [--help] [--version] [file...]
* All output is to the standard output.
*/
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <publib.h>
/*
* Special marks for input characters used in the description of the FSM.
*/
#define DFL UCHAR_MAX+1 /* any character */
#define NONE UCHAR_MAX+2 /* don't print any character */
#define SELF UCHAR_MAX+3 /* print last input character */
/*
* Possible states for the FSM.
*/
enum state {
code, chr_lit, chr_esc, str_lit, str_esc, slash, c_cmt, star
};
/*
* Rules for describing the state changes and associated actions for the
* FSM.
*/
struct rule {
enum state state;
int c;
enum state new_state;
int print1, print2, print3, print4;
};
/*
* The FSM itself.
*/
static const struct rule fsm[] = {
{ code, '"', str_lit, NONE, NONE, NONE, NONE },
{ code, '\'', chr_lit, NONE, NONE, NONE, NONE },
{ code, '/', slash, NONE, NONE, NONE, NONE },
{ code, DFL, code, NONE, NONE, NONE, NONE },
{ str_lit, '\\', str_esc, SELF, NONE, NONE, NONE },
{ str_lit, '"', code, '\n', NONE, NONE, NONE },
{ str_lit, DFL, str_lit, SELF, NONE, NONE, NONE },
{ str_esc, DFL, str_lit, SELF, NONE, NONE, NONE },
{ chr_lit, '\\', chr_esc, NONE, NONE, NONE, NONE },
{ chr_lit, '\'', code, NONE, NONE, NONE, NONE },
{ chr_lit, DFL, chr_lit, NONE, NONE, NONE, NONE },
{ chr_esc, DFL, chr_lit, NONE, NONE, NONE, NONE },
{ slash, '/', slash, NONE, NONE, NONE, NONE },
{ slash, '*', c_cmt, NONE, NONE, NONE, NONE },
{ slash, DFL, code, NONE, NONE, NONE, NONE },
{ c_cmt, '*', star, NONE, NONE, NONE, NONE },
{ c_cmt, DFL, c_cmt, NONE, NONE, NONE, NONE },
{ star, '/', code, NONE, NONE, NONE, NONE },
{ star, '*', star, NONE, NONE, NONE, NONE },
{ star, DFL, c_cmt, NONE, NONE, NONE, NONE },
};
static int cstr(FILE *f, char *filename, void *);
static const char usage[] = "[-hv] [file ...]\n";
static const char version[] = "version 1.0";
/*
* Do the usual main() stuff: interpret options, process all input files.
*/
int main(int argc, char **argv) {
int opt;
set_progname(argv[0], "cstr");
while ((opt = getopt(argc, argv, "hv")) != EOF) {
switch (opt) {
case 'h':
case '?':
errormsg(1, 0, "%s", usage);
case 'v':
errormsg(1, 0, "%s", version);
}
}
if (main_filter(argc-optind, argv+optind, cstr, NULL) == -1)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
/*
* Process one file. Output is to stdout. Return 0 for OK, -1 for error.
*/
#define print(x) (void)((x) != NONE && printf("%c", (x) == SELF ? c : (x)))
static int cstr(FILE *f, char *filename, void *dummy) {
enum state state;
const struct rule *p;
int c, i;
state = code;
for (;;) {
c = getc(f);
if (c == EOF) {
if (ferror(f)) {
errormsg(0, -1, "error reading %s\n");
return -1;
}
if (state != code) {
errormsg(0, 0, "%s ends funnily\n", filename);
return -1;
}
return 0; /* everything seems to be OK */
}
for (i = 0; i < sizeof(fsm) / sizeof(fsm[0]); ++i) {
p = &fsm[i];
if (p->state == state && (p->c == c || p->c == DFL)) {
state = p->new_state;
print(p->print1);
print(p->print2);
print(p->print3);
print(p->print4);
break;
}
}
}
}