Skip to content

promovicz/cplr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jul 10, 2024
cc1cca7 · Jul 10, 2024
Jul 6, 2024
Jan 25, 2023
Jul 6, 2024
Jul 10, 2024
Jan 20, 2023
Nov 1, 2020
Jan 4, 2022
Jan 21, 2023
May 23, 2023
Oct 30, 2020
Jan 22, 2023
Jan 24, 2023
Jan 25, 2023
Nov 2, 2020
Oct 28, 2021
Nov 3, 2020

Repository files navigation

cplr - the C piler

The C piler cplr is a tool for executing C code from the shell and is meant to break the barrier between the shell and the core language of UNIX, which it does by making the compiler a tool like sed, grep and awk.

But why!?

The practical purpose of cplr is to allow the compilation and execution of short C programs for testing, system information retrieval, live coding and interactive development of small C sequences and programs.

$ cplr 'puts("hello!")'
hello!
$ cplr 'printf("%d\n", getpagesize())'
4096

Tell me more!

cplr itself is a simple compiler front-end that does not have any comprehension of the C language or even data types, but this is why it works well because it does not try to do something hard. Instead we follow the UNIX philosophy and do just one thing: act as the interactive equivalent of cc.

The current backend of cplr is based on TinyCC, which gives it a language level of C99 or better and the ability to interact with almost any C library on a normal Linux system including large and complicated ones. You can run Gtk or Python in this tool without having to expect any issues.

Usage examples

A basic use is to retrieve type information:

$ cplr -i linux/kvm.h 'printf("%zu\n", sizeof(struct kvm_regs))'
144

You can also use cplr to create ad-hoc executables:

$ cplr -o realpath 'puts(realpath(argv[1],NULL))'
$ ./realpath .
/home/user

Small C libraries can be used directly from source:

$ cplr -s linenoise.c -i linenoise.h 'printf("Line: %s\n", linenoise("% "))'
% hello
Line: hello

Thanks to pkg-config you can easily call any library:

$ cplr -P tinfo -i term.h 'setupterm(NULL,1,NULL)' 'printf("%d\n", tigetnum("colors"))'
256

Further examples can be found here.

Building cplr

We build using CMake. You need readline. We bring our own copy of TinyCC.

$ git clone https://github.com/promovicz/cplr.git
$ cd cplr
$ cmake .
$ make
$ make install

Future possibilities

  • C REPL (experimental form exists)
  • Garbage collection (in progress)
  • Crash handlers (easy)
  • Backtrace support
  • Bounds checking
  • Instruction trace
  • Call trace
  • Other line editors (not readline)
  • Internal disassembler (capstone)
  • Support other compilers using dlopen
  • Support launching into gdb/lldb
  • Source code and assembly dumping
  • Editor and pager integration
  • Live process code injection (researching)
  • Live kernel code injection (needs gcc)
  • Support for C++ and ObjC (needs gcc/clang)