/*
* This OS/2 port was hacked by Harald Kipp from the
*
* Network News Transfer Protocol server
*
* Phil Lapsley
* University of California, Berkeley
* Stan Barber
* Baylor College of Medicine
*
* Bug reports related to THIS modified version should be sent to
*
* harald@os2point.ping.de
* harald@sesam.com
* Fido: 2:2448/434
*
*/
/*
* Parse a string of words separated by spaces into an
* array of pointers to characters, just like good ol' argv[]
* and argc.
*
* Usage:
*
* char line[132];
* char **argv;
* int argc;
*
* argv = (char **) NULL;
* argc = parsit(line, &argv);
*
* returns the number of words parsed in argc. argv[argc] will
* be (char *) NULL to indicate end of list, if you're not
* happy with just knowing how many words you have.
*
* Note that setting argv = (char **) NULL is only done the first
* time the routine is called with a new "argv" -- it tells
* parsit that "argv" is a new array, and parsit shouldn't free
* up the elements (as it would do if it were an old array).
*
* Phil Lapsley
* College of Engineering
* University of California, Berkeley
* (ARPA: phil@Berkeley.ARPA; UUCP: ...!ucbvax!phil)
*/
#include <os2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dbgheap.h>
#include "globals.h"
#include "changi.h"
int parsit(char *line, char ***array)
{
char **argv;
char *word;
char *linecp;
int i, j, num_words, longest_word;
argv = *array;
if (argv != (char **)NULL) { /* Check to see if we should */
for (i = 0; argv[i] != (char *)NULL; i++) /* free */
free(argv[i]); /* the old array */
free((char *)argv); /* and then free the ptr itself */
}
linecp = line;
num_words = longest_word = 0;
for (;;) { /* count words in input */
for (; *linecp == ' ' || *linecp == '\t'; ++linecp) ;
if (*linecp == '\0')
break;
word = linecp;
for (; *linecp != ' ' && *linecp != '\t' && *linecp != '\0'; ++linecp) ;
++num_words;
if ((i = linecp - word) > longest_word)
longest_word = i;
if (*linecp == '\0')
break;
}
/*
* Allocate enough for that many words plus 1 for null
*/
argv = malloc((num_words + 1) * sizeof(char *));
/*
* Allocate enough to fit the longest word
*/
word = malloc(longest_word + 1);
j = i = 0;
for (;;) { /* Now build the list of words */
for (; *line == ' ' || *line == '\t'; ++line) ;
if (*line == '\0')
break;
i = 0;
for (; *line != ' ' && *line != '\t' && *line != '\0'; ++line)
word[i++] = *line;
word[i] = '\0';
argv[j++] = strdup(word);
if (*line == '\0')
break;
}
argv[j] = NULL; /* remember null at end of list */
*array = argv;
free(word);
return (j);
}