#include <unistd.h>
#include <getopt.h>
-#undef BB_FEATURE_SH_WORDEXP
+//#define BB_FEATURE_SH_WORDEXP
-#if BB_FEATURE_SH_WORDEXP
+#ifdef BB_FEATURE_SH_WORDEXP
#include <wordexp.h>
#define expand_t wordexp_t
#undef BB_FEATURE_SH_BACKTICKS
cmd->job_list = keep;
}
-/* remove a job from the job_list */
-static void remove_job(struct jobset *job_list, struct job *job)
+/* remove a job from a jobset */
+static void remove_job(struct jobset *j_list, struct job *job)
{
struct job *prevjob;
free_job(job);
- if (job == job_list->head) {
- job_list->head = job->next;
+ if (job == j_list->head) {
+ j_list->head = job->next;
} else {
- prevjob = job_list->head;
+ prevjob = j_list->head;
while (prevjob->next != job)
prevjob = prevjob->next;
prevjob->next = job->next;
/* Checks to see if any background processes have exited -- if they
have, figure out why and see if a job has completed */
-static void checkjobs(struct jobset *job_list)
+static void checkjobs(struct jobset *j_list)
{
struct job *job;
pid_t childpid;
int prognum = 0;
while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
- for (job = job_list->head; job; job = job->next) {
+ for (job = j_list->head; job; job = job->next) {
prognum = 0;
while (prognum < job->num_progs &&
job->progs[prognum].pid != childpid) prognum++;
if (!job->running_progs) {
printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
- remove_job(job_list, job);
+ remove_job(j_list, job);
}
} else {
/* child stopped */
return 1;
}
- /* remove trailing newline */
- chomp(command);
-
return 0;
}
#endif
#if defined BB_FEATURE_SH_ENVIRONMENT && ! defined BB_FEATURE_SH_WORDEXP
-char * strsep_space( char *string, int * index)
+char * strsep_space( char *string, int * ix)
{
char *token, *begin;
begin = string;
/* Short circuit the trivial case */
- if ( !string || ! string[*index])
+ if ( !string || ! string[*ix])
return NULL;
/* Find the end of the token. */
- while( string && string[*index] && !isspace(string[*index]) ) {
- (*index)++;
+ while( string && string[*ix] && !isspace(string[*ix]) ) {
+ (*ix)++;
}
/* Find the end of any whitespace trailing behind
* the token and let that be part of the token */
- while( string && string[*index] && isspace(string[*index]) ) {
- (*index)++;
+ while( string && string[*ix] && isspace(string[*ix]) ) {
+ (*ix)++;
}
- if (! string && *index==0) {
+ if (! string && *ix==0) {
/* Nothing useful was found */
return NULL;
}
- token = xmalloc(*index);
- token[*index] = '\0';
- strncpy(token, string, *index);
+ token = xmalloc(*ix+1);
+ token[*ix] = '\0';
+ strncpy(token, string, *ix);
return token;
}
#ifdef BB_FEATURE_SH_ENVIRONMENT
expand_t expand_result;
char *src, *dst, *var;
- int index = 0;
+ int ix = 0;
int i=0, length, total_length=0, retval;
const char *out_of_space = "out of space during expansion";
#endif
chomp(command);
/* Fix up escape sequences to be the Real Thing(tm) */
- while( command && command[index]) {
- if (command[index] == '\\') {
- char *tmp = command+index+1;
- command[index+1] = process_escape_sequence( &tmp );
- memmove(command+index, command+index+1, strlen(command+index));
+ while( command && command[ix]) {
+ if (command[ix] == '\\') {
+ char *tmp = command+ix+1;
+ command[ix] = process_escape_sequence( &tmp );
+ memmove(command+ix + 1, tmp, strlen(tmp)+1);
}
- index++;
+ ix++;
}
#ifdef BB_FEATURE_SH_ENVIRONMENT
-#if BB_FEATURE_SH_WORDEXP
+#ifdef BB_FEATURE_SH_WORDEXP
/* This first part uses wordexp() which is a wonderful C lib
* function which expands nearly everything. */
retval = wordexp (command, &expand_result, WRDE_SHOWERR);
* we write stuff into the original (in a minute) */
cmd = cmd_copy = strdup(command);
*command = '\0';
- for (index = 0, tmpcmd = cmd;
- (tmpcmd = strsep_space(cmd, &index)) != NULL; cmd += index, index=0) {
+ for (ix = 0, tmpcmd = cmd;
+ (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
if (*tmpcmd == '\0')
break;
retval = glob(tmpcmd, flags, NULL, &expand_result);
/* Convert from char** (one word per string) to a simple char*,
* but don't overflow command which is BUFSIZ in length */
for (i=0; i < expand_result.gl_pathc; i++) {
- length=strlen(expand_result.gl_pathv[i])+1;
- if (BUFSIZ-total_length-length <= 0) {
+ length=strlen(expand_result.gl_pathv[i]);
+ if (total_length+length+1 >= BUFSIZ) {
error_msg(out_of_space);
return FALSE;
}
+ if (i>0) {
+ strcat(command+total_length, " ");
+ total_length+=1;
+ }
strcat(command+total_length, expand_result.gl_pathv[i]);
- strcat(command+total_length, " ");
total_length+=length;
}
globfree (&expand_result);
case '0':case '1':case '2':case '3':case '4':
case '5':case '6':case '7':case '8':case '9':
{
- int index=*(dst + 1)-48;
- if (index >= argc) {
+ int ixx=*(dst + 1)-48;
+ if (ixx >= argc) {
var='\0';
} else {
- var = argv[index];
+ var = argv[ixx];
}
}
break;
} else {
/* Looks like an environment variable */
char delim_hold;
- int num_skip_chars=1;
+ int num_skip_chars=0;
int dstlen = strlen(dst);
/* Is this a ${foo} type variable? */
if (dstlen >=2 && *(dst+1) == '{') {
src=strchr(dst+1, '}');
- num_skip_chars=2;
+ num_skip_chars=1;
} else {
- src=strpbrk(dst+1, " \t~`!$^&*()=|\\[];\"'<>?./");
+ src=dst+1;
+ while(isalnum(*src) || *src=='_') src++;
}
if (src == NULL) {
src = dst+dstlen;
}
delim_hold=*src;
*src='\0'; /* temporary */
- var = getenv(dst + num_skip_chars);
+ var = getenv(dst + 1 + num_skip_chars);
*src=delim_hold;
- if (num_skip_chars==2) {
- src++;
- }
+ src += num_skip_chars;
}
if (var == NULL) {
/* Seems we got an un-expandable variable. So delete it. */
}
/* Move stuff to the end of the string to accommodate
* filling the created gap with the new stuff */
- memmove(dst+subst_len, src, trail_len);
- *(dst+subst_len+trail_len)='\0';
+ memmove(dst+subst_len, src, trail_len+1);
/* Now copy in the new stuff */
memcpy(dst, var, subst_len);
src = dst+subst_len;
break;
#else
error_msg("character expected after \\");
- free(command);
free_job(job);
return 1;
#endif
#endif
{
- char** argv=child->argv;
+ char** argv_l=child->argv;
int argc_l;
- for(argc_l=0;*argv!=NULL; argv++, argc_l++);
+ for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
optind = 1;
run_applet_by_name(name, argc_l, child->argv);
}
static void insert_job(struct job *newjob, int inbg)
{
struct job *thejob;
- struct jobset *job_list=newjob->job_list;
+ struct jobset *j_list=newjob->job_list;
/* find the ID for thejob to use */
newjob->jobid = 1;
- for (thejob = job_list->head; thejob; thejob = thejob->next)
+ for (thejob = j_list->head; thejob; thejob = thejob->next)
if (thejob->jobid >= newjob->jobid)
newjob->jobid = thejob->jobid + 1;
/* add thejob to the list of running jobs */
- if (!job_list->head) {
- thejob = job_list->head = xmalloc(sizeof(*thejob));
+ if (!j_list->head) {
+ thejob = j_list->head = xmalloc(sizeof(*thejob));
} else {
- for (thejob = job_list->head; thejob->next; thejob = thejob->next) /* nothing */;
+ for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
thejob->next = xmalloc(sizeof(*thejob));
thejob = thejob->next;
}