2010-02-02 23:20:29 +00:00
|
|
|
/** \file shared/str.c
|
|
|
|
|
* Commmand / argument parsing functions (for use in clients).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
|
* This file is part of LCDproc.
|
|
|
|
|
*
|
|
|
|
|
* This file is released under the GNU General Public License.
|
|
|
|
|
* Refer to the COPYING file distributed with this package.
|
|
|
|
|
*/
|
|
|
|
|
|
1999-12-09 23:15:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2010-02-02 23:20:29 +00:00
|
|
|
#include "report.h"
|
1999-12-09 23:15:14 +00:00
|
|
|
#include "str.h"
|
|
|
|
|
|
2010-02-02 23:20:29 +00:00
|
|
|
/** Split elements of a string into an array of strings.
|
|
|
|
|
* Elements are typically commands and arguments.
|
|
|
|
|
* \param **argv Pointer to the array which will store the arguments
|
|
|
|
|
* \param *str The string to be parsed
|
|
|
|
|
* \param max_args Number of arguments to parse (typically the size of argv)
|
|
|
|
|
* \retval <0 Error.
|
|
|
|
|
* \retval >=0 The number of arguments parsed.
|
|
|
|
|
*/
|
2000-03-30 20:28:01 +00:00
|
|
|
int
|
|
|
|
|
get_args (char **argv, char *str, int max_args)
|
1999-12-09 23:15:14 +00:00
|
|
|
{
|
2000-04-03 22:13:57 +00:00
|
|
|
char *delimiters = " \n\0";
|
|
|
|
|
char *item;
|
|
|
|
|
int i = 0;
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2000-04-03 22:13:57 +00:00
|
|
|
if (!argv)
|
|
|
|
|
return -1;
|
|
|
|
|
if (!str)
|
|
|
|
|
return 0;
|
|
|
|
|
if (max_args < 1)
|
|
|
|
|
return 0;
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2010-02-02 23:20:29 +00:00
|
|
|
debug(RPT_DEBUG, "get_args(%i): string=%s", max_args, str);
|
2000-03-30 20:28:01 +00:00
|
|
|
|
2010-02-02 23:20:29 +00:00
|
|
|
/* Parse the command line... */
|
2000-04-03 22:13:57 +00:00
|
|
|
for (item = strtok (str, delimiters); item; item = strtok (NULL, delimiters)) {
|
2010-02-02 23:20:29 +00:00
|
|
|
debug(RPT_DEBUG, "get_args: item=%s", item);
|
2000-04-03 22:13:57 +00:00
|
|
|
if (i < max_args) {
|
|
|
|
|
argv[i] = item;
|
|
|
|
|
i++;
|
|
|
|
|
} else
|
|
|
|
|
return i;
|
|
|
|
|
}
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2000-04-03 22:13:57 +00:00
|
|
|
return i;
|
1999-12-09 23:15:14 +00:00
|
|
|
}
|