Skip to content

Commit

Permalink
Rework main(), remove unused headers
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolasburr committed Aug 12, 2017
1 parent 4fdd003 commit 33e22bf
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 61 deletions.
30 changes: 27 additions & 3 deletions include/argv.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,33 @@
#ifndef GIT_STASHD_ARGV_H
#define GIT_STASHD_ARGV_H

#include <stdio.h>
#include <string.h>

int opt_get_index();
int opt_in_array();
#define GIT_STASHD_LOG_FILE "git-stashd.log"
#define GIT_STASHD_LOG_MODE "a+"

#endif
#define GIT_STASHD_OPT_HELP_M "git stashd [OPTIONS]"
#define GIT_STASHD_OPT_HELP_L "--help"
#define GIT_STASHD_OPT_HELP_S "-h"

#define GIT_STASHD_OPT_DAEMON_L "--daemon"
#define GIT_STASHD_OPT_DAEMON_S "-D"

#define GIT_STASHD_OPT_REPOPATH_L "--repository-path"
#define GIT_STASHD_OPT_REPOPATH_S "-R"

#define GIT_STASHD_OPT_INTERVAL_L "--interval"
#define GIT_STASHD_OPT_INTERVAL_S "-I"

#define GIT_STASHD_OPT_COUNT 4

const char* const options[GIT_STASHD_OPT_COUNT];
int pfindent;
size_t size;

int opt_get_index(const char* const option, const char *arr[], size_t size);
int opt_in_array(const char* const option, const char *arr[], size_t size);
void pfusage(void);

#endif /* GIT_STASHD_ARGV_H */
3 changes: 0 additions & 3 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,4 @@
#include <stdlib.h>
#include <unistd.h>

#define GIT_STASHD_LOG_FILE "git-stashd.log"
#define GIT_STASHD_LOG_MODE "a+"

#endif
23 changes: 0 additions & 23 deletions include/usage.h

This file was deleted.

1 change: 1 addition & 0 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
*/

#include "argv.h"
#include "common.h"
#include "daemon.h"

Expand Down
80 changes: 48 additions & 32 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,85 @@
* Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
*/

#include "common.h"
#include "argv.h"
#include "common.h"
#include "daemon.h"
#include "usage.h"
#include "git2.h"

int main (int argc, char *argv[]) {
FILE *fp;
int oindex, pindex;
int opt_index, arg_index, daemonize;
long pid;
char *path;
char *pathname;
struct repo_info *repo_info;

/**
* If the `--help` option was given, display usage details and exit.
*/
if (opt_in_array(GIT_STASHD_USAGE_OPT, argv, argc)) {
if (opt_in_array(GIT_STASHD_OPT_HELP_L, argv, argc) ||
opt_in_array(GIT_STASHD_OPT_HELP_S, argv, argc)) {
pfusage();

exit(EXIT_SUCCESS);
}

/**
* If the `--daemon` option was given:
*
* 1) Look for a `--repository-path` option before starting a daemon. If it was given,
* If the `--daemon` (`-D`) option was given:
*
* 1) Look for a `--repository-path` (`-R`) option before starting a daemon. If it was given,
* validate the option argument is a path to a Git repository.
* 2) If no `--repository-path` option was specified, check the CWD. Alas, if the CWD
* is not a Git repository, emit an error and die.
*
*/
if (opt_in_array("--daemon", argv, argc)) {
if (opt_in_array(GIT_STASHD_OPT_DAEMON_L, argv, argc) ||
opt_in_array(GIT_STASHD_OPT_DAEMON_S, argv, argc)) {
daemonize = 1;
}

// Check for `--repository-path` option and argument
if (opt_in_array(GIT_STASHD_OPT_REPOPATH_L, argv, argc) ||
opt_in_array(GIT_STASHD_OPT_REPOPATH_S, argv, argc)) {

// Check for `--repository-path` option
if (opt_in_array("--repository-path", argv, argc)) {
// Index of `--repository-path` option in `argv`
opt_index = (opt_get_index(GIT_STASHD_OPT_REPOPATH_L, argv, argc) != -1)
? opt_get_index(GIT_STASHD_OPT_REPOPATH_L, argv, argc)
: opt_get_index(GIT_STASHD_OPT_REPOPATH_S, argv, argc);

// `--repository-path` index in `argv`
oindex = opt_get_index("--repository-path", argv, argc);
pindex = (oindex + 1);
path = argv[pindex];
// Index of `--repository-path` option argument in `argv`
arg_index = (opt_index + 1);

// Die if `path` is not a valid directory
if (!is_dir(path)) {
printf("%s is not a valid directory!\n", path);
// Actual pathname string given as the option argument
pathname = argv[arg_index];

exit(EXIT_FAILURE);
} else {
repo_info = (struct repo_info *) malloc(sizeof(struct repo_info));
fp = get_log_file(GIT_STASHD_LOG_FILE, GIT_STASHD_LOG_MODE);
// Die if `pathname` is not a valid directory
if (!is_dir(pathname)) {
printf("%s is not a valid directory!\n", pathname);

repo_info->path = path;
fprintf(fp, "main -> path -> %s\n", path);
exit(EXIT_FAILURE);
}

start_daemon(path, &pid);
write_log_file(GIT_STASHD_LOG_FILE, GIT_STASHD_LOG_MODE);
repo_info = (struct repo_info *) malloc(sizeof(struct repo_info));
fp = get_log_file(GIT_STASHD_LOG_FILE, GIT_STASHD_LOG_MODE);

repo_info->path = path;
repo_info->pid = pid;
repo_info->path = pathname;
fprintf(fp, "main -> pathname -> %s\n", pathname);

fprintf(fp, "main -> pid -> %ld\n", pid);
// start_daemon(pathname, &pid);
// write_log_file(GIT_STASHD_LOG_FILE, GIT_STASHD_LOG_MODE);

free(repo_info);
fclose(fp);
}
}
repo_info->path = pathname;
repo_info->pid = pid;

// fprintf(fp, "main -> pid -> %ld\n", pid);

free(repo_info);
fclose(fp);
}

if (daemonize) {
printf("%d\n", daemonize);
}

return EXIT_SUCCESS;
Expand Down

0 comments on commit 33e22bf

Please sign in to comment.