| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /* tripl/main.c
- *
- * This file is part of tripl.
- *
- * tripl 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.
- *
- * tripl 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 tripl. If not, see <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <tripl/tripl.h>
- int usage(void);
- char *basename, *fbuffer, *fname;
- int main(int argc, const char **argv) {
- off_t fsize;
- int fd;
- int retstat;
- basename = (char *) argv[0];
- if (argc < 2)
- return usage();
- if (unlikely(!(fname = (char *) argv[1])))
- return usage();
- /* i don't like getopt()... */
- if (!strcmp(argv[1], "-c")) {
- fname = strdup("stdin");
- fbuffer = (char *) argv[2];
- fsize = strlen(fbuffer);
- fd = 0;
- fbuffer[fsize] = 0;
- goto compile;
- }
- if (unlikely((fd = open(fname, O_RDONLY)) < 0)) {
- perror(basename);
- return usage();
- }
- fsize = lseek(fd, 0, SEEK_END);
- lseek(fd, 0, SEEK_SET);
- if (unlikely((fbuffer = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)) {
- perror(basename);
- return usage();
- }
- compile:
- retstat = do_tripl_compile(fbuffer, fsize);
- if (fbuffer == argv[2])
- goto skip;
- munmap(fbuffer, fsize);
- close(fd);
- skip: if (retstat) {
- error("compliation failed");
- return retstat;
- }
- return 0;
- }
- int usage(void) {
- error("it is very easy to learn how to use tripl :-)");
- error("Usage: tripl FILE");
- return 1;
- }
|