2005-01-25 13:54:56 +03:00
|
|
|
/* 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 */
|
|
|
|
|
2005-03-22 02:04:14 +03:00
|
|
|
#include <my_global.h>
|
2005-02-11 14:21:59 +03:00
|
|
|
#include "parse.h"
|
|
|
|
|
2005-01-25 13:54:56 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <my_sys.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
2005-02-11 14:21:59 +03:00
|
|
|
/*
|
|
|
|
Parse output of the given command
|
2005-01-25 13:54:56 +03:00
|
|
|
|
2005-02-11 14:21:59 +03:00
|
|
|
SYNOPSYS
|
|
|
|
parse_output_and_get_value()
|
2005-01-25 13:54:56 +03:00
|
|
|
|
2005-02-11 14:21:59 +03:00
|
|
|
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)
|
|
|
|
result_len self-explanatory
|
2005-01-25 13:54:56 +03:00
|
|
|
|
2005-02-11 14:21:59 +03:00
|
|
|
DESCRIPTION
|
2005-01-25 13:54:56 +03:00
|
|
|
|
2005-02-11 14:21:59 +03:00
|
|
|
Parse output of the "command". Find the "word" and return the next one
|
2005-02-13 15:13:33 +03:00
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 - ok
|
|
|
|
1 - error occured
|
2005-02-11 14:21:59 +03:00
|
|
|
*/
|
2005-01-25 13:54:56 +03:00
|
|
|
|
|
|
|
int parse_output_and_get_value(const char *command, const char *word,
|
|
|
|
char *result, size_t result_len)
|
|
|
|
{
|
|
|
|
FILE *output;
|
2005-02-11 14:21:59 +03:00
|
|
|
uint wordlen;
|
|
|
|
/* should be enought to store the string from the output */
|
|
|
|
enum { MAX_LINE_LEN= 512 };
|
|
|
|
char linebuf[MAX_LINE_LEN];
|
2005-01-25 13:54:56 +03:00
|
|
|
|
|
|
|
wordlen= strlen(word);
|
|
|
|
|
2005-02-27 18:41:34 +03:00
|
|
|
if (!(output= popen(command, "r")))
|
2005-02-13 15:13:33 +03:00
|
|
|
goto err;
|
2005-01-25 13:54:56 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
We want fully buffered stream. We also want system to
|
|
|
|
allocate appropriate buffer.
|
|
|
|
*/
|
|
|
|
setvbuf(output, NULL, _IOFBF, 0);
|
|
|
|
|
2005-02-11 14:21:59 +03:00
|
|
|
while (fgets(linebuf, sizeof(linebuf) - 1, output))
|
2005-01-25 13:54:56 +03:00
|
|
|
{
|
2005-02-11 14:21:59 +03:00
|
|
|
uint lineword_len= 0;
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
get_word((const char **) &linep, &lineword_len, NONSPACE);
|
2005-02-13 15:13:33 +03:00
|
|
|
if (!strncmp(word, linep, wordlen))
|
2005-02-11 14:21:59 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
If we have found the word, return the next one. This is usually
|
|
|
|
an option value.
|
|
|
|
*/
|
2005-02-13 15:13:33 +03:00
|
|
|
linep+= lineword_len; /* swallow the previous one */
|
2005-02-11 14:21:59 +03:00
|
|
|
get_word((const char **) &linep, &lineword_len, NONSPACE);
|
2005-02-13 15:13:33 +03:00
|
|
|
if (result_len <= lineword_len)
|
|
|
|
goto err;
|
2005-02-11 14:21:59 +03:00
|
|
|
strncpy(result, linep, lineword_len);
|
2005-02-13 15:13:33 +03:00
|
|
|
result[lineword_len]= '\0';
|
2005-02-11 14:21:59 +03:00
|
|
|
goto pclose;
|
|
|
|
}
|
2005-01-25 13:54:56 +03:00
|
|
|
}
|
|
|
|
|
2005-02-11 14:21:59 +03:00
|
|
|
pclose:
|
2005-02-15 04:38:33 +03:00
|
|
|
/* we are not interested in the termination status */
|
|
|
|
pclose(output);
|
2005-01-25 13:54:56 +03:00
|
|
|
|
|
|
|
return 0;
|
2005-02-13 15:13:33 +03:00
|
|
|
|
|
|
|
err:
|
|
|
|
return 1;
|
2005-01-25 13:54:56 +03:00
|
|
|
}
|