-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
138 lines (109 loc) · 5.5 KB
/
parser.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//QuickSSH
//Copyright (C) 2023 Jose Phillips ([email protected])
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
#include <stdbool.h>
#define MAX_ALIAS_HOSTNAME_LENGTH 50
void generateGrid(const char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("No entries found.\n");
return;
}
struct HostInfo hosts[MAX_HOSTS];
int hostCount = -1;
for (int i = 0; i < MAX_HOSTS; i++) {
hosts[i].alias[0] = '\0';
hosts[i].hostname[0] = '\0';
hosts[i].username[0] = '\0';
hosts[i].port = 0;
}
char line[MAX_LINE_LENGTH];
char currentAlias[MAX_ALIAS_HOSTNAME_LENGTH] = "";
char LastcurrentAlias[MAX_ALIAS_HOSTNAME_LENGTH] = "";
int maxAliasLength = 0;
int maxHostnameLength = 0;
int maxUsernameLength = 0;
char tempUsername[MAX_ALIAS_HOSTNAME_LENGTH] = "" ;
while (fgets(line, sizeof(line), file) != NULL && hostCount +1 < MAX_HOSTS) {
if (strstr(line, "Host ") != NULL) {
sscanf(line, "Host %s", currentAlias);
int aliasLength = strlen(currentAlias);
if (aliasLength > MAX_ALIAS_HOSTNAME_LENGTH) {
aliasLength = MAX_ALIAS_HOSTNAME_LENGTH;
}
if (aliasLength > maxAliasLength) {
maxAliasLength = aliasLength;
}
if ( LastcurrentAlias != currentAlias){
hostCount++;
}
} else if (strstr(line, "HostName") != NULL) {
sscanf(line, " HostName %s", hosts[hostCount].hostname);
int hostnameLength = strlen(hosts[hostCount].hostname);
if (hostnameLength > MAX_ALIAS_HOSTNAME_LENGTH) {
hostnameLength = MAX_ALIAS_HOSTNAME_LENGTH;
}
if (hostnameLength > maxHostnameLength) {
maxHostnameLength = hostnameLength;
}
} else if (strstr(line, "User") !=NULL) {
sscanf(line, " User %s", tempUsername);
strncpy(hosts[hostCount].username, tempUsername, MAX_ALIAS_HOSTNAME_LENGTH - 1);
hosts[hostCount].username[MAX_ALIAS_HOSTNAME_LENGTH - 1] = '\0';
char tempUsername[MAX_ALIAS_HOSTNAME_LENGTH] = "\0" ;
} else if (strstr(line, "Port") != NULL) {
sscanf(line, " Port %d", &hosts[hostCount].port);
strncpy(hosts[hostCount].alias, currentAlias, MAX_LINE_LENGTH - 1);
hosts[hostCount].alias[MAX_LINE_LENGTH - 1] = '\0';
}
int usernameLength = strlen(hosts[hostCount].username);
if (usernameLength > MAX_ALIAS_HOSTNAME_LENGTH) {
usernameLength = MAX_ALIAS_HOSTNAME_LENGTH;
}
if (usernameLength > maxUsernameLength) {
maxUsernameLength = usernameLength;
}
if (strlen (hosts[hostCount].username) == NULL ){
const char* defaultUsername = getenv("USER");
strncpy(hosts[hostCount].username, defaultUsername, MAX_ALIAS_HOSTNAME_LENGTH - 1);
hosts[hostCount].username[MAX_ALIAS_HOSTNAME_LENGTH - 1] = '\0';
}
}
fclose(file);
// Calculate the total width of the table
int totalWidth = maxAliasLength + maxHostnameLength + maxUsernameLength + 34;
char replacementChar = '-';
char replacementStringAlias[maxAliasLength + 3];
char replacementStringUsername[maxUsernameLength + 3];
memset(replacementStringAlias, replacementChar, maxAliasLength +2);
replacementStringAlias[maxAliasLength +2 ] = '\0';
memset(replacementStringUsername,replacementChar,maxUsernameLength + 2);
replacementStringUsername[maxUsernameLength +2 ] = '\0';
if (hostCount >= 0 ){
// Printing Head of Title
printf("--%-*s%-19.19s%-*s%-9.9s\n", maxAliasLength, replacementStringAlias, "-----------------------", maxUsernameLength, replacementStringUsername, "----------------");
// Printing the parsed data in tabular form
printf("| %-*s | %-15s | %--*s | %-6s |\n", maxAliasLength, "Alias", "Hostname", maxUsernameLength, "Username", "Port");
printf("|%-*s|%-17.17s|%-*s|%-8.8s|\n", maxAliasLength, replacementStringAlias, "---------------------", maxUsernameLength, replacementStringUsername, "----------");
for (int i = 0; i < hostCount +1 ; i++) {
printf("| %-*.*s | %-15.14s | %-*.*s | %-6d |\n", maxAliasLength, maxAliasLength, hosts[i].alias, hosts[i].hostname, maxUsernameLength, maxUsernameLength, hosts[i].username, hosts[i].port);
}
//print bottom of table
printf("--%-*s%-19.19s%-*s%-9.9s\n", maxAliasLength, replacementStringAlias, "-----------------------", maxUsernameLength, replacementStringUsername, "----------------");
}else {
printf("No entries found. \n");
}
}