1 /* This file Copyright 1992 by Clifford A. Adams */
2 /* samain.c
3 *
4 * main working routines (may change later)
5 */
6
7 #include "EXTERN.h"
8 #include "common.h"
9 #ifdef SCAN_ART
10 #include "list.h"
11 #include "hash.h"
12 #include "cache.h"
13 #include "ngdata.h"
14 #include "bits.h"
15 #include "head.h" /* for fetching misc header lines... */
16 #include "util.h"
17 #ifdef SCORE
18 #include "score.h"
19 #endif
20 #include "scan.h"
21 #include "scmd.h"
22 #include "sdisp.h"
23 #include "smisc.h" /* needed? */
24 #include "sorder.h"
25 #include "spage.h"
26 #include "scanart.h"
27 #include "samisc.h"
28 #include "sacmd.h"
29 #include "sadesc.h"
30 #include "sadisp.h"
31 #include "sathread.h"
32 #include "INTERN.h"
33 #include "samain.h"
34
35
36 void
sa_init()37 sa_init()
38 {
39 sa_init_context();
40 if (lastart == 0 || absfirst > lastart)
41 return; /* no articles */
42 if (s_initscreen()) /* If not able to init screen...*/
43 return; /* ...most likely dumb terminal */
44 sa_initmode(); /* mode differences */
45 sa_init_threads();
46 sa_mode_read_elig = FALSE;
47 if (firstart > lastart) /* none unread */
48 sa_mode_read_elig = TRUE; /* unread+read in some situations */
49 if (!sa_initarts()) /* init article array(s) */
50 return; /* ... no articles */
51 #ifdef SCORE
52 #ifdef PENDING
53 if (sa_mode_read_elig) {
54 sc_fill_read = TRUE;
55 sc_fill_max = absfirst - 1;
56 }
57 #endif
58 #endif
59 s_save_context();
60 sa_initialized = TRUE; /* all went well... */
61 }
62
63 void
sa_init_ents()64 sa_init_ents()
65 {
66 sa_num_ents = sa_ents_alloc = 0;
67 sa_ents = (SA_ENTRYDATA*)NULL;
68 }
69
70 void
sa_clean_ents()71 sa_clean_ents()
72 {
73 free(sa_ents);
74 }
75
76 /* returns entry number that was added */
77 long
sa_add_ent(artnum)78 sa_add_ent(artnum)
79 ART_NUM artnum; /* article number to be added */
80 {
81 long cur;
82
83 sa_num_ents++;
84 if (sa_num_ents > sa_ents_alloc) {
85 sa_ents_alloc += 100;
86 if (sa_ents_alloc == 100) { /* newly allocated */
87 /* don't use number 0, just allocate it and skip it */
88 sa_num_ents = 2;
89 sa_ents = (SA_ENTRYDATA*)safemalloc(sa_ents_alloc
90 * sizeof (SA_ENTRYDATA));
91 } else {
92 sa_ents = (SA_ENTRYDATA*)saferealloc((char*)sa_ents,
93 sa_ents_alloc * sizeof (SA_ENTRYDATA));
94 }
95 }
96 cur = sa_num_ents-1;
97 sa_ents[cur].artnum = artnum;
98 sa_ents[cur].subj_thread_num = 0;
99 sa_ents[cur].sa_flags = (char)0;
100 s_order_add(cur);
101 return cur;
102 }
103
104 void
sa_cleanmain()105 sa_cleanmain()
106 {
107 sa_clean_ents();
108
109 sa_mode_zoom = FALSE; /* doesn't survive across groups */
110 /* remove the now-unused scan-context */
111 s_delete_context(sa_scan_context);
112 sa_context_init = FALSE;
113 sa_scan_context = -1;
114 /* no longer "in" article scan mode */
115 sa_mode_read_elig = FALSE; /* the default */
116 sa_in = FALSE;
117 }
118
119 void
sa_growarts(oldlast,last)120 sa_growarts(oldlast,last)
121 long oldlast,last;
122 {
123 int i;
124
125 for (i = oldlast+1; i <= last; i++)
126 (void)sa_add_ent(i);
127 }
128
129 /* Initialize the scan-context to enter article scan mode. */
130 void
sa_init_context()131 sa_init_context()
132 {
133 if (sa_context_init)
134 return; /* already initialized */
135 if (sa_scan_context == -1)
136 sa_scan_context = s_new_context(S_ART);
137 s_change_context(sa_scan_context);
138 }
139
140 bool
sa_initarts()141 sa_initarts()
142 {
143 int a;
144
145 sa_init_ents();
146 /* add all available articles to entry list */
147 for (a = article_first(absfirst); a <= lastart; a = article_next(a)) {
148 if (article_exists(a))
149 (void)sa_add_ent(a);
150 }
151 sa_order_read = sa_mode_read_elig;
152 return TRUE;
153 }
154
155 /* note: initscreen must be called before (for scr_width) */
156 void
sa_initmode()157 sa_initmode()
158 {
159 /* set up screen sizes */
160 sa_set_screen();
161
162 sa_mode_zoom = 0; /* reset zoom */
163 }
164
165 int
sa_mainloop()166 sa_mainloop()
167 {
168 int i;
169
170 #ifdef SCORE
171 /* Eventually, strn will need a variable in score.[ch] set when the
172 * scoring initialization *failed*, so that strn could try to
173 * initialize the scoring again or decide to disallow score operations.)
174 */
175 /* If strn is in score mode but scoring is not initialized,
176 * then try to initialize.
177 * If that fails then strn will just use arrival ordering.
178 */
179 if (!sc_initialized && sa_mode_order == 2) {
180 sc_delay = FALSE; /* yes, actually score... */
181 sc_init(TRUE); /* wait for articles to score */
182 if (!sc_initialized)
183 sa_mode_order = 1; /* arrival order */
184 }
185 #endif
186 /* redraw it *all* */
187 s_ref_all = TRUE;
188 if (s_top_ent < 1)
189 s_top_ent = s_first();
190 i = s_fillpage();
191 if (i == -1 || i == 0) {
192 /* for now just quit if no page could be filled. */
193 return SA_QUIT;
194 }
195 i = s_cmdloop();
196 if (i == SA_READ) {
197 i = SA_NORM;
198 }
199 if (i > 0) {
200 sa_art = sa_ents[i].artnum;
201 return SA_NORM;
202 }
203 /* something else (quit, return, etc...) */
204 return i;
205 }
206
207 /* do something useful until a key is pressed. */
208 void
sa_lookahead()209 sa_lookahead()
210 {
211 #ifdef PENDING
212 #ifdef SCORE
213 sc_lookahead(TRUE,FALSE); /* do resorting now... */
214 #else /* !SCORE */
215 /* consider looking forward from the last article on the page... */
216 ; /* so the function isn't empty */
217 #endif /* SCORE */
218 #else /* !PENDING */
219 ; /* so the function isn't empty */
220 #endif
221 }
222
223 /* Returns first marked entry number, or 0 if no articles are marked. */
224 long
sa_readmarked_elig()225 sa_readmarked_elig()
226 {
227 long e;
228
229 e = s_first();
230 if (!e)
231 return 0L;
232 for ( ; e; e = s_next(e)) {
233 if (!sa_basic_elig(e))
234 continue;
235 if (sa_marked(e))
236 return e;
237 }
238 /* This is possible since the marked articles might not be eligible. */
239 return 0;
240 }
241 #endif /* SCAN */
242