interp_parse.c (50477) | interp_parse.c (64187) |
---|---|
1/* 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions 4 * are met: 5 * 1. Redistributions of source code must retain the above copyright 6 * notice, this list of conditions and the following disclaimer. 7 * 2. Redistributions in binary form must reproduce the above copyright 8 * notice, this list of conditions and the following disclaimer in the 9 * documentation and/or other materials provided with the distribution. 10 * 11 * Jordan K. Hubbard 12 * 29 August 1998 13 * | 1/* 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions 4 * are met: 5 * 1. Redistributions of source code must retain the above copyright 6 * notice, this list of conditions and the following disclaimer. 7 * 2. Redistributions in binary form must reproduce the above copyright 8 * notice, this list of conditions and the following disclaimer in the 9 * documentation and/or other materials provided with the distribution. 10 * 11 * Jordan K. Hubbard 12 * 29 August 1998 13 * |
14 * $FreeBSD: head/sys/boot/common/interp_parse.c 50477 1999-08-28 01:08:13Z peter $ | 14 * $FreeBSD: head/sys/boot/common/interp_parse.c 64187 2000-08-03 09:14:02Z jhb $ |
15 * 16 * The meat of the simple parser. 17 */ 18 19#include <stand.h> 20#include <string.h> | 15 * 16 * The meat of the simple parser. 17 */ 18 19#include <stand.h> 20#include <string.h> |
21#include "bootstrap.h" |
|
21 | 22 |
22/* Forward decls */ 23extern char *backslash(char *str); | 23static void clean(void); 24static int insert(int *argcp, char *buf); 25static char *variable_lookup(char *name); |
24 | 26 |
25static void clean(void); 26static int insert(int *argcp, char *buf); 27static char *variable_lookup(char *name); 28 | |
29#define PARSE_BUFSIZE 1024 /* maximum size of one element */ 30#define MAXARGS 20 /* maximum number of elements */ 31static char *args[MAXARGS]; 32 33/* 34 * parse: accept a string of input and "parse" it for backslash 35 * substitutions and environment variable expansions (${var}), 36 * returning an argc/argv style vector of whitespace separated --- 19 unchanged lines hidden (view full) --- 56 clean(); \ 57 free(copy); \ 58 free(buf); \ 59 return 1; \ 60} 61 62/* Accept the usual delimiters for a variable, returning counterpart */ 63static char | 27#define PARSE_BUFSIZE 1024 /* maximum size of one element */ 28#define MAXARGS 20 /* maximum number of elements */ 29static char *args[MAXARGS]; 30 31/* 32 * parse: accept a string of input and "parse" it for backslash 33 * substitutions and environment variable expansions (${var}), 34 * returning an argc/argv style vector of whitespace separated --- 19 unchanged lines hidden (view full) --- 54 clean(); \ 55 free(copy); \ 56 free(buf); \ 57 return 1; \ 58} 59 60/* Accept the usual delimiters for a variable, returning counterpart */ 61static char |
64isdelim(char ch) | 62isdelim(int ch) |
65{ 66 if (ch == '{') 67 return '}'; 68 else if (ch == '(') 69 return ')'; 70 return '\0'; 71} 72 73static int | 63{ 64 if (ch == '{') 65 return '}'; 66 else if (ch == '(') 67 return ')'; 68 return '\0'; 69} 70 71static int |
74isquote(char ch) | 72isquote(int ch) |
75{ 76 return (ch == '\'' || ch == '"'); 77} 78 79int 80parse(int *argc, char ***argv, char *str) 81{ 82 int ac; 83 char *val, *p, *q, *copy = NULL; | 73{ 74 return (ch == '\'' || ch == '"'); 75} 76 77int 78parse(int *argc, char ***argv, char *str) 79{ 80 int ac; 81 char *val, *p, *q, *copy = NULL; |
84 int i = 0; | 82 size_t i = 0; |
85 char token, tmp, quote, *buf; 86 enum { STR, VAR, WHITE } state; 87 88 ac = *argc = 0; 89 quote = 0; 90 if (!str || (p = copy = backslash(str)) == NULL) 91 return 1; 92 --- 49 unchanged lines hidden (view full) --- 142 } else { 143 q = p; 144 while (*q && !isspace(*q)) 145 ++q; 146 } 147 tmp = *q; 148 *q = '\0'; 149 if ((val = variable_lookup(p)) != NULL) { | 83 char token, tmp, quote, *buf; 84 enum { STR, VAR, WHITE } state; 85 86 ac = *argc = 0; 87 quote = 0; 88 if (!str || (p = copy = backslash(str)) == NULL) 89 return 1; 90 --- 49 unchanged lines hidden (view full) --- 140 } else { 141 q = p; 142 while (*q && !isspace(*q)) 143 ++q; 144 } 145 tmp = *q; 146 *q = '\0'; 147 if ((val = variable_lookup(p)) != NULL) { |
150 int len = strlen(val); | 148 size_t len = strlen(val); |
151 152 strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1)); 153 i += min(len, PARSE_BUFSIZE - 1); 154 } 155 *q = tmp; /* restore value */ 156 p = q + (token ? 1 : 0); 157 state = STR; 158 break; --- 47 unchanged lines hidden --- | 149 150 strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1)); 151 i += min(len, PARSE_BUFSIZE - 1); 152 } 153 *q = tmp; /* restore value */ 154 p = q + (token ? 1 : 0); 155 state = STR; 156 break; --- 47 unchanged lines hidden --- |