]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/i4b/isdntel/main.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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$
33  *
34  *      last edit-date: [Mon Dec 13 21:54:26 1999]
35  *
36  *----------------------------------------------------------------------------*/
37
38 #define MAIN
39 #include <locale.h>
40 #include <unistd.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         setlocale( LC_ALL, "");
67         
68         while ((i = getopt(argc, argv, "a:d:p:t:")) != -1)
69         {
70                 switch (i)
71                 {
72                         case 'a':
73                                 aliasfile = optarg;
74                                 break;
75
76                         case 'd':
77                                 spooldir = optarg;
78                                 break;
79
80                         case 'p':
81                                 playstring = optarg;
82                                 break;
83
84                         case 't':
85                                 if(isdigit(*optarg))
86                                 {
87                                         rrtimeout = strtoul(optarg, NULL, 10);
88                                 }
89                                 else
90                                 {
91                                         usage();
92                                 }
93                                 break;
94
95                         case '?':
96                         default:
97                                 usage();
98                                 break;
99                 }
100         }
101
102         if(rrtimeout < 10)
103                 rrtimeout = 10;
104
105         if((chdir(spooldir)) != 0)
106                 fatal("cannot change directory to spooldir %s!", spooldir);
107
108         init_alias(aliasfile);
109         
110         init_screen();
111
112         init_files(0);
113         
114         /* go into loop */
115
116         for (;;)
117         {
118                 fd_set set;
119                 struct timeval timeout;
120
121                 FD_ZERO(&set);
122                 FD_SET(STDIN_FILENO, &set);
123                 timeout.tv_sec = rrtimeout;
124                 timeout.tv_usec = 0;
125
126                 /* if no char is available within timeout, reread spool */
127                 
128                 if((select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout)) <= 0)
129                 {
130                         reread();
131                         continue;
132                 }
133
134                 kchar = wgetch(main_w);         /* get char */
135
136                 switch (kchar)
137                 {
138                         case CR:
139                         case LF:
140 #ifdef KEY_ENTER
141                         case KEY_ENTER:
142 #endif
143                                 do_menu();
144                                 break;
145
146                         case KEY_UP:    /* up-move cursor */
147                                 if(cur_file && cur_file->prev)
148                                 {
149                                         cur_file = cur_file->prev;
150                                         cur_pos--;
151                                 }
152                                 break;
153
154
155                         case TAB:
156                         case KEY_DOWN:  /* down-move cursor */
157                                 if(cur_file && cur_file->next)
158                                 {
159                                         cur_file = cur_file->next;
160                                         cur_pos++;
161                                 }
162                                 break;
163
164                         case KEY_HOME:  /* move cursor to first dir */
165                                 break;
166
167                         case KEY_LL:    /* move cursor to last file */
168                                 break;
169
170                         case CNTRL_D:
171                                 do_quit(0);
172                                 break;
173
174                         case CNTRL_L:   /* refresh */
175                                 touchwin(curscr);
176                                 wrefresh(curscr);
177                                 break;
178
179                 }
180                 makecurrent(cur_pos, cur_file, 0);
181         }
182
183         do_quit(0);
184         
185         return(0);
186 }
187
188 /*---------------------------------------------------------------------------*
189  *      handle horizontal selection bar movement
190  *---------------------------------------------------------------------------*/
191 static void
192 makecurrent(int cur_pos, struct onefile *cur_file, int cold)
193 {
194         static int lastpos;
195         static struct onefile *lastfile;
196         char buffer[256];
197
198         /* un-higlight current horizontal bar */
199
200         if(!cold && lastfile && cur_file)
201         {
202                 sprintf(buffer, "%s %s %-16s %-16s %-20s %-6s%*s",
203                         lastfile->date, lastfile->time,
204                         lastfile->dstnumber, lastfile->srcnumber,
205                         lastfile->alias == NULL ? "-/-" : lastfile->alias,
206                         lastfile->seconds,
207                         COLS - LAST_POS - 2, "");
208                         
209                 wattroff(main_w, A_REVERSE);
210                 mvwprintw(main_w, lastpos, 0, "%s", buffer);
211                 wattroff(main_w, A_REVERSE);
212         }
213
214         if(cur_file == NULL)
215         {
216                 lastpos = cur_pos_scr;
217                 lastfile = cur_file;
218                 return;
219         }
220                 
221         /* have to scroll up or down ? */
222
223         if(cur_pos >= bot_dis)          
224         {
225                 /* scroll up */
226
227                 wscrl(main_w, 1);
228
229                 bot_dis++;
230                 top_dis++;
231                 cur_pos_scr = LINES-START_O-3;
232         }
233         else if(cur_pos < top_dis)
234         {
235                 /* scroll down */
236
237                 wscrl(main_w, -1);
238
239                 bot_dis--;
240                 top_dis--;
241                 cur_pos_scr = 0;                
242         }
243         else
244         {
245                 cur_pos_scr = cur_pos - top_dis;
246         }               
247
248         sprintf(buffer, "%s %s %-16s %-16s %-20s %-6s%*s",
249                         cur_file->date, cur_file->time,
250                         cur_file->dstnumber, cur_file->srcnumber,
251                         cur_file->alias == NULL ? "-/-" : cur_file->alias,
252                         cur_file->seconds,
253                         COLS - LAST_POS - 2, "");
254                         
255         wattron(main_w, A_REVERSE);
256         mvwprintw(main_w, cur_pos_scr, 0, "%s", buffer);
257         wattroff(main_w, A_REVERSE);
258
259         lastpos = cur_pos_scr;
260         lastfile = cur_file;
261
262         wrefresh(main_w);       
263 }
264
265 /*---------------------------------------------------------------------------*
266  *      exit program
267  *---------------------------------------------------------------------------*/
268 void
269 do_quit(int exitval)
270 {
271         move(LINES-1, 0);
272         clrtoeol();
273         refresh();
274         endwin();
275         exit(exitval);
276 }
277
278 /*---------------------------------------------------------------------------*
279  *      usage display and exit
280  *---------------------------------------------------------------------------*/
281 static void
282 usage(void)
283 {
284         fprintf(stderr, "\n");
285         fprintf(stderr, "isdntel - isdn telephone answering management support utility (version %d.%d.%d)\n", VERSION, REL, STEP);
286         fprintf(stderr, "    usage: isdntel -a <filename> -d <directory> -p <command> -t <timeout>\n");
287         fprintf(stderr, "           -a <filename>   use filename as alias file\n");
288         fprintf(stderr, "           -d <directory>  use directory as spool directory\n");
289         fprintf(stderr, "           -p <command>    specify commandline for play command\n");
290         fprintf(stderr, "           -t <timeout>    spool directory reread timeout in seconds\n");      
291         fprintf(stderr, "\n");
292         exit(1);
293 }
294
295 /*---------------------------------------------------------------------------*
296  *      fatal error exit
297  *---------------------------------------------------------------------------*/
298 void
299 fatal(char *fmt, ...)
300 {
301         va_list ap;
302
303         va_start(ap, fmt);
304
305         if(curses_ready)
306         {       
307                 move(LINES-1, 0);
308                 clrtoeol();
309                 refresh();
310                 endwin();
311         }
312
313         fprintf(stderr, "\nFatal error: ");
314         vfprintf(stderr, fmt, ap);
315         fprintf(stderr, "\n\n");
316                 
317         va_end(ap);
318
319         exit(1);
320 }
321
322 /*---------------------------------------------------------------------------*
323  *      error printing
324  *---------------------------------------------------------------------------*/
325 void
326 error(char *fmt, ...)
327 {
328         va_list ap;
329
330         va_start(ap, fmt);
331
332         if(curses_ready)
333         {
334                 wprintw(main_w, "ERROR: ");
335                 vwprintw(main_w, fmt, ap);
336                 wprintw(main_w, "\n");
337                 wrefresh(main_w);
338         }
339         else
340         {
341                 fprintf(stderr, "ERROR: ");
342                 vfprintf(stderr, fmt, ap);
343                 fprintf(stderr, "\n");
344         }
345                 
346         va_end(ap);
347 }
348
349 /*---------------------------------------------------------------------------*
350  *      read files and fill display
351  *---------------------------------------------------------------------------*/
352 void
353 init_files(int inipos)
354 {
355         int i;
356
357         nofiles = fill_list();
358                 
359         top_dis = 0;
360         bot_dis = 0;
361
362         cur_file = first;
363
364         cur_pos = 0;
365         cur_pos_scr = 0;        
366
367         if(nofiles == 0)
368                 return;
369         
370         for(i=0; (i < nofiles) && (i < (LINES-START_O-2)); i++)
371         {
372                 mvwprintw(main_w, i, 0, "%s %s", cur_file->date, cur_file->time);
373                 mvwprintw(main_w, i, DST_POS, "%s", cur_file->dstnumber);
374                 mvwprintw(main_w, i, SRC_POS, "%s", cur_file->srcnumber);
375                 mvwprintw(main_w, i, ALI_POS,"%s", cur_file->alias == NULL ? "-/-" : cur_file->alias);
376                 mvwprintw(main_w, i, SEC_POS,"%s", cur_file->seconds);
377
378                 bot_dis++;
379
380                 if((cur_file = cur_file->next) == NULL)
381                         break;          
382         }
383         
384         cur_file = first;
385
386         if(inipos)
387         {
388                 for(i=0; i < inipos; i++)
389                 {
390                         if(cur_file->next != NULL)
391                                 cur_file = cur_file->next;
392                         else
393                                 break;
394                 }
395         }
396         makecurrent(cur_pos, cur_file, 1);
397 }
398
399 /* EOF */