/* System: Structured text retrieval tool sgrep. Module: main.c Author: Pekka Kilpeläinen & Jani Jaakkola Description: Parsing of command line options, Reading command files Scanning for input files Calling other modules for preprocessing, parsing, pattern matching, evaluation and output Version history: Original version February 1995 by JJ & PK Copyright: University of Helsinki, Dept. of Computer Science Distributed under GNU General Public Lisence See file COPYING for details */ #include #include #include #include #include #include #include #include "defines.h" void check_files(int ,char *[],int); void clear_stats(); void show_stats(); void show_times(); int get_options(char *[]); void add_command(char *); int read_stdin(); void read_expressions(); int environ_options(); void run_stream(struct TREE_NODE *, struct PHRASE_NODE *p_list); void run_one_by_one(struct TREE_NODE *, struct PHRASE_NODE *p_list); void create_constant_lists(); void copyright_notice(); /* * The global variables common to all modules. See declarations in defines.h */ char *output_style=SHORT_OUTPUT; /* default is short */ int open_failure=OPEN_FAILURE; struct STATS stats; int print_newline=TRUE; int stdin_fd=-1; /* not opened yet */ int print_all=FALSE; int gc_lists_now=0; struct GC_LIST *end_list=NULL; struct GC_LIST *start_list=NULL; struct GC_LIST *chars_list=NULL; int ignore_case=FALSE; #ifdef STREAM_MODE int stream_mode=TRUE; #else int stream_mode=FALSE; #endif #ifdef PROGRESS_REPORTS int progress_output=FALSE; #endif /* * Global variables used inside main.c . These are mainly used for storing * information about given options */ int have_stats=FALSE; /* Should we show statistics in the end (-T) */ int have_times=FALSE; /* Should we show info about used time in the end (-t) */ int do_concat=TRUE; /* Shall we do concat operation on result list (-d) */ int display_count=FALSE;/* Should we display only count of matching regions (-c) */ int no_output=FALSE; /* Should we supress normal output (-q) */ int command_file_given=FALSE; /* If a command file name was given with -f option this is set, and no commands are read from command line anymore */ int show_expr=FALSE; /* only show expression, don't execute it (-P) */ char *preprocessor=PRE_PROCESSOR; /* Which preprocessor to use (-p) */ int read_sgreprc; /* are we going to read sgreprc (-n) */ char com_buf[COMBUF_SIZE]; /* preprosessed command buffer */ char com_file_buf[COMBUF_SIZE]; /* not preprocessed command file buffer */ int com_buf_size; /* How much it is actually used */ int com_file_buf_used; char *home_file; /* pointer to whole path of $HOME/USER_SGREPRC */ struct INPUT_FILE *input_files=NULL; /* Table of input files */ int last_file=0; /* Index of last input file */ int stdin_read=FALSE; /* Since expressions and files can both be read from stdin, we got to make sure that stdin is read only once */ /* * struct for list of expression strings ( or files ) to be executed */ struct EXPR_TYPE { int type; /* If this is a file, or command line */ char *expr; /* Pointer to either filename or expression */ } expr_table[MAX_EXPRESSIONS]; int exprs; /* How many expressions there were */ enum EXPR_TYPES { E_FILE,E_TEXT }; /* * Struct for time information */ struct time_points { struct tms start; struct tms parsing; struct tms acsearch; struct tms eval; struct tms output; } tps; /* * The copyright notice text. */ char *copyright_text[]={ "sgrep version "VERSION" - search a file for structured pattern", "Copyright (C) 1996 University of Helsinki", "", "This program is free software; you can redistribute it and/or modify", "it under the terms of the GNU General Public License as published by", "the Free Software Foundation; either version 2 of the License, or", "(at your option) any later version.", "", "This program is distributed in the hope that it will be useful,", "but WITHOUT ANY WARRANTY; without even the implied warranty of", "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the", "GNU General Public License for more details.", "", "You should have received a copy of the GNU General Public License", "along with this program; if not, write to the Free Software", "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.", "", "Authors: Pekka Kilpeläinen Pekka.Kilpelainen@cc.helsinki.fi", " Jani Jaakkola Jani.Jaakkola@cc.helsinki.fi", NULL }; /* * Struct for options */ struct opt_data { char opt; char *have_param; char *what_does; }; /* * List & description of options * If you add more options, add the descriptions here. Put the implementation * of option in get_options() */ struct opt_data options[]= { { 'a',NULL,"act as a filter" }, { 'C',NULL,"display copyright notice" }, { 'c',NULL,"display only count of matching regions" }, #ifdef PROGRESS_REPORTS { 'D',NULL,"verbose, show progress" }, #endif { 'd',NULL,"don't do concat on result list"}, { 'h',NULL,"help (means this text)" }, { 'i',NULL,"ignore case distinctions in phrases" }, { 'l',NULL,"long output format" }, { 'N',NULL,"don't add trailing newline" }, { 'n',NULL,"don't read $HOME/"USER_SGREPRC" or "SYSTEM_SGREPRC}, { 'P',NULL,"show preprocessed expression, don't execute it." }, { 'q',NULL,"supress normal output" }, { 'S',NULL,"stream mode (regions extend across files)"}, { 's',NULL,"short output format" }, { 'T',NULL,"show statistics about what was done" }, { 't',NULL,"show information about time spent"}, { 'V',NULL,"display version information" }, { 'e',"","execute expression (after preprocessing)" }, { 'f',"","reads commands from file" }, { 'O',"","reads output style from file"}, { 'o',"