/*1line_instructionのparse (入力は\nで終わっていることを前提とする。)*/ instruction *parse_one_line_instruction(char *str, instruction * inst) { int len = strlen(str); int i = 0; int name_number = 0; int name_now = 0; int flag = 0; while (str[i] != '\n') { /*まずフラグが立っていない場合。最初のスペースは無視する */ while (flag == 0 && is_seperator(str[i])) { i++; } if (is_seperator(str[i])) { inst->name[name_number][name_now] = '\0'; name_number++; name_now = 0; flag = 0; i++; } else { if (name_number == NAME_MAX_COUNT) { printf("Error:operant is too many!: %s\n", str); exit(1); } inst->name[name_number][name_now] = str[i]; name_now++; flag = 1; i++; } } inst->name[name_number][name_now] = '\0'; name_number++; //オペラント数が少ない場合残りのオペラントをnullで埋めておく。 for (i = name_number; i < NAME_MAX_COUNT; ++i) { inst->name[i][0] = '\0'; } return inst; }
void fix_echo_line(char *line) { // to substitute the variables with their values char *pointer = line; char *ret = malloc(sizeof(char) * BUFFER_SIZE); bzero(ret, BUFFER_SIZE); int in_match = 0; char *var_name = malloc(sizeof(char) * BUFFER_SIZE); while (*pointer != '\0') { if (!in_match) { if (*pointer == '$') { in_match = 1; bzero(var_name, BUFFER_SIZE); } else { strncat(ret, pointer, 1); } } else { if (is_seperator(*pointer)) { if (is_env_var(var_name)) { char *val = getenv(var_name); strncat(ret, val, strlen(val)); } else { char *val = get_var_val(var_name); strncat(ret, val, strlen(val)); } in_match = 0; if (*pointer != '$') { strncat(ret, pointer, 1); } else { in_match = 1; } } else { strncat(var_name, pointer, 1); } } pointer++; } if (in_match) { if (is_env_var(var_name)) { char *val = getenv(var_name); strncat(ret, val, strlen(val)); } else { char *val = get_var_val(var_name); strncat(ret, val, strlen(val)); } in_match = 0; if (*pointer != '$') { strncat(var_name, pointer, 1); } else { in_match = 1; } } strncat(ret, "\0", 1); strcpy(line, ret); }
/* * Return the next token. Words within quotes are considered * to be one token. * Return null if no more tokens */ static char * next_token(char *s1) { char symbol[128]; int i = 0; static char *p = NULL; if (s1 != NULL) p = s1; if (*p == '\0') return (0); while (is_seperator(*p)) p++; if (*p == '\0') return (0); /* Remove comments. Comments begin with <!-- and end with --> */ if (strncmp(p, "<!--", 4) == 0) { while (*p != '\0') { if ((*p == '>') && (*(p-1) == '-') && (*(p-2) == '-')) { ++p; break; } p++; } return (next_token(NULL)); } /* Remove xml junk. begins with <? and end with ?> */ if (p[0] == '<' && p[1] == '?') { while (*p != '\0') { if ((*p == '>') && (*(p-1) == '?')) { ++p; break; } p++; } return (next_token(NULL)); } while (!is_seperator(*p) && *p != '\0') { if (*p == '"') { if (*p == '"') p++; /* Ignore " */ while (*p != '"' && *p != '\0') { /* replace newline by space */ if (*p == '\n' || *p == '\r') *p = ' '; symbol[i++] = tolower(*p++); } if (*p == '"') p++; /* Ignore " */ break; } symbol[i++] = tolower(*p++); } symbol[i] = '\0'; return (strdup(symbol)); }