Commit | Line | Data |
---|---|---|
48435ab0 WJ |
1 | /* sstr2arg: convert string into argument list */ |
2 | ||
3 | #ifndef lint | |
4 | static char *rcsid = "$Header: /f/osi/compat/RCS/sstr2arg.c,v 7.3 91/02/22 09:15:54 mrose Interim $"; | |
5 | #endif | |
6 | ||
7 | /* | |
8 | * $Header: /f/osi/compat/RCS/sstr2arg.c,v 7.3 91/02/22 09:15:54 mrose Interim $ | |
9 | * | |
10 | * | |
11 | * $Log: sstr2arg.c,v $ | |
12 | * Revision 7.3 91/02/22 09:15:54 mrose | |
13 | * Interim 6.8 | |
14 | * | |
15 | * Revision 7.2 90/10/16 11:20:29 mrose | |
16 | * partial-backoff-from-jpo | |
17 | * | |
18 | * Revision 7.1 90/10/15 18:19:57 mrose | |
19 | * sync | |
20 | * | |
21 | * Revision 7.0 89/11/23 21:23:34 mrose | |
22 | * Release 6.0 | |
23 | * | |
24 | */ | |
25 | ||
26 | /* | |
27 | * NOTICE | |
28 | * | |
29 | * Acquisition, use, and distribution of this module and related | |
30 | * materials are subject to the restrictions of a license agreement. | |
31 | * Consult the Preface in the User's Manual for the full terms of | |
32 | * this agreement. | |
33 | * | |
34 | */ | |
35 | ||
36 | ||
37 | /* LINTLIBRARY */ | |
38 | ||
39 | #include <stdio.h> | |
40 | #include "manifest.h" | |
41 | #include <errno.h> | |
42 | ||
43 | extern int errno; | |
44 | ||
45 | /* \f */ | |
46 | ||
47 | /* | |
48 | stash a pointer to each field into the passed array. any common seperators | |
49 | split the words. extra white-space between fields is ignored. | |
50 | ||
51 | specially-interpreted characters: | |
52 | double-quote, backslash (preceding a special char with a backslash | |
53 | removes its interpretation. A backslash not followed by a special is | |
54 | used to preface an octal specification for one character a string begun | |
55 | with double-quote has only double-quote and backslash as special | |
56 | characters. | |
57 | ||
58 | */ | |
59 | ||
60 | ||
61 | ||
62 | ||
63 | sstr2arg (srcptr, maxpf, argv, dlmstr) | |
64 | register char *srcptr; /* source data */ | |
65 | int maxpf; /* maximum number of permitted fields */ | |
66 | char *argv[]; /* where to put the pointers */ | |
67 | char *dlmstr; /* Delimiting character */ | |
68 | { | |
69 | char gotquote; /* currently parsing quoted string */ | |
70 | register int ind; | |
71 | register char *destptr; | |
72 | ||
73 | if (srcptr == 0) { | |
74 | errno = EINVAL; /* emulate system-call failure */ | |
75 | return (NOTOK); | |
76 | } | |
77 | ||
78 | for (ind = 0, maxpf -= 2;; ind++) { | |
79 | if (ind >= maxpf) { | |
80 | errno = E2BIG; /* emulate system-call failure */ | |
81 | return (NOTOK); | |
82 | } | |
83 | ||
84 | /* Skip leading white space */ | |
85 | for (; *srcptr == '\t' || *srcptr == ' '; srcptr++); | |
86 | ||
87 | argv [ind] = srcptr; | |
88 | destptr = srcptr; | |
89 | ||
90 | for (gotquote = 0; ; ) { | |
91 | register char *cp; | |
92 | ||
93 | for (cp = dlmstr; (*cp != '\0') && (*cp != *srcptr); cp++); | |
94 | ||
95 | if (*cp != '\0') | |
96 | { | |
97 | if (gotquote) { /* don't interpret the char */ | |
98 | *destptr++ = *srcptr++; | |
99 | continue; | |
100 | } | |
101 | ||
102 | srcptr++; | |
103 | *destptr = '\0'; | |
104 | goto nextarg; | |
105 | } else { | |
106 | switch (*srcptr) { | |
107 | default: /* just copy it */ | |
108 | *destptr++ = *srcptr++; | |
109 | break; | |
110 | ||
111 | case '\"': /* beginning or end of string */ | |
112 | gotquote = (gotquote) ? 0 : 1 ; | |
113 | srcptr++; /* just toggle */ | |
114 | break; | |
115 | ||
116 | case '\\': /* quote next character */ | |
117 | srcptr++; /* skip the back-slash */ | |
118 | switch (*srcptr) { | |
119 | /* Octal character */ | |
120 | case '0': case '1': case '2': case '3': | |
121 | case '4': case '5': case '6': case '7': | |
122 | *destptr = '\0'; | |
123 | do | |
124 | *destptr = (*destptr << 3) | (*srcptr++ - '0'); | |
125 | while (*srcptr >= '0' && *srcptr <= '7'); | |
126 | destptr++; | |
127 | break; | |
128 | /* C escape char */ | |
129 | case 'b': *destptr++ = '\b'; srcptr++; break; | |
130 | case 'n': *destptr++ = '\n'; srcptr++; break; | |
131 | case 'r': *destptr++ = '\r'; srcptr++; break; | |
132 | case 't': *destptr++ = '\t'; srcptr++; break; | |
133 | /* Boring -- just copy ASIS */ | |
134 | default: | |
135 | *destptr++ = *srcptr++; | |
136 | } | |
137 | break; | |
138 | ||
139 | case '\0': | |
140 | *destptr = '\0'; | |
141 | ind++; | |
142 | argv[ind] = (char *) 0; | |
143 | return (ind); | |
144 | } | |
145 | } | |
146 | } | |
147 | nextarg: | |
148 | continue; | |
149 | } | |
150 | } |