parse.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "main.h"
  2. #include "parse.h"
  3. const char *newline = "\n";
  4. const char *equal = "=";
  5. const char *space = " ";
  6. // Check if a file exists (index.html)
  7. int file_exists(char *fname) {
  8. struct stat buffer;
  9. if (stat (fname, &buffer) == 0)
  10. return 0;
  11. else
  12. return 1;
  13. }
  14. // Check if the path is a directory (darklight)
  15. int is_dir(char* p) {
  16. char * stripslash;
  17. struct stat st;
  18. stripslash = p + 1; // strip the first forward 'slash' from the string
  19. if (stat(stripslash, &st) == 0 && (st.st_mode & S_IFDIR)) {
  20. return 1;
  21. }
  22. else if (stat(stripslash, &st) == 0 && (st.st_mode & S_IFREG)) {
  23. return 2;
  24. }
  25. return 0;
  26. }
  27. // write to struct
  28. void parse(char *filename)
  29. {
  30. // open config as readable
  31. FILE *file = fopen (filename, "r");
  32. printf("Parsing %s\n", filename);
  33. // if file is null, end
  34. if (file != NULL)
  35. {
  36. // line buffer for config
  37. char line[CONFBUF];
  38. // int used to track config line
  39. //int i = 0;
  40. // config while loop, loops fgets until end of file
  41. while(fgets(line, sizeof(line), file) != NULL)
  42. {
  43. char *cfline; // setup string
  44. cfline = strtok(line, newline);
  45. // If its the crunchbang, note for now
  46. if (strncmp("#!",line,2)==0) {
  47. continue;
  48. }
  49. // if line is commented out, skip
  50. if (strncmp("#",line,1)==0) {
  51. continue;
  52. }
  53. if (strncmp(newline,line,1)==0) {
  54. continue;
  55. }
  56. char *argvlist[50];
  57. int argc = 0;
  58. char *moocow = cfline;
  59. // Replace tabs with spaces
  60. while((cfline = strtok(moocow, space)) != NULL) {
  61. moocow = NULL;
  62. argvlist[argc++] = cfline;
  63. }
  64. cdsh_execute(argvlist);
  65. } // End while
  66. } // End if file
  67. fclose(file);
  68. cdsh_exit(0);
  69. }