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