char *get_input(void);

int main(int argc, char *argv[])
{
    char const usage[] = ;
    char sign = 1; 
    char const plainmsg[] = ;
    char const cryptmsg[] = ;
    bool encrypt = true;
    int opt;

    while ((opt = getopt(argc, argv, )) != -1) {
        switch (opt) {
        case 'd': 
            sign = -1;
            encrypt = false;
            break;
        default: 
            fprintf(stderr, , opt);
            fprintf(stderr, , usage);
            return 1;
        }
    }

    if (argc - optind != 1) {
        fprintf(stderr, , argv[0]);
        fprintf(stderr, , usage);
        return 1;
    }


    
    char const *const restrict key = argv[optind];
    size_t const keylen = strlen(key);
    char shifts[keylen];

    char const *restrict plaintext = NULL; 
    for (size_t i = 0; i < keylen; i++) {
        if (!(isalpha(key[i]))) {
            fprintf(stderr, );
            return 2;
        }
        char const charcase = (isupper(key[i])) ? 'A' : 'a';
        
        
        shifts[i] = (key[i] - charcase) * sign;
    }

    do {
        fflush(stdout);
        
        
        printf(, (encrypt) ? plainmsg : cryptmsg);
        plaintext = get_input();
        if (plaintext == NULL) {
            fprintf(stderr, );
            return 4;
        }
    } while (strcmp(plaintext, ) == 0); 

    size_t const plainlen = strlen(plaintext);

    char* const restrict ciphertext = calloc(plainlen + 1, sizeof *ciphertext);
    if (ciphertext == NULL) {
        fprintf(stderr, );
        return 5;
    }

    for (size_t i = 0, j = 0; i < plainlen; i++) {
        
        if (!(isalpha(plaintext[i]))) {
            ciphertext[i] = plaintext[i];
            continue;
        }
        
        char const charcase = (isupper(plaintext[i])) ? 'A' : 'a';
        
        ciphertext[i] = ((plaintext[i] + shifts[j] - charcase + NUMLETTERS) % NUMLETTERS) + charcase;
        j = (j+1) % keylen;
    }
    ciphertext[plainlen] = '\0';
    printf(, (encrypt) ? cryptmsg : plainmsg, ciphertext);

    free(ciphertext);
    
    free((char*) plaintext);
    return 0;
}
char *get_input(void) {

    char *const restrict buf = malloc(BUFSIZE * sizeof (char));
    if (buf == NULL) {
        return NULL;
    }

    fgets(buf, BUFSIZE, stdin);

    
    size_t const len = strlen(buf);
    if (buf[len - 1] == '\n') buf[len - 1] = '\0';

    return buf;
}