#include #include "robust.h" int main() { char buf[BUFSIZ]; char *b; struct { QTICKET qptr; int inuse; } store[100]; int i, j; for(i = 0; i < 100; i++) store[i].inuse = 0; while(printf("> "), fgets(buf, BUFSIZ-1, stdin) != NULL){ for(b = buf; isspace(*b); b++) ; switch(*b++){ case 'c': for(i = 0; i < 100 && store[i].inuse; i++) ; if (i == 100) printf("All queues in use.\n"); else if (QE_ISERROR(j = create_queue())) printf("%s\n", qe_errbuf); else{ store[i].qptr = j; store[i].inuse = 1; printf("Queue %d created\n", i); } break; case 'd': if (sscanf(b, "%d", &i) != 1) printf("delete: d \n"); else if (i < 0 || 99 < i) printf("Bad queue number %d\n", i); else if (QE_ISERROR(delete_queue(store[i].qptr))) printf("%s\n", qe_errbuf); else{ store[i].inuse = 0; printf("Queue %d deleted\n", i); } break; case '+': if (sscanf(b, "%d %d", &i, &j) != 2) printf("enqueue: + \n"); else if (i < 0 || 99 < i) printf("Bad queue number %d\n", i); else if (QE_ISERROR(put_on_queue(store[i].qptr, j))) printf("%s\n", qe_errbuf); else printf("Put %d on queue %d\n", j, i); break; case '-': if (sscanf(b, "%d", &i) != 1) printf("dequeue: - \n"); else if (i < 0 || 99 < i) printf("Bad queue number %d\n", i); else{ qe_errbuf[0] = '\0'; j = take_off_queue(store[i].qptr); if (qe_errbuf[0]) printf("%s\n", qe_errbuf); else printf("Took %d off queue %d\n", j, i); } break; case 'l': for(i = 0; i < 100; i++) if (store[i].inuse) printf("queue %2d in use\n", i); break; default: printf("help:\n"); printf("c create queue\n"); printf("d n delete queue n\n"); printf("+ n v add integer v to queue n\n"); printf("- n remove head of queue n\n"); printf("EOF quit\n"); break; } } putchar('\n'); }