FOSSology  4.4.0
Open Source License Compliance by Open Source Software
json_writer.c
1 /*
2  SPDX-FileCopyrightText: © 2019 Siemens AG
3  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include "json_writer.h"
9 #include "nomos.h"
10 #include "nomos_utils.h"
11 #include <json-c/json.h>
12 
13 void writeJson()
14 {
15  char realPathOfTarget[PATH_MAX];
16  json_object *result = json_object_new_object();
17  json_object *licenses = json_object_new_array();
18  json_object *fileLocation = NULL;
19  json_object *aLicense = NULL;
20  size_t i = 0;
21 
23  while (cur.licenseList[i] != NULL)
24  {
25  aLicense = json_object_new_string(cur.licenseList[i]);
26  cur.licenseList[i] = NULL;
27  json_object_array_add(licenses, aLicense);
28  ++i;
29  }
30  if (optionIsSet(OPTS_LONG_CMD_OUTPUT)
31  && realpath(cur.targetFile, realPathOfTarget))
32  {
33  fileLocation = json_object_new_string(realPathOfTarget);
34  }
35  else
36  {
37  fileLocation = json_object_new_string(basename(cur.targetFile));
38  }
39  json_object_object_add(result, "file", fileLocation);
40  json_object_object_add(result, "licenses", licenses);
41  char *prettyJson = unescapePathSeparator(
42  json_object_to_json_string_ext(result, JSON_C_TO_STRING_PRETTY));
43  sem_wait(mutexJson);
44  if (*printcomma)
45  {
46  printf(",%s\n", prettyJson);
47  }
48  else
49  {
50  *printcomma = true;
51  printf("%s\n", prettyJson);
52  }
53  fflush(stdout);
54  sem_post(mutexJson);
55  free(prettyJson);
56  json_object_put(result);
57 }
58 
59 char *unescapePathSeparator(const char* json)
60 {
61  const char *escapedSeparator = "\\/";
62  const char *pathSeparator = "/";
63  const int escPathLen = 2;
64  const int pathSepLen = 1;
65  size_t resultLength = 0;
66  size_t remainingLength = -1;
67  char *result;
68  char *tmp;
69  char *tempjson;
70  int count;
71  if (!json)
72  {
73  return NULL;
74  }
75  tempjson = strdup(json);
76 
77  tmp = tempjson;
78  for (count = 0; (tmp = strstr(tmp, escapedSeparator)); count++)
79  {
80  tmp += escPathLen;
81  }
82 
83  resultLength = strlen(tempjson) - ((escPathLen - pathSepLen) * count);
84 
85  result = (char*) calloc(resultLength + 1, sizeof(char));
86 
87  strncpy(result, strtok(tempjson, escapedSeparator), resultLength);
88  remainingLength = resultLength - strlen(result);
89 
90  while (count-- && remainingLength > 0)
91  {
92  strncat(result, pathSeparator, remainingLength);
93  strncat(result, strtok(NULL, escapedSeparator), remainingLength - 1);
94  remainingLength = resultLength - strlen(result);
95  }
96  free(tempjson);
97  return result;
98 }
99 
100 inline void initializeJson()
101 {
102  mutexJson = (sem_t *) mmap(NULL, sizeof(sem_t),
103  PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
104  printcomma = (gboolean *) mmap(NULL, sizeof(gboolean),
105  PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
106  sem_init(mutexJson, 2, SEM_DEFAULT_VALUE);
107 }
108 
109 inline void destroyJson()
110 {
111  sem_destroy(mutexJson);
112  munmap(printcomma, sizeof(gboolean));
113  munmap(mutexJson, sizeof(sem_t));
114 }
Handle JSON outputs.
void writeJson()
Write the scan output as a JSON.
Definition: json_writer.c:13
void initializeJson()
Definition: json_writer.c:100
char * unescapePathSeparator(const char *json)
Unescape the path separator from JSON.
Definition: json_writer.c:59
void destroyJson()
Definition: json_writer.c:109
Nomos header file.
int optionIsSet(int val)
Check if an CLI option is set.
Definition: nomos_utils.c:560
sem_t * mutexJson
Mutex to handle JSON writes.
Definition: nomos_utils.c:21
FUNCTION void parseLicenseList()
parse the comma separated list of license names found
Definition: nomos_utils.c:450
gboolean * printcomma
True to print comma while printing JSON object.
Definition: nomos_utils.c:22
char targetFile[myBUFSIZ]
Definition: nomos.h:394
char * licenseList[512]
Definition: nomos.h:414