~ubuntu-branches/ubuntu/edgy/lynx/edgy

« back to all changes in this revision

Viewing changes to src/LYTraversal.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-09-16 12:14:10 UTC
  • Revision ID: james.westby@ubuntu.com-20040916121410-cz1gu92c4nqfeyrg
Tags: upstream-2.8.5
ImportĀ upstreamĀ versionĀ 2.8.5

Show diffs side-by-side

added added

removed removed

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