summaryrefslogtreecommitdiff
path: root/7ed.c
blob: cd31f342bb7f28d6913b2092f9f07fa22df6a6bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "7ed.h"
#include "input.h"
#include <stdint.h>

#define VERSION 1
#define USAGE "7ed [OPTION] [FILENAME]\n" \
              "\nVALID OPTIONS:\n\n"\
              "    -v   Prints out the version number\n"\
              "    -i   Input file\n"

#define PROGRAM_NAME "7ed"

int main (int argc, char *argv[]) {

    if (argc == 1) {
        fprintf(stderr, "%s: Please provide a file.\n%s\n", argv[0], USAGE);
        return EXIT_FAILURE;
    }

    int i_used = FALSE_7ED; // These flags are to make sure -v and -i dont get used together. It makes the program feel a bit nicer.
    int v_used = FALSE_7ED;

    int opt;
    int returnval;
    char *optarg_copy = NULL; // We will copy optarg to this
    while ((opt = getopt(argc, argv, "i:v")) != -1) {
        switch (opt) {
        case 'i':

            optarg_copy = malloc(strlen(optarg) + 1); // malloc memory for optarg_copy
            strcpy(optarg_copy, optarg); // Copy optarg to optarg_copy
            i_used = TRUE_7ED;

        break;
        case 'v':

            v_used = TRUE_7ED;
        break;

        }
    }
    if (i_used == TRUE_7ED && v_used == TRUE_7ED) { // If both flags are used print usage and exit
        free(optarg_copy);
        fprintf(stderr, "%s", USAGE);
        return EXIT_FAILURE;
    }
    
    if (i_used == TRUE_7ED) { // enter startmode if we used 'i'
        returnval = startmode(optarg_copy);
        if (returnval == 1) {
            free(optarg_copy);
            return EXIT_FAILURE;
        }
        free(optarg_copy);
    }
    if (v_used == TRUE_7ED) { // print version
        fprintf(stdout, "7Editor Version %d\n", VERSION);
    }

    return EXIT_SUCCESS;

}