繁体   English   中英

解析C中的命令行条目:实现Shell

[英]Parsing command-line entries in C: implementing a shell

我正在用C实现Shell,但在解析命令行条目时遇到了一些问题。 我希望我的解析器方法分隔由空格字符分隔的命令行条目,并将结果作为双字符指针返回。 即,说我有“ ls -l> ls.txt”,我的解析器应返回一个char ** r,其中r [0] =“ ls”,r [1] =“-l”和r [2] = “> ls.txt”。

这是我当前的解析方法的代码,顺便说一句,它是段错误的,我对如何解决此问题一无所知:

 char **parser(int *argc, char *s)
 {
     char **r;
     char *t, *m;
     int i,n,size;

     t = malloc(strlen(s)); // firs i used this instead of *r, but i run 
                            // into trouble when i have more than two
                            // argc. ( You see why, right?)
    //strcpy(t,s);
    i = 0;
    size = 5;
    r = malloc(size*sizeof(char *));
    while (( m = strchr(s, ' '))) {
        n = ((int)m) - ((int)s);
        if (i==0) {
          *r = malloc(n);
        } else {
           *r = realloc(*r, n);
        }
        strncpy(*r, s, n);
        *r[n]= '\0';
        s = (char*)(s+n+1);
        if (i == size)
            r = realloc(r, (size = 2*size)*sizeof(char*));
        i++;
        r = (char **)(r + sizeof(char*));
   }

   s[strlen(s)-1] = '\0';
   if ((i<1) || (strlen(s)>1)) {
       *r = s;
   }
   *argcp = ++i;
   return r;
}

我知道我的代码并不理想。 使用strsep可以使它更好,但是我的主要考虑是如何管理要返回的double char指针的内存。

谢谢您的帮助!

这是一个快速的过程。

我的C太生锈了,所有铰链都卡住了,所以。

前提是您将最终获得一个指向指针数组的指针。 但是,关键细节位于该指针列表的末尾,是参数数据本身。 因此,完成后,您只需要释放返回的指针即可。

未经测试。 可能在这里潜行了一个错误。

编辑,我编译并对其进行了快速测试。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


char **parser(int *argc, char *s) {
    char **r, **rp;
    char *t, *p, *w;
    void *vp;
    int l;

    l = strlen(s); // size of cmd line
    vp = malloc(l + (*argc * sizeof(char *))); // total buffer size
    t = (char *)(vp + (*argc * sizeof(char *))); // offset into buffer for argument copy
    r = (char **)vp;  // start of buffer, start of pointer array to arguments
    strcpy(t, s);  // copy arguments in to buffer
    p = t;  // parsing pointer
    w = t;  // word pointer for each argument
    rp = r;  // storage for first pointer

    while(*p) {  // while not at end of string
        if (*p == ' ') {  // if we find a space
            if (w) {  // if we have a word pointer assigned
                *rp++ = w;  // store the word pointer
                w = NULL;  // set word pointer to null
                *p = '\0';  // terminate argument with a 0
            } // else do nothing continue to skip spaces
        } else {
            if (w == NULL) {  // If we haven't got a new arg yet
                w = p;  // set it
            }  // otherwise, just keep scanning
        }
        p++;  // move along the string
    }
    if (w) {  // clean up at the end if we have an arg
        *rp++ = w;
        w = NULL;  // no reason to set 0 at the end, it's already there from strcpy
    }

    return r;
}

int main() {
    char *cmd = "arg1 arg2";
    int argc = 2;

    char **r = parser(&argc, cmd);

    printf("%s\n",r[0]);
    printf("%s\n",r[1]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM