main.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. basename = (char *) argv[0];
  30. if (argc != 2)
  31. return usage();
  32. if (!(fname = (char *) argv[1]))
  33. return usage();
  34. if ((fd = open(fname, O_RDONLY)) < 0) {
  35. perror(basename);
  36. return usage();
  37. }
  38. fsize = lseek(fd, 0, SEEK_END);
  39. lseek(fd, 0, SEEK_SET);
  40. if ((fbuffer = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
  41. perror(basename);
  42. return usage();
  43. }
  44. do_tripl_compile(fbuffer, fsize);
  45. munmap(fbuffer, fsize);
  46. close(fd);
  47. return 0;
  48. }
  49. int usage(void) {
  50. error("it is very easy to learn how to use tripl :-)");
  51. error("Usage: tripl FILE");
  52. return 1;
  53. }