FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
cli.c
1 /*
2 Author: Daniele Fognini, Andreas Wuerl
3 Copyright (C) 2013-2014, Siemens AG
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 #include "cli.h"
19 #include "file_operations.h"
20 #include "database.h"
21 #include "match.h"
22 
23 MatchCallbacks cliCallbacks =
24  { .onNo = cli_onNoMatch,
25  .onFull = cli_onFullMatch,
26  .onBeginOutput = cli_onBeginOutput,
27  .onBetweenIndividualOutputs = cli_onBetweenIndividualOutputs,
28  .onEndOutput = cli_onEndOutput,
29  .onDiff = cli_onDiff
30  };
31 
32 int matchCliFileWithLicenses(MonkState* state, const Licenses* licenses, int argi, char** argv) {
33  File file;
34  file.id = argi;
35  file.fileName = argv[argi];
36  if (!readTokensFromFile(file.fileName, &(file.tokens), DELIMITERS))
37  return 0;
38 
39  int result = matchFileWithLicenses(state, &file, licenses, &cliCallbacks);
40 
41  tokens_free(file.tokens);
42 
43  return result;
44 }
45 
46 int handleCliMode(MonkState* state, const Licenses* licenses, int argc, char** argv, int fileOptInd) {
47 #ifdef MONK_MULTI_THREAD
48  #pragma omp parallel
49 #endif
50  {
51  MonkState threadLocalStateStore = *state;
52  MonkState* threadLocalState = &threadLocalStateStore;
53 
54 #ifdef MONK_MULTI_THREAD
55  #pragma omp for schedule(dynamic)
56 #endif
57  for (int fileId = fileOptInd; fileId < argc; fileId++) {
58  matchCliFileWithLicenses(threadLocalState, licenses, fileId, argv);
59  }
60  }
61 
62  return 1;
63 }
64 
65 int cli_onNoMatch(MonkState* state, const File* file) {
66  if (state->verbosity >= 1) {
67  printf("File %s contains license(s) No_license_found\n", file->fileName);
68  }
69  if (state->json) {
70  printf("{\"type\":\"no-match\"}");
71  }
72  return 1;
73 }
74 
75 int cli_onFullMatch(MonkState* state, const File* file, const License* license, const DiffMatchInfo* matchInfo) {
76  if (state->json) {
77  printf("{\"type\":\"full\",\"license\":\"%s\",\"ref-pk\":%ld,\"matched\":\"%zu+%zu\"}",
78  license->shortname, license->refId,
79  matchInfo->text.start, matchInfo->text.length);
80  } else {
81  printf("found full match between \"%s\" and \"%s\" (rf_pk=%ld); matched: %zu+%zu\n",
82  file->fileName, license->shortname, license->refId,
83  matchInfo->text.start, matchInfo->text.length);
84  }
85  return 1;
86 }
87 
88 int cli_onDiff(MonkState* state, const File* file, const License* license, const DiffResult* diffResult) {
89  unsigned short rank = diffResult->percentual;
90 
91  char * formattedMatchArray = formatMatchArray(diffResult->matchedInfo);
92 
93  if (state->json) {
94  printf("{\"type\":\"diff\",\"license\":\"%s\",\"ref-pk\":%ld,\"rank\":%u,\"diffs\":\"%s\"}",
95  license->shortname, license->refId,
96  rank, formattedMatchArray);
97  } else {
98  printf("found diff match between \"%s\" and \"%s\" (rf_pk=%ld); rank %u; diffs: {%s}\n",
99  file->fileName, license->shortname, license->refId,
100  rank,
101  formattedMatchArray);
102  }
103 
104  free(formattedMatchArray);
105  return 1;
106 }
107 
108 
109 int cli_onBeginOutput(MonkState* state) {
110  if (state->json) {
111  printf("[");
112  }
113  return 1;
114 }
115 int cli_onBetweenIndividualOutputs(MonkState* state) {
116  if (state->json) {
117  printf(",");
118  }
119  return 1;
120 }
121 int cli_onEndOutput(MonkState* state) {
122  if (state->json) {
123  printf("]");
124  }
125  return 1;
126 }
127 
Definition: monk.h:78
Definition: monk.h:55
Definition: monk.h:72
Definition: nomos.h:439
Definition: monk.h:66
void matchFileWithLicenses(const string &sContent, unsigned long pFileId, CopyrightState const &state, int agentId, CopyrightDatabaseHandler &databaseHandler)
Scan a given file with all available scanners and save findings to database.