1/*	$NetBSD: nxtarg.c,v 1.5 2002/07/10 20:19:41 wiz Exp $	*/
2
3/*
4 * Copyright (c) 1991 Carnegie Mellon University
5 * All Rights Reserved.
6 *
7 * Permission to use, copy, modify and distribute this software and its
8 * documentation is hereby granted, provided that both the copyright
9 * notice and this permission notice appear in all copies of the
10 * software, derivative works or modified versions, and any portions
11 * thereof, and that both notices appear in supporting documentation.
12 *
13 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
14 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
15 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 *
17 * Carnegie Mellon requests users of this software to return to
18 *
19 *  Software Distribution Coordinator   or   Software.Distribution@CS.CMU.EDU
20 *  School of Computer Science
21 *  Carnegie Mellon University
22 *  Pittsburgh PA 15213-3890
23 *
24 * any improvements or extensions that they make and grant Carnegie the rights
25 * to redistribute these changes.
26 */
27/*
28 *  nxtarg -- strip off arguments from a string
29 *
30 *  Usage:  p = nxtarg (&q,sep);
31 *	char *p,*q,*sep;
32 *	extern char _argbreak;
33 *
34 *	q is pointer to next argument in string
35 *	after call, p points to string containing argument,
36 *	q points to remainder of string
37 *
38 *  Leading blanks and tabs are skipped; the argument ends at the
39 *  first occurence of one of the characters in the string "sep".
40 *  When such a character is found, it is put into the external
41 *  variable "_argbreak", and replaced by a null character; if the
42 *  arg string ends before that, then the null character is
43 *  placed into _argbreak;
44 *  If "sep" is 0, then " " is substituted.
45 *
46 *  HISTORY
47 * 01-Jul-83  Steven Shafer (sas) at Carnegie-Mellon University
48 *	Bug fix: added check for "back >= front" in loop to chop trailing
49 *	white space.
50 *
51 * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
52 *	Rewritten for VAX.  By popular demand, a table of break characters
53 *	has been added (implemented as a string passed into nxtarg).
54 *
55 *  Originally	from klg (Ken Greer); IUS/SUS UNIX.
56 */
57#include "supcdefs.h"
58#include "supextern.h"
59
60char _argbreak;
61
62char *
63nxtarg(char **q, const char *sep)
64{
65	char *front, *back;
66	front = *q;		/* start of string */
67	/* leading blanks and tabs */
68	while (*front && (*front == ' ' || *front == '\t'))
69		front++;
70	/* find break character at end */
71	if (sep == 0)
72		sep = " ";
73	back = skipto(front, sep);
74	_argbreak = *back;
75	*q = (*back ? back + 1 : back);	/* next arg start loc */
76	/* elim trailing blanks and tabs */
77	back -= 1;
78	while ((back >= front) && (*back == ' ' || *back == '\t'))
79		back--;
80	back++;
81	if (*back)
82		*back = '\0';
83	return (front);
84}
85