86a0c25c9860827bdf674904e4e72f2eb53d5df7
[dragonfly.git] / usr.bin / doscmd / ParseBuffer.c
1 /*
2  * Copyright (c) 1992, 1993, 1996
3  *      Berkeley Software Design, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Berkeley Software
16  *      Design, Inc.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      BSDI ParseBuffer.c,v 2.2 1996/04/08 19:32:15 bostic Exp
31  *
32  * $FreeBSD: src/usr.bin/doscmd/ParseBuffer.c,v 1.2.2.2 2002/04/25 11:04:50 tg Exp $
33  * $DragonFly: src/usr.bin/doscmd/ParseBuffer.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
34  */
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "doscmd.h"
40 int
41 ParseBuffer(obuf, av, mac)
42 char *obuf;
43 char **av;
44 int mac;
45 {
46         static char *_buf;
47         char *buf;
48         static int buflen = 0;
49         int len;
50
51         register char *b = buf;
52         register char *p;
53         register char **a;
54         register char **e;
55
56         len = strlen(obuf) + 1;
57         if (len > buflen) {
58                 if (buflen)
59                         free(_buf);
60                 buflen = (len + 1023) & ~1023;
61                 _buf = malloc(buflen);
62         } 
63         buf = _buf;
64         strcpy(buf, obuf);
65
66         a = av;
67         e = &av[mac];
68
69         while (*buf) {
70                 while (*buf == ' ' || *buf == '\t' || *buf == '\n')
71                         ++buf;
72                 if (*buf) {
73                         p = b = buf;
74
75                         *a++ = buf;
76                         if (a == e) {
77                                 a[-1] = (char *)0;
78                                 return(mac - 1);
79                         }
80
81                         while (*p && !(*p == ' ' || *p == '\t' || *p == '\n')) {
82                                 *b++ = *p++ & 0177;
83                         }
84                         if (*p)
85                                 ++p;
86                         *b = 0;
87                         buf = p;
88                 }
89         }
90         *a = (char *)0;
91         return(a - av);
92 }