Motr  M0
ff2c.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
79 #include <err.h>
80 #include <sysexits.h>
81 #include <unistd.h> /* getopt, close, open */
82 #include <sys/mman.h> /* mmap, munmap */
83 #include <sys/types.h>
84 #include <sys/stat.h> /* stat */
85 #include <fcntl.h> /* O_RDONLY */
86 #include <libgen.h> /* dirname */
87 #include <string.h> /* basename, strdup, strlen */
88 #include <stdlib.h> /* malloc, free */
89 #include <ctype.h> /* toupper */
90 #include <stdio.h> /* fopen, fclose */
91 
92 #include "xcode/ff2c/lex.h"
93 #include "xcode/ff2c/parser.h"
94 #include "xcode/ff2c/sem.h"
95 #include "xcode/ff2c/gen.h"
96 
97 int main(int argc, char **argv)
98 {
99  int fd;
100  int optch;
101  int result;
102  const char *path;
103  void *addr;
104  struct stat buf;
105  char *scratch;
106  char *ch;
107  char *bname;
108  char *gname;
109  char *out_h;
110  char *out_c;
111  size_t len;
112  FILE *c;
113  FILE *h;
114 
115  struct ff2c_context ctx;
116  struct ff2c_term *t;
117  struct ff2c_ff ff;
118  struct ff2c_gen_opt opt;
119 
120  while ((optch = getopt(argc, argv, "")) != -1) {
121  }
122 
123  path = argv[optind];
124  fd = open(path, O_RDONLY);
125  if (fd == -1)
126  err(EX_NOINPUT, "cannot open \"%s\"", path);
127  result = fstat(fd, &buf);
128  if (result == -1)
129  err(EX_UNAVAILABLE, "cannot fstat \"%s\"", path);
130  addr = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
131  if (addr == MAP_FAILED)
132  err(EX_OSERR, "cannot mmap \"%s\"", path);
133 
134  scratch = fmt("%s", path);
135  len = strlen(scratch);
136  if (len > 3 && strcmp(scratch + len - 3, ".ff") == 0)
137  *(scratch + len - 3) = 0;
138 
139  out_h = fmt("%s_ff.h", scratch);
140  out_c = fmt("%s_ff.c", scratch);
141 
142  bname = basename(scratch);
143  gname = fmt("__MOTR_%s_%s_FF_H__", basename(dirname(scratch)), bname);
144 
145  for (ch = gname; *ch != 0; ch++) {
146  *ch = toupper(*ch);
147  if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", *ch) == NULL)
148  *ch = '_';
149  }
150 
151  opt.go_basename = bname;
152  opt.go_guardname = gname;
153 
154  c = fopen(out_c, "w");
155  if (c == NULL)
156  err(EX_CANTCREAT, "cannot open \"%s\" for writing", out_c);
157 
158  h = fopen(out_h, "w");
159  if (h == NULL)
160  err(EX_CANTCREAT, "cannot open \"%s\" for writing", out_h);
161 
162  memset(&ctx, 0, sizeof ctx);
163  memset(&ff, 0, sizeof ff);
164 
165  ff2c_context_init(&ctx, addr, buf.st_size);
166  result = ff2c_parse(&ctx, &t);
167  if (result != 0)
168  err(EX_DATAERR, "cannot parse");
169 
170  ff2c_sem_init(&ff, t);
171 
172  opt.go_out = h;
173  ff2c_h_gen(&ff, &opt);
174  opt.go_out = c;
175  ff2c_c_gen(&ff, &opt);
176 
177  ff2c_sem_fini(&ff);
178  ff2c_term_fini(t);
180 
181  fclose(h);
182  fclose(c);
183 
184  free(out_c);
185  free(out_h);
186  free(gname);
187  free(scratch);
188  result = munmap(addr, buf.st_size);
189  if (result == -1)
190  warn("cannot munmap");
191  result = close(fd);
192  if (result == -1)
193  warn("cannot close");
194  return EXIT_SUCCESS;
195 }
196 
199 /*
200  * Local variables:
201  * c-indentation-style: "K&R"
202  * c-basic-offset: 8
203  * tab-width: 8
204  * fill-column: 80
205  * scroll-step: 1
206  * End:
207  */
int optind
M0_INTERNAL int struct dentry struct kstat * stat
Definition: dir.c:1433
#define NULL
Definition: misc.h:38
void ff2c_context_fini(struct ff2c_context *ctx)
Definition: lex.c:230
void ff2c_sem_fini(struct ff2c_ff *ff)
Definition: sem.c:267
Definition: sem.h:90
FILE * go_out
Definition: gen.h:41
Definition: sock.c:887
const char * go_guardname
Definition: gen.h:40
static int ff(struct ff2c_context *ctx, struct ff2c_term *term)
Definition: parser.c:212
static char * addr
Definition: node_k.c:37
int ff2c_c_gen(const struct ff2c_ff *ff, const struct ff2c_gen_opt *opt)
Definition: gen.c:198
static struct m0_addb2_callback c
Definition: consumer.c:41
static struct m0_thread t[8]
Definition: service_ut.c:1230
int main(int argc, char **argv)
Definition: ff2c.c:97
int ff2c_h_gen(const struct ff2c_ff *ff, const struct ff2c_gen_opt *opt)
Definition: gen.c:93
char * fmt(const char *format,...) __attribute__((format(printf
Definition: xcode.h:73
void ff2c_sem_init(struct ff2c_ff *ff, struct ff2c_term *top)
Definition: sem.c:247
static const char * bname(const struct btype *bt)
Definition: beck.c:1205
struct m0t1fs_filedata * fd
Definition: dir.c:1030
const char * go_basename
Definition: gen.h:39
Definition: nucleus.c:42
void ff2c_term_fini(struct ff2c_term *term)
Definition: parser.c:231
void ff2c_context_init(struct ff2c_context *ctx, const char *buf, size_t size)
Definition: lex.c:224
int ff2c_parse(struct ff2c_context *ctx, struct ff2c_term **out)
Definition: parser.c:225