FOSSology  3.2.0rc1
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  Copyright (C) 2007-2013 Hewlett-Packard Development Company, L.P.
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***************************************************************/
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <signal.h>
37 #include <libgen.h>
38 
39 #include <regex.h>
40 #include <stdbool.h>
41 
42 #include "libfossology.h"
43 
44 #define MAXCMD 4096
45 char SQL[256];
46 
47 #define myBUFSIZ 2048
48 
49 /*
50 #ifdef COMMIT_HASH
51 char BuildVersion[]="Build version: " COMMIT_HASH ".\n";
52 #endif
53 */
54 
55 PGconn *pgConn = NULL;
56 
64 int regexScan(char *regexStr, FILE *scanFilePtr, char *fileName)
65 {
66  int retCode;
67 
68  regex_t regex;
69  bool match = false; /* regex match found indicator */
70 
71  char msgBuff[250];
72  char textBuff[2000]; /* line buffer for regex match processing */
73 
74  regmatch_t rm[1];
75  int lineCount = 0;
76 
77  /* Compile the regex for improved performance */
78  retCode = regcomp(&regex, regexStr, REG_ICASE+REG_EXTENDED);
79  if (retCode)
80  {
81  fprintf(stderr, "regex %s failed to compile\n", regexStr);
82  return 1;
83  }
84 
85  /* Now scan the file for regex line by line */
86  while (fgets(textBuff, 1024, scanFilePtr) != NULL)
87  {
88  lineCount++; /* Another line read */
89  retCode = regexec(&regex, textBuff, 1, rm, 0); /* nmatch = 1, matchptr = rm */
90  if (!retCode)
91  {
92  sprintf(msgBuff, "%s: regex found at line %d at position %d. -> %.*s \n",
93  fileName, lineCount, rm[0].rm_so+1, rm[0].rm_eo-rm[0].rm_so, textBuff + rm[0].rm_so);
94  puts(msgBuff);
95  if (!match)
96  {
97  match = true; /* Indicate we've had at least one match */
98  }
99  }
100  else if (retCode == REG_NOMATCH)
101  {
102  /* Skip the "no match" retCode */
103  }
104  else
105  {
106  regerror(retCode, &regex, msgBuff, sizeof(msgBuff));
107  fprintf(stderr, "Out of memory? - regex match failure: %s\n", msgBuff);
108  fclose(scanFilePtr);
109  return 3;
110  }
111  }
112 
113  /* Report if no matches found */
114  if (!match)
115  {
116  sprintf(msgBuff, "%s: %s not found\n", fileName, regexStr);
117  puts(msgBuff);
118  }
119 
120  /* clean up and exit */
121  regfree(&regex);
122  fclose(scanFilePtr);
123  return 0;
124 }
125 
126 
127 /*********************************************************
128  Usage():
129  *********************************************************/
130 void Usage (char *Name)
131 {
132  printf("Usage: %s [options] [id [id ...]]\n",Name);
133  printf(" -i :: initialize the database, then exit.\n");
134  printf(" -c SYSCONFDIR :: FOSSology configuration directory.\n");
135  printf(" -h :: show available command line options.\n");
136  printf(" -v :: increase agent logging verbosity.\n");
137  printf(" -r :: regex expression to load from command line.\n");
138  printf(" filename :: filename to process with regex.\n");
139 } /* Usage() */
140 
141 /*********************************************************/
142 int main (int argc, char *argv[])
143 {
144  int index, nonoptargs = 0;
145 
146  int c;
147 
148  regex_t regex;
149  char regexStr[1024]; /* string storage for the regex expression */
150 
151  FILE *scanFilePtr;
152  char fileName[1000];
153 
154  int user_pk;
155  long UploadPK=-1;
156 
157  char *COMMIT_HASH;
158  char *VERSION;
159  char agent_rev[myBUFSIZ];
160 
161  /* connect to scheduler. Noop if not run from scheduler. */
162  fo_scheduler_connect(&argc, argv, &pgConn);
163 
164 /*
165  Version reporting.
166 */
167  COMMIT_HASH = fo_sysconfig("regexscan", "COMMIT_HASH");
168  VERSION = fo_sysconfig("regexscan", "VERSION");
169  sprintf(agent_rev, "%s.%s", VERSION, COMMIT_HASH);
170 #ifdef REGEX_DEBUG
171  fprintf(stdout, "regexscan reports version info as '%s.%s'.\n", VERSION, COMMIT_HASH);
172 #endif
173 
174  /* Process command-line */
175  while((c = getopt(argc,argv,"chir:v")) != -1)
176  {
177  switch(c)
178  {
179  case 'c':
180  break; /* handled by fo_scheduler_connect() */
181  case 'i':
182  PQfinish(pgConn);
183  return(0);
184  case 'r':
185  sprintf(regexStr, "%s", optarg);
186  break;
187  case 'v':
188  agent_verbose++;
189  break;
190  case 'h':
191  default:
192  Usage(argv[0]);
193  fflush(stdout);
194  PQfinish(pgConn);
195  exit(-1);
196  }
197  }
198 
199  /* process filename after switches */
200  for (index = optind; index < argc; index++)
201  {
202 /* process no option arguments
203  printf("Non-option argument %s\n", argv[index]); /* diag display */
204  nonoptargs++;
205  }
206 
207  if (nonoptargs == 0)
208  {
209  /* Assume it was a scheduler call */
210  user_pk = fo_scheduler_userID();
211  while(fo_scheduler_next())
212  {
213  UploadPK = atol(fo_scheduler_current());
214 
215  printf("UploadPK is: %ld\n", UploadPK);
216  }
217  }
218  else
219  {
220  /* File access initialization */
221  sprintf(fileName, "%s", argv[optind]); /* Grab first non-switch argument as filename */
222  scanFilePtr = fopen(fileName, "r");
223  if (!scanFilePtr)
224  {
225  fprintf(stderr, "failed to open text inout file %s\n", fileName);
226  regfree(&regex);
227  return 2;
228  }
229 
230  /* Call scan function */
231  regexScan(regexStr, scanFilePtr, fileName);
232  }
233 
234  PQfinish(pgConn);
236 
237  return 0;
238 } /* main() */
239 
Store the results of a regex match.
Definition: scanners.hpp:39
void Usage(char *Name)
Say how to run this program.
int regexScan(char *regexStr, FILE *scanFilePtr, char *fileName)
Regex scanner.
char * fo_scheduler_current()
Get the last read string from the scheduler.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
char SQL[256]
For DB.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
PGconn * pgConn
Database connection.
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
The main FOSSology C library.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.