/[jscoverage]/trunk/jscoverage-server.c
ViewVC logotype

Diff of /trunk/jscoverage-server.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 177 by siliconforks, Sat Sep 20 23:30:26 2008 UTC revision 339 by siliconforks, Fri Oct 24 16:13:57 2008 UTC
# Line 22  Line 22 
22  #include <assert.h>  #include <assert.h>
23  #include <ctype.h>  #include <ctype.h>
24  #include <signal.h>  #include <signal.h>
25    #include <stdint.h>
26  #include <string.h>  #include <string.h>
27    
28  #include <dirent.h>  #include <dirent.h>
# Line 29  Line 30 
30  #include <pthread.h>  #include <pthread.h>
31  #endif  #endif
32    
33    #include "encoding.h"
34  #include "global.h"  #include "global.h"
35  #include "http-server.h"  #include "http-server.h"
36  #include "instrument-js.h"  #include "instrument-js.h"
# Line 36  Line 38 
38  #include "stream.h"  #include "stream.h"
39  #include "util.h"  #include "util.h"
40    
41    static const char * specified_encoding = NULL;
42  const char * jscoverage_encoding = "ISO-8859-1";  const char * jscoverage_encoding = "ISO-8859-1";
43    bool jscoverage_highlight = true;
44    
45  typedef struct SourceCache {  typedef struct SourceCache {
46    char * url;    char * url;
47    Stream * source;    uint16_t * characters;
48      size_t num_characters;
49    struct SourceCache * next;    struct SourceCache * next;
50  } SourceCache;  } SourceCache;
51    
# Line 81  Line 86 
86  #define UNLOCK pthread_mutex_unlock  #define UNLOCK pthread_mutex_unlock
87  #endif  #endif
88    
89  static Stream * find_cached_source(const char * url) {  static const SourceCache * find_cached_source(const char * url) {
90    Stream * result = NULL;    SourceCache * result = NULL;
91    LOCK(&source_cache_mutex);    LOCK(&source_cache_mutex);
92    for (SourceCache * p = source_cache; p != NULL; p = p->next) {    for (SourceCache * p = source_cache; p != NULL; p = p->next) {
93      if (strcmp(url, p->url) == 0) {      if (strcmp(url, p->url) == 0) {
94        result = p->source;        result = p;
95        break;        break;
96      }      }
97    }    }
# Line 94  Line 99 
99    return result;    return result;
100  }  }
101    
102  static void add_cached_source(const char * url, Stream * source) {  static void add_cached_source(const char * url, uint16_t * characters, size_t num_characters) {
103    SourceCache * new_source_cache = xmalloc(sizeof(SourceCache));    SourceCache * new_source_cache = xmalloc(sizeof(SourceCache));
104    new_source_cache->url = xstrdup(url);    new_source_cache->url = xstrdup(url);
105    new_source_cache->source = source;    new_source_cache->characters = characters;
106      new_source_cache->num_characters = num_characters;
107    LOCK(&source_cache_mutex);    LOCK(&source_cache_mutex);
108    new_source_cache->next = source_cache;    new_source_cache->next = source_cache;
109    source_cache = new_source_cache;    source_cache = new_source_cache;
110    UNLOCK(&source_cache_mutex);    UNLOCK(&source_cache_mutex);
111  }  }
112    
113  static int get(const char * url, Stream * stream) __attribute__((warn_unused_result));  static int get(const char * url, uint16_t ** characters, size_t * num_characters) __attribute__((warn_unused_result));
114    
115  static int get(const char * url, Stream * stream) {  static int get(const char * url, uint16_t ** characters, size_t * num_characters) {
116    char * host = NULL;    char * host = NULL;
117    uint16_t port;    uint16_t port;
118    char * abs_path = NULL;    char * abs_path = NULL;
119    char * query = NULL;    char * query = NULL;
120    HTTPConnection * connection = NULL;    HTTPConnection * connection = NULL;
121    HTTPExchange * exchange = NULL;    HTTPExchange * exchange = NULL;
122      Stream * stream = NULL;
123    
124    int result = URL_parse(url, &host, &port, &abs_path, &query);    int result = URL_parse(url, &host, &port, &abs_path, &query);
125    if (result != 0) {    if (result != 0) {
# Line 137  Line 144 
144      goto done;      goto done;
145    }    }
146    
147      stream = Stream_new(0);
148    result = HTTPExchange_read_entire_response_entity_body(exchange, stream);    result = HTTPExchange_read_entire_response_entity_body(exchange, stream);
149    if (result != 0) {    if (result != 0) {
150      goto done;      goto done;
151    }    }
152      char * encoding = HTTPMessage_get_charset(HTTPExchange_get_response_message(exchange));
153      if (encoding == NULL) {
154        encoding = xstrdup(jscoverage_encoding);
155      }
156      result = jscoverage_bytes_to_characters(encoding, stream->data, stream->length, characters, num_characters);
157      free(encoding);
158      if (result != 0) {
159        goto done;
160      }
161    
162    result = 0;    result = 0;
163    
164  done:  done:
165      if (stream != NULL) {
166        Stream_delete(stream);
167      }
168    if (exchange != NULL) {    if (exchange != NULL) {
169      HTTPExchange_delete(exchange);      HTTPExchange_delete(exchange);
170    }    }
# Line 218  Line 238 
238    return result;    return result;
239  }  }
240    
241    static unsigned int hex_value(char c) {
242      if ('0' <= c && c <= '9') {
243        return c - '0';
244      }
245      else if ('A' <= c && c <= 'F') {
246        return c - 'A' + 10;
247      }
248      else if ('a' <= c && c <= 'f') {
249        return c - 'a' + 10;
250      }
251      else {
252        return 0;
253      }
254    }
255    
256    static char * decode_uri_component(const char * s) {
257      size_t length = strlen(s);
258      char * result = xmalloc(length + 1);
259      char * p = result;
260      while (*s != '\0') {
261        if (*s == '%') {
262          if (s[1] == '\0' || s[2] == '\0') {
263            *p = '\0';
264            return result;
265          }
266          *p = hex_value(s[1]) * 16 + hex_value(s[2]);
267          s += 2;
268        }
269        else {
270          *p = *s;
271        }
272        p++;
273        s++;
274      }
275      *p = '\0';
276      return result;
277    }
278    
279  static const char * get_entity(char c) {  static const char * get_entity(char c) {
280    switch(c) {    switch(c) {
281    case '<':    case '<':
# Line 386  Line 444 
444      case '\t':      case '\t':
445        fputs("\\t", f);        fputs("\\t", f);
446        break;        break;
447        /* IE doesn't support this */
448        /*
449      case '\v':      case '\v':
450        fputs("\\v", f);        fputs("\\v", f);
451        break;        break;
452        */
453      case '"':      case '"':
454        fputs("\\\"", f);        fputs("\\\"", f);
455        break;        break;
# Line 403  Line 464 
464    putc('"', f);    putc('"', f);
465  }  }
466    
467    static void write_source(const char * id, const uint16_t * characters, size_t num_characters, FILE * f) {
468      Stream * output = Stream_new(num_characters);
469      jscoverage_write_source(id, characters, num_characters, output);
470      fwrite(output->data, 1, output->length, f);
471      Stream_delete(output);
472    }
473    
474  static void write_json_for_file(const FileCoverage * file_coverage, int i, void * p) {  static void write_json_for_file(const FileCoverage * file_coverage, int i, void * p) {
475    FILE * f = p;    FILE * f = p;
476    
# Line 413  Line 481 
481    write_js_quoted_string(f, file_coverage->id, strlen(file_coverage->id));    write_js_quoted_string(f, file_coverage->id, strlen(file_coverage->id));
482    
483    fputs(":{\"coverage\":[", f);    fputs(":{\"coverage\":[", f);
484    for (uint32_t i = 0; i <= file_coverage->num_lines; i++) {    for (uint32_t i = 0; i < file_coverage->num_coverage_lines; i++) {
485      if (i > 0) {      if (i > 0) {
486        putc(',', f);        putc(',', f);
487      }      }
488      int timesExecuted = file_coverage->lines[i];      int timesExecuted = file_coverage->coverage_lines[i];
489      if (timesExecuted < 0) {      if (timesExecuted < 0) {
490        fputs("null", f);        fputs("null", f);
491      }      }
# Line 426  Line 494 
494      }      }
495    }    }
496    fputs("],\"source\":", f);    fputs("],\"source\":", f);
497    if (file_coverage->source == NULL) {    if (file_coverage->source_lines == NULL) {
498      if (proxy) {      if (proxy) {
499        Stream * stream = find_cached_source(file_coverage->id);        const SourceCache * cached = find_cached_source(file_coverage->id);
500        if (stream == NULL) {        if (cached == NULL) {
501          stream = Stream_new(0);          uint16_t * characters;
502          if (get(file_coverage->id, stream) == 0) {          size_t num_characters;
503            write_js_quoted_string(f, stream->data, stream->length);          if (get(file_coverage->id, &characters, &num_characters) == 0) {
504            add_cached_source(file_coverage->id, stream);            write_source(file_coverage->id, characters, num_characters, f);
505              add_cached_source(file_coverage->id, characters, num_characters);
506          }          }
507          else {          else {
508            fputs("\"\"", f);            fputs("[]", f);
509            HTTPServer_log_err("Warning: cannot retrieve URL: %s\n", file_coverage->id);            HTTPServer_log_err("Warning: cannot retrieve URL: %s\n", file_coverage->id);
           Stream_delete(stream);  
510          }          }
511        }        }
512        else {        else {
513          write_js_quoted_string(f, stream->data, stream->length);          write_source(file_coverage->id, cached->characters, cached->num_characters, f);
514        }        }
515      }      }
516      else {      else {
# Line 452  Line 520 
520          FILE * source_file = fopen(source_path, "rb");          FILE * source_file = fopen(source_path, "rb");
521          free(source_path);          free(source_path);
522          if (source_file == NULL) {          if (source_file == NULL) {
523            fputs("\"\"", f);            fputs("[]", f);
524            HTTPServer_log_err("Warning: cannot open file: %s\n", file_coverage->id);            HTTPServer_log_err("Warning: cannot open file: %s\n", file_coverage->id);
525          }          }
526          else {          else {
527            Stream * stream = Stream_new(0);            Stream * stream = Stream_new(0);
528            Stream_write_file_contents(stream, source_file);            Stream_write_file_contents(stream, source_file);
529            fclose(source_file);            fclose(source_file);
530            write_js_quoted_string(f, stream->data, stream->length);            uint16_t * characters;
531              size_t num_characters;
532              int result = jscoverage_bytes_to_characters(jscoverage_encoding, stream->data, stream->length, &characters, &num_characters);
533            Stream_delete(stream);            Stream_delete(stream);
534              if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
535                fputs("[]", f);
536                HTTPServer_log_err("Warning: encoding %s not supported\n", jscoverage_encoding);
537              }
538              else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
539                fputs("[]", f);
540                HTTPServer_log_err("Warning: error decoding %s in file %s\n", jscoverage_encoding, file_coverage->id);
541              }
542              else {
543                write_source(file_coverage->id, characters, num_characters, f);
544                free(characters);
545              }
546          }          }
547        }        }
548        else {        else {
549          /* path does not begin with / */          /* path does not begin with / */
550          fputs("\"\"", f);          fputs("[]", f);
551          HTTPServer_log_err("Warning: invalid source path: %s\n", file_coverage->id);          HTTPServer_log_err("Warning: invalid source path: %s\n", file_coverage->id);
552        }        }
553      }      }
554    }    }
555    else {    else {
556      write_js_quoted_string(f, file_coverage->source, strlen(file_coverage->source));      fputc('[', f);
557        for (uint32_t i = 0; i < file_coverage->num_source_lines; i++) {
558          if (i > 0) {
559            fputc(',', f);
560          }
561          char * source_line = file_coverage->source_lines[i];
562          write_js_quoted_string(f, source_line, strlen(source_line));
563        }
564        fputc(']', f);
565    }    }
566    fputc('}', f);    fputc('}', f);
567  }  }
# Line 480  Line 570 
570    
571  static int write_json(Coverage * coverage, const char * path) {  static int write_json(Coverage * coverage, const char * path) {
572    /* write the JSON */    /* write the JSON */
573    FILE * f = fopen(path, "w");    FILE * f = fopen(path, "wb");
574    if (f == NULL) {    if (f == NULL) {
575      return -1;      return -1;
576    }    }
# Line 528  Line 618 
618      }      }
619    
620      mkdir_if_necessary(report_directory);      mkdir_if_necessary(report_directory);
621      char * path = make_path(report_directory, "jscoverage.json");      char * current_report_directory;
622        if (str_starts_with(abs_path, "/jscoverage-store/") && abs_path[18] != '\0') {
623          char * dir = decode_uri_component(abs_path + 18);
624          current_report_directory = make_path(report_directory, dir);
625          free(dir);
626        }
627        else {
628          current_report_directory = xstrdup(report_directory);
629        }
630        mkdir_if_necessary(current_report_directory);
631        char * path = make_path(current_report_directory, "jscoverage.json");
632    
633      /* check if the JSON file exists */      /* check if the JSON file exists */
634      struct stat buf;      struct stat buf;
635      if (stat(path, &buf) == 0) {      if (stat(path, &buf) == 0) {
636        /* it exists: merge */        /* it exists: merge */
637        FILE * f = fopen(path, "r");        FILE * f = fopen(path, "rb");
638        if (f == NULL) {        if (f == NULL) {
639          result = 1;          result = 1;
640        }        }
# Line 545  Line 645 
645          }          }
646        }        }
647        if (result != 0) {        if (result != 0) {
648            free(current_report_directory);
649          free(path);          free(path);
650          Coverage_delete(coverage);          Coverage_delete(coverage);
651          send_response(exchange, 500, "Could not merge with existing coverage data\n");          send_response(exchange, 500, "Could not merge with existing coverage data\n");
# Line 556  Line 657 
657      free(path);      free(path);
658      Coverage_delete(coverage);      Coverage_delete(coverage);
659      if (result != 0) {      if (result != 0) {
660          free(current_report_directory);
661        send_response(exchange, 500, "Could not write coverage data\n");        send_response(exchange, 500, "Could not write coverage data\n");
662        return;        return;
663      }      }
664    
665      /* copy other files */      /* copy other files */
666      jscoverage_copy_resources(report_directory);      jscoverage_copy_resources(current_report_directory);
667      path = make_path(report_directory, "jscoverage.js");      path = make_path(current_report_directory, "jscoverage.js");
668        free(current_report_directory);
669      FILE * f = fopen(path, "ab");      FILE * f = fopen(path, "ab");
670      free(path);      free(path);
671      if (f == NULL) {      if (f == NULL) {
# Line 619  Line 722 
722    }    }
723  }  }
724    
725  static void instrument_js(const char * id, const char * encoding, Stream * input_stream, Stream * output_stream) {  static void instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output_stream) {
   LOCK(&javascript_mutex);  
   jscoverage_instrument_js(id, encoding, input_stream, output_stream);  
   UNLOCK(&javascript_mutex);  
   
726    const struct Resource * resource = get_resource("report.js");    const struct Resource * resource = get_resource("report.js");
727    Stream_write(output_stream, resource->data, resource->length);    Stream_write(output_stream, resource->data, resource->length);
728    
729      LOCK(&javascript_mutex);
730      jscoverage_instrument_js(id, characters, num_characters, output_stream);
731      UNLOCK(&javascript_mutex);
732  }  }
733    
734  static bool is_hop_by_hop_header(const char * h) {  static bool is_hop_by_hop_header(const char * h) {
# Line 743  Line 846 
846      }      }
847    
848      const char * request_uri = HTTPExchange_get_request_uri(client_exchange);      const char * request_uri = HTTPExchange_get_request_uri(client_exchange);
     Stream * output_stream = Stream_new(0);  
