1 /*
2  * $LynxId: LYTraversal.c,v 1.30 2010/09/24 22:57:01 tom Exp $
3  */
4 #include <HTUtils.h>
5 #include <LYGlobalDefs.h>
6 #include <LYUtils.h>
7 #include <LYClean.h>
8 #include <LYCurses.h>
9 #include <LYStrings.h>
10 #include <LYTraversal.h>
11 
12 #include <LYexit.h>
13 #include <LYLeaks.h>
14 
15 /* routines to handle special traversal feature */
16 
final_perror(const char * msg,int clean_flag)17 static void final_perror(const char *msg, int clean_flag)
18 {
19     int saved_errno = errno;
20 
21     if (LYCursesON) {
22 	if (clean_flag)
23 	    cleanup();
24 	else
25 	    stop_curses();
26     }
27     set_errno(saved_errno);
28     perror(msg);
29 }
30 
exit_with_perror(const char * msg)31 static void exit_with_perror(const char *msg)
32 {
33     final_perror(msg, TRUE);
34     exit_immediately(EXIT_FAILURE);
35 }
36 
lookup_link(char * target)37 BOOLEAN lookup_link(char *target)
38 {
39     FILE *ifp;
40     char *buffer = NULL;
41     char *line = NULL;
42     int result = FALSE;
43 
44     if ((ifp = fopen(TRAVERSE_FILE, TXT_R)) == NULL) {
45 	if ((ifp = LYNewTxtFile(TRAVERSE_FILE)) == NULL) {
46 	    exit_with_perror(CANNOT_OPEN_TRAV_FILE);
47 	} else {
48 	    LYCloseOutput(ifp);
49 	    return (FALSE);
50 	}
51     }
52 
53     HTSprintf0(&line, "%s\n", target);
54 
55     while (LYSafeGets(&buffer, ifp) != NULL) {
56 	if (STREQ(line, buffer)) {
57 	    result = TRUE;
58 	    break;
59 	}
60     }				/* end while */
61     FREE(line);
62     FREE(buffer);
63 
64     LYCloseInput(ifp);
65     return (BOOL) (result);
66 }
67 
add_to_table(char * target)68 void add_to_table(char *target)
69 {
70 
71     FILE *ifp;
72 
73     if ((ifp = LYAppendToTxtFile(TRAVERSE_FILE)) == NULL) {
74 	exit_with_perror(CANNOT_OPEN_TRAV_FILE);
75     }
76 
77     fprintf(ifp, "%s\n", target);
78 
79     LYCloseOutput(ifp);
80 }
81 
add_to_traverse_list(char * fname,char * prev_link_name)82 void add_to_traverse_list(char *fname, char *prev_link_name)
83 {
84 
85     FILE *ifp;
86 
87     if ((ifp = LYAppendToTxtFile(TRAVERSE_FOUND_FILE)) == NULL) {
88 	exit_with_perror(CANNOT_OPEN_TRAF_FILE);
89     }
90 
91     fprintf(ifp, "%s\t%s\n", fname, prev_link_name);
92 
93     LYCloseOutput(ifp);
94 }
95 
dump_traversal_history(void)96 void dump_traversal_history(void)
97 {
98     int x;
99     FILE *ifp;
100 
101     if (nhist <= 0)
102 	return;
103 
104     if ((ifp = LYAppendToTxtFile(TRAVERSE_FILE)) == NULL) {
105 	final_perror(CANNOT_OPEN_TRAV_FILE, FALSE);
106 	return;
107     }
108 
109     fprintf(ifp, "\n\n%s\n\n\t    %s\n\n",
110 	    TRAV_WAS_INTERRUPTED,
111 	    gettext("here is a list of the history stack so that you may rebuild"));
112 
113     for (x = nhist - 1; x >= 0; x--) {
114 	fprintf(ifp, "%s\t%s\n", HDOC(x).title, HDOC(x).address);
115     }
116 
117     LYCloseOutput(ifp);
118 }
119 
add_to_reject_list(char * target)120 void add_to_reject_list(char *target)
121 {
122 
123     FILE *ifp;
124 
125     CTRACE((tfp, "add_to_reject_list(%s)\n", target));
126 
127     if ((ifp = LYAppendToTxtFile(TRAVERSE_REJECT_FILE)) == NULL) {
128 	exit_with_perror(CANNOT_OPEN_REJ_FILE);
129     }
130 
131     fprintf(ifp, "%s\n", target);
132 
133     LYCloseOutput(ifp);
134 }
135 
136 /* there need not be a reject file, so if it doesn't open, just return
137    FALSE, meaning "target not in reject file" If the last character in
138    a line in a reject file is "*", then also reject if target matches up to
139    that point in the string
140    Blank lines are ignored
141    Lines that contain just a * are allowed, but since they mean "reject
142    everything" it shouldn't come up much!
143  */
144 
lookup_reject(char * target)145 BOOLEAN lookup_reject(char *target)
146 {
147     FILE *ifp;
148     char *buffer = NULL;
149     char *line = NULL;
150     size_t len;
151     int result = FALSE;
152 
153     if ((ifp = fopen(TRAVERSE_REJECT_FILE, TXT_R)) == NULL) {
154 	return (FALSE);
155     }
156 
157     HTSprintf0(&line, "%s", target);
158 
159     while (LYSafeGets(&buffer, ifp) != NULL && !result) {
160 	LYTrimTrailing(buffer);
161 	len = strlen(buffer);
162 	if (len != 0) {		/* if not an empty line */
163 	    if (buffer[len - 1] == '*') {
164 		/* if last char is * and the rest of the chars match */
165 		if ((len == 1) || (StrNCmp(line, buffer, len - 1) == 0)) {
166 		    result = TRUE;
167 		}
168 	    } else {
169 		if (STREQ(line, buffer)) {
170 		    result = TRUE;
171 		}
172 	    }
173 	}
174     }				/* end while loop over the file */
175     FREE(buffer);
176     FREE(line);
177 
178     LYCloseInput(ifp);
179 
180     CTRACE((tfp, "lookup_reject(%s) -> %d\n", target, result));
181     return (BOOL) (result);
182 }
183