X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Ftoken.c;h=ec86ac5be48e7028a49cafc5588d973fb2eab350;hp=fcb1aeb0dbcc01658777f2655f936165852661b2;hb=b0f981469cdea23225ca5c3ef0ca6759d6a92b43;hpb=1f490c5ea15d073c9ec918b78a5e6117a0a8bd3e diff --git a/src/token.c b/src/token.c index fcb1aeb..ec86ac5 100644 --- a/src/token.c +++ b/src/token.c @@ -6,11 +6,11 @@ OPD *opdtok(const char *str) { OPD *opd = malloc(sizeof(OPD)); char *p, *q, *sepp; - int sepc = ','; + int sepc = ',', qcnt = 0; bool quoting = false; opd->opdc = 0; - if(str == NULL || strlen(str) == 0) { + if(str == NULL) { return opd; } p = q = strdup(str); @@ -24,26 +24,39 @@ OPD *opdtok(const char *str) if(*q == '=') { q++; } - /* 「'」で囲まれた文字列の処理。「''」は無視 */ - if(*q == '\'' && *(q+1) != '\'' && !(p < q && *(q-1) == '\'')) { - quoting = !quoting; + /* 「'」の場合 */ + if(*q == '\'') { + /* 「''」以外の場合はquote値を反転 */ + if(*(q+1) != '\'' && !(p < q && *(q-1) == '\'')) { + quoting = !quoting; + } + /* 「'」の分、文字列の長さを小さくする */ + if(*(q+1) != '\'') { + qcnt++; + } } if(quoting == true) { + /* 閉じ「'」がないまま文字列が終了した場合 */ + if(*q == '\0') { + setcerr(123, str); /* unclosed quote */ + break; + } q++; } else { sepp = q + strcspn(q, ", "); sepc = *sepp; *sepp = '\0'; - if(strlen(p) == 0) { + if(*p == '\0') { setcerr(121, NULL); /* cannot get operand token */ break; } - if(strlen(p) > OPDSIZE + 2) { - setcerr(118, p); /* operand length is too long */ + if(strlen(p) - qcnt > OPDSIZE) { + setcerr(118, NULL); /* operand length is too long */ break; } opd->opdv[(++opd->opdc)-1] = strdup(p); p = q = sepp + 1; + qcnt = 0; } } while(sepc == ','); return opd; @@ -52,19 +65,28 @@ OPD *opdtok(const char *str) /* 空白またはタブで区切られた1行から、トークンを取得 */ CMDLINE *linetok(const char *line) { - char *tokens, *p, *q, *sepp; + char *tokens, *p, *sepp; + bool quoting = false; CMDLINE *cmdl = malloc(sizeof(CMDLINE)); + if(line == NULL || strlen(line) == 0) { return NULL; } - tokens = p = strdup(line); + tokens = strdup(line); /* コメントを削除 */ - if((q = strchr(tokens, ';')) != NULL) { - *q = '\0'; + for(p = tokens; *p != '\0'; p++) { + /* 「'」で囲まれた文字列の処理。「''」は無視 */ + if(*p == '\'' && *(p+1) != '\'' && !(p > tokens && *(p-1) == '\'')) { + quoting = !quoting; + } else if(quoting == false && *p == ';') { + *p = '\0'; + break; + } } - if(*p == '\0') { + if(*tokens == '\0') { return NULL; } + p = tokens; /* 行の先頭が空白またはタブの場合、ラベルは空 */ if((sepp = p + strcspn(p, " \t\n")) == p){ cmdl->label = NULL;