binutils/ld: Don't add /usr/lib to the library search path twice.
[dragonfly.git] / usr.sbin / i4b / isdntel / main.c
1 /*
2  * Copyright (c) 1997, 1999 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      isdntel - isdn4bsd telephone answering machine support
28  *      ======================================================
29  *
30  *      $Id: main.c,v 1.12 1999/12/13 21:25:26 hm Exp $ 
31  *
32  * $FreeBSD: src/usr.sbin/i4b/isdntel/main.c,v 1.8.2.1 2001/08/01 17:45:07 obrien Exp $
33  * $DragonFly: src/usr.sbin/i4b/isdntel/main.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
34  *
35  *      last edit-date: [Mon Dec 13 21:54:26 1999]
36  *
37  *----------------------------------------------------------------------------*/
38
39 #define MAIN
40 #include <locale.h>
41 #include "defs.h"
42 #include "alias.h"
43
44 static void usage( void );
45
46 static int top_dis = 0;
47 static int bot_dis = 0;
48 static int cur_pos_scr = 0;
49
50 static void makecurrent(int cur_pos, struct onefile *cur_file, int cold);
51
52 /*---------------------------------------------------------------------------*
53  *      program entry
54  *---------------------------------------------------------------------------*/
55 int
56 main(int argc, char **argv)
57 {
58         int i;
59         int kchar;
60         
61         char *spooldir = SPOOLDIR;
62         char *playstring = PLAYCMD;
63         char *aliasfile = ALIASFILE;
64         int rrtimeout = REREADTIMEOUT;
65         
66         extern char *optarg;    
67
68         setlocale( LC_ALL, "");
69         
70         while ((i = getopt(argc, argv, "a:d:p:t:")) != -1)
71         {
72                 switch (i)
73                 {
74                         case 'a':
75                                 aliasfile = optarg;
76                                 break;
77
78                         case 'd':
79                                 spooldir = optarg;
80                                 break;
81
82                         case 'p':
83                                 playstring = optarg;
84                                 break;
85
86                         case 't':
87                                 if(isdigit(*optarg))
88                                 {
89                                         rrtimeout = strtoul(optarg, NULL, 10);
90                                 }
91                                 else
92                                 {
93                                         usage();
94                                 }
95                                 break;
96
97                         case '?':
98                         default:
99                                 usage();
100                                 break;
101                 }
102         }
103
104         if(rrtimeout < 10)
105                 rrtimeout = 10;
106
107         if((chdir(spooldir)) != 0)
108                 fatal("cannot change directory to spooldir %s!", spooldir);
109
110         init_alias(aliasfile);
111         
112         init_screen();
113
114         init_files(0);
115         
116         /* go into loop */
117
118         for (;;)
119         {
120                 fd_set set;
121                 struct timeval timeout;
122
123                 FD_ZERO(&set);
124                 FD_SET(STDIN_FILENO, &set);
125                 timeout.tv_sec = rrtimeout;
126                 timeout.tv_usec = 0;
127
128                 /* if no char is available within timeout, reread spool */
129                 
130                 if((select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout)) <= 0)
131                 {
132                         reread();
133                         continue;
134                 }
135
136                 kchar = wgetch(main_w);         /* get char */
137
138                 switch (kchar)
139                 {
140                         case CR:
141                         case LF:
142 #ifdef KEY_ENTER
143                         case KEY_ENTER:
144 #endif
145                                 do_menu();
146                                 break;
147
148                         case KEY_UP:    /* up-move cursor */
149                                 if(cur_file && cur_file->prev)
150                                 {
151                                         cur_file = cur_file->prev;
152                                         cur_pos--;
153                                 }
154                                 break;
155
156
157                         case TAB:
158                         case KEY_DOWN:  /* down-move cursor */
159                                 if(cur_file && cur_file->next)
160                                 {
161                                         cur_file = cur_file->next;
162                                         cur_pos++;
163                                 }
164                                 break;
165
166                         case KEY_HOME:  /* move cursor to first dir */
167                                 break;
168
169                         case KEY_LL:    /* move cursor to last file */
170                                 break;
171
172                         case CNTRL_D:
173                                 do_quit(0);
174                                 break;
175
176                         case CNTRL_L:   /* refresh */
177                                 touchwin(curscr);
178                                 wrefresh(curscr);
179                                 break;
180
181                 }
182                 makecurrent(cur_pos, cur_file, 0);
183         }
184
185         do_quit(0);
186         
187         return(0);
188 }
189
190 /*---------------------------------------------------------------------------*
191  *      handle horizontal selection bar movement
192  *---------------------------------------------------------------------------*/
193 static void
194 makecurrent(int cur_pos, struct onefile *cur_file, int cold)
195 {
196         static int lastpos;
197         static struct onefile *lastfile;
198         char buffer[256];
199
200         /* un-higlight current horizontal bar */
201
202         if(!cold && lastfile && cur_file)
203         {
204                 sprintf(buffer, "%s %s %-16s %-16s %-20s %-6s%*s",
205                         lastfile->date, lastfile->time,
206                         lastfile->dstnumber, lastfile->srcnumber,
207                         lastfile->alias == NULL ? "-/-" : lastfile->alias,
208                         lastfile->seconds,
209                         COLS - LAST_POS - 2, "");
210                         
211                 wattroff(main_w, A_REVERSE);
212                 mvwprintw(main_w, lastpos, 0, "%s", buffer);
213                 wattroff(main_w, A_REVERSE);
214         }
215
216         if(cur_file == NULL)
217         {
218                 lastpos = cur_pos_scr;
219                 lastfile = cur_file;
220                 return;
221         }
222                 
223         /* have to scroll up or down ? */
224
225         if(cur_pos >= bot_dis)          
226         {
227                 /* scroll up */
228
229                 wscrl(main_w, 1);
230
231                 bot_dis++;
232                 top_dis++;
233                 cur_pos_scr = LINES-START_O-3;
234         }
235         else if(cur_pos < top_dis)
236         {
237                 /* scroll down */
238
239                 wscrl(main_w, -1);
240
241                 bot_dis--;
242                 top_dis--;
243                 cur_pos_scr = 0;                
244         }
245         else
246         {
247                 cur_pos_scr = cur_pos - top_dis;
248         }               
249
250         sprintf(buffer, "%s %s %-16s %-16s %-20s %-6s%*s",
251                         cur_file->date, cur_file->time,
252                         cur_file->dstnumber, cur_file->srcnumber,
253                         cur_file->alias == NULL ? "-/-" : cur_file->alias,
254                         cur_file->seconds,
255                         COLS - LAST_POS - 2, "");
256                         
257         wattron(main_w, A_REVERSE);
258         mvwprintw(main_w, cur_pos_scr, 0, "%s", buffer);
259         wattroff(main_w, A_REVERSE);
260
261         lastpos = cur_pos_scr;
262         lastfile = cur_file;
263
264         wrefresh(main_w);       
265 }
266
267 /*---------------------------------------------------------------------------*
268  *      exit program
269  *---------------------------------------------------------------------------*/
270 void
271 do_quit(int exitval)
272 {
273         move(LINES-1, 0);
274         clrtoeol();
275         refresh();
276         endwin();
277         exit(exitval);
278 }
279
280 /*---------------------------------------------------------------------------*
281  *      usage display and exit
282  *---------------------------------------------------------------------------*/
283 static void
284 usage(void)
285 {
286         fprintf(stderr, "\n");
287         fprintf(stderr, "isdntel - isdn telephone answering management support utility (version %d.%d.%d)\n", VERSION, REL, STEP);
288         fprintf(stderr, "    usage: isdntel -a <filename> -d <directory> -p <command> -t <timeout>\n");
289         fprintf(stderr, "           -a <filename>   use filename as alias file\n");
290         fprintf(stderr, "           -d <directory>  use directory as spool directory\n");
291         fprintf(stderr, "           -p <command>    specify commandline for play command\n");
292         fprintf(stderr, "           -t <timeout>    spool directory reread timeout in seconds\n");      
293         fprintf(stderr, "\n");
294         exit(1);
295 }
296
297 /*---------------------------------------------------------------------------*
298  *      fatal error exit
299  *---------------------------------------------------------------------------*/
300 void
301 fatal(char *fmt, ...)
302 {
303         va_list ap;
304
305         va_start(ap, fmt);
306
307         if(curses_ready)
308         {       
309                 move(LINES-1, 0);
310                 clrtoeol();
311                 refresh();
312                 endwin();
313         }
314
315         fprintf(stderr, "\nFatal error: ");
316         vfprintf(stderr, fmt, ap);
317         fprintf(stderr, "\n\n");
318                 
319         va_end(ap);
320
321         exit(1);
322 }
323
324 /*---------------------------------------------------------------------------*
325  *      error printing
326  *---------------------------------------------------------------------------*/
327 void
328 error(char *fmt, ...)
329 {
330         va_list ap;
331
332         va_start(ap, fmt);
333
334         if(curses_ready)
335         {
336                 wprintw(main_w, "ERROR: ");
337                 vwprintw(main_w, fmt, ap);
338                 wprintw(main_w, "\n");
339                 wrefresh(main_w);
340         }
341         else
342         {
343                 fprintf(stderr, "ERROR: ");
344                 vfprintf(stderr, fmt, ap);
345                 fprintf(stderr, "\n");
346         }
347                 
348         va_end(ap);
349 }
350
351 /*---------------------------------------------------------------------------*
352  *      read files and fill display
353  *---------------------------------------------------------------------------*/
354 void
355 init_files(int inipos)
356 {
357         int i;
358
359         nofiles = fill_list();
360                 
361         top_dis = 0;
362         bot_dis = 0;
363
364         cur_file = first;
365
366         cur_pos = 0;
367         cur_pos_scr = 0;        
368
369         if(nofiles == 0)
370                 return;
371         
372         for(i=0; (i < nofiles) && (i < (LINES-START_O-2)); i++)
373         {
374                 mvwprintw(main_w, i, 0, "%s %s", cur_file->date, cur_file->time);
375                 mvwprintw(main_w, i, DST_POS, "%s", cur_file->dstnumber);
376                 mvwprintw(main_w, i, SRC_POS, "%s", cur_file->srcnumber);
377                 mvwprintw(main_w, i, ALI_POS,"%s", cur_file->alias == NULL ? "-/-" : cur_file->alias);
378                 mvwprintw(main_w, i, SEC_POS,"%s", cur_file->seconds);
379
380                 bot_dis++;
381
382                 if((cur_file = cur_file->next) == NULL)
383                         break;          
384         }
385         
386         cur_file = first;
387
388         if(inipos)
389         {
390                 for(i=0; i < inipos; i++)
391                 {
392                         if(cur_file->next != NULL)
393                                 cur_file = cur_file->next;
394                         else
395                                 break;
396                 }
397         }
398         makecurrent(cur_pos, cur_file, 1);
399 }
400
401 /* EOF */