849      char * encoding = HTTPMessage_get_charset(HTTPExchange_get_response_message(server_exchange));      char * encoding = HTTPMessage_get_charset(HTTPExchange_get_response_message(server_exchange));
850      if (encoding == NULL) {      if (encoding == NULL) {
851        instrument_js(request_uri, jscoverage_encoding, input_stream, output_stream);        encoding = xstrdup(jscoverage_encoding);
852      }      }
853      else {      uint16_t * characters;
854        instrument_js(request_uri, encoding, input_stream, output_stream);      size_t num_characters;
855        free(encoding);      int result = jscoverage_bytes_to_characters(encoding, input_stream->data, input_stream->length, &characters, &num_characters);
856        free(encoding);
857        Stream_delete(input_stream);
858        if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
859          send_response(client_exchange, 500, "Encoding not supported\n");
860          goto done;
861      }      }
862        else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
863          send_response(client_exchange, 502, "Error decoding response\n");
864          goto done;
865        }
866    
867        Stream * output_stream = Stream_new(0);
868        instrument_js(request_uri, characters, num_characters, output_stream);
869    
870      /* send the headers to the client */      /* send the headers to the client */
871      for (const HTTPHeader * h = HTTPExchange_get_response_headers(server_exchange); h != NULL; h = h->next) {      for (const HTTPHeader * h = HTTPExchange_get_response_headers(server_exchange); h != NULL; h = h->next) {
872        if (is_hop_by_hop_header(h->name) || strcasecmp(h->name, HTTP_CONTENT_LENGTH) == 0) {        if (is_hop_by_hop_header(h->name) || strcasecmp(h->name, HTTP_CONTENT_LENGTH) == 0) {
873          continue;          continue;
874        }        }
875          else if (strcasecmp(h->name, HTTP_CONTENT_TYPE) == 0) {
876            HTTPExchange_add_response_header(client_exchange, HTTP_CONTENT_TYPE, "text/javascript; charset=ISO-8859-1");
877            continue;
878          }
879        HTTPExchange_add_response_header(client_exchange, h->name, h->value);        HTTPExchange_add_response_header(client_exchange, h->name, h->value);
880      }      }
881      add_via_header(HTTPExchange_get_response_message(client_exchange), HTTPExchange_get_response_http_version(server_exchange));      add_via_header(HTTPExchange_get_response_message(client_exchange), HTTPExchange_get_response_http_version(server_exchange));
# Line 768  Line 886 
886        HTTPServer_log_err("Warning: error writing to client\n");        HTTPServer_log_err("Warning: error writing to client\n");
887      }      }
888    
889      /* input_stream goes on the cache */      /* characters go on the cache */
890      /*      /*
891      Stream_delete(input_stream);      free(characters);
892      */      */
893      Stream_delete(output_stream);      Stream_delete(output_stream);
894      add_cached_source(request_uri, input_stream);      add_cached_source(request_uri, characters, num_characters);
895    }    }
896    else {    else {
897      /* does not need instrumentation */      /* does not need instrumentation */
# Line 821  Line 939 
939    /* add the `Server' response-header (RFC 2616 14.38, 3.8) */    /* add the `Server' response-header (RFC 2616 14.38, 3.8) */
940    HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION);    HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION);
941    
942      char * decoded_path = NULL;
943    char * filesystem_path = NULL;    char * filesystem_path = NULL;
944    
945    const char * abs_path = HTTPExchange_get_abs_path(exchange);    const char * abs_path = HTTPExchange_get_abs_path(exchange);
946    assert(*abs_path != '\0');    assert(*abs_path != '\0');
947    
948    if (str_starts_with(abs_path, "/jscoverage")) {    decoded_path = decode_uri_component(abs_path);
949    
950      if (str_starts_with(decoded_path, "/jscoverage")) {
951      handle_jscoverage_request(exchange);      handle_jscoverage_request(exchange);
952      goto done;      goto done;
953    }    }
954    
955    if (strstr(abs_path, "..") != NULL) {    if (strstr(decoded_path, "..") != NULL) {
956      send_response(exchange, 403, "Forbidden\n");      send_response(exchange, 403, "Forbidden\n");
957      goto done;      goto done;
958    }    }
959    
960    filesystem_path = make_path(document_root, abs_path + 1);    filesystem_path = make_path(document_root, decoded_path + 1);
961    size_t filesystem_path_length = strlen(filesystem_path);    size_t filesystem_path_length = strlen(filesystem_path);
962    if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') {    if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') {
963      /* stat on Windows doesn't work with trailing slash */      /* stat on Windows doesn't work with trailing slash */
# Line 891  Line 1012 
1012        goto done;        goto done;
1013      }      }
1014    
1015        /*
1016        When do we send a charset with Content-Type?
1017        if Content-Type is "text" or "application"
1018          if instrumented JavaScript
1019            use Content-Type: application/javascript; charset=ISO-8859-1
1020          else if --encoding is given
1021            use that encoding
1022          else
1023            send no charset
1024        else
1025          send no charset
1026        */
1027      const char * content_type = get_content_type(filesystem_path);      const char * content_type = get_content_type(filesystem_path);
     HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type);  
