src/scanner.c

Thu, 23 Aug 2018 19:45:36 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Aug 2018 19:45:36 +0200
changeset 57
68018eac46c3
parent 54
76d46533b9a9
child 58
bb512754aaf3
permissions
-rw-r--r--

adds simple tiny test suite and updates license headers

universe@10 1 /*
universe@34 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@57 3 * Copyright 2018 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@57 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
universe@10 25 */
universe@1 26
universe@1 27
universe@10 28 #include "scanner.h"
universe@10 29 #include "suffix_fnc.h"
universe@21 30 #include "bfile_heuristics.h"
universe@27 31 #include "regex_parser.h"
universe@23 32 #include <sys/stat.h>
universe@3 33
universe@41 34 typedef struct filelist filelist_t;
universe@23 35
universe@41 36 struct filelist {
universe@41 37 char *displayname;
universe@41 38 int displayname_len;
universe@41 39 char *filename;
universe@41 40 int st_mode;
universe@41 41 filelist_t *next;
universe@41 42 };
universe@41 43
universe@41 44 filelist_t *buildFileList(scanner_t scanner, settings_t* settings,
universe@41 45 filelist_t* list) {
universe@41 46
universe@23 47 DIR *dirf;
universe@3 48 struct dirent *entry;
universe@23 49 struct stat statbuf;
universe@41 50
universe@23 51 if ((dirf = opendir(scanner.dir)) == NULL) {
universe@34 52 printf("%s", scanner.dir);
universe@23 53 perror(" Directory access failed");
universe@23 54 return 0;
universe@23 55 }
universe@23 56
universe@23 57 while ((entry = readdir(dirf)) != NULL) {
universe@3 58 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
universe@41 59
universe@41 60 /* Create new filelist entry */
universe@41 61 filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t));
universe@42 62 newentry->next = NULL;
universe@41 63
universe@42 64 newentry->displayname_len = strlen(entry->d_name);
universe@42 65 newentry->displayname = (char*) malloc(newentry->displayname_len+1);
universe@42 66 memcpy(newentry->displayname, entry->d_name, newentry->displayname_len);
universe@42 67 newentry->displayname[newentry->displayname_len] = 0;
universe@41 68
universe@42 69 newentry->st_mode = 0;
universe@41 70
universe@40 71 /* Construct absolute pathname string */
universe@41 72 size_t dirnamelen = strlen(scanner.dir);
universe@42 73 char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len);
universe@41 74 memcpy(filename, scanner.dir, dirnamelen);
universe@41 75 filename[dirnamelen] = settings->fileSeparator;
universe@42 76 memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len);
universe@42 77 filename[1+dirnamelen+newentry->displayname_len] = 0;
universe@42 78 newentry->filename = filename;
universe@14 79
universe@22 80 /* Check for subdirectory */
universe@23 81 if (stat(filename, &statbuf) == 0) {
universe@42 82 newentry->st_mode = statbuf.st_mode;
universe@23 83 } else {
universe@23 84 perror(" Error in stat call");
universe@3 85 continue;
universe@3 86 }
universe@42 87
universe@42 88 if (list) {
universe@42 89 // create fake root to have a pointer on the true root
universe@42 90 filelist_t root;
universe@42 91 root.next = list;
universe@42 92 filelist_t *parent = &root;
universe@42 93 while (parent->next &&
universe@42 94 (strcasecmp(parent->next->displayname, newentry->displayname) < 0 ||
universe@42 95 (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode))
universe@42 96 ) &&
universe@42 97 (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode))
universe@42 98 ) {
universe@42 99 parent = parent->next;
universe@42 100 }
universe@42 101 newentry->next = parent->next;
universe@42 102 parent->next = newentry;
universe@42 103 list = root.next;
universe@42 104 } else {
universe@42 105 list = newentry;
universe@42 106 }
universe@41 107 }
universe@41 108 }
universe@41 109
universe@41 110 closedir(dirf);
universe@41 111
universe@41 112 return list;
universe@41 113 }
universe@3 114
universe@44 115 int scanDirectory(scanner_t scanner, settings_t* settings,
universe@44 116 string_list_t* output) {
universe@41 117
universe@41 118 int lines, a;
universe@41 119 int lineSum = 0;
universe@41 120 bool bfile;
universe@44 121 char *outbuf;
universe@41 122
universe@41 123 filelist_t *filelist = buildFileList(scanner, settings, NULL);
universe@41 124
universe@41 125 while (filelist != NULL) {
universe@41 126
universe@41 127 /* Scan subdirectories */
universe@42 128 if (!S_ISREG(filelist->st_mode)) {
universe@44 129 if (settings->recursive && S_ISDIR(filelist->st_mode)) {
universe@44 130 string_list_t *recoutput = new_string_list_t();
universe@44 131 lines = scanDirectory(
universe@44 132 (scanner_t) {filelist->filename, scanner.spaces+1},
universe@44 133 settings, recoutput);
universe@44 134 lineSum += lines;
universe@44 135 if (!settings->matchesOnly || recoutput->count > 0) {
universe@44 136 outbuf = (char*) malloc(81);
universe@44 137 snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
universe@44 138 filelist->displayname_len+scanner.spaces, filelist->displayname,
universe@44 139 60-filelist->displayname_len-scanner.spaces-1, "", lines);
universe@44 140 add_string(output, outbuf);
universe@44 141 for (int i = 0 ; i < recoutput->count ; i++) {
universe@44 142 add_string(output, recoutput->items[i]);
universe@44 143 }
universe@44 144 }
universe@44 145 destroy_string_list_t(recoutput);
universe@44 146 } else {
universe@44 147 outbuf = (char*) malloc(81);
universe@44 148 snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
universe@41 149 filelist->displayname);
universe@44 150 add_string(output, outbuf);
universe@41 151 }
universe@41 152 } else {
universe@30 153 if ((settings->includeSuffixes->count == 0
universe@41 154 || testSuffix(filelist->displayname, settings->includeSuffixes))
universe@41 155 && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
universe@41 156
universe@25 157 /* Count lines */
universe@25 158 lines = 0;
universe@25 159 bfile = false;
universe@25 160 bfile_reset(settings->bfileHeuristics);
universe@54 161 regex_parser_reset(settings->regex);
universe@27 162 char line_buffer[REGEX_MAX_LINELENGTH];
universe@25 163 int line_buffer_offset = 0;
universe@25 164
universe@41 165 FILE *file = fopen(filelist->filename, "r");
universe@3 166 if (file == NULL) {
universe@44 167 outbuf = (char*) malloc(81);
universe@44 168 snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
universe@41 169 filelist->displayname);
universe@44 170 add_string(output, outbuf);
universe@3 171 perror(" File acces failed");
universe@41 172 } else {
universe@41 173 do {
universe@41 174 a = fgetc(file);
universe@3 175
universe@41 176 bfile = bfile_check(settings->bfileHeuristics, a);
universe@3 177
universe@41 178 if (a == 10 || a == EOF) {
universe@41 179 line_buffer[line_buffer_offset] = 0;
universe@41 180 if (regex_parser_do(settings->regex, line_buffer) == 0) {
universe@41 181 /* Only subtract lines when matching has finished */
universe@41 182 if (!regex_parser_matching(settings->regex)) {
universe@41 183 lines -= settings->regex->matched_lines;
universe@41 184 }
universe@41 185 }
universe@21 186
universe@41 187 line_buffer_offset = 0;
universe@41 188 lines++;
universe@41 189 } else {
universe@41 190 if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
universe@41 191 line_buffer[line_buffer_offset] = a;
universe@41 192 line_buffer_offset++;
universe@41 193 } else {
universe@41 194 line_buffer[line_buffer_offset-1] = 0;
universe@41 195 settings->confusing_lnlen = true;
universe@28 196 }
universe@28 197 }
universe@41 198 } while (!bfile && a != EOF);
universe@41 199 fclose(file);
universe@25 200
universe@41 201 /* Print and sum line count */
universe@41 202 if (bfile) {
universe@41 203 if (!settings->matchesOnly) {
universe@44 204 outbuf = (char*) malloc(81);
universe@44 205 snprintf(outbuf, 81,
universe@44 206 "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
universe@41 207 filelist->displayname,
universe@41 208 60-filelist->displayname_len-scanner.spaces, "", "binary");
universe@44 209 add_string(output, outbuf);
universe@41 210 }
universe@25 211 } else {
universe@41 212 lineSum += lines;
universe@44 213 outbuf = (char*) malloc(81);
universe@44 214 snprintf(outbuf, 81, "%*s%*s%13d lines\n",
universe@41 215 filelist->displayname_len+scanner.spaces, filelist->displayname,
universe@41 216 60-filelist->displayname_len-scanner.spaces, "", lines);
universe@44 217 add_string(output, outbuf);
universe@3 218 }
universe@21 219 }
universe@16 220 } else {
universe@3 221 if (!settings->matchesOnly) {
universe@22 222 /* Print hint */
universe@44 223 outbuf = (char*) malloc(81);
universe@44 224 snprintf(outbuf, 81, "%*s%*s%19s\n",
universe@41 225 filelist->displayname_len+scanner.spaces, filelist->displayname,
universe@41 226 60-filelist->displayname_len-scanner.spaces, "", "no match");
universe@44 227 add_string(output, outbuf);
universe@3 228 }
universe@3 229 }
universe@3 230 }
universe@41 231
universe@41 232 free(filelist->filename);
universe@41 233 free(filelist->displayname);
universe@41 234 filelist_t *freethis = filelist;
universe@41 235 filelist = filelist->next;
universe@41 236 free(freethis);
universe@3 237 }
universe@23 238
universe@3 239 return lineSum;
universe@3 240 }

mercurial