FOSSology  4.4.0
Open Source License Compliance by Open Source Software
regexscan-Stage2.c
Go to the documentation of this file.
1 /*
2  regexscan: Scan file(s) for regular expression(s)
3 
4  SPDX-FileCopyrightText: © 2007-2013 Hewlett-Packard Development Company, L.P.
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <signal.h>
26 #include <libgen.h>
27 
28 #include <regex.h>
29 #include <stdbool.h>
30 
31 #include "libfossology.h"
32 
33 #define MAXCMD 4096
34 char SQL[256];
35 
36 #define myBUFSIZ 2048
37 
38 /*
39 #ifdef COMMIT_HASH
40 char BuildVersion[]="Build version: " COMMIT_HASH ".\n";
41 #endif
42 */
43 
44 PGconn *pgConn = NULL;
45 
53 int regexScan(char *regexStr, FILE *scanFilePtr, char *fileName)
54 {
55  int retCode;
56 
57  regex_t regex;
58  bool match = false; /* regex match found indicator */
59 
60  char msgBuff[250];
61  char textBuff[2000]; /* line buffer for regex match processing */
62 
63  regmatch_t rm[1];
64  int lineCount = 0;
65 
66  /* Compile the regex for improved performance */
67  retCode = regcomp(&regex, regexStr, REG_ICASE+REG_EXTENDED);
68  if (retCode)
69  {
70  fprintf(stderr, "regex %s failed to compile\n", regexStr);
71  return 1;
72  }
73 
74  /* Now scan the file for regex line by line */
75  while (fgets(textBuff, 1024, scanFilePtr) != NULL)
76  {
77  lineCount++; /* Another line read */
78  retCode = regexec(&regex, textBuff, 1, rm, 0); /* nmatch = 1, matchptr = rm */
79  if (!retCode)
80  {
81  sprintf(msgBuff, "%s: regex found at line %d at position %d. -> %.*s \n",
82  fileName, lineCount, rm[0].rm_so+1, rm[0].rm_eo-rm[0].rm_so, textBuff + rm[0].rm_so);
83  puts(msgBuff);
84  if (!match)
85  {
86  match = true; /* Indicate we've had at least one match */
87  }
88  }
89  else if (retCode == REG_NOMATCH)
90  {
91  /* Skip the "no match" retCode */
92  }
93  else
94  {
95  regerror(retCode, &regex, msgBuff, sizeof(msgBuff));
96  fprintf(stderr, "Out of memory? - regex match failure: %s\n", msgBuff);
97  fclose(scanFilePtr);
98  return 3;
99  }
100  }
101 
102  /* Report if no matches found */
103  if (!match)
104  {
105  sprintf(msgBuff, "%s: %s not found\n", fileName, regexStr);
106  puts(msgBuff);
107  }
108 
109  /* clean up and exit */
110  regfree(&regex);
111  fclose(scanFilePtr);
112  return 0;
113 }
114 
115 
116 /*********************************************************
117  Usage():
118  *********************************************************/
119 void Usage (char *Name)
120 {
121  printf("Usage: %s [options] [id [id ...]]\n",Name);
122  printf(" -i :: initialize the database, then exit.\n");
123  printf(" -c SYSCONFDIR :: FOSSology configuration directory.\n");
124  printf(" -h :: show available command line options.\n");
125  printf(" -v :: increase agent logging verbosity.\n");
126  printf(" -r :: regex expression to load from command line.\n");
127  printf(" filename :: filename to process with regex.\n");
128 } /* Usage() */
129 
130 /*********************************************************/
131 int main (int argc, char *argv[])
132 {
133  int index, nonoptargs = 0;
134 
135  int c;
136 
137  regex_t regex;
138  char regexStr[1024]; /* string storage for the regex expression */
139 
140  FILE *scanFilePtr;
141  char fileName[1000];
142 
143  int user_pk;
144  long UploadPK=-1;
145 
146  char *COMMIT_HASH;
147  char *VERSION;
148  char agent_rev[myBUFSIZ];
149 
150  /* connect to scheduler. Noop if not run from scheduler. */
151  fo_scheduler_connect(&argc, argv, &pgConn);
152 
153 /*
154  Version reporting.
155 */
156  COMMIT_HASH = fo_sysconfig("regexscan", "COMMIT_HASH");
157  VERSION = fo_sysconfig("regexscan", "VERSION");
158  sprintf(agent_rev, "%s.%s", VERSION, COMMIT_HASH);
159 #ifdef REGEX_DEBUG
160  fprintf(stdout, "regexscan reports version info as '%s.%s'.\n", VERSION, COMMIT_HASH);
161 #endif
162 
163  /* Process command-line */
164  while((c = getopt(argc,argv,"chir:v")) != -1)
165  {
166  switch(c)
167  {
168  case 'c':
169  break; /* handled by fo_scheduler_connect() */
170  case 'i':
171  PQfinish(pgConn);
172  return(0);
173  case 'r':
174  sprintf(regexStr, "%s", optarg);
175  break;
176  case 'v':
177  agent_verbose++;
178  break;
179  case 'h':
180  default:
181  Usage(argv[0]);
182  fflush(stdout);
183  PQfinish(pgConn);
184  exit(-1);
185  }
186  }
187 
188  /* process filename after switches */
189  for (index = optind; index < argc; index++)
190  {
191 /* process no option arguments
192  printf("Non-option argument %s\n", argv[index]); /* diag display */
193  nonoptargs++;
194  }
195 
196  if (nonoptargs == 0)
197  {
198  /* Assume it was a scheduler call */
199  user_pk = fo_scheduler_userID();
200  while(fo_scheduler_next())
201  {
202  UploadPK = atol(fo_scheduler_current());
203 
204  printf("UploadPK is: %ld\n", UploadPK);
205  }
206  }
207  else
208  {
209  /* File access initialization */
210  sprintf(fileName, "%s", argv[optind]); /* Grab first non-switch argument as filename */
211  scanFilePtr = fopen(fileName, "r");
212  if (!scanFilePtr)
213  {
214  fprintf(stderr, "failed to open text inout file %s\n", fileName);
215  regfree(&regex);
216  return 2;
217  }
218 
219  /* Call scan function */
220  regexScan(regexStr, scanFilePtr, fileName);
221  }
222 
223  PQfinish(pgConn);
225 
226  return 0;
227 } /* main() */
228 
The main FOSSology C library.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
char * fo_scheduler_current()
Get the last read string from the scheduler.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
void Usage(char *Name)
Say how to run this program.
int regexScan(char *regexStr, FILE *scanFilePtr, char *fileName)
Regex scanner.
char SQL[256]
For DB.
PGconn * pgConn
Database connection.
Store the results of a regex match.
Definition: scanners.hpp:28