1028      if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(abs_path)) {      if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(abs_path)) {
1029        Stream * input_stream = Stream_new(0);        HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, "text/javascript; charset=ISO-8859-1");
       Stream * output_stream = Stream_new(0);  
1030    
1031          Stream * input_stream = Stream_new(0);
1032        Stream_write_file_contents(input_stream, f);        Stream_write_file_contents(input_stream, f);
1033    
1034        instrument_js(abs_path, jscoverage_encoding, input_stream, output_stream);        uint16_t * characters;
1035          size_t num_characters;
1036          int result = jscoverage_bytes_to_characters(jscoverage_encoding, input_stream->data, input_stream->length, &characters, &num_characters);
1037          Stream_delete(input_stream);
1038    
1039          if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
1040            send_response(exchange, 500, "Encoding not supported\n");
1041            goto done;
1042          }
1043          else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
1044            send_response(exchange, 500, "Error decoding JavaScript file\n");
1045            goto done;
1046          }
1047    
1048          Stream * output_stream = Stream_new(0);
1049          instrument_js(abs_path, characters, num_characters, output_stream);
1050          free(characters);
1051    
1052        if (HTTPExchange_write_response(exchange, output_stream->data, output_stream->length) != 0) {        if (HTTPExchange_write_response(exchange, output_stream->data, output_stream->length) != 0) {
1053          HTTPServer_log_err("Warning: error writing to client\n");          HTTPServer_log_err("Warning: error writing to client\n");
1054        }        }
   
       Stream_delete(input_stream);  
