main.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* tripl/main.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * tripl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <sys/mman.h>
  22. #include <sys/types.h>
  23. #include <tripl/tripl.h>
  24. int usage(void);
  25. char *basename, *fbuffer, *fname;
  26. int main(int argc, const char **argv) {
  27. off_t fsize;
  28. int fd;
  29. int retstat;
  30. basename = (char *) argv[0];
  31. if (argc != 2)
  32. return usage();
  33. if (unlikely(!(fname = (char *) argv[1])))
  34. return usage();
  35. if (unlikely((fd = open(fname, O_RDONLY)) < 0)) {
  36. perror(basename);
  37. return usage();
  38. }
  39. fsize = lseek(fd, 0, SEEK_END);
  40. lseek(fd, 0, SEEK_SET);
  41. if (unlikely((fbuffer = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)) {
  42. perror(basename);
  43. return usage();
  44. }
  45. retstat = do_tripl_compile(fbuffer, fsize);
  46. munmap(fbuffer, fsize);
  47. close(fd);
  48. if (retstat) {
  49. error("compliation failed");
  50. return retstat;
  51. }
  52. return 0;
  53. }
  54. int usage(void) {
  55. error("it is very easy to learn how to use tripl :-)");
  56. error("Usage: tripl FILE");
  57. return 1;
  58. }