Thu, 23 Aug 2018 17:58:38 +0200
adds test file skeleton to build system
universe@10 | 1 | /* |
universe@34 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@48 | 3 | * Copyright 2017 Mike Becker. All rights reserved. |
universe@34 | 4 | * |
universe@34 | 5 | * Redistribution and use in source and binary forms, with or without |
universe@34 | 6 | * modification, are permitted provided that the following conditions are met: |
universe@34 | 7 | * |
universe@34 | 8 | * 1. Redistributions of source code must retain the above copyright |
universe@34 | 9 | * notice, this list of conditions and the following disclaimer. |
universe@34 | 10 | * |
universe@34 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@34 | 12 | * notice, this list of conditions and the following disclaimer in the |
universe@34 | 13 | * documentation and/or other materials provided with the distribution. |
universe@34 | 14 | * |
universe@34 | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@34 | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@34 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
universe@34 | 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
universe@34 | 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
universe@34 | 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
universe@34 | 21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
universe@34 | 22 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
universe@34 | 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
universe@34 | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
universe@34 | 25 | * |
universe@20 | 26 | * scanner.c |
universe@10 | 27 | * |
universe@10 | 28 | * Created on: 23.05.2011 |
universe@20 | 29 | * Author: Mike |
universe@10 | 30 | */ |
universe@1 | 31 | |
universe@1 | 32 | |
universe@10 | 33 | #include "scanner.h" |
universe@10 | 34 | #include "suffix_fnc.h" |
universe@21 | 35 | #include "bfile_heuristics.h" |
universe@27 | 36 | #include "regex_parser.h" |
universe@23 | 37 | #include <sys/stat.h> |
universe@3 | 38 | |
universe@41 | 39 | typedef struct filelist filelist_t; |
universe@23 | 40 | |
universe@41 | 41 | struct filelist { |
universe@41 | 42 | char *displayname; |
universe@41 | 43 | int displayname_len; |
universe@41 | 44 | char *filename; |
universe@41 | 45 | int st_mode; |
universe@41 | 46 | filelist_t *next; |
universe@41 | 47 | }; |
universe@41 | 48 | |
universe@41 | 49 | filelist_t *buildFileList(scanner_t scanner, settings_t* settings, |
universe@41 | 50 | filelist_t* list) { |
universe@41 | 51 | |
universe@23 | 52 | DIR *dirf; |
universe@3 | 53 | struct dirent *entry; |
universe@23 | 54 | struct stat statbuf; |
universe@41 | 55 | |
universe@23 | 56 | if ((dirf = opendir(scanner.dir)) == NULL) { |
universe@34 | 57 | printf("%s", scanner.dir); |
universe@23 | 58 | perror(" Directory access failed"); |
universe@23 | 59 | return 0; |
universe@23 | 60 | } |
universe@23 | 61 | |
universe@23 | 62 | while ((entry = readdir(dirf)) != NULL) { |
universe@3 | 63 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
universe@41 | 64 | |
universe@41 | 65 | /* Create new filelist entry */ |
universe@41 | 66 | filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t)); |
universe@42 | 67 | newentry->next = NULL; |
universe@41 | 68 | |
universe@42 | 69 | newentry->displayname_len = strlen(entry->d_name); |
universe@42 | 70 | newentry->displayname = (char*) malloc(newentry->displayname_len+1); |
universe@42 | 71 | memcpy(newentry->displayname, entry->d_name, newentry->displayname_len); |
universe@42 | 72 | newentry->displayname[newentry->displayname_len] = 0; |
universe@41 | 73 | |
universe@42 | 74 | newentry->st_mode = 0; |
universe@41 | 75 | |
universe@40 | 76 | /* Construct absolute pathname string */ |
universe@41 | 77 | size_t dirnamelen = strlen(scanner.dir); |
universe@42 | 78 | char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len); |
universe@41 | 79 | memcpy(filename, scanner.dir, dirnamelen); |
universe@41 | 80 | filename[dirnamelen] = settings->fileSeparator; |
universe@42 | 81 | memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len); |
universe@42 | 82 | filename[1+dirnamelen+newentry->displayname_len] = 0; |
universe@42 | 83 | newentry->filename = filename; |
universe@14 | 84 | |
universe@22 | 85 | /* Check for subdirectory */ |
universe@23 | 86 | if (stat(filename, &statbuf) == 0) { |
universe@42 | 87 | newentry->st_mode = statbuf.st_mode; |
universe@23 | 88 | } else { |
universe@23 | 89 | perror(" Error in stat call"); |
universe@3 | 90 | continue; |
universe@3 | 91 | } |
universe@42 | 92 | |
universe@42 | 93 | if (list) { |
universe@42 | 94 | // create fake root to have a pointer on the true root |
universe@42 | 95 | filelist_t root; |
universe@42 | 96 | root.next = list; |
universe@42 | 97 | filelist_t *parent = &root; |
universe@42 | 98 | while (parent->next && |
universe@42 | 99 | (strcasecmp(parent->next->displayname, newentry->displayname) < 0 || |
universe@42 | 100 | (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode)) |
universe@42 | 101 | ) && |
universe@42 | 102 | (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode)) |
universe@42 | 103 | ) { |
universe@42 | 104 | parent = parent->next; |
universe@42 | 105 | } |
universe@42 | 106 | newentry->next = parent->next; |
universe@42 | 107 | parent->next = newentry; |
universe@42 | 108 | list = root.next; |
universe@42 | 109 | } else { |
universe@42 | 110 | list = newentry; |
universe@42 | 111 | } |
universe@41 | 112 | } |
universe@41 | 113 | } |
universe@41 | 114 | |
universe@41 | 115 | closedir(dirf); |
universe@41 | 116 | |
universe@41 | 117 | return list; |
universe@41 | 118 | } |
universe@3 | 119 | |
universe@44 | 120 | int scanDirectory(scanner_t scanner, settings_t* settings, |
universe@44 | 121 | string_list_t* output) { |
universe@41 | 122 | |
universe@41 | 123 | int lines, a; |
universe@41 | 124 | int lineSum = 0; |
universe@41 | 125 | bool bfile; |
universe@44 | 126 | char *outbuf; |
universe@41 | 127 | |
universe@41 | 128 | filelist_t *filelist = buildFileList(scanner, settings, NULL); |
universe@41 | 129 | |
universe@41 | 130 | while (filelist != NULL) { |
universe@41 | 131 | |
universe@41 | 132 | /* Scan subdirectories */ |
universe@42 | 133 | if (!S_ISREG(filelist->st_mode)) { |
universe@44 | 134 | if (settings->recursive && S_ISDIR(filelist->st_mode)) { |
universe@44 | 135 | string_list_t *recoutput = new_string_list_t(); |
universe@44 | 136 | lines = scanDirectory( |
universe@44 | 137 | (scanner_t) {filelist->filename, scanner.spaces+1}, |
universe@44 | 138 | settings, recoutput); |
universe@44 | 139 | lineSum += lines; |
universe@44 | 140 | if (!settings->matchesOnly || recoutput->count > 0) { |
universe@44 | 141 | outbuf = (char*) malloc(81); |
universe@44 | 142 | snprintf(outbuf, 81, "%*s/%*s%13d lines\n", |
universe@44 | 143 | filelist->displayname_len+scanner.spaces, filelist->displayname, |
universe@44 | 144 | 60-filelist->displayname_len-scanner.spaces-1, "", lines); |
universe@44 | 145 | add_string(output, outbuf); |
universe@44 | 146 | for (int i = 0 ; i < recoutput->count ; i++) { |
universe@44 | 147 | add_string(output, recoutput->items[i]); |
universe@44 | 148 | } |
universe@44 | 149 | } |
universe@44 | 150 | destroy_string_list_t(recoutput); |
universe@44 | 151 | } else { |
universe@44 | 152 | outbuf = (char*) malloc(81); |
universe@44 | 153 | snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces, |
universe@41 | 154 | filelist->displayname); |
universe@44 | 155 | add_string(output, outbuf); |
universe@41 | 156 | } |
universe@41 | 157 | } else { |
universe@30 | 158 | if ((settings->includeSuffixes->count == 0 |
universe@41 | 159 | || testSuffix(filelist->displayname, settings->includeSuffixes)) |
universe@41 | 160 | && !testSuffix(filelist->displayname, settings->excludeSuffixes)) { |
universe@41 | 161 | |
universe@25 | 162 | /* Count lines */ |
universe@25 | 163 | lines = 0; |
universe@25 | 164 | bfile = false; |
universe@25 | 165 | bfile_reset(settings->bfileHeuristics); |
universe@54 | 166 | regex_parser_reset(settings->regex); |
universe@27 | 167 | char line_buffer[REGEX_MAX_LINELENGTH]; |
universe@25 | 168 | int line_buffer_offset = 0; |
universe@25 | 169 | |
universe@41 | 170 | FILE *file = fopen(filelist->filename, "r"); |
universe@3 | 171 | if (file == NULL) { |
universe@44 | 172 | outbuf = (char*) malloc(81); |
universe@44 | 173 | snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces, |
universe@41 | 174 | filelist->displayname); |
universe@44 | 175 | add_string(output, outbuf); |
universe@3 | 176 | perror(" File acces failed"); |
universe@41 | 177 | } else { |
universe@41 | 178 | do { |
universe@41 | 179 | a = fgetc(file); |
universe@3 | 180 | |
universe@41 | 181 | bfile = bfile_check(settings->bfileHeuristics, a); |
universe@3 | 182 | |
universe@41 | 183 | if (a == 10 || a == EOF) { |
universe@41 | 184 | line_buffer[line_buffer_offset] = 0; |
universe@41 | 185 | if (regex_parser_do(settings->regex, line_buffer) == 0) { |
universe@41 | 186 | /* Only subtract lines when matching has finished */ |
universe@41 | 187 | if (!regex_parser_matching(settings->regex)) { |
universe@41 | 188 | lines -= settings->regex->matched_lines; |
universe@41 | 189 | } |
universe@41 | 190 | } |
universe@21 | 191 | |
universe@41 | 192 | line_buffer_offset = 0; |
universe@41 | 193 | lines++; |
universe@41 | 194 | } else { |
universe@41 | 195 | if (line_buffer_offset < REGEX_MAX_LINELENGTH) { |
universe@41 | 196 | line_buffer[line_buffer_offset] = a; |
universe@41 | 197 | line_buffer_offset++; |
universe@41 | 198 | } else { |
universe@41 | 199 | line_buffer[line_buffer_offset-1] = 0; |
universe@41 | 200 | settings->confusing_lnlen = true; |
universe@28 | 201 | } |
universe@28 | 202 | } |
universe@41 | 203 | } while (!bfile && a != EOF); |
universe@41 | 204 | fclose(file); |
universe@25 | 205 | |
universe@41 | 206 | /* Print and sum line count */ |
universe@41 | 207 | if (bfile) { |
universe@41 | 208 | if (!settings->matchesOnly) { |
universe@44 | 209 | outbuf = (char*) malloc(81); |
universe@44 | 210 | snprintf(outbuf, 81, |
universe@44 | 211 | "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces, |
universe@41 | 212 | filelist->displayname, |
universe@41 | 213 | 60-filelist->displayname_len-scanner.spaces, "", "binary"); |
universe@44 | 214 | add_string(output, outbuf); |
universe@41 | 215 | } |
universe@25 | 216 | } else { |
universe@41 | 217 | lineSum += lines; |
universe@44 | 218 | outbuf = (char*) malloc(81); |
universe@44 | 219 | snprintf(outbuf, 81, "%*s%*s%13d lines\n", |
universe@41 | 220 | filelist->displayname_len+scanner.spaces, filelist->displayname, |
universe@41 | 221 | 60-filelist->displayname_len-scanner.spaces, "", lines); |
universe@44 | 222 | add_string(output, outbuf); |
universe@3 | 223 | } |
universe@21 | 224 | } |
universe@16 | 225 | } else { |
universe@3 | 226 | if (!settings->matchesOnly) { |
universe@22 | 227 | /* Print hint */ |
universe@44 | 228 | outbuf = (char*) malloc(81); |
universe@44 | 229 | snprintf(outbuf, 81, "%*s%*s%19s\n", |
universe@41 | 230 | filelist->displayname_len+scanner.spaces, filelist->displayname, |
universe@41 | 231 | 60-filelist->displayname_len-scanner.spaces, "", "no match"); |
universe@44 | 232 | add_string(output, outbuf); |
universe@3 | 233 | } |
universe@3 | 234 | } |
universe@3 | 235 | } |
universe@41 | 236 | |
universe@41 | 237 | free(filelist->filename); |
universe@41 | 238 | free(filelist->displayname); |
universe@41 | 239 | filelist_t *freethis = filelist; |
universe@41 | 240 | filelist = filelist->next; |
universe@41 | 241 | free(freethis); |
universe@3 | 242 | } |
universe@23 | 243 | |
universe@3 | 244 | return lineSum; |
universe@3 | 245 | } |