1055        Stream_delete(output_stream);        Stream_delete(output_stream);
1056      }      }
1057      else {      else {
1058          /* send the Content-Type with charset if necessary */
1059          if (specified_encoding != NULL && (str_starts_with(content_type, "text/") || str_starts_with(content_type, "application/"))) {
1060            char * content_type_with_charset = NULL;
1061            xasprintf(&content_type_with_charset, "%s; charset=%s", content_type, specified_encoding);
1062            HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type_with_charset);
1063            free(content_type_with_charset);
1064          }
1065          else {
1066            HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type);
1067          }
1068    
1069        char buffer[8192];        char buffer[8192];
1070        size_t bytes_read;        size_t bytes_read;
1071        while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) {        while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) {
# Line 926  Line 1083 
1083    
1084  done:  done:
1085    free(filesystem_path);    free(filesystem_path);
1086      free(decoded_path);
1087  }  }
1088    
1089  static void handler(HTTPExchange * exchange) {  static void handler(HTTPExchange * exchange) {
# Line 956  Line 1114 
1114        exit(EXIT_SUCCESS);        exit(EXIT_SUCCESS);
1115      }      }
1116      else if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) {      else if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) {
1117        printf("jscoverage-server %s\n", VERSION);        version();
       exit(EXIT_SUCCESS);  
1118      }      }
1119      else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {      else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
1120        verbose = 1;        verbose = 1;
# Line 966  Line 1123 
1123      else if (strcmp(argv[i], "--report-dir") == 0) {      else if (strcmp(argv[i], "--report-dir") == 0) {
1124        i++;        i++;
1125        if (i == argc) {        if (i == argc) {
1126          fatal("--report-dir: option requires an argument");          fatal_command_line("--report-dir: option requires an argument");
1127        }        }
1128        report_directory = argv[i];        report_directory = argv[i];
1129      }      }
# Line 977  Line 1134 
1134      else if (strcmp(argv[i], "--document-root") == 0) {      else if (strcmp(argv[i], "--document-root") == 0) {
1135        i++;        i++;
1136        if (i == argc) {        if (i == argc) {
1137          fatal("--document-root: option requires an argument");          fatal_command_line("--document-root: option requires an argument");
1138        }        }
1139        document_root = argv[i];        document_root = argv[i];
1140      }      }
# Line 988  Line 1145 
1145      else if (strcmp(argv[i], "--encoding") == 0) {      else if (strcmp(argv[i], "--encoding") == 0) {
1146        i++;        i++;
1147        if (i == argc) {        if (i == argc) {
1148          fatal("--encoding: option requires an argument");          fatal_command_line("--encoding: option requires an argument");
1149        }        }
1150        jscoverage_encoding = argv[i];        jscoverage_encoding = argv[i];
1151          specified_encoding = jscoverage_encoding;
1152      }      }
1153      else if (strncmp(argv[i], "--encoding=", 11) == 0) {      else if (strncmp(argv[i], "--encoding=", 11) == 0) {
1154        jscoverage_encoding = argv[i] + 11;        jscoverage_encoding = argv[i] + 11;
1155          specified_encoding = jscoverage_encoding;
1156      }      }
1157    
1158      else if (strcmp(argv[i], "--ip-address") == 0) {      else if (strcmp(argv[i], "--ip-address") == 0) {
1159        i++;        i++;
1160        if (i == argc) {        if (i == argc) {
1161          fatal("--ip-address: option requires an argument");          fatal_command_line("--ip-address: option requires an argument");
1162        }        }
1163        ip_address = argv[i];        ip_address = argv[i];
1164      }      }
# Line 1007  Line 1166 
1166        ip_address = argv[i] + 13;        ip_address = argv[i] + 13;
1167      }      }
1168    
1169        else if (strcmp(argv[i], "--js-version") == 0) {
1170          i++;
1171          if (i == argc) {
1172            fatal_command_line("--js-version: option requires an argument");
1173          }
1174          jscoverage_set_js_version(argv[i]);
1175        }
1176        else if (strncmp(argv[i], "--js-version=", 13) == 0) {
1177          jscoverage_set_js_version(argv[i] + 13);
1178        }
1179    
1180        else if (strcmp(argv[i], "--no-highlight") == 0) {
1181          jscoverage_highlight = false;
1182        }
1183    
1184      else if (strcmp(argv[i], "--no-instrument") == 0) {      else if (strcmp(argv[i], "--no-instrument") == 0) {
1185        i++;        i++;
1186        if (i == argc) {        if (i == argc) {
1187          fatal("--no-instrument: option requires an argument");          fatal_command_line("--no-instrument: option requires an argument");
1188        }        }
1189        no_instrument[num_no_instrument] = argv[i];        no_instrument[num_no_instrument] = argv[i];
1190        num_no_instrument++;        num_no_instrument++;
# Line 1023  Line 1197 
1197      else if (strcmp(argv[i], "--port") == 0) {      else if (strcmp(argv[i], "--port") == 0) {
1198        i++;        i++;
1199        if (i == argc) {        if (i == argc) {
1200          fatal("--port: option requires an argument");          fatal_command_line("--port: option requires an argument");
1201        }        }
1202        port = argv[i];        port = argv[i];
1203      }      }
# Line 1040  Line 1214 
1214      }      }
1215    
1216      else if (strncmp(argv[i], "-", 1) == 0) {      else if (strncmp(argv[i], "-", 1) == 0) {
1217        fatal("unrecognized option `%s'", argv[i]);        fatal_command_line("unrecognized option `%s'", argv[i]);
1218      }      }
1219      else {      else {
1220        fatal("too many arguments");        fatal_command_line("too many arguments");
1221      }      }
1222    }    }
1223    
# Line 1051  Line 1225 
1225    char * end;    char * end;
1226    unsigned long numeric_port = strtoul(port, &end, 10);    unsigned long numeric_port = strtoul(port, &end, 10);
1227    if (*end != '\0') {    if (*end != '\0') {
1228      fatal("--port: option must be an integer");      fatal_command_line("--port: option must be an integer");
1229    }    }
1230    if (numeric_port > UINT16_MAX) {    if (numeric_port > UINT16_MAX) {
1231      fatal("--port: option must be 16 bits");      fatal_command_line("--port: option must be 16 bits");
1232    }    }
1233    
1234    /* is this a shutdown? */    /* is this a shutdown? */
# Line 1124  Line 1298 
1298      SourceCache * p = source_cache;      SourceCache * p = source_cache;
1299      source_cache = source_cache->next;      source_cache = source_cache->next;
1300      free(p->url);      free(p->url);
1301      Stream_delete(p->source);      free(p->characters);
1302      free(p);      free(p);
1303    }    }
1304    UNLOCK(&source_cache_mutex);    UNLOCK(&source_cache_mutex);

Legend:
Removed from v.177  
changed lines
  Added in v.339

  ViewVC Help
Powered by ViewVC 1.1.24