request.c 3.74 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1 2 3 4 5 6 7 8 9 10 11
/********************************************************************/
/*                                                                  */
/*  The Why3 Verification Platform   /   The Why3 Development Team  */
/*  Copyright 2010-2016   --   INRIA - CNRS - Paris-Sud University  */
/*                                                                  */
/*  This software is distributed under the terms of the GNU Lesser  */
/*  General Public License version 2.1, with the special exception  */
/*  on linking described in file LICENSE.                           */
/*                                                                  */
/********************************************************************/

12 13 14 15
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "request.h"
16
#include "options.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

//count the semicolons in <buf>, up to <len>
int count_semicolons(char* buf, int len);

//in <buf>, starting from index <begin> and up to index <len-1>, search for a
//semicolon. If a semicolon is found at index i, copy the substring from
//<begin> to <i-1>, that is, excluding the semicolon, into <result>, which
//will be allocated to contain that much space + a null terminator.
//If no semicolon is found, the part of <buf> starting from <begin> up to <len-1>
//is copied instead, and a null terminator added.
int copy_up_to_semicolon(char* buf, int begin, int len, char** result);

int count_semicolons(char* buf, int len) {
  int cnt = 0;
  int i = 0;
  for (i = 0; i < len; i++) {
    if (buf[i] == ';') {
      cnt++;
    }
  }
  return cnt;
}

int copy_up_to_semicolon(char* buf, int begin, int len, char** result) {
  int i;
  for (i = begin; i < len; i++) {
    if (buf[i] == ';') {
      break;
    }
  }
  (*result) = (char*) malloc(sizeof(char) * (i - begin + 1));
  memcpy((*result), buf + begin, i - begin);
  (*result)[i - begin] = '\0';
  if (i == len) {
    return 0;
  } else {
    return i + 1;
  }
}

prequest parse_request(char* str_req, int len, int key) {
58
  int numargs, semic, parallel_arg;
59 60 61 62
  int i = 0;
  int pos = 0;
  prequest req;
  char* tmp;
63
  bool runstdin = false;
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  semic = count_semicolons(str_req, len);
  if (semic == 0) {
    return NULL;
  }
  //  might be a 'parallel' command
  if (semic == 1) {
    pos = copy_up_to_semicolon (str_req, pos, len, &tmp);
    if (strncmp(tmp, "parallel", pos) == 0) {
        pos = copy_up_to_semicolon (str_req, pos, len, &tmp);
        parallel_arg = atoi(tmp);
        if (parallel_arg >= 1) {
          parallel = parallel_arg;
        }
    }
    return NULL;
  }

  numargs = semic - 4;
83 84 85
  if (numargs < 0) {
    return NULL;
  }
86 87
  pos = copy_up_to_semicolon(str_req, pos, len, &tmp);
  if (strncmp(tmp, "run", pos) != 0) {
88 89 90 91 92
    if (strncmp(tmp, "runstdin", pos) == 0) {
      runstdin = true;
    } else {
      return NULL;
    }
93
  }
94 95 96
  req = (prequest) malloc(sizeof(request));
  req->key = key;
  req->numargs = numargs;
97
  req->usestdin = runstdin;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  pos = copy_up_to_semicolon(str_req, pos, len, &(req->id));
  pos = copy_up_to_semicolon(str_req, pos, len, &tmp);
  req->timeout = atoi(tmp);
  free(tmp);
  pos = copy_up_to_semicolon(str_req, pos, len, &tmp);
  req->memlimit = atoi(tmp);
  free(tmp);
  pos = copy_up_to_semicolon(str_req, pos, len, &(req->cmd));
  req->args = (char**)malloc(sizeof(char*) * (numargs));
  for (i = 0; i < numargs; i++) {
    pos = copy_up_to_semicolon(str_req, pos, len, &(req->args[i]));
  }
  return req;
}

void print_request(prequest r) {
  int i;
  if (r) {
    printf("%s %d %d %s", r->id, r->timeout, r->memlimit, r->cmd);
    for (i = 0; i < r->numargs; i++) {
       printf(" %s", r->args[i]);
    }
  } else {
    printf("<null>");
  }
}

void free_request(prequest r) {
  int i;
  free(r->cmd);
  for (i = 0;i < r->numargs; i++) {
    free(r->args[i]);
  }
  free(r->args);
  free(r);
}