Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
HarveyHunt committed Dec 6, 2014
2 parents 5a05f8c + d8e4dfd commit bae095e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ COMPILE_FLAGS = -std=c99 -Wall -Wextra
# Additional release-specific flags
RCOMPILE_FLAGS = -D NDEBUG
# Additional debug-specific flags
DCOMPILE_FLAGS = -g
DCOMPILE_FLAGS = -g3
# Add additional include paths
INCLUDES = -I $(SRC_PATH)/
# General linker settings
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cottage
[![Build Status](https://travis-ci.org/HarveyHunt/cottage.svg?branch=develop)](https://travis-ci.org/HarveyHunt/cottage)


###Use howm's commands and operators through a UNIX socket.
###Use howm's commands, operators and set configuration values through a UNIX socket.

Contents
========
Expand All @@ -29,10 +29,24 @@ Configuration is extremely minimal and is done from within the cottage source fi

## Usage

Cottage should be used in the following manner:
Changing howm's config value is done in the following manner:

```
cottage [command/operator] <args>
cottage -c config_var value
```

Calling one of howm's functions is done in the following manner:

```
cottage -f function_name <args>
```

It is possible to call operators using cottage, but it requires you call functions to set the operator, the count and finally the motion. Here is an example:

```
cottage -f op_kill
cottage -f set_count 2
cottage -f motion w
```

## Errors
Expand Down
71 changes: 55 additions & 16 deletions cottage.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,58 @@
#include <ctype.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>

#define SOCK_PATH "/tmp/howm"
#define BUF_SIZE 1024

/* The errors (or lack of) that could be sent back by howm. */
enum ipc_errs { IPC_ERR_NONE, IPC_ERR_SYNTAX, IPC_ERR_ALLOC, IPC_ERR_NO_CMD, IPC_ERR_TOO_MANY_ARGS,
IPC_ERR_TOO_FEW_ARGS, IPC_ERR_ARG_NOT_INT, IPC_ERR_ARG_TOO_LARGE };
enum ipc_errs { IPC_ERR_NONE, IPC_ERR_SYNTAX, IPC_ERR_ALLOC, IPC_ERR_NO_FUNC, IPC_ERR_TOO_MANY_ARGS,
IPC_ERR_TOO_FEW_ARGS, IPC_ERR_ARG_NOT_INT, IPC_ERR_ARG_TOO_LARGE, IPC_ERR_UNKNOWN_TYPE };
enum msg_type { MSG_FUNCTION = 1, MSG_CONFIG };

static void usage(void);

/* Send a command to howm and wait for its reply. */
int main(int argc, char *argv[])
{
struct sockaddr_un addr;
int sock, len = 0, off = 0, n = 0;
char data[BUF_SIZE];
int ret, rec;
int ret, rec, ch, type = 0;

if (argc < 2)
usage();

if (argc < 2) {
printf("usage: cottage <command> [<args>]\n");
return EXIT_FAILURE;
while ((ch = getopt(argc, argv, "cf")) != -1) {
switch (ch) {
case 'c':
if (type)
usage();
else
type = MSG_CONFIG;
break;
case 'f':
if (type)
usage();
else
type = MSG_FUNCTION;
break;
default:
usage();
}
}

argc--, argv++;
if (!type)
usage();

argc -= 2;
argv += 2;

memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", SOCK_PATH);
Expand All @@ -41,6 +68,9 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}

n = snprintf(data, sizeof(data) - (2 * sizeof(char)), "%c%c", type, 0);
len += n;
off += n;
for (; argc > 0 && sizeof(data) - off > 0; off += n, argc--, argv++) {
n = snprintf(data + off, sizeof(data) - off, "%s%c", *argv, 0);
len += n;
Expand All @@ -57,32 +87,41 @@ int main(int argc, char *argv[])

switch (ret) {
case IPC_ERR_SYNTAX:
printf("Invalid syntax.\n");
fprintf(stderr, "Invalid syntax.\n");
break;
case IPC_ERR_ALLOC:
printf("Couldn't allocate memory to store args.\n");
fprintf(stderr, "Couldn't allocate memory to store args.\n");
break;
case IPC_ERR_NO_CMD:
printf("No such command.\n");
case IPC_ERR_NO_FUNC:
fprintf(stderr, "No such function.\n");
break;
case IPC_ERR_TOO_MANY_ARGS:
printf("Too many args.\n");
fprintf(stderr, "Too many args.\n");
break;
case IPC_ERR_TOO_FEW_ARGS:
printf("Too few args.\n");
fprintf(stderr, "Too few args.\n");
break;
case IPC_ERR_ARG_NOT_INT:
printf("Argument wasn't an int\n");
fprintf(stderr, "Argument wasn't an int\n");
break;
case IPC_ERR_ARG_TOO_LARGE:
printf("Argument was too large\n");
fprintf(stderr, "Argument was too large\n");
break;
case IPC_ERR_UNKNOWN_TYPE:
fprintf(stderr, "Unknown type of message\n");
break;
}

if (close(sock) == -1) {
printf("Failed to close socket.\n");
perror("Failed to close socket.\n");
exit(EXIT_FAILURE);
}

return ret;
}

void usage(void)
{
fprintf(stderr, "usage: cottage -f|-c <args>\n");
exit(EXIT_FAILURE);
}

0 comments on commit bae095e

Please sign in to comment.