FOSSology  4.4.0
Open Source License Compliance by Open Source Software
cli.c
1 /*
2  Author: Daniele Fognini, Andreas Wuerl
3  SPDX-FileCopyrightText: © 2013-2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include "cli.h"
9 #include "file_operations.h"
10 #include "database.h"
11 #include "match.h"
12 
13 MatchCallbacks cliCallbacks =
14  { .onNo = cli_onNoMatch,
15  .onFull = cli_onFullMatch,
16  .onBeginOutput = cli_onBeginOutput,
17  .onBetweenIndividualOutputs = cli_onBetweenIndividualOutputs,
18  .onEndOutput = cli_onEndOutput,
19  .onDiff = cli_onDiff
20  };
21 
22 int matchCliFileWithLicenses(MonkState* state, const Licenses* licenses, int argi, char** argv) {
23  File file;
24  file.id = argi;
25  file.fileName = argv[argi];
26  if (!readTokensFromFile(file.fileName, &(file.tokens), DELIMITERS))
27  return 0;
28 
29  int result = matchFileWithLicenses(state, &file, licenses, &cliCallbacks);
30 
31  tokens_free(file.tokens);
32 
33  return result;
34 }
35 
36 int handleCliMode(MonkState* state, const Licenses* licenses, int argc, char** argv, int fileOptInd) {
37 #ifdef MONK_MULTI_THREAD
38  #pragma omp parallel
39 #endif
40  {
41  MonkState threadLocalStateStore = *state;
42  MonkState* threadLocalState = &threadLocalStateStore;
43 
44 #ifdef MONK_MULTI_THREAD
45  #pragma omp for schedule(dynamic)
46 #endif
47  for (int fileId = fileOptInd; fileId < argc; fileId++) {
48  matchCliFileWithLicenses(threadLocalState, licenses, fileId, argv);
49  }
50  }
51 
52  return 1;
53 }
54 
55 int cli_onNoMatch(MonkState* state, const File* file) {
56  if (state->verbosity >= 1) {
57  printf("File %s contains license(s) No_license_found\n", file->fileName);
58  }
59  if (state->json) {
60  printf("{\"type\":\"no-match\"}");
61  }
62  return 1;
63 }
64 
65 int cli_onFullMatch(MonkState* state, const File* file, const License* license, const DiffMatchInfo* matchInfo) {
66  if (state->json) {
67  printf("{\"type\":\"full\",\"license\":\"%s\",\"ref-pk\":%ld,\"matched\":\"%zu+%zu\"}",
68  license->shortname, license->refId,
69  matchInfo->text.start, matchInfo->text.length);
70  } else {
71  printf("found full match between \"%s\" and \"%s\" (rf_pk=%ld); matched: %zu+%zu\n",
72  file->fileName, license->shortname, license->refId,
73  matchInfo->text.start, matchInfo->text.length);
74  }
75  return 1;
76 }
77 
78 int cli_onDiff(MonkState* state, const File* file, const License* license, const DiffResult* diffResult) {
79  unsigned short rank = diffResult->percentual;
80 
81  char * formattedMatchArray = formatMatchArray(diffResult->matchedInfo);
82 
83  if (state->json) {
84  printf("{\"type\":\"diff\",\"license\":\"%s\",\"ref-pk\":%ld,\"rank\":%u,\"diffs\":\"%s\"}",
85  license->shortname, license->refId,
86  rank, formattedMatchArray);
87  } else {
88  printf("found diff match between \"%s\" and \"%s\" (rf_pk=%ld); rank %u; diffs: {%s}\n",
89  file->fileName, license->shortname, license->refId,
90  rank,
91  formattedMatchArray);
92  }
93 
94  free(formattedMatchArray);
95  return 1;
96 }
97 
98 
99 int cli_onBeginOutput(MonkState* state) {
100  if (state->json) {
101  printf("[");
102  }
103  return 1;
104 }
105 int cli_onBetweenIndividualOutputs(MonkState* state) {
106  if (state->json) {
107  printf(",");
108  }
109  return 1;
110 }
111 int cli_onEndOutput(MonkState* state) {
112  if (state->json) {
113  printf("]");
114  }
115  return 1;
116 }
117 
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.
Definition: monk.h:61
Definition: monk.h:55
Definition: monk.h:67
Definition: monk.h:44
Definition: nomos.h:426