parse_output.cc 3.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (C) 2004 MySQL AB

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

17
#include <my_global.h>
18
#include "parse.h"
unknown's avatar
unknown committed
19
#include "parse_output.h"
20

21 22
#include <stdio.h>
#include <my_sys.h>
unknown's avatar
unknown committed
23
#include <m_string.h>
24 25


26 27
/*
  Parse output of the given command
28

29 30
  SYNOPSYS
    parse_output_and_get_value()
31

unknown's avatar
unknown committed
32 33 34 35 36 37
    command          the command to execue with popen.
    word             the word to look for (usually an option name)
    result           the buffer to store the next word (option value)
    input_buffer_len self-explanatory
    flag             this equals to GET_LINE if we want to get all the line after
                     the matched word and GET_VALUE otherwise.
38

39
  DESCRIPTION
40

41
    Parse output of the "command". Find the "word" and return the next one
unknown's avatar
unknown committed
42
    if flag is GET_VALUE. Return the rest of the parsed string otherwise.
unknown's avatar
unknown committed
43 44 45 46

  RETURN
    0 - ok
    1 - error occured
47
*/
48 49

int parse_output_and_get_value(const char *command, const char *word,
unknown's avatar
unknown committed
50 51
                               char *result, size_t input_buffer_len,
                               uint flag)
52 53
{
  FILE *output;
54
  uint wordlen;
55
  /* should be enough to store the string from the output */
56 57
  enum { MAX_LINE_LEN= 512 };
  char linebuf[MAX_LINE_LEN];
58 59 60

  wordlen= strlen(word);

61
  if (!(output= popen(command, "r")))
unknown's avatar
unknown committed
62
    goto err;
63 64 65 66 67 68 69

  /*
    We want fully buffered stream. We also want system to
    allocate appropriate buffer.
  */
  setvbuf(output, NULL, _IOFBF, 0);

70
  while (fgets(linebuf, sizeof(linebuf) - 1, output))
71
  {
unknown's avatar
unknown committed
72
    uint found_word_len= 0;
73 74 75 76 77 78 79 80
    char *linep= linebuf;

    linebuf[sizeof(linebuf) - 1]= '\0';        /* safety */

    /*
      Get the word, which might contain non-alphanumeric characters. (Usually
      these are '/', '-' and '.' in the path expressions and filenames)
    */
unknown's avatar
unknown committed
81
    get_word((const char **) &linep, &found_word_len, NONSPACE);
unknown's avatar
unknown committed
82
    if (!strncmp(word, linep, wordlen))
83 84
    {
      /*
unknown's avatar
unknown committed
85 86
        If we have found the word, return the next one (this is usually
        an option value) or the whole line (if flag)
87
      */
unknown's avatar
unknown committed
88 89
      linep+= found_word_len;                     /* swallow the previous one */
      if (flag & GET_VALUE)    /* not GET_LINE */
90
      {
unknown's avatar
unknown committed
91 92
        get_word((const char **) &linep, &found_word_len, NONSPACE);
        if (input_buffer_len <= found_word_len)
93
          goto err;
unknown's avatar
unknown committed
94
        strmake(result, linep, found_word_len);
95
      }
unknown's avatar
unknown committed
96
      else         /* currently there are only two options */
97
      {
unknown's avatar
unknown committed
98
        strmake(result, linep, input_buffer_len - 1);
99
      }
100 101
      goto pclose;
    }
102 103
  }

104
pclose:
unknown's avatar
unknown committed
105 106
  /* we are not interested in the termination status */
  pclose(output);
107 108

  return 0;
unknown's avatar
unknown committed
109 110 111

err:
  return 1;
112
}
113