1 21 22 package gnu.regexp.util; 23 24 import gnu.getopt.Getopt; 25 import gnu.getopt.LongOpt; 26 import gnu.regexp.RE; 27 import gnu.regexp.REException; 28 import gnu.regexp.REMatch; 29 import gnu.regexp.RESyntax; 30 import java.io.BufferedReader ; 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.FileNotFoundException ; 34 import java.io.InputStream ; 35 import java.io.InputStreamReader ; 36 import java.io.IOException ; 37 import java.io.PrintStream ; 38 import java.io.UnsupportedEncodingException ; 39 import java.util.Enumeration ; 40 import java.util.Vector ; 41 import java.util.zip.*; 42 43 44 55 public class Grep { 56 private static final int BYTE_OFFSET = 0; 57 private static final int COUNT = 1; 58 private static final int LINE_NUMBER = 2; 59 private static final int QUIET = 3; 60 private static final int SILENT = 4; 61 private static final int NO_FILENAME = 5; 62 private static final int REVERT_MATCH = 6; 63 private static final int FILES_WITH_MATCHES = 7; 64 private static final int LINE_REGEXP = 8; 65 private static final int FILES_WITHOUT_MATCH = 9; 66 private static final int EXPAND_ZIP_FILES = 10; 67 68 private static final String PROGNAME = "gnu.regexp.util.Grep"; 69 private static final String PROGVERSION = "1.03"; 70 71 private Grep() { } 72 77 public static void main(String [] argv) { 78 System.exit(grep(argv, RESyntax.RE_SYNTAX_GREP, System.out)); 79 } 80 81 88 public static int grep(String [] argv, RESyntax syntax, PrintStream out) { 89 int cflags = 0; 91 92 boolean[] options = new boolean [10]; 93 94 String encoding = null; 95 96 LongOpt[] longOptions = { 97 new LongOpt("byte-offset", LongOpt.NO_ARGUMENT, null, 'b'), 98 new LongOpt("count", LongOpt.NO_ARGUMENT, null, 'c'), 99 new LongOpt("no-filename", LongOpt.NO_ARGUMENT, null, 'h'), 100 new LongOpt("ignore-case", LongOpt.NO_ARGUMENT, null, 'i'), 101 new LongOpt("files-with-matches", LongOpt.NO_ARGUMENT, null, 'l'), 102 new LongOpt("help", LongOpt.NO_ARGUMENT, null, '!'), 103 new LongOpt("line-number", LongOpt.NO_ARGUMENT, null, 'n'), 104 new LongOpt("quiet", LongOpt.NO_ARGUMENT, null, 'q'), 105 new LongOpt("silent", LongOpt.NO_ARGUMENT, null, 'q'), 106 new LongOpt("no-messages", LongOpt.NO_ARGUMENT, null, 's'), 107 new LongOpt("revert-match", LongOpt.NO_ARGUMENT, null, 'v'), 108 new LongOpt("line-regexp", LongOpt.NO_ARGUMENT, null, 'x'), 109 new LongOpt("extended-regexp", LongOpt.NO_ARGUMENT, null, 'E'), 110 new LongOpt("fixed-strings", LongOpt.NO_ARGUMENT, null, 'F'), new LongOpt("basic-regexp", LongOpt.NO_ARGUMENT, null, 'G'), 112 new LongOpt("files-without-match", LongOpt.NO_ARGUMENT, null, 'L'), 113 new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V'), 114 new LongOpt("zip", LongOpt.NO_ARGUMENT, null, 'z'), 115 new LongOpt("encoding", LongOpt.REQUIRED_ARGUMENT, null, 'N') 116 }; 117 118 Getopt g = new Getopt(PROGNAME, argv, "bchilnqsvxyEFGLVzN:", longOptions); 119 int c; 120 String arg; 121 while ((c = g.getopt()) != -1) { 122 switch (c) { 123 case 'b': 124 options[BYTE_OFFSET] = true; 125 break; 126 case 'c': 127 options[COUNT] = true; 128 break; 129 case 'h': 130 options[NO_FILENAME] = true; 131 break; 132 case 'i': 133 case 'y': 134 cflags |= RE.REG_ICASE; 135 break; 136 case 'l': 137 options[FILES_WITH_MATCHES] = true; 138 break; 139 case 'n': 140 options[LINE_NUMBER] = true; 141 break; 142 case 'q': 143 options[QUIET] = true; 144 break; 145 case 's': 146 options[SILENT] = true; 147 break; 148 case 'v': 149 options[REVERT_MATCH] = true; 150 break; 151 case 'x': 152 options[LINE_REGEXP] = true; 153 break; 154 case 'E': syntax = RESyntax.RE_SYNTAX_EGREP; 156 break; 157 case 'F': break; 159 case 'G': 160 syntax = RESyntax.RE_SYNTAX_GREP; 161 break; 162 case 'L': 163 options[FILES_WITHOUT_MATCH] = true; 164 break; 165 case 'V': 166 System.err.println(PROGNAME+' '+PROGVERSION); 167 return 0; 168 case 'z': 169 options[EXPAND_ZIP_FILES] = true; 170 break; 171 case 'N': 172 encoding = g.getOptarg(); 173 try { "".getBytes(encoding); 175 } catch (UnsupportedEncodingException uee) { 176 System.err.println(PROGNAME+": (Warning)" 177 + " Unsupported Encoding: " + encoding 178 + "; reverting to default"); 179 encoding = null; 180 } 181 break; 182 case '!': try { 184 BufferedReader br = new BufferedReader (new InputStreamReader ((Grep.class).getResourceAsStream("GrepUsage.txt"),"UTF8")); 185 String line; 186 while ((line = br.readLine()) != null) 187 out.println(line); 188 } catch (IOException ie) { } 189 return 0; 190 } 191 } 192 193 InputStream is = null; 194 RE pattern = null; 195 if (g.getOptind() >= argv.length) { 196 System.err.println("Usage: java " + PROGNAME + " [OPTION]... PATTERN [FILE]..."); 197 System.err.println("Try `java " + PROGNAME + " --help' for more information."); 198 return 2; 199 } 200 try { 201 pattern = new RE(argv[g.getOptind()],cflags,syntax); 202 } catch (REException e) { 203 System.err.println("Error in expression: "+e); 204 return 2; 205 } 206 207 boolean notFound = true; 208 if (argv.length >= g.getOptind()+2) { 209 for (int i = g.getOptind() + 1; i < argv.length; i++) { 210 boolean no_filename = (argv.length == g.getOptind()+2) 211 || options[NO_FILENAME]; 212 if (argv[i].equals("-")) { 213 final String filename = no_filename ? null : "(standard input)"; 214 if (processStream(pattern,System.in,encoding,options,filename,null,out)) 215 notFound = false; 216 } else { 217 final String filename = no_filename ? null : argv[i]; 218 try { 219 File file = new File (argv[i]); 220 if(file.isDirectory()) { 221 System.err.println(PROGNAME + ": " + argv[i] + ": Is a directory"); 222 } else if(!file.canRead()) { 223 System.err.println(PROGNAME + ": " + argv[i] + ": Permission denied"); 224 } else if (options[EXPAND_ZIP_FILES] && argv[i].endsWith(".zip")) { 225 try { 227 ZipFile zf = new ZipFile(file); 228 Enumeration list = zf.entries(); 229 while (list.hasMoreElements()) { 230 ZipEntry ze = (ZipEntry) list.nextElement(); 231 if (! ze.isDirectory()) { 232 if (processStream(pattern, zf.getInputStream(ze), encoding, options, filename, ze.getName(), out)) 233 notFound = false; 234 } 235 } 236 } catch (Exception ex) { 237 System.err.println(PROGNAME + ": " + argv[i] + ": Problem reading ZIP file"); 238 return 2; 239 } 240 } else { 241 if (processStream(pattern, 242 new FileInputStream (argv[i]), 243 encoding, options, filename, null, out)) 244 notFound = false; 245 } 246 } catch (FileNotFoundException e) { 247 if (!options[SILENT]) 248 System.err.println(PROGNAME+": "+e); 249 } 250 } 251 } 252 } else { 253 if (processStream(pattern,System.in,encoding,options,null,null,out)) 254 notFound = false; 255 } 256 return notFound ? 1 : 0; 257 } 258 259 private static boolean processStream(RE pattern, InputStream is, 260 String encoding, boolean[] options, 261 String filename, String zipName, 262 PrintStream out) { 263 try { 264 final InputStreamReader isr = encoding == null? 265 new InputStreamReader (is) : new InputStreamReader (is,encoding); 266 final BufferedReader r = new BufferedReader (isr); 267 return processReader(pattern, r, options, filename, zipName, out); 268 } catch (UnsupportedEncodingException uee) { 269 273 throw new Error (PROGNAME + ": programming logic error"); 274 } 275 } 276 277 private static String fileNameString (String fileName, String zipName) { 278 if (zipName == null) 279 return fileName; 280 else 281 return zipName + " in " + fileName; 282 } 283 284 private static boolean processReader(RE pattern, 285 BufferedReader br, 286 boolean[] options, String filename, 287 String zipName, PrintStream out) { 288 289 int newlineLen = System.getProperty("line.separator").length(); 290 int count = 0; 291 long atByte = 0; 292 int atLine = 1; 293 String line; 294 REMatch match; 295 296 try { 297 while ((line = br.readLine()) != null) { 298 match = pattern.getMatch(line); 299 if (((options[LINE_REGEXP] && pattern.isMatch(line)) 300 || (!options[LINE_REGEXP] && (match != null))) 301 ^ options[REVERT_MATCH]) { 302 count++; 303 if (!options[COUNT]) { 304 if (options[QUIET]) { 305 return true; 306 } 307 if (options[FILES_WITH_MATCHES]) { 308 if (filename != null) 309 out.println(fileNameString(filename, zipName)); 310 return true; 311 } 312 if (options[FILES_WITHOUT_MATCH]) { 313 return false; 314 } 315 if (filename != null) { 316 out.print(fileNameString(filename, zipName)); 317 out.print(':'); 318 } 319 if (options[LINE_NUMBER]) { 320 out.print(atLine); 321 out.print(':'); 322 } 323 if (options[BYTE_OFFSET]) { 324 out.print(atByte + match.getStartIndex() ); 325 out.print(':'); 326 } 327 out.println(line); 328 } 329 } atByte += line.length() + newlineLen; atLine++; 332 } br.close(); 334 335 if (options[COUNT]) { 336 if (filename != null) 337 out.println(fileNameString(filename, zipName)+':'); 338 out.println(count); 339 } 340 if (options[FILES_WITHOUT_MATCH] && count==0) { 341 if (filename != null) 342 out.println(fileNameString(filename, zipName)); 343 } 344 } catch (IOException e) { 345 System.err.println(PROGNAME+": "+e); 346 } 347 return ((count > 0) ^ options[REVERT_MATCH]); 348 } 349 } 350 | Popular Tags |