Changeset 6 in Main
- Timestamp:
- Sep 13, 2024, 7:28:20 PM (2 months ago)
- Location:
- trunk
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/Makefile
r3 r6 11 11 12 12 common.a: $(OBJS) 13 arrcs $@ $(OBJS)13 $(AR) rcs $@ $(OBJS) 14 14 15 15 .c.o: -
trunk/Common/log.c
r3 r6 40 40 result = cm_strcat(tmp, va_arg(args, char*)); 41 41 free(tmp); 42 } else if(log[i] == 'd') { 43 int a = va_arg(args, int); 44 char buf[128]; 45 sprintf(buf, "%d", a); 46 char* tmp = result; 47 result = cm_strcat(tmp, buf); 48 free(tmp); 42 49 } 43 50 } else { -
trunk/Common/string.c
r5 r6 16 16 char* cm_strdup(const char* str) { return cm_strcat(str, ""); } 17 17 18 char* cm_trimstart(const char* str) {18 char* cm_trimstart(const char* str) { 19 19 int i; 20 for(i = 0; str[i] != 0; i++) {21 if(str[i] != ' ' && str[i] != '\t') {20 for(i = 0; str[i] != 0; i++) { 21 if(str[i] != ' ' && str[i] != '\t') { 22 22 return cm_strdup(str + i); 23 23 } … … 26 26 } 27 27 28 char* cm_trimend(const char* str) {28 char* cm_trimend(const char* str) { 29 29 char* s = cm_strdup(str); 30 30 int i; 31 for(i = strlen(s) - 1; i >= 0; i--) {32 if(s[i] != '\t' && s[i] != ' ') {31 for(i = strlen(s) - 1; i >= 0; i--) { 32 if(s[i] != '\t' && s[i] != ' ') { 33 33 s[i + 1] = 0; 34 34 break; … … 38 38 } 39 39 40 char* cm_trim(const char* str) {40 char* cm_trim(const char* str) { 41 41 char* tmp = cm_trimstart(str); 42 42 char* s = cm_trimend(tmp); … … 45 45 } 46 46 47 char** cm_split(const char* str, const char* by) {47 char** cm_split(const char* str, const char* by) { 48 48 int i; 49 49 char** r = malloc(sizeof(*r)); … … 55 55 bool dq = false; 56 56 bool sq = false; 57 for(i = 0;; i++) {57 for(i = 0;; i++) { 58 58 int j; 59 59 bool has = false; 60 for(j = 0; by[j] != 0; j++) {61 if(by[j] == str[i]) {60 for(j = 0; by[j] != 0; j++) { 61 if(by[j] == str[i]) { 62 62 has = true; 63 63 break; 64 64 } 65 65 } 66 if(!(dq || sq) && (has || str[i] == 0)) {67 if(strlen(b) > 0) {66 if(!(dq || sq) && (has || str[i] == 0)) { 67 if(strlen(b) > 0) { 68 68 char** old = r; 69 69 int j; 70 for(j = 0; old[j] != NULL; j++); 70 for(j = 0; old[j] != NULL; j++) 71 ; 71 72 r = malloc(sizeof(*r) * (j + 2)); 72 73 for(j = 0; old[j] != NULL; j++) r[j] = old[j]; … … 78 79 b[0] = 0; 79 80 if(str[i] == 0) break; 80 } else{81 if(str[i] == '"' && !sq) {81 } else { 82 if(str[i] == '"' && !sq) { 82 83 dq = !dq; 83 } else if(str[i] == '\'' && !dq){84 } else if(str[i] == '\'' && !dq) { 84 85 sq = !sq; 85 } else{86 } else { 86 87 cbuf[0] = str[i]; 87 88 char* tmp = b; … … 95 96 } 96 97 97 bool cm_strcaseequ(const char* a, const char* b) {98 bool cm_strcaseequ(const char* a, const char* b) { 98 99 if(a == NULL) return false; 99 100 if(b == NULL) return false; 100 101 if(strlen(a) != strlen(b)) return false; 101 102 int i; 102 for(i = 0; a[i] != 0; i++) {103 for(i = 0; a[i] != 0; i++) { 103 104 if(tolower(a[i]) != tolower(b[i])) return false; 104 105 } -
trunk/Makefile
r4 r6 18 18 19 19 format: 20 clang-format --verbose -i `find . -name "*.c" -or -name "*.h"`20 clang-format --verbose -i `find ./Server ./Common -name "*.c" -or -name "*.h"` 21 21 22 22 clean: -
trunk/Platform/generic.mk
r3 r6 6 6 LDFLAGS = 7 7 LIBS = 8 EXEC = -
trunk/Server/Makefile
r4 r6 11 11 12 12 tewi$(EXEC): $(OBJS) ../Common/common.a 13 $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) ../Common/common.a 13 $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) ../Common/common.a -lssl -lcrypto 14 14 15 15 .c.o: -
trunk/Server/config.c
r5 r6 10 10 #include <cm_log.h> 11 11 12 int tw_config_read(const char* path){ 12 struct tw_config config; 13 14 void tw_config_init(void) {} 15 16 int tw_config_read(const char* path) { 13 17 cm_log("Config", "Reading %s", path); 14 18 char cbuf[2]; 15 19 cbuf[1] = 0; 20 int ln = 0; 16 21 FILE* f = fopen(path, "r"); 17 if(f != NULL) {22 if(f != NULL) { 18 23 char* line = malloc(1); 19 24 line[0] = 0; 20 while(1){ 25 int stop = 0; 26 char* vhost = NULL; 27 while(stop == 0) { 21 28 int c = fread(cbuf, 1, 1, f); 22 if(cbuf[0] == '\n' || c <= 0){ 29 if(cbuf[0] == '\n' || c <= 0) { 30 ln++; 23 31 char* l = cm_trim(line); 24 if(strlen(l) > 0 && l[0] != '#') {32 if(strlen(l) > 0 && l[0] != '#') { 25 33 char** r = cm_split(l, " \t"); 26 34 int i; 27 if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")){ 28 for(i = 1; r[i] != NULL; i++){ 29 if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")){ 30 for(i = 0; r[i] != NULL; i++) free(r[i]); 31 free(r); 32 free(line); 33 free(l); 34 fclose(f); 35 return 1; 35 if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) { 36 for(i = 1; r[i] != NULL; i++) { 37 if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) { 38 stop = 1; 39 break; 36 40 } 37 41 } 42 } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) { 43 if(vhost != NULL) { 44 cm_log("Config", "Already in virtual host section"); 45 stop = 1; 46 } else { 47 if(r[1] == NULL) { 48 cm_log("Config", "Missing virtual host"); 49 stop = 1; 50 } else { 51 vhost = cm_strdup(r[1]); 52 } 53 } 54 } else if(cm_strcaseequ(r[0], "EndVirtualHost")) { 55 if(vhost == NULL) { 56 cm_log("Config", "Not in virtual host section"); 57 stop = 1; 58 } else { 59 free(vhost); 60 vhost = NULL; 61 } 62 } else { 63 if(r[0] != NULL) { 64 cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln); 65 } 66 stop = 1; 38 67 } 39 68 for(i = 0; r[i] != NULL; i++) free(r[i]); … … 45 74 line[0] = 0; 46 75 if(c <= 0) break; 47 } else if(cbuf[0] != '\r'){76 } else if(cbuf[0] != '\r') { 48 77 char* tmp = line; 49 78 line = cm_strcat(tmp, cbuf); … … 53 82 free(line); 54 83 fclose(f); 55 return 0;56 } else{84 return stop; 85 } else { 57 86 cm_log("Config", "Could not open the file"); 58 87 return 1; -
trunk/Server/main.c
r4 r6 4 4 #include <stdbool.h> 5 5 #include <string.h> 6 7 #include <openssl/opensslv.h> 6 8 7 9 #include <cm_log.h> … … 20 22 if(!cm_do_log) { 21 23 cm_do_log = true; 22 cm_log("", "This is Tewi HTTPd, version %s ", tw_get_version());24 cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT); 23 25 } else { 24 26 cm_do_log = true; 25 27 } 26 } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {28 } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) { 27 29 i++; 28 if(argv[i] == NULL) {30 if(argv[i] == NULL) { 29 31 fprintf(stderr, "Missing argument\n"); 30 32 return 1; … … 37 39 } 38 40 } 39 if(tw_config_read(config) != 0){ 41 tw_config_init(); 42 if(tw_config_read(config) != 0) { 40 43 fprintf(stderr, "Could not read the config\n"); 41 44 return 1; -
trunk/Server/tw_config.h
r4 r6 4 4 #define __TW_CONFIG_H__ 5 5 6 struct tw_config_entry {}; 7 8 struct tw_config { 9 struct tw_config_entry root; 10 }; 11 12 void tw_config_init(void); 6 13 int tw_config_read(const char* path); 7 14
Note:
See TracChangeset
for help on using the changeset viewer.