简体   繁体   English

关于grep的c程序

[英]c program about grep

im having trouble with this code ,its about search string from file after converting to dfa and nfa whenever i try to access argv value, it give " 0x7fffffffe977 "XDG_MENU_PREFIX=gnom"... " maybe garbage value? 即使我在尝试访问argv值时转换为dfa和nfa,但是这个代码有问题,它会从文件中搜索字符串,它会给出“0x7fffffffe977”XDG_MENU_PREFIX = gnom“...”可能是垃圾值吗? though im not increment the argv (?) 虽然我不增加argv(?)

though i have parameter in arguments such as -s 'char' /usr/share/dict/words can someone suggest why this bug happen? 虽然我在-s'char'/ usr / share / dict / words等参数中有参数可以有人建议为什么会发生这个错误?

//parameter -s 'c.h.a.r' /usr/share/dict/words

#include "regmatch.h"
#include <string.h>
#include <strings.h>

#define BUFSIZE 256

int debug = 0;     
char *reg_string;

static void do_grep(FILE *fp);
static void usage_exit(void);
static void show_region(char *p, char *from, char *to);
static char *match_line(char *str, char **cpp);
static char *match_string(char *str);

static int vflag = 0;  
static int sflag = 0;  
static int dflag = 0; 
static char *progname;

int main(int argc, char *argv[])
{
  FILE *fp;
  char c, doption = '0';
  ptree *root;


  if ((progname = strrchr(*argv, '/')) == NULL)
    progname = *argv;
  else
    progname++;

/*option*/
  while (--argc > 0 && (*++argv)[0] == '-' ) { //parameter -s 'c.h.a.r' /usr/share/dict/words, but it not detect any string in argv[0] so this not 
enter while loop why?
    while ((c = *++argv[0])) {
      switch(c) {
      case 'v':
    vflag = 1;
    break;
      case 's':
    sflag = 1;
    break;
      case 'd':
    dflag = 1;

    if (!(isdigit(doption = *++argv[0]))) {
      fatal_error("error");
      usage_exit();
    }
    break;
      default:
    fatal_error("there is error in option");
    usage_exit();
    break;
      }
    }
  }

  if (argc-- < 1) {
    fatal_error("error");
    usage_exit();
  }

  reg_string = *argv++;

  if (dflag) {
    if (sflag || vflag) {

      fatal_error("cannot input -d and -s option together");
      usage_exit();
    }
    switch(doption) {
    case '1':
      lexer();
      break;
    case '2':
      parse();
      break;
    case '3':
      make_nfa();
      break;
    case '4':
      make_dfa();
      break;
    default: 
      fatal_error("-d option 1 until 4");
      usage_exit();
      break;
    }
    exit(0);  
  }

  //make dfa
  get_token();
  root = eval_expr();  //make tree
  if (curr_token != EOREG) 
    parse_error();
  gen_nfa(root);    //change to nfa
  gen_dfa();        //change to dfa



  if (argc < 1) {

    do_grep(stdin);
  } else { //if parameter include file name


    while(*argv){ //here also the value of argv is weird , it give endless loop ....

    printf("%s",argv[0]);


    while (argc-- > 0) {
      if ((fp = fopen(*argv++, "r")) == NULL) {
    fatal_error("cant open file");
    exit(1);
      }
      do_grep(fp);
      fclose(fp);
    }

    }

  }

  return 0;
}

argv change to weird value after programename line argv在programename行之后变为奇怪的值

when first debug 第一次调试时

You need to breakdown the problem, perhaps ask about specific samples you would create. 您需要分解问题,或者询问您将创建的具体样本。 There could be multiple issues in this code. 此代码中可能存在多个问题。

For the segfault I recommend single stepping in a debugger .. (such as gdb). 对于段错误,我建议单步执行调试器..(例如gdb)。 Also, it is best if you isolate the problem to few lines, rather than expect someone to debug your whole code for you. 此外,最好将问题隔离到几行,而不是希望有人为您调试整个代码。

Eg: 例如:

//parameter -s 'char' /usr/share/dict/words, but it not detect any string in argv[0] so this not enter while loop //参数-s'char'/ usr / share / dict / words,但它没有检测到argv [0]中的任何字符串,所以这不会进入while循环

Test just this with parameters -a 'char' 用参数-a'char'测试这个

/*option*/
  while (--argc > 0 && (*++argv)[0] == '-' )
    printf("opt=> '%s'\n", argv[0]);

This works fine, picks up the '-'. 这很好用,拿起' - '。

And so on. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM