machinatrix
A very silly Matrix bot written in C
Loading...
Searching...
No Matches
main.c File Reference

Main robot implementation. More...

#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/wait.h>
#include <unistd.h>
#include "common.h"
#include <tidybuffio.h>
#include "config.h"
#include "dlpo.h"
#include "hash.h"
#include "html.h"
#include "numeraria.h"
#include "utils.h"
#include "wikt.h"
Include dependency graph for main.c:

Data Structures

struct  config
 machinatrix-specific configuration. More...
 
struct  mtrix_cmd
 Structure that associates a mtrix_cmd_f to a command. More...
 
struct  stats
 Accumulates lookup statistics. More...
 

Macros

#define DICT_FILE   "/usr/share/dict/words"
 Dictionary file used for commands that require a list of words.
 

Typedefs

typedef bool mtrix_cmd_f(const struct config *, const char *const *)
 Function that handles a command.
 

Enumerations

enum  { MAX_PATH = MTRIX_MAX_PATH , MAX_UNIX_PATH = MTRIX_MAX_UNIX_PATH , STATS_WIKT = 0 , STATS_DLPO = 1 }
 

Functions

int main (int argc, const char *const *argv)
 Program entry point.
 
static bool parse_args (int argc, char *const **argv, struct config *config)
 Parses command-line arguments and fills config.
 
static void usage (FILE *f)
 Prints a usage message.
 
static void config_init (struct config *config)
 Initializes values in config.
 
static bool config_init_numeraria (struct config *config)
 Initializes values in config.
 
static bool config_destroy (struct config *config)
 Destructs config.
 
static bool config_record_command (const struct config *config, const char *const *argv)
 Records that a command will be executed, along with its arguments.
 
static bool handle_cmd (const struct config *config, const char *const *argv)
 Handles a command passed via the command line.
 
static bool handle_file (const struct config *config, FILE *f)
 Handles commands read as lines from a file.
 
static bool str_to_args (char *str, size_t max_args, char **argv)
 Breaks string into space-separated parts.
 
static bool cmd_help (const struct config *config, const char *const *argv)
 Implements the help command.
 
static bool cmd_ping (const struct config *config, const char *const *argv)
 Implements the ping command.
 
static bool cmd_word (const struct config *config, const char *const *argv)
 Implements the word command.
 
static bool cmd_abbr (const struct config *config, const char *const *argv)
 Implements the abbr command.
 
static bool cmd_damn (const struct config *config, const char *const *argv)
 Implements the damn command.
 
static bool cmd_parl (const struct config *config, const char *const *argv)
 Implements the parl command.
 
static bool cmd_bard (const struct config *config, const char *const *argv)
 Implements the bard command.
 
static bool cmd_dlpo (const struct config *config, const char *const *argv)
 Implements the dlpo command.
 
static bool cmd_wikt (const struct config *config, const char *const *argv)
 Implements the wikt command.
 
static bool cmd_tr (const struct config *config, const char *const *argv)
 Implements the tr command.
 
static bool cmd_stats (const struct config *config, const char *const *argv)
 Implements the stats command.
 
static bool stats_increment (const struct config *config, uint8_t opt)
 Increments the count on wikt and dlpo lookups.
 
static bool stats_file (const struct config *config)
 Print stats from local file.
 
static bool stats_numeraria (const struct config *config)
 Print stats from numeraria.
 

Variables

const char * PROG_NAME = NULL
 To be filled by argv[0] later, for logging.
 
const char * CMD_NAME = NULL
 Set while processing a command.
 
struct mtrix_cmd COMMANDS []
 Maps a command name to the function that handles it.
 

Detailed Description

Main robot implementation.

Macro Definition Documentation

◆ DICT_FILE

#define DICT_FILE   "/usr/share/dict/words"

Dictionary file used for commands that require a list of words.

Typedef Documentation

◆ mtrix_cmd_f

typedef bool mtrix_cmd_f(const struct config *, const char *const *)

Function that handles a command.

Function Documentation

◆ cmd_abbr()

bool cmd_abbr ( const struct config * config,
const char *const * argv )
static

Implements the abbr command.

◆ cmd_bard()

bool cmd_bard ( const struct config * config,
const char *const * argv )
static

Implements the bard command.

◆ cmd_damn()

bool cmd_damn ( const struct config * config,
const char *const * argv )
static

Implements the damn command.

◆ cmd_dlpo()

bool cmd_dlpo ( const struct config * config,
const char *const * argv )
static

Implements the dlpo command.

Here is the call graph for this function:

◆ cmd_help()

bool cmd_help ( const struct config * config,
const char *const * argv )
static

Implements the help command.

Here is the call graph for this function:

◆ cmd_parl()

bool cmd_parl ( const struct config * config,
const char *const * argv )
static

Implements the parl command.

◆ cmd_ping()

