c-error.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. ** The goal of this program is to find errors in c code.
  3. **
  4. ** open the file
  5. ** parse the file, spotting errors along the way
  6. **
  7. ** Copyright @ 2020 Joshua Branson <jbranso@dismail.de>
  8. **
  9. ** This program is free software; you can redistribute it and/or modify it
  10. ** under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 3 of the License, or (at
  12. ** your option) any later version.
  13. **
  14. ** It is distributed in the hope that it will be useful, but
  15. ** WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. //#include <fcntl.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <argp.h>
  27. //#include <sys/mman.h> //mmap
  28. #include <sys/stat.h>
  29. //#include <unistd.h>
  30. #include "structs.h"
  31. char fileName [128];
  32. char * program_invocation_short_name;
  33. const char * argp_program_version = "0.1";
  34. const char * argp_program_bug_address = "jbranso@dismail.de";
  35. static const struct argp_option options [] =
  36. {
  37. {"file" , 'f', "FILE", OPTION_ARG_OPTIONAL, "Output the caesar cipher of FILE." },
  38. { 0 }
  39. };
  40. //define an argp parse function
  41. error_t argp_parser (int opt, char *arg, struct argp_state *state)
  42. {
  43. extern char fileName [];
  44. switch (opt)
  45. {
  46. // if this parser function is called on an option that it doesn't recognize, then don't do anything.
  47. default:
  48. return ARGP_ERR_UNKNOWN;
  49. case 'f':
  50. {
  51. memcpy (fileName, arg, strlen (arg));
  52. break;
  53. }
  54. }
  55. return 0;
  56. }
  57. /* a string containing the basic usage of this program. */
  58. struct argp argp =
  59. {
  60. options, argp_parser, 0,
  61. "A simple program implementing a caesar cipher.\nThe default shift is 1."
  62. };
  63. /* This function will return a buffer whose contents match that of the
  64. input file. */
  65. FILE * maybe_open_file ()
  66. {
  67. extern char fileName [];
  68. extern char * program_invocation_short_name;
  69. FILE * stream;
  70. if (strlen (fileName) > 0)
  71. {
  72. if ((stream = fopen (fileName, "r")) == NULL)
  73. {
  74. fprintf (stderr, "%s: Couldn't open file %s; %s\n",
  75. program_invocation_short_name, fileName, strerror (errno));
  76. exit (EXIT_FAILURE);
  77. }
  78. else
  79. return stream;
  80. }
  81. else
  82. return stdin;
  83. }
  84. /* This function puts the file in the buffer and returns it. */
  85. char * get_buffer (FILE * stream)
  86. {
  87. /* size_t page_size = (size_t) sysconf (_SC_PAGESIZE); */
  88. /* int filedes = shm_open (fileName, O_RDONLY, S_IRWXU); */
  89. /* shm_unlink (fileName); */
  90. /* return mmap (NULL, page_size, PROT_WRITE, MAP_PRIVATE, filedes, 0); */
  91. extern char fileName[];
  92. char * buf;
  93. struct stat * file_attrib = malloc (sizeof (struct stat));
  94. stat (fileName, file_attrib);
  95. buf = malloc (file_attrib->st_size + 1);
  96. char c;
  97. int i;
  98. for (i = 0; ((c = getc (stream)) != EOF); i++)
  99. *(buf + i) = c;
  100. *(buf + i) = '\0';
  101. return buf;
  102. }
  103. char * remove_backslashes_from_buffer (FILE * stream)
  104. {
  105. char * c;
  106. return c;
  107. }
  108. char * remove_comments_from_buffer (char * buf)
  109. {
  110. char c, d;
  111. for (int i = 0; ((c = *(buf + i) ) != '\0'); i++)
  112. {
  113. // for now this just removes line comments like this comment.
  114. //loop forward until you see a '/'
  115. if (c == '/')
  116. {
  117. if ((d = ( *(buf + i + 1))) == '/')
  118. {
  119. //loop forward until you see a '\n'.
  120. i += 2;
  121. while (*(buf + i) != '\n')
  122. i++;
  123. }
  124. else
  125. {
  126. // if the next char is not a '\', then put the
  127. // "\\[a-zA-Z0-9]" in the buffer. eg: if you find a "'\a'"
  128. // then just put it in the buffer.
  129. i++;
  130. continue;
  131. }
  132. }
  133. else
  134. {
  135. *(buf + i) = c;
  136. }
  137. }
  138. }
  139. struct string_t * parse_buffer_into_string_ts (char * c)
  140. {
  141. struct string_t * strings;
  142. strings = malloc (sizeof (struct string_t));
  143. return strings;
  144. }
  145. int main (int argc, char **argv)
  146. {
  147. argp_parse (&argp, argc, argv, 0, 0, 0);
  148. FILE * stream = maybe_open_file ();
  149. char * buf = get_buffer (stream);
  150. printf ("%s\n", buf);
  151. remove_comments_from_buffer (buf);
  152. printf ("%s\n", buf);
  153. return 0;
  154. }