brainfuck  0.1
A full fledged brainfuck compiler
Functions
common.c File Reference

Common and utility functions. More...

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"

Go to the source code of this file.

Functions

int is_uint (const char *str)
 Checks if the given string can represent a positive integer. More...
 
void print_help (void)
 Prints the help message when "-h" or "--help" are command line arguments.
 
int my_getopt (int argc, char **argv, int *stack_size, int *array_size, char **output_file, char **input_file, int *mode)
 Custom getopt function. More...
 
char * remove_extenstion (const char *str)
 Removes the file extension from a string, if there is one at all. More...
 
char * get_as (int mode, const char *name, const char *original, char **ret)
 Gets the name of the assembly code file. More...
 
char * get_obj (int mode, const char *name, const char *original, char **ret)
 Gets the name of the assembled file. More...
 
int get_as_file (int mode, FILE **out, const char *name, const char *input_name)
 Gets the file pointer to the assembly code file. More...
 
int close_as_file (int mode, FILE *f)
 Closes the file pointer to the assembly source file. More...
 
int assemble (int mode, const char *as_file, const char *obj_file)
 Wrapper around assembling the assembly code. More...
 
int get_in (int mode, const char *input_file, FILE **in)
 Gets the source code file pointer and deals with IO errors. More...
 

Detailed Description

Common and utility functions.

Author
Benjamin James
Date
15 September 2015

Definition in file common.c.

Function Documentation

int assemble ( int  mode,
const char *  as_file,
const char *  obj_file 
)

Wrapper around assembling the assembly code.

This also removes the assembly code file if we aren't linking the object code afterwards.

Parameters
modeThe state of the program
as_fileThe name of the assembly source code file
obj_fileThe name of the object code file that will be generated
Returns
the success of the assembler process

Definition at line 289 of file common.c.

Referenced by main().

int close_as_file ( int  mode,
FILE *  f 
)

Closes the file pointer to the assembly source file.

Its magic is in the fact that if this was a process the compier was writing to, it waits for the process to end using pclose(3), but otherwise using fclose(3) to close the file.

Parameters
modeThe state of the program
fThe file pointer to close
Returns
if the file pointer was successfully closed

Definition at line 267 of file common.c.

Referenced by main().

char* get_as ( int  mode,
const char *  name,
const char *  original,
char **  ret 
)

Gets the name of the assembly code file.

Parameters
modeThe state of the program
nameThe name of the file set as output by the -o option
originalThe name of the original file that is being compiled A pointer to the malloc'd name of the assembly code file, or NULL if the compiler is piping out the assembly code
Returns
the malloc'd name of the assembly code file, or NULL if the compiler is piping out the assembly code

Definition at line 153 of file common.c.

References remove_extenstion().

Referenced by get_as_file(), and main().

int get_as_file ( int  mode,
FILE **  out,
const char *  name,
const char *  input_name 
)

Gets the file pointer to the assembly code file.

If -pipe is specified and if the assembly will be assembled, the file pointer will be pointing to the piped process assembling the code concurrently.

Parameters
modeThe state of the program
outThe pointer to the file pointer to return
nameThe name of the file set as output by the -o option
input_nameThe name of the original file that is being compiled
Returns
0 on success, -1 on failure

Definition at line 217 of file common.c.

References get_as(), and get_obj().

Referenced by main().

int get_in ( int  mode,
const char *  input_file,
FILE **  in 
)

Gets the source code file pointer and deals with IO errors.

Parameters
modeThe state of the program
input_fileThe file specified by the command line args
inThe pointer to the file pointer to set

Definition at line 310 of file common.c.

Referenced by main().

char* get_obj ( int  mode,
const char *  name,
const char *  original,
char **  ret 
)

Gets the name of the assembled file.

Parameters
modeThe state of the program
nameThe name of the file set as output by the -o option
originalThe name of the original file that is being compiled
retA pointer to the malloc'd name of the assembled file if no errors occur The malloc'd name of the assembled file if no errors occur

Definition at line 184 of file common.c.

References remove_extenstion().

Referenced by get_as_file(), and main().

int is_uint ( const char *  str)

Checks if the given string can represent a positive integer.

Parameters
strThe string to check
Returns
if the string is a positive integer or not

Definition at line 23 of file common.c.

Referenced by my_getopt().

int my_getopt ( int  argc,
char **  argv,
int *  stack_size,
int *  array_size,
char **  output_file,
char **  input_file,
int *  mode 
)

Custom getopt function.

Since I don't want to break compatibility with all systems by including unistd.h, this function exists. It sets variables based on command line arguments and flags.

Parameters
argcThe amount of command line args
argvThe command line args
stack_sizeA pointer to set a variable which determines the loop stack size
array_sizeA pointer to set a variable which determines the internal array size for the compiled program
output_fileA pointer to set the argument of the -o option
input_fileA pointer to set the file to compile
modeA pointer to the overall state of the program determined by the flags
Returns
mode, the state of the program

Definition at line 71 of file common.c.

References is_uint(), and print_help().

Referenced by main().

char* remove_extenstion ( const char *  str)

Removes the file extension from a string, if there is one at all.

Parameters
strThe string to check the file extension of
Returns
The malloc'd string containing the filename without the extension

Definition at line 125 of file common.c.

Referenced by get_as(), and get_obj().