Skip to content

Commit ce0f201

Browse files
committed
Debug EOL issue with heading
Found that getline pulls in \n which messes up the curese menus - replaced \n with \0. Added tests in for menu headings that are too long.
1 parent aa9473a commit ce0f201

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

menu.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ void helpCallback(newtComponent co, void * tag) {
152152

153153
/*BOTH - Menu file related settings*/
154154
FILE * menu_file;
155-
char menu_heading[80];
156-
char menu_description[80];
155+
#define MENU_HEADING_LENGTH 80
156+
char menu_heading[MENU_HEADING_LENGTH];
157+
#define MENU_DESC_LENGTH 80
158+
char menu_description[MENU_DESC_LENGTH];
157159
char * file_name;
158160

159161
/* BOTH - Variables */
@@ -200,12 +202,34 @@ int main(int argc, char * argv[]) {
200202

201203

202204
/*Read in first two heading lines from file and then read*/
203-
/*the rest into our array of structures*/
204-
getline(&line, &len, menu_file);
205-
strcpy(menu_heading, line);
206-
getline(&line, &len, menu_file);
207-
strcpy(menu_description, line);
208-
205+
/*the rest into our array of structures - doesn't rely on code path*/
206+
/*Truncate them as they don't seem to truncate properly */
207+
208+
/* Menu Heading */
209+
nread=getline(&line, &len, menu_file);
210+
if (nread-1 > MENU_HEADING_LENGTH) {
211+
printf("\nMenu Heading is too long (needs to be less than %d characters it is %ld characters long.)\n", MENU_HEADING_LENGTH, nread-1);
212+
printf(" - Description: %s\n", line);
213+
usage(argv[0]);
214+
} else {
215+
/* Store the description away - replace the \n character brought in by getline*/
216+
strcpy(menu_heading, line);
217+
menu_heading[nread-1] = '\0';
218+
}
219+
220+
/* Menu Description */
221+
nread=getline(&line, &len, menu_file);
222+
if (nread-1 > MENU_DESC_LENGTH) {
223+
printf("\nMenu Desicription is too long (needs to be less than %d characters it is %ld characters long.)\n", MENU_DESC_LENGTH, nread-1);
224+
printf(" - Description: %s\n", line);
225+
usage(argv[0]);
226+
} else {
227+
/* Store the description away - replace the \n character brought in by getline*/
228+
strcpy(menu_description, line);
229+
menu_description[nread-1] = '\0';
230+
}
231+
232+
209233
/* Process the rest of the file as options and commands */
210234
/* Process the menu_file */
211235
count=0;
@@ -224,9 +248,9 @@ int main(int argc, char * argv[]) {
224248
printf(" - Description: %s\n", line);
225249
usage(argv[0]);
226250
} else {
227-
228-
/* Store the description away */
251+
/* Store the description away - replace the \n character brought in by getline*/
229252
strcpy(menu[count].description,line);
253+
menu[count].description[nread-1] = '\0';
230254
}
231255
}
232256

@@ -243,8 +267,9 @@ int main(int argc, char * argv[]) {
243267
printf(" - Command: %s\n", line);
244268
usage(argv[0]);
245269
} else {
246-
/* Store the command away */
270+
/* Store the command away - replace the \n character brought in by getline */
247271
strcpy(menu[count].command,line);
272+
menu[count].command[nread-1] = '\0';
248273
}
249274
}
250275

menufile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Test Menu Heading - Menu 1
2+
Test Menu Description - Menu 1
3+
List
4+
clear;ls -l;sleep 10;clear
5+
Disk Usage
6+
./df.sh
7+
Menu 2
8+
clear;./menu -c menufile2

0 commit comments

Comments
 (0)