Skip to content

Running BRIE

Michael Popov edited this page Sep 12, 2023 · 3 revisions

Running BRIE

There are a few ways to run BRIE. (syntax, functions and data types are explained on other pages)

Running in a REPL mode

Just run BRIE without any command-line argument and you will get into REPL mode.

$ brie
> dofile('bmp.decl.brie')      -- Load data structures stored in a file
> open('test-16-color.bmp')    -- Open a data source
> header = read('header')      -- Read a BMP file header
> info = read('info')          -- Read a BMP info
>                              -- Print some fields from the info structure
> printf('File: %s\nWidth: %d\nHeight: %d\nBits Per Pixel: %d\n',
>> BRIE_PATH, info.width, info.height, info.bits_per_pixel)
File: test-16-color.bmp
Width: 1112
Height: 702
Bits Per Pixel: 4
> quit

You can get into a REPL mode by running a script file and then switching into interactive mode by setting a first command-line argument as a script path and the second command-line argument as a special symbol '@'.

$ brie bmp.decl.brie @
> open('test-16-color.bmp')
> header = read('header')
> print(header.signature)
BM
> quit

Command-line script ("one-liner")

You can put a command as a first command-line argument and a file path as a second. This command will be executed for the file.

brie 'scanf("Width: %d", "void#18 u32")' test-16-color.bmp

Running script

You can store a script in a file and use path to the file as a first command-line argument. You must set one or many data sources in a following command line arguments.

$ cat > print_widths.brie
printf("File: %s\t\t", BRIE_PATH)
scanf("Width: %d", "void#18 u32")
^D

$ brie print_widths.brie test-16-color.bmp
File: test-16-color.bmp         Width: 1112

$ brie print_widths.brie *.bmp
File: test-16-color.bmp         Width: 1112
File: test-24-bit.bmp           Width: 1112
File: test-256-color.bmp                Width: 1112
File: test-monochrome.bmp               Width: 1112

You can set a shebang in a script file and run it.

$ cat > print_widths.brie
#!/bin/brie
printf("File: %s\t\t", BRIE_PATH)
scanf("Width: %d", "void#18 u32")
^D
$ chmod +x print_widths.brie
$ ./print_widths.brie test-16-color.bmp
File: test-16-color.bmp         Width: 1112

Running script for a very large number of files

In some cases the number of files you want to process exceeds the capacity of a command-line arguments buffer. In this case you can run the utility in a special mode where it gets each file's path from stdin. Just set a second command-line argument as '-'.

$ ls *.bmp | brie print_widths.brie -
File: test-16-color.bmp         Width: 1112
File: test-24-bit.bmp           Width: 1112
File: test-256-color.bmp                Width: 1112
File: test-monochrome.bmp               Width: 1112