void add(struct oper op, char finish_rem, int curline) { if (finish_rem == 'f') { if (op.type == LEFT) pusho(&o, op); else if (sizeo(&o) == 0) pusho(&o, op); else { while (sizeo(&o) > 0 && topo(&o).type != LEFT && priority[topo(&o).type] >= priority[op.type]) { struct command* newcom = (struct command*)malloc(sizeof(struct command)); newcom->input = newcom->output = NULL; newcom->type = types[topo(&o).type]; if (sizec(&s) < 2) printError(curline); newcom->u.command[1] = topc(&s); popc(&s); newcom->u.command[0] = topc(&s); popc(&s); pushc(&s, newcom); popo(&o); } if (op.type == RIGHT && topo(&o).type == LEFT) popo(&o); else pusho(&o, op); if (op.type == RIGHT) { struct command* ncom = (struct command*)malloc(sizeof(struct command)); ncom->input = ncom->output = NULL; ncom->type = SUBSHELL_COMMAND; ncom->u.subshell_command = topc(&s); popc(&s); pushc(&s, ncom); } } } else if (finish_rem == 't') { while (sizeo(&o) > 0) { struct command* newcom = (struct command*)malloc(sizeof(struct command)); newcom->input = newcom->output = NULL; newcom->type = types[topo(&o).type]; if (sizec(&s) < 2) printError(curline); newcom->u.command[1] = topc(&s); popc(&s); newcom->u.command[0] = topc(&s); popc(&s); pushc(&s, newcom); popo(&o); } } }
void reset(struct command** com, char** word, int* length, int *count, int* csize, int* mlength, char sub) { if (sub == 'f') { (*com) = (struct command*)malloc(sizeof(struct command)); (*com)->type = SIMPLE_COMMAND; (*com)->u.word = (char**)malloc(10 * sizeof(char*)); } else if (sub == 't') { (*com) = topc(&s); } (*com)->input = (*com)->output = NULL; free((*word)); *word = (char*)malloc(12 * sizeof(char)); *length = 0; *count = 0; *csize = 10; *mlength = 12; }
void spincube() { theta+=0.5+speed; if(theta==360.0) theta-=360.0; if(theta>=90.0) { rotationcomplete=1; glutIdleFunc(NULL); if(rotation==1&&inverse==0) { topc(); } if(rotation==1&&inverse==1) { topc(); topc(); topc(); } if(rotation==2&&inverse==0) { rightc(); } if(rotation==2&&inverse==1) { rightc(); rightc(); rightc(); } if(rotation==3&&inverse==0) { frontc(); } if(rotation==3&&inverse==1) { frontc(); frontc(); frontc(); } if(rotation==4&&inverse==0) { leftc(); } if(rotation==4&&inverse==1) { leftc(); leftc(); leftc(); } if(rotation==5&&inverse==0) { backc(); } if(rotation==5&&inverse==1) { backc(); backc(); backc(); } if(rotation==6&&inverse==0) { bottomc(); } if(rotation==6&&inverse==1) { bottomc(); bottomc(); bottomc(); } rotation=0; theta=0; } glutPostRedisplay(); }
command_stream_t make_command_stream(int(*getbyte) (void *), void *arg) { initStackc(&s); initStacko(&o); struct command_stream *command_stream_t = (struct command_stream *)malloc(sizeof(struct command_stream)); initStream(command_stream_t); struct command*com = (struct command*)malloc(sizeof(struct command)); com->type = SIMPLE_COMMAND; com->input = com->output = NULL; struct oper o; char c; bool extra = false; bool done = false; int csize = 10; com->u.word = (char**)malloc(10 * sizeof(char*)); int count = 0; int length = 0; int mlength = 12; char* word = (char*)malloc(12 * sizeof(char)); c = getbyte(arg); int curline = 1; skipws(getbyte, arg, &c, &curline); while (c >= 0) { done = false; if (!isValidChar(c)) printError(curline); extra = false; if (isvalid(c)) { word[length] = c; length++; if (length >= mlength) { mlength += 5; word = (char*)realloc(word, mlength*sizeof(char)); } } else { if (c == '#') { while ((c = (char)getbyte(arg)) >= 0 && c != '\n'); } else if (c == '\\') { while (c == '\\') { c = (char)getbyte(arg); c = (char)getbyte(arg); } continue; } else if (c == ' ') { if (length > 0) { addWord(&com, &count, &length, word); if (count == csize - 1) { csize += 5; com->u.word = (char**)realloc(com->u.word, csize*sizeof(int)); } } else { c = (char)getbyte(arg); continue; } } else if (c == '|' || c == '&' || c == ';' || c == '\n' || c == '(' || c == ')') { /*if (com->type == SUBSHELL_COMMAND) { com = (struct command*)malloc(sizeof(struct command)); com->type = SIMPLE_COMMAND; com->u.word = (char**)malloc(10 * sizeof(char*)); com->input = com->output = NULL; count = 0; csize = 10; } */ addWord(&com, &count, &length, word); if (c == '|') { c = (char)getbyte(arg); if (c == '|') { o.type = OR; } else { o.type = PIPE; } } else if (c == '&') { o.type = AND; c = (char)getbyte(arg); if (c != '&') printError(curline); } else if (c == ';') { o.type = SEQ; c = (char)getbyte(arg); } else if (c == '(') { o.type = LEFT; } else if (c == ')') { o.type = RIGHT; } else if (c == '\n') { curline += 1; while ((c = (char)getbyte(arg)) == ' '); if (c < 0) break; while (c == '\\') { c = (char)getbyte(arg); c = (char)getbyte(arg); } if (c == '#') { while ((c = getbyte(arg)) >= 0 && c != '\n'); } if (c != '\n' && (com->u.word != NULL || com->u.subshell_command != NULL)) { o.type = SEQ; extra = true; } else { extra = false; if (count > 0) { com->u.word[count] = '\0'; pushc(&s, com); } o.type = SEQ; add(o, 't', curline); insert(command_stream_t, topc(&s)); popc(&s); done = true; reset(&com, &word, &length, &count, &csize, &mlength, 'f'); c = (char)getbyte(arg); skipws(getbyte, arg, &c, &curline); continue; } } if (o.type != PIPE && o.type != SEQ) { c = (char)getbyte(arg); } if (count > 0) { com->u.word[count] = '\0'; pushc(&s, com); } add(o, 'f', curline); if (o.type == RIGHT) reset(&com, &word, &length, &count, &csize, &mlength, 't'); else reset(&com, &word, &length, &count, &csize, &mlength, 'f'); if (o.type != RIGHT) { skipws(getbyte, arg, &c,&curline); } extra = true; } else if (c == '>' || c == '<') { char* ptr; int insize = 12; ptr = (char*)malloc(insize * sizeof(char)); int i = 0; char t = c; while ((c = (char)getbyte(arg)) == ' '); if (!isvalid(c)) printError(curline); while ((c >= 0)) { if (!isvalid(c)) { extra = true; break; } ptr[i] = c; i++; if (i == insize - 1) { insize += 5; ptr = (char*)realloc(ptr, insize*sizeof(char)); } c = (char)getbyte(arg); } ptr[i] = '\0'; if (t == '<') com->input = ptr; else com->output = ptr; } } if (!extra) c = (char)getbyte(arg); } if (!done) { addWord(&com, &count, &length, word); if (count > 0) { com->u.word[count] = '\0'; pushc(&s, com); } o.type = SEQ; add(o, 't', curline); if (sizec(&s) != 1) printError(curline); insert(command_stream_t, topc(&s)); popc(&s); } return command_stream_t; }