main.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <string.h>
  22. #include <sys/mman.h>
  23. #include <sys/types.h>
  24. #include <tripl/tripl.h>
  25. int usage(void);
  26. char *basename, *fbuffer, *fname;
  27. int main(int argc, const char **argv) {
  28. off_t fsize;
  29. int fd;
  30. int retstat;
  31. basename = (char *) argv[0];
  32. if (argc < 2)
  33. return usage();
  34. if (unlikely(!(fname = (char *) argv[1])))
  35. return usage();
  36. /* i don't like getopt()... */
  37. if (!strcmp(argv[1], "-c")) {
  38. fname = strdup("stdin");
  39. fbuffer = (char *) argv[2];
  40. fsize = strlen(fbuffer);
  41. fd = 0;
  42. fbuffer[fsize] = 0;
  43. goto compile;
  44. }
  45. if (unlikely((fd = open(fname, O_RDONLY)) < 0)) {
  46. perror(basename);
  47. return usage();
  48. }
  49. fsize = lseek(fd, 0, SEEK_END);
  50. lseek(fd, 0, SEEK_SET);
  51. if (unlikely((fbuffer = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)) {
  52. perror(basename);
  53. return usage();
  54. }
  55. compile:
  56. retstat = do_tripl_compile(fbuffer, fsize);
  57. if (fbuffer == argv[2])
  58. goto skip;
  59. munmap(fbuffer, fsize);
  60. close(fd);
  61. skip: if (retstat) {
  62. error("compliation failed");
  63. return retstat;
  64. }
  65. return 0;
  66. }
  67. int usage(void) {
  68. error("it is very easy to learn how to use tripl :-)");
  69. error("Usage: tripl FILE");
  70. return 1;
  71. }