~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to system/doc/tutorial/port_driver.c

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2010-03-09 17:34:57 UTC
  • mfrom: (10.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100309173457-4yd6hlcb2osfhx31
Tags: 1:13.b.4-dfsg-3
Manpages in section 1 are needed even if only arch-dependent packages are
built. So, re-enabled them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* port_driver.c */
 
2
 
 
3
#include <stdio.h>
 
4
#include "erl_driver.h"
 
5
 
 
6
typedef struct {
 
7
    ErlDrvPort port;
 
8
} example_data;
 
9
 
 
10
static ErlDrvData example_drv_start(ErlDrvPort port, char *buff)
 
11
{
 
12
    example_data* d = (example_data*)driver_alloc(sizeof(example_data));
 
13
    d->port = port;
 
14
    return (ErlDrvData)d;
 
15
}
 
16
 
 
17
static void example_drv_stop(ErlDrvData handle)
 
18
{
 
19
    driver_free((char*)handle);
 
20
}
 
21
 
 
22
static void example_drv_output(ErlDrvData handle, char *buff, int bufflen)
 
23
{
 
24
    example_data* d = (example_data*)handle;
 
25
    char fn = buff[0], arg = buff[1], res;
 
26
    if (fn == 1) {
 
27
      res = foo(arg);
 
28
    } else if (fn == 2) {
 
29
      res = bar(arg);
 
30
    }
 
31
    driver_output(d->port, &res, 1);
 
32
}
 
33
 
 
34
ErlDrvEntry example_driver_entry = {
 
35
    NULL,                       /* F_PTR init, N/A */
 
36
    example_drv_start,          /* L_PTR start, called when port is opened */
 
37
    example_drv_stop,           /* F_PTR stop, called when port is closed */
 
38
    example_drv_output,         /* F_PTR output, called when erlang has sent */
 
39
    NULL,                       /* F_PTR ready_input, called when input descriptor ready */
 
40
    NULL,                       /* F_PTR ready_output, called when output descriptor ready */
 
41
    "example_drv",              /* char *driver_name, the argument to open_port */
 
42
    NULL,                       /* F_PTR finish, called when unloaded */
 
43
    NULL,                       /* F_PTR control, port_command callback */
 
44
    NULL,                       /* F_PTR timeout, reserved */
 
45
    NULL                        /* F_PTR outputv, reserved */
 
46
};
 
47
 
 
48
DRIVER_INIT(example_drv) /* must match name in driver_entry */
 
49
{
 
50
    return &example_driver_entry;
 
51
}
 
52