Posts

Showing posts with the label C

YAML syntax processing in C with libyaml and lemon parser generator (part 2)

Image
In previous part of the blog we have created the basis of the program. In this part we will work on lemon parser grammar file "parser.y". Lemon is an application that can be installed from packages (comes almost with all Linux distributions), with Homebrew on Mac OS. Or you can build it from the sources. It is to find in Internet how to do that.  When lemon is installed, we can run command "lemon" with a path to the grammar file as an argument.  Using Makefile will much improve our build process: We can simply run "make" command in our working directory and it will generate the parser .c and .h files. Libyaml wiki has a grammar explained for events: http://pyyaml.org/wiki/LibYAML#Events This is basically the same for tokens. Let's start with a very minimal ( minimum minimorum ) YAML syntax (file minimum.yaml):   key : value If we run now our program, we will have this in output: Here, the "key" and the ...

YAML syntax processing in C with libyaml and lemon parser generator (part 1)

Image
This article continues previous blog on YAML data serialization format processing with C. I have mentioned there that libyaml perfectly fits to be used with parser generators like GNU bison, yacc or lemon. Usually, these tools are used with lexical analyser (flex, lex) that parses input sequence of bytes (text) and produces tokens for parser. In case of YAML the C library "libyaml" is do the job. Libyaml can scan YAML file or stream and produces events or tokens. In this article I will try use libyaml with lemon parser to implement YAML processing. Sure thing, bison and yacc can be used too. Let's with simple function that will use libyaml to scan file. For that I took the example from "libyaml" wiki:  http://pyyaml.org/wiki/LibYAML#ParserAPISynopsis Here what I got for the bootstrap main.c file: Note that here we use function "yaml_parser_scan" that produces tokens. To compile the scenario we can run command: # gcc main.c -...

SIP stack libre: creating simple OPTIONS SIP request

Libre is a powerful and simple library than implements SIP and many other real-time communication protocol. Full list of features and protocols can be found on its web-page:  http://creytiv.com/re.html . There are several open source SIP stacks on C available: Libre Sofia-SIP PJSIP libeXosip2 reSIProcate bell-sip I have tried do some simple SIP UA with four of them: libre, sofia-sip, pjsip and libeXosip2. These scripts can be found at my github project . Some of them are used with big projects. For example, PJSIP is now a part of Asterisk project (starting from version 11) and Sofia-sip is a core SIP library of FreeSWITCH project. Asterisk team has made some good research on SIP Stacks when they were chosing one for their project. It can be found here: https://wiki.asterisk.org/wiki/display/AST/SIP+Stack+Research I do like sofia-sip, but, unfortunately, it was not updated for very long time (since 2011).  Libre looks like relatively new to other...

YAML documents parsing with libyaml in C

YAML markup standard (or human-readable data serialization language as it stands in wikipedia ) is widely used these days as alternative to XML, JSON or others. Anybody who does some ruby-on-rails uses YAML configuration for database connection. It is simple, but in the same time, flexible standard which definitely should replace outdated formats like INI. YAML is supported by lots of programming languages. Check projects list at yaml.org .

Test Anything Protocol (TAP) with autotools and cmocka

Image
Autotoole can handle test suits of several types. Usually, I store my tests in "test" directory in the root of the project and set up each test in "Makefile.am" file. Each test to run is a separate executable file listed in "TEST" variable in makefile. For example: TESTS = foo_test bar_test xyz_test

Mongoose - small, fast and simply awesome network library

Recently, I have been looking for a C library which can provide handy API for web-socket protocol. While searching the Web for "websocket" I have found many popular and good libraries, but just when I was going to give a try one of them, I have suddenly saw the link to "mongoose" library. After reading some documentation and introduction , I had a feeling that this is exactly what I am looking for! Mongoose developers describe their library as a "embedded web server" and as a "multi-protocol network library". Web-socket is only the part of its functionality, there are several other protocols as well as pure TCP/UDP communication functions that can help to implement any other protocol you wish. Basically, all that you need to start developing with mongoose can be found in the brief documentation page or on the  github repository . It is amazingly easy to use! Just put files "mongoose.c" and "mongoose.h" in you project and ...

Using Ragel to implement TFTP protocol client

Image
This blog is about how to implement TFTP protocol (RFC1350) with ragel. Ragel is a very powerful State Machine compiler, perfect for text parsing. One of the tasks where ragel can be used for is "Writing robust protocol implementations". More detailed information about ragel can be found here:  http://www.colm.net/open-source/ragel . Indeed, there are projects which successfully use ragel to parse network protocol packets: Mongrel , the web-server developed by Zed Shaw. Mongrel and its derivatives (like Thin) are using ragel to parse HTTP protocol. OverSIP , SIP proxy developed by IƱaki Baz Castillo and using ragel to parse SIP protocol. Both HTTP and SIP are text based protocols. There are many articles and blogs that explain how to parse text with ragel. TFTP is a bit different and I thought it would be fun to try implement this protocol with ragel.

URL parser ruby C extension binding with C library re2c lexer

This is step-by-step description how to create Ruby C extension using static library. Creating Ruby extensions is not a complicated task. There are plenty of tutorials and articles. Also Ruby API is clean and well described. For me, an interesting task was to create a Ruby project using external library included in the project within git repository. Lets imaging we want to create Ruby library for parsing text. Lets say, URL. Of course, there is standard Ruby library (URI), and it is ruby code. But if we want something faster,  it is good idea to use C extension.

Boolean types using function pointers

There are many ways to use Boolean  date types in C. C99 standard library <stdbool.h> or many others, one just need to "google". While reading book "Understanding and Using C Pointers" it came to my mind that another way to define Boolean type is C pointers. Something like this: typedef void (*bool)(); void true() {} void false(){} To use it we can simply do that: bool is_correct; is_correct = false; Here is a working example: