Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/stringTable.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Copyright 1993 NeXT, Inc.
26 * All rights reserved.
27 */
28
29#include "libsaio.h"
30#include "bootstruct.h"
31#include "platform.h"
32#include "xml.h"
33
34static char * AllocInitStringWithLength(const char * oldString, int len);
35static char * AllocInitZeroEndedStringWithLength(const char * oldString, int len);
36
37#ifndef MIN
38#define MIN(x, y) ((x) < (y) ? (x) : (y))
39#endif
40
41#define ASSERT_CONFIG \
42if (config == DEFAULT_BOOT_CONFIG) \
43config = &bootInfo->bootConfig ; \
44else if (config == DEFAULT_SYSTEM_CONFIG) \
45config = &bootInfo->SystemConfig ; \
46else if (config == DEFAULT_OVERRIDE_CONFIG) \
47config = &bootInfo->overrideConfig ; \
48else if (config == DEFAULT_SMBIOS_CONFIG) \
49config = &bootInfo->smbiosConfig ; \
50else if (config == DEFAULT_HELPER_CONFIG) \
51config = &bootInfo->helperConfig ;
52
53#if UNUSED
54/*
55 * Compare a string to a key with quoted characters
56 */
57static inline int
58keyncmp(const char *str, const char *key, int n)
59{
60 int c;
61 while (n--) {
62c = *key++;
63if (c == '\\') {
64switch(c = *key++) {
65case 'n':
66c = '\n';
67break;
68case 'r':
69c = '\r';
70break;
71case 't':
72c = '\t';
73break;
74default:
75break;
76}
77} else if (c == '\"') {
78/* Premature end of key */
79return 1;
80}
81if (c != *str++) {
82return 1;
83}
84 }
85 return 0;
86}
87
88static void eatThru(char val, const char **table_p)
89{
90register const char *table = *table_p;
91register bool found = false;
92
93while (*table && !found)
94{
95if (*table == '\\') table += 2;
96else
97{
98if (*table == val) found = true;
99table++;
100}
101}
102*table_p = table;
103}
104
105/* Remove key and its associated value from the table. */
106
107bool
108removeKeyFromTable(const char *key, char *table)
109{
110 register int len;
111 register char *tab;
112 char *buf;
113
114 len = strlen(key);
115 tab = (char *)table;
116 buf = (char *)malloc(len + 3);
117if (!buf) {
118 return false;
119 }
120 sprintf(buf, "\"%s\"", key);
121 len = strlen(buf);
122
123 while(*tab) {
124 if(strncmp(buf, tab, len) == 0) {
125 char c;
126
127 while((c = *(tab + len)) != ';') {
128 if(c == 0) {
129 len = -1;
130 goto out;
131 }
132 len++;
133 }
134 len++;
135 if(*(tab + len) == '\n') len++;
136 goto out;
137 }
138 tab++;
139 }
140 len = -1;
141out:
142 free(buf);
143
144 if(len == -1) return false;
145
146 while((*tab = *(tab + len))) {
147 tab++;
148 }
149
150 return true;
151}
152
153char *
154newStringFromList(
155 char **list,
156 int *size
157 )
158{
159 char *begin = *list, *end;
160 char *newstr;
161 int newsize = *size;
162 int bufsize;
163
164 while (*begin && newsize && isspace(*begin)) {
165begin++;
166newsize--;
167 }
168 end = begin;
169 while (*end && newsize && !isspace(*end)) {
170end++;
171newsize--;
172 }
173 if (begin == end)
174return 0;
175 bufsize = end - begin + 1;
176 newstr = malloc(bufsize);
177 if (!newstr) {
178 return 0;
179 }
180 strlcpy(newstr, begin, bufsize);
181 *list = end;
182 *size = newsize;
183 return newstr;
184}
185
186/*
187 * compress == compress escaped characters to one character
188 */
189int stringLength(const char *table, int compress)
190{
191int ret = 0;
192
193while (*table)
194{
195if (*table == '\\')
196{
197table += 2;
198ret += 1 + (compress ? 0 : 1);
199}
200else
201{
202if (*table == '\"') return ret;
203ret++;
204table++;
205}
206}
207return ret;
208}
209#endif
210
211bool getValueForConfigTableKey(config_file_t *config, const char *key, const char **val, int *size)
212{
213 ASSERT_CONFIG
214
215if (config->dictionary != 0 ) {
216// Look up key in XML dictionary
217TagPtr value;
218value = XMLGetProperty(config->dictionary, key);
219if (value != 0) {
220if (value->type != kTagTypeString) {
221error("Non-string tag '%s' found in config file\n",
222 key);
223return false;
224}
225*val = value->string;
226*size = strlen(value->string);
227return true;
228}
229} else {
230
231// Legacy plist-style table
232
233}
234
235return false;
236}
237
238#if UNUSED
239
240/*
241 * Returns a new malloc'ed string if one is found
242 * in the string table matching 'key'. Also translates
243 * \n escapes in the string.
244 */
245char *newStringForStringTableKey(
246 char *table,
247 char *key,
248 config_file_t *config
249 )
250{
251 const char *val;
252 char *newstr, *p;
253 int size;
254
255 ASSERT_CONFIG
256
257 if (getValueForConfigTableKey(config, key, &val, &size)) {
258newstr = (char *)malloc(size+1);
259 if (!newstr) {
260 return 0;
261 }
262for (p = newstr; size; size--, p++, val++) {
263if ((*p = *val) == '\\') {
264switch (*++val) {
265case 'r':
266*p = '\r';
267break;
268case 'n':
269*p = '\n';
270break;
271case 't':
272*p = '\t';
273break;
274default:
275*p = *val;
276break;
277}
278size--;
279}
280}
281*p = '\0';
282return newstr;
283 } else {
284return 0;
285 }
286}
287
288#endif
289
290char *
291newStringForKey(char *key, config_file_t *config)
292{
293 const char *val;
294 char *newstr;
295 int size;
296
297 ASSERT_CONFIG
298
299 if (getValueForKey(key, &val, &size, config) && size) {
300newstr = (char *)malloc(size + 1);
301 if (!newstr) {
302 return 0;
303 }
304strlcpy(newstr, val, size + 1);
305return newstr;
306 } else {
307return 0;
308 }
309}
310
311/* parse a command line
312 * in the form: [<argument> ...] [<option>=<value> ...]
313 * both <option> and <value> must be either composed of
314 * non-whitespace characters, or enclosed in quotes.
315 */
316
317static const char *getToken(const char *line, const char **begin, int *len)
318{
319 if (*line == '\"') {
320*begin = ++line;
321while (*line && *line != '\"')
322line++;
323*len = line++ - *begin;
324 } else {
325*begin = line;
326while (*line && !isspace(*line) && *line != '=')
327line++;
328*len = line - *begin;
329 }
330 return line;
331}
332
333bool getValueForBootKey(const char *line, const char *match, const char **matchval, int *len)
334{
335 const char *key, *value;
336 int key_len, value_len;
337 bool retval = false;
338
339 while (*line) {
340/* look for keyword or argument */
341while (isspace(*line)) line++;
342
343/* now look for '=' or whitespace */
344line = getToken(line, &key, &key_len);
345/* line now points to '=' or space */
346if (*line && !isspace(*line)) {
347line = getToken(++line, &value, &value_len);
348} else {
349value = line;
350value_len = 0;
351}
352if ((strlen(match) == key_len)
353&& strncmp(match, key, key_len) == 0) {
354*matchval = value;
355*len = value_len;
356retval = true;
357 /* Continue to look for this key; last one wins. */
358}
359 }
360 return retval;
361}
362
363/* Return NULL if no option has been successfully retrieved, or the string otherwise */
364const char * getStringForKey(const char * key, config_file_t *config)
365{
366static const char* value =0;
367int len=0;
368
369 ASSERT_CONFIG
370
371if(!getValueForKey(key, &value, &len, config)) value = 0;
372return value;
373}
374
375
376/* Returns TRUE if a value was found, FALSE otherwise.
377 * The boolean value of the key is stored in 'val'.
378 */
379
380bool getBoolForKey( const char *key, bool *result_val, config_file_t *config )
381{
382 const char *key_val;
383 int size;
384
385 ASSERT_CONFIG
386
387 if (getValueForKey(key, &key_val, &size, config)) {
388 if ( (size >= 1) && (key_val[0] == 'Y' || key_val[0] == 'y') ) {
389 *result_val = true;
390 } else {
391 *result_val = false;
392 }
393 return true;
394 }
395 return false;
396}
397
398bool getIntForKey( const char *key, int *value, config_file_t *config )
399{
400 const char *val;
401 int size, sum;
402 bool negative = false;
403
404 ASSERT_CONFIG
405
406 if (getValueForKey(key, &val, &size, config))
407{
408if ( size )
409{
410if (*val == '-')
411{
412negative = true;
413val++;
414size--;
415}
416
417for (sum = 0; size > 0; size--)
418{
419if (*val < '0' || *val > '9')
420return false;
421
422sum = (sum * 10) + (*val++ - '0');
423}
424
425if (negative)
426sum = -sum;
427
428*value = sum;
429return true;
430}
431}
432 return false;
433}
434
435config_file_t *resolveConfig(config_file_t *config)
436{
437ASSERT_CONFIG
438config_file_t *new_config = config;
439return new_config;
440}
441#if UNUSED
442/*
443 *
444 */
445
446bool getDimensionForKey( const char *key, unsigned int *value, config_file_t *config, unsigned int dimension_max, unsigned int object_size )
447{
448const char *val;
449
450 int size = 0;
451int sum = 0;
452
453bool negative = false;
454bool percentage = false;
455
456 ASSERT_CONFIG
457
458 if (getValueForKey(key, &val, &size, config))
459{
460if ( size )
461{
462if (*val == '-')
463{
464negative = true;
465val++;
466size--;
467}
468
469if (val[size-1] == '%')
470{
471percentage = true;
472size--;
473}
474
475// convert string to integer
476for (sum = 0; size > 0; size--)
477{
478if (*val < '0' || *val > '9')
479return false;
480
481sum = (sum * 10) + (*val++ - '0');
482}
483
484if (percentage)
485sum = ( dimension_max * sum ) / 100;
486
487// calculate offset from opposite origin
488if (negative)
489sum = ( ( dimension_max - object_size ) - sum );
490
491} else {
492
493// null value calculate center
494sum = ( dimension_max - object_size ) / 2;
495
496}
497
498*value = (uint16_t) sum;
499return true;
500}
501
502// key not found
503 return false;
504}
505
506/*
507 *get color value from plist format #RRGGBB
508 */
509
510bool getColorForKey( const char *key, unsigned int *value, config_file_t *config )
511{
512 const char *val;
513 int size;
514
515 ASSERT_CONFIG
516
517 if (getValueForKey(key, &val, &size, config))
518{
519if (*val == '#')
520{
521 val++;
522*value = strtol(val, NULL, 16);
523return true;
524 }
525 }
526 return false;
527}
528#endif
529
530bool getValueForKey( const char *key, const char **val, int *size, config_file_t *config )
531{
532 const char *overrideVal;
533 int overrideSize;
534 bool ret;
535
536 if (getValueForBootKey(bootArgs->CommandLine, key, val, size))
537 return true;
538
539 ASSERT_CONFIG
540
541 ret = getValueForConfigTableKey(config, key, val, size);
542
543 // Try to find alternate keys in bootInfo->overrideConfig
544 // and prefer its values with the exceptions for
545 // "Kernel"="mach_kernel" and "Kernel Flags"="".
546
547 if (config->canOverride)
548 {
549 if (getValueForConfigTableKey(&bootInfo->overrideConfig, key, &overrideVal, &overrideSize) && overrideSize)
550 {
551 *val = overrideVal;
552 *size = overrideSize;
553 return true;
554 }
555 else if (getValueForConfigTableKey(&bootInfo->SystemConfig, key, &overrideVal, &overrideSize) && overrideSize)
556 {
557 *val = overrideVal;
558 *size = overrideSize;
559 return true;
560 }
561 else if (getValueForConfigTableKey(&bootInfo->bootConfig, key, &overrideVal, &overrideSize) && overrideSize)
562 {
563 *val = overrideVal;
564 *size = overrideSize;
565 return true;
566 }
567 }
568
569 return ret;
570}
571
572
573#if UNUSED
574void
575printSystemConfig(char *p1)
576{
577 char *p2 = p1, tmp;
578
579 while (*p1 != '\0') {
580while (*p2 != '\0' && *p2 != '\n') p2++;
581tmp = *p2;
582*p2 = '\0';
583printf("%s\n", p1);
584*p2 = tmp;
585if (tmp == '\0') break;
586p1 = ++p2;
587 }
588}
589#endif
590
591//==========================================================================
592// ParseXMLFile
593// Modifies the input buffer.
594// Expects to see one dictionary in the XML file.
595// Puts the first dictionary it finds in the
596// tag pointer and returns 0, or returns -1 if not found
597// (and does not modify dict pointer).
598// Prints an error message if there is a parsing error.
599//
600int ParseXMLFile( char * buffer, TagPtr * dict )
601{
602 long length, pos;
603 TagPtr tag;
604 pos = 0;
605 char *configBuffer;
606
607 configBuffer = malloc(strlen(buffer)+1);
608 if (!configBuffer) {
609 return -1;
610 }
611
612 strcpy(configBuffer, buffer);
613
614 while (1)
615 {
616 length = XMLParseNextTag(configBuffer + pos, &tag);
617 if (length == -1) break;
618
619 pos += length;
620
621 if (tag == 0) continue;
622 if (tag->type == kTagTypeDict) break;
623
624 XMLFreeTag(tag);
625 }
626 free(configBuffer);
627 if (length < 0) {
628 error ("Error parsing plist file\n");
629 return -1;
630 }
631 *dict = tag;
632 return 0;
633}
634
635/* loadConfigFile
636 *
637 * Returns 0 - successful.
638 * -1 - unsuccesful.
639 */
640int loadConfigFile (const char *configFile, config_file_t *config)
641{
642int fd, count, fixedsize;
643
644 ASSERT_CONFIG
645
646if ((fd = open(configFile)) < 0) {
647return -1;
648}
649
650 fixedsize = MIN(file_size(fd),IO_CONFIG_DATA_SIZE);
651
652// read file
653count = read(fd, config->plist, fixedsize);
654close(fd);
655 if (count != fixedsize) return -1;
656
657// build xml dictionary
658return ParseXMLFile(config->plist, &config->dictionary);
659}
660
661/* loadBooterConfig
662 *
663 * Returns 0 - successful.
664 * -1 - unsuccesful.
665 */
666int loadBooterConfig(void)
667{
668char *dirspec[] = {
669"rd(0,0)/Extra/com.apple.Boot.plist",
670"/Extra/com.apple.Boot.plist",
671"bt(0,0)/Extra/com.apple.Boot.plist",
672"rd(0,0)/Extra/org.chameleon.Boot.plist", // Add compatibility with the trunk
673"/Extra/org.chameleon.Boot.plist", // Add compatibility with the trunk
674"bt(0,0)/Extra/org.chameleon.Boot.plist" // Add compatibility with the trunk
675
676};
677
678 config_file_t *config = &bootInfo->bootConfig;
679
680int i, fd, count, ret=-1, fixedsize;
681
682for(i = 0; (unsigned)i< sizeof(dirspec)/sizeof(dirspec[0]); i++)
683{
684if ((fd = open(dirspec[i])) >= 0)
685{
686// read file
687 fixedsize = MIN(file_size(fd),IO_CONFIG_DATA_SIZE);
688count = read(fd, config->plist, fixedsize);
689close(fd);
690 if (count != fixedsize) continue;
691
692// build xml dictionary
693ParseXMLFile(config->plist, &config->dictionary);
694 safe_set_env(envSysConfigValid,true);
695ret=0;
696
697// enable canOverride flag
698config->canOverride = true;
699
700break;
701}
702}
703#ifdef BOOT_HELPER_SUPPORT
704if(ret == -1)
705{
706ret = loadHelperConfig();
707}
708#endif
709
710return ret;
711}
712
713/* loadOverrideConfig
714 *
715 * Returns 0 - successful.
716 * -1 - unsuccesful.
717 */
718int loadOverrideConfig(void)
719{
720char *dirspec[] = {
721"/Extra/com.apple.Boot.plist",
722"/Extra/org.chameleon.Boot.plist"
723};
724
725 config_file_t *config = &bootInfo->overrideConfig;
726
727
728int i, fd, count, ret=-1, fixedsize;
729
730for(i = 0; (unsigned)i< sizeof(dirspec)/sizeof(dirspec[0]); i++)
731{
732if ((fd = open(dirspec[i])) >= 0)
733{
734// read file
735fixedsize = MIN(file_size(fd),IO_CONFIG_DATA_SIZE);
736count = read(fd, config->plist, fixedsize);
737close(fd);
738 if (count != fixedsize) continue;
739
740// build xml dictionary
741ParseXMLFile(config->plist, &config->dictionary);
742 safe_set_env(envSysConfigValid,true);
743ret=0;
744break;
745}
746}
747#ifdef BOOT_HELPER_SUPPORT
748if(ret == -1)
749{
750ret = loadHelperConfig();
751}
752#endif
753
754return ret;
755}
756
757/* loadSystemConfig
758 *
759 * Returns 0 - successful.
760 * -1 - unsuccesful.
761 */
762int loadSystemConfig(void)
763{
764char *dirspec[] = {
765 "rd(0,0)/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
766"/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
767"bt(0,0)/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
768 "rd(0,0)/OS X Install Data/com.apple.Boot.plist",
769"/OS X Install Data/com.apple.Boot.plist",
770"bt(0,0)/OS X Install Data/com.apple.Boot.plist",
771 "rd(0,0)/Mac OS X Install Data/com.apple.Boot.plist",
772"/Mac OS X Install Data/com.apple.Boot.plist",
773"bt(0,0)/Mac OS X Install Data/com.apple.Boot.plist"
774};
775
776 config_file_t *config = &bootInfo->SystemConfig;
777
778
779int i, fd, count, ret=-1, fixedsize;
780
781for(i = 0; (unsigned)i< sizeof(dirspec)/sizeof(dirspec[0]); i++)
782{
783if ((fd = open(dirspec[i])) >= 0)
784{
785// read file
786fixedsize = MIN(file_size(fd),IO_CONFIG_DATA_SIZE);
787count = read(fd, config->plist, fixedsize);
788close(fd);
789 if (count != fixedsize) continue;
790
791// build xml dictionary
792ParseXMLFile(config->plist, &config->dictionary);
793 safe_set_env(envSysConfigValid,true);
794ret=0;
795break;
796}
797}
798
799return ret;
800}
801
802#ifdef BOOT_HELPER_SUPPORT
803
804/* loadHelperConfig
805 *
806 * Returns 0 - successful.
807 * -1 - unsuccesful.
808 */
809int loadHelperConfig(void)
810{
811int rfd, pfd, sfd, count, ret=-1, fixedsize;
812
813char *dirspec[] = {
814"/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
815"/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
816"/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"
817};
818
819 config_file_t *config = &bootInfo->helperConfig;
820
821
822// This is a simple rock - paper scissors algo. R beats S, P beats R, S beats P
823// If all three, S is used for now. This should be change dto something else (say, timestamp?)
824
825pfd = open(dirspec[0]);
826if(pfd >= 0)// com.apple.boot.P exists
827{
828sfd = open(dirspec[2]); // com.apple.boot.S takes precidence if it also exists
829if(sfd >= 0)
830{
831// Use sfd
832 fixedsize = MIN(file_size(sfd),IO_CONFIG_DATA_SIZE);
833count = read(sfd, config->plist, fixedsize);
834close(sfd);
835close(pfd);
836 if (count != fixedsize) return -1;
837
838// build xml dictionary
839ParseXMLFile(config->plist, &config->dictionary);
840 safe_set_env(envSysConfigValid,true);
841ret=0;
842
843}
844else
845{
846// used pfd
847 fixedsize = MIN(file_size(pfd),IO_CONFIG_DATA_SIZE);
848count = read(pfd, config->plist, fixedsize);
849close(pfd);
850 if (count != fixedsize) return -1;
851
852// build xml dictionary
853ParseXMLFile(config->plist, &config->dictionary);
854 safe_set_env(envSysConfigValid,true);
855ret=0;
856}
857
858}
859else
860{
861rfd = open(dirspec[1]); // com.apple.boot.R exists
862if(rfd >= 0)
863{
864pfd = open(dirspec[2]); // com.apple.boot.P takes recidence if it exists
865if(pfd >= 0)
866{
867// use sfd
868 fixedsize = MIN(file_size(pfd),IO_CONFIG_DATA_SIZE);
869count = read(pfd, config->plist, fixedsize);
870close(pfd);
871close(rfd);
872 if (count != fixedsize) return -1;
873
874// build xml dictionary
875ParseXMLFile(config->plist, &config->dictionary);
876 safe_set_env(envSysConfigValid,true);
877ret=0;
878
879}
880else
881{
882// use rfd
883 fixedsize = MIN(file_size(rfd),IO_CONFIG_DATA_SIZE);
884count = read(rfd, config->plist, fixedsize);
885close(rfd);
886 if (count != fixedsize) return -1;
887
888// build xml dictionary
889ParseXMLFile(config->plist, &config->dictionary);
890 safe_set_env(envSysConfigValid,true);
891ret=0;
892
893}
894
895}
896else
897{
898sfd = open(dirspec[2]); // com.apple.boot.S exists, but nothing else does
899if(sfd >= 0)
900{
901// use sfd
902 fixedsize = MIN(file_size(sfd),IO_CONFIG_DATA_SIZE);
903count = read(sfd, config->plist, fixedsize);
904close(sfd);
905 if (count != fixedsize) return -1;
906
907// build xml dictionary
908ParseXMLFile(config->plist, &config->dictionary);
909 safe_set_env(envSysConfigValid,true);
910ret=0;
911
912}
913}
914
915}
916
917return ret;
918}
919#endif
920
921static char * AllocInitStringWithLength(const char * oldString, int len)
922{
923char *Buf = NULL;
924Buf = malloc(len);
925if (Buf == NULL) return NULL;
926
927if (oldString != NULL)
928strlcpy(Buf, oldString,len);
929
930return Buf;
931}
932
933static char * AllocInitZeroEndedStringWithLength(const char * oldString, int len)
934{
935if (len > 0)
936{
937return AllocInitStringWithLength( oldString, len + 1);
938}
939return NULL;
940}
941
942char * newString(const char * oldString)
943{
944 if ( oldString )
945{
946int len = strlen(oldString);
947
948if (len > 0)
949{
950return AllocInitZeroEndedStringWithLength(oldString, len);
951}
952}
953 return NULL;
954}
955
956char * newStringWithLength(const char * oldString, int len)
957{
958if (len > 0)
959{
960return AllocInitZeroEndedStringWithLength(oldString, len);
961}
962 return NULL;
963}
964
965char * newEmptyStringWithLength(int len)
966{
967if (len > 0)
968{
969return AllocInitZeroEndedStringWithLength(NULL, len);
970}
971 return NULL;
972}
973
974/*
975 * Extracts the next argument from the command line, double quotes are allowed here.
976 */
977char * getNextArg(char ** argPtr, char * val)
978{
979char * ptr = *argPtr;
980const char * strStart;
981int len = 0;
982bool isQuoted = false;
983
984*val = '\0';
985
986// Scan for the next non-whitespace character.
987while ( *ptr && (*ptr == ' ' || *ptr == '=') )
988{
989ptr++;
990}
991
992strStart = ptr;
993
994// Skip the leading double quote character.
995if (*ptr == '\"')
996{
997isQuoted = true;
998ptr++;
999strStart++;
1000}
1001
1002// Scan for the argument terminator character.
1003// This can be either a NULL character - in case we reach the end of the string,
1004// a double quote in case of quoted argument,
1005// or a whitespace character (' ' or '=') for non-quoted argument.
1006while (*ptr && !( (isQuoted && (*ptr == '\"')) ||
1007 (!isQuoted && (*ptr == ' ' || *ptr == '=')) )
1008 )
1009{
1010ptr++;
1011}
1012
1013len = ptr - strStart;
1014
1015// Skip the closing double quote character and adjust
1016// the starting pointer for the next getNextArg call.
1017if (*ptr && isQuoted && *ptr == '\"')
1018ptr++;
1019
1020// Copy the extracted argument to val.
1021strncat(val, strStart, len);
1022
1023// Set command line pointer.
1024*argPtr = ptr;
1025
1026return ptr;
1027}
1028

Archive Download this file

Revision: 1984