#include #include "ccag.h" #define FALSE 0 #define TRUE 1 #define OUTPATH "/tmp/" #define DATAFILE "data.df" /* This is the CCAG file to use */ /* The program needs to know what string to insert in the "base href" tag */ #define URLPATH "http://spidey.cs.rit.edu/jgm8824bin/" char * find_key(char *keystr, char *v[]); main(int c, char *v[], char *e[]) { int i = 0; char *str; char *keys[100]; int keycount = 0; char *input; char outname[50]; FILE *fd, *fd2; int flag = 0, ticket = 0; char error[1000], command[1000], outfile[50], ipstr[20], buffer[150]; char *currlabel; qnode *currnode; qhead = NULL; /* This turns buffering off, so if the program crashes we will be sure to receive all the data at the browser end */ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); keys[0] = 0; /* First we read in the entire data file */ fd = fopen(DATAFILE, "r"); if (fd == NULL) { printf("Could not open %s for reading...\n", DATAFILE); exit(1); } read_data(fd, &qhead); /* Now we interpret the form data */ printf("Content-type: text/html\n\n"); if (getenv("CONTENT_LENGTH") != NULL) { input = (char *) malloc(atoi(getenv("CONTENT_LENGTH"))+1); scanf("%s", input); str = (char *) strtok(input, "&"); do { keys[i] = (char *) malloc(strlen(str)); strcpy(keys[i], str); keycount ++; makespace(keys[i]); keys[i][strlen(keys[i])-1] = 0; i ++; str = (char *) strtok(NULL, "&"); } while (str != NULL); keys[i] = 0; /* cap off string vector table */ /* i = 0; printf("
");
    while (i < keycount)
      printf("(%d)  |%s|\n", i, keys[i++]);
    exit(0);  /*  */
  }

  if (keys[0] == 0)  {
    /* this is the user's init session  */
    if (strlen(kbbanner) != 0)   printf("
%s

\n", kbbanner); currlabel = initexp; } else currlabel = find_key("ANSWER", keys); currnode = find_qnode(currlabel, qhead); if (currnode == NULL) { /* We couldn't find it among the question nodes; let's look for it among the answer nodes */ show_answer(currlabel, ahead); exit(0); } show_formatted_node(currnode); printf("\n"); /* Das ENDE */ } show_answer(char *label, anode *node) { while (node != NULL) { if (strcmp(node->label, label) == 0) { printf("

Recommendation:

\n"); printf("%s

\n", node->answer); return; } node = node->next; } /* Node not found -- internal error */ printf("

%s\n", bugrep);
  exit(0);
}

show_formatted_node(qnode *node)  {
  char *tmp, *copy;

  printf("

Please answer the following question:

\n"); printf("%s

\n", node->question); printf("

", URLPATH); copy = (char *) malloc(strlen(node->legal)+1); strcpy(copy, node->legal); tmp = (char *) strtok(copy, ","); while (tmp != NULL) { while (tmp[0] == ' ') tmp++; /* Skip any leading whitespace */ printf("  ", tmp); printf(" %s

\n", tmp); tmp = (char *) strtok(NULL, ","); } free(copy); printf("Explanation: %s

\n", node->explain); } qnode * find_qnode(char *label, qnode *node) { while (node != NULL) { if (strcmp(label, node->label) == 0) return node; else node = node->next; } return NULL; } read_data(FILE *fd, qnode **head) { char word[100], label[100], c; char longstr[2000]; word[0] = 0; skip_white_space(fd); while (read_word(fd, word, " (") != EOF) { if (strcmp(word, "initial_expression") == 0) { while (fgetc(fd) != '\''); /* find start of string */ read_word(fd, word, "'"); initexp = (char *) malloc(strlen(word)+1); strcpy(initexp, word); /* printf("Found initial_expression = '%s'\n", initexp); */ fgetc(fd); fgetc(fd); /* skips trailing junk */ } if (strcmp(word, "program_bug_report") == 0) { while (fgetc(fd) != '\''); /* find start of string */ read_word(fd, bugrep, "'"); /* printf("Found bug_rep = '%s'\n", kbbanner); */ fgetc(fd); fgetc(fd); /* skip trailing junk */ } if (strcmp(word, "kb_banner") == 0) { while (fgetc(fd) != '\''); /* find start of string */ read_word(fd, kbbanner, "'"); /* printf("Found kb_banner = '%s'\n", kbbanner); */ fgetc(fd); fgetc(fd); /* skip trailing junk */ } if (strcmp(word, "recommendations") == 0) { fgetc(fd); read_word(fd, word, ")"); while (fgetc(fd) != '\''); read_long(fd, longstr, "\'", "\n\r\t"); insert_answer(&ahead, word, longstr); fgetc(fd); fgetc(fd); } if (strcmp(word, "ques") == 0) { fgetc(fd); /* skip '(' */ read_word(fd, word, ")"); while (fgetc(fd) != '\''); read_long(fd, longstr, "\'", "\n\r\t"); insert_question(&qhead, word, longstr); fgetc(fd); fgetc(fd); } if (strcmp(word, "legalvalues") == 0) { fgetc(fd); /* skip '(' */ read_word(fd, word, ")"); while (fgetc(fd) != '['); read_long(fd, longstr, "]", "\r\n"); insert_legal(&qhead, word, longstr); fgetc(fd); fgetc(fd); fgetc(fd); } if (strcmp(word, "expl") == 0) { fgetc(fd); /* skip '(' */ read_word(fd, word, ")"); while (fgetc(fd) != '\''); read_long(fd, longstr, "\'", "\r\n\t"); insert_expl(&qhead, word, longstr); fgetc(fd); fgetc(fd); } skip_white_space(fd); } } insert_answer(anode **head, char *label, char *answer) { anode *curr; if (*head == NULL) { /* empty list */ *head = (anode *) malloc(sizeof(anode)); (*head)->label = (char *) malloc(strlen(label)+1); (*head)->answer = (char *) malloc(strlen(answer)+1); strcpy((*head)->label, label); strcpy((*head)->answer, answer); (*head)->next = NULL; return; } curr = *head; while (curr->next != NULL) curr = curr->next; insert_answer(&(curr->next), label, answer); } insert_question(qnode **node, char *label, char *text) { qnode *curr; if (*node == NULL) { *node = (qnode *) malloc(sizeof(qnode)); (*node)->label = (char *) malloc(strlen(label)+1); strcpy((*node)->label, label); (*node)->question = (char *) malloc(strlen(text)+1); strcpy((*node)->question, text); (*node)->legal = NULL; (*node)->explain = NULL; (*node)->next; return; } curr = *node; while (curr->next != NULL) { if (strcmp(curr->label, label) == 0) { curr->question = (char *) malloc(strlen(text)+1); strcpy(curr->question, text); return; } curr = curr->next; } curr->next = (qnode *) malloc(sizeof(qnode)); curr = curr->next; curr->label = (char *) malloc(strlen(label)+1); strcpy(curr->label, label); curr->question = (char *) malloc(strlen(text)+1); strcpy(curr->question, text); curr->legal = NULL; curr->explain = NULL; curr->next = NULL; return; } insert_legal(qnode **node, char *label, char *text) { qnode *curr; if (*node == NULL) { *node = (qnode *) malloc(sizeof(qnode)); (*node)->label = (char *) malloc(strlen(label)+1); strcpy((*node)->label, label); (*node)->legal = (char *) malloc(strlen(text)+1); strcpy((*node)->legal, text); (*node)->question = NULL; (*node)->explain = NULL; (*node)->next; return; } curr = *node; while (curr->next != NULL) { if (strcmp(curr->label, label) == 0) { curr->legal = (char *) malloc(strlen(text)+1); strcpy(curr->legal, text); return; } curr = curr->next; } if (strcmp(curr->label, label) == 0) { curr->legal = (char *) malloc(strlen(text)+1); strcpy(curr->legal, text); return; } curr->next = (qnode *) malloc(sizeof(qnode)); curr = curr->next; curr->label = (char *) malloc(strlen(label)+1); strcpy(curr->label, label); curr->legal = (char *) malloc(strlen(text)+1); strcpy(curr->legal, text); curr->question = NULL; curr->explain = NULL; curr->next = NULL; return; } insert_expl(qnode **node, char *label, char *text) { qnode *curr; if (*node == NULL) { *node = (qnode *) malloc(sizeof(qnode)); (*node)->label = (char *) malloc(strlen(label)+1); strcpy((*node)->label, label); (*node)->explain = (char *) malloc(strlen(text)+1); strcpy((*node)->explain, text); (*node)->legal = NULL; (*node)->question = NULL; (*node)->next; return; } curr = *node; while (curr->next != NULL) { if (strcmp(curr->label, label) == 0) { curr->explain = (char *) malloc(strlen(text)+1); strcpy(curr->explain, text); return; } curr = curr->next; } if (strcmp(curr->label, label) == 0) { curr->explain = (char *) malloc(strlen(text)+1); strcpy(curr->explain, text); return; } curr->next = (qnode *) malloc(sizeof(qnode)); curr = curr->next; curr->label = (char *) malloc(strlen(label)+1); strcpy(curr->label, label); curr->explain = (char *) malloc(strlen(text)+1); strcpy(curr->explain, text); curr->legal = NULL; curr->question = NULL; curr->next = NULL; return; } dump_questions(qnode *node) { while (node != NULL) { printf("Question node: %s\n", node->label); printf("Question: %s\n", node->question); printf("Legal values: %s\n", node->legal); printf("Explanation: %s\n", node->explain); node = node->next; } } read_long(FILE *fd, char *str, char *end, char *skip) { int i = 0; char c = 'a'; while (1) { c = fgetc(fd); if (strchr(end, c)) { ungetc(c, fd); str[i] = 0; return; } if (!strchr(skip, c)) { if (c == '\\') c = fgetc(fd); str[i++] = c; } } } read_word(FILE *fd, char *str, char *del) { char sm[2]; char c = 'a'; str[0] = 0; sm[1] = 0; while (!strchr(del, c)) { c = fgetc(fd); if (c == EOF) return EOF; sm[0] = c; strcat(str, sm); } str[strlen(str)-1] = 0; ungetc(c, fd); } skip_white_space(FILE *fd) { char *str = " \r\n\t"; char c = ' '; while ((strchr(str, c)) && (c != EOF)) c = fgetc(fd); ungetc(c, fd); } char * find_key(char *keystr, char *v[]) { int i = 0; char *str; while (v[i] != 0) { if (strncmp(keystr, v[i], strlen(keystr)) == 0) { str = &v[i][strlen(keystr)+1]; return str; } i ++; } return NULL; } /* Takes a string and replaces all occurences of the '+' character with a ' ' (space), and some other conversions */ makespace(char *str) { char *new; char num[5]; int i = 0; int p = 0; new = (char *) malloc(strlen(str)); while (str[i] != 0) { new[p] = str[i]; if (str[i] == '+') new[p] = ' '; if (str[i] == '%') { num[0] = str[i+1]; num[1] = str[i+2]; num[2] = 0; new[p] = strtol(num, NULL, 16); i = i + 2; if (new[p] == 10) { new[p] = ' '; if (new[p-1] == ' ') p--; } } i++; p++; } new[p] = 0; strcpy(str, new); } test(int n) { printf("FLAG #%d\n", n); fflush(stdout); }