CodeMirror: C-like mode

1
/* C demo code */
2
 
3
#include <zmq.h>
4
#include <pthread.h>
5
#include <semaphore.h>
6
#include <time.h>
7
#include <stdio.h>
8
#include <fcntl.h>
9
#include <malloc.h>
10
 
11
typedef struct {
12
  void* arg_socket;
13
  zmq_msg_t* arg_msg;
14
  char* arg_string;
15
  unsigned long arg_len;
16
  int arg_int, arg_command;
17
 
18
  int signal_fd;
19
  int pad;
20
  void* context;
21
  sem_t sem;
22
} acl_zmq_context;
23
 
24
#define p(X) (context->arg_##X)
25
 
26
void* zmq_thread(void* context_pointer) {
27
  acl_zmq_context* context = (acl_zmq_context*)context_pointer;
28
  char ok = 'K', err = 'X';
29
  int res;
30
 
31
  while (1) {
32
    while ((res = sem_wait(&context->sem)) == EINTR);
33
    if (res) {write(context->signal_fd, &err, 1); goto cleanup;}
34
    switch(p(command)) {
35
    case 0: goto cleanup;
36
    case 1: p(socket) = zmq_socket(context->context, p(int)); break;
37
    case 2: p(int) = zmq_close(p(socket)); break;
38
    case 3: p(int) = zmq_bind(p(socket), p(string)); break;
39
    case 4: p(int) = zmq_connect(p(socket), p(string)); break;
40
    case 5: p(int) = zmq_getsockopt(p(socket), p(int), (void*)p(string), &p(len)); break;
41
    case 6: p(int) = zmq_setsockopt(p(socket), p(int), (void*)p(string), p(len)); break;
42
    case 7: p(int) = zmq_send(p(socket), p(msg), p(int)); break;
43
    case 8: p(int) = zmq_recv(p(socket), p(msg), p(int)); break;
44
    case 9: p(int) = zmq_poll(p(socket), p(int), p(len)); break;
45
    }
46
    p(command) = errno;
47
    write(context->signal_fd, &ok, 1);
48
  }
49
 cleanup:
50
  close(context->signal_fd);
51
  free(context_pointer);
52
  return 0;
53
}
54
 
55
void* zmq_thread_init(void* zmq_context, int signal_fd) {
56
  acl_zmq_context* context = malloc(sizeof(acl_zmq_context));
57
  pthread_t thread;
58
 
59
  context->context = zmq_context;
60
  context->signal_fd = signal_fd;
61
  sem_init(&context->sem, 1, 0);
62
  pthread_create(&thread, 0, &zmq_thread, context);
63
  pthread_detach(thread);
64
  return context;
65
}
66
 
 
 

Simple mode that tries to handle C-like languages as well as it can. Takes two configuration parameters: keywords, an object whose property names are the keywords in the language, and useCPP, which determines whether C preprocessor directives are recognized.

MIME types defined: text/x-csrc (C code), text/x-c++src (C++ code), text/x-java (Java code), text/x-csharp (C#).