Exemple #1
0
/* opens a temporary file and adds it as the specified virtual fd
 * ----------------------------------------------------------------------- */
int fd_tempfile(struct fd *fd) {
  int e;
  
  /* reset the template to what mkstemp expects */
  str_copy(&fd_tempname[sizeof(fd_tempname)-7], "XXXXXX");

  /* try to create a tempfile and exit on failure 
     otherwise track the newly created file descriptor */
  if((e = open_temp(fd_tempname)) == -1)
    return e;
  
  fdtable_track(e, FDTABLE_LAZY);
  
  /* tempfiles are initially in write mode only */
  fd->mode = FD_WRITE;

  fd_setfd(fd, e);

  /* unlink the file, the fd should now be the only reference */
  unlink(fd_tempname);

  /* make a copy of the name because the 
     next call will change fd_tempname */
  fd->name = shell_strdup(fd_tempname);
  fd->mode |= FD_FREENAME;
  
  return e;
}
int parse_line(const char *buf, char *argv[], int max_args, char *redirect_in, char *redirect_out)
{
    const char *scan=buf;
    const char *type_char;
    const char *start;
    char out[SCAN_SIZE+1];
    char tmp[SCAN_SIZE+1];
    char *arg_tmp;
    int arg_cnt = 0;
    int len = 0;
    int replace;
    //int param;
    redirect_in[0] = 0;
    redirect_out[0] = 0;

//printf("parse '%s'", buf );

    while(1){

        while((*scan != 0) && (isspace(*scan))) scan++;

        if(*scan == 0) break;

        replace = 1;
        type_char = scan;
        start = scan;
        //param = 1;

        switch(*type_char){
        case '\'':
            replace  = 0;

        case  '"':
            scan++;
            start++;
            while((*scan != 0) && (*scan != *start)) scan++;
            break;

        case '>':
        case '<':
            start++;
            while((*start != 0) && (isspace(*start))) start++;
            scan = start;

        default:

            while((*scan != 0) && (!isspace(*scan))) scan++;

        }

        len = scan - start;
        if(*scan != 0) scan++;

        if(replace){

            memcpy(tmp,start,len);
            tmp[len]=0;
            parse_vars_in_string(tmp,out,SCAN_SIZE);

        } else {

            memcpy(out,start,len);
            out[len]=0;

        }

        switch(*type_char){

        case '>':
            strcpy(redirect_out,out);
            break;

        case '<':
            strcpy(redirect_in,out);
            break;

        default:
            if(arg_cnt < max_args){

                arg_tmp = shell_strdup(out);
                if(arg_tmp != NULL) argv[arg_cnt++] = arg_tmp;

            }

        }
    }

    return arg_cnt;
}