bool cmd_ping ( const struct config * config,
const char *const * argv )
static

Implements the ping command.

◆ cmd_stats()

bool cmd_stats ( const struct config * config,
const char *const * argv )
static

Implements the stats command.

Here is the call graph for this function:

◆ cmd_tr()

bool cmd_tr ( const struct config * config,
const char *const * argv )
static

Implements the tr command.

Here is the call graph for this function:

◆ cmd_wikt()

bool cmd_wikt ( const struct config * config,
const char *const * argv )
static

Implements the wikt command.

Here is the call graph for this function:

◆ cmd_word()

bool cmd_word ( const struct config * config,
const char *const * argv )
static

Implements the word command.

◆ config_destroy()

bool config_destroy ( struct config * config)
static

Destructs config.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ config_init()

void config_init ( struct config * config)
static

Initializes values in config.

Here is the caller graph for this function:

◆ config_init_numeraria()

bool config_init_numeraria ( struct config * config)
static

Initializes values in config.

Here is the caller graph for this function:

◆ config_record_command()

bool config_record_command ( const struct config * config,
const char *const * argv )
static

Records that a command will be executed, along with its arguments.

Here is the caller graph for this function:

◆ handle_cmd()

bool handle_cmd ( const struct config * config,
const char *const * argv )
static

Handles a command passed via the command line.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_file()

bool handle_file ( const struct config * config,
FILE * f )
static

Handles commands read as lines from a file.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int argc,
const char *const * argv )

Program entry point.

Here is the call graph for this function:

◆ parse_args()

bool parse_args ( int argc,
char *const ** argv,
struct config * config )
static

Parses command-line arguments and fills config.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stats_file()

bool stats_file ( const struct config * config)
static

Print stats from local file.

Here is the caller graph for this function:

◆ stats_increment()

bool stats_increment ( const struct config * config,
uint8_t opt )
static

Increments the count on wikt and dlpo lookups.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stats_numeraria()

bool stats_numeraria ( const struct config * config)
static

Print stats from numeraria.

Here is the caller graph for this function:

◆ str_to_args()

bool str_to_args ( char * str,
size_t max_args,
char ** argv )
static

Breaks string into space-separated parts.

Parameters
strInput string, modified to include null terminators.
max_argsMaximum length for argv, including the null terminator.
argvPopulated with pointers to the null-terminated strings in str.
Returns
false if str contains more than max_args
Here is the caller graph for this function:

◆ usage()

void usage ( FILE * f)
static

Prints a usage message.

Here is the caller graph for this function:

Variable Documentation

◆ CMD_NAME

const char* CMD_NAME = NULL

Set while processing a command.

Command name, used as a second prefix for log messages if non-NULL.

◆ COMMANDS

struct mtrix_cmd COMMANDS[]
Initial value:
= {
{0x00005979ab, "tr", cmd_tr},
{0x017c93ee3c, "abbr", cmd_abbr},
{0x017c94785e, "bard", cmd_bard},
{0x017c959085, "damn", cmd_damn},
{0x017c95bfb4, "dlpo", cmd_dlpo},
{0x017c97d2ee, "help", cmd_help},
{0x017c9c25b4, "parl", cmd_parl},
{0x017c9c4733, "ping", cmd_ping},
{0x017ca01d84, "wikt", cmd_wikt},
{0x017ca037e1, "word", cmd_word},
{0x3110614a14, "stats", cmd_stats},
{0},
}
static bool cmd_abbr(const struct config *config, const char *const *argv)
Implements the abbr command.
Definition main.c:440
static bool cmd_ping(const struct config *config, const char *const *argv)
Implements the ping command.
Definition main.c:416
static bool cmd_wikt(const struct config *config, const char *const *argv)
Implements the wikt command.
Definition main.c:842
static bool cmd_parl(const struct config *config, const char *const *argv)
Implements the parl command.
Definition main.c:550
static bool cmd_tr(const struct config *config, const char *const *argv)
Implements the tr command.
Definition main.c:891
static bool cmd_help(const struct config *config, const char *const *argv)
Implements the help command.
Definition main.c:408
static bool cmd_bard(const struct config *config, const char *const *argv)
Implements the bard command.
Definition main.c:673
static bool cmd_word(const struct config *config, const char *const *argv)
Implements the word command.
Definition main.c:424
static bool cmd_damn(const struct config *config, const char *const *argv)
Implements the damn command.
Definition main.c:507
static bool cmd_stats(const struct config *config, const char *const *argv)
Implements the stats command.
Definition main.c:991
static bool cmd_dlpo(const struct config *config, const char *const *argv)
Implements the dlpo command.
Definition main.c:805

Maps a command name to the function that handles it.

Terminated by a {NULL, NULL} entry.

◆ PROG_NAME

const char* PROG_NAME = NULL

To be filled by argv[0] later, for logging.

Program name, used as a prefix for log messages if non-NULL.