Minor style change
[dragonfly.git] / usr.sbin / i4b / isdntel / main.c
blob45c54a31e7b81d3c99e6a68c9be2e2c4d5e8ee9a
1 /*
2 * Copyright (c) 1997, 1999 Hellmuth Michaelis. All rights reserved.
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.
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.
25 *---------------------------------------------------------------------------
27 * isdntel - isdn4bsd telephone answering machine support
28 * ======================================================
30 * $Id: main.c,v 1.12 1999/12/13 21:25:26 hm Exp $
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 $
35 * last edit-date: [Mon Dec 13 21:54:26 1999]
37 *----------------------------------------------------------------------------*/
39 #define MAIN
40 #include <locale.h>
41 #include "defs.h"
42 #include "alias.h"
44 static void usage( void );
46 static int top_dis = 0;
47 static int bot_dis = 0;
48 static int cur_pos_scr = 0;
50 static void makecurrent(int cur_pos, struct onefile *cur_file, int cold);
52 /*---------------------------------------------------------------------------*
53 * program entry
54 *---------------------------------------------------------------------------*/
55 int
56 main(int argc, char **argv)
58 int i;
59 int kchar;
61 char *spooldir = SPOOLDIR;
62 char *playstring = PLAYCMD;
63 char *aliasfile = ALIASFILE;
64 int rrtimeout = REREADTIMEOUT;
66 extern char *optarg;
68 setlocale( LC_ALL, "");
70 while ((i = getopt(argc, argv, "a:d:p:t:")) != -1)
72 switch (i)
74 case 'a':
75 aliasfile = optarg;
76 break;
78 case 'd':
79 spooldir = optarg;
80 break;
82 case 'p':
83 playstring = optarg;
84 break;
86 case 't':
87 if(isdigit(*optarg))
89 rrtimeout = strtoul(optarg, NULL, 10);
91 else
93 usage();
95 break;
97 case '?':
98 default:
99 usage();
100 break;
104 if(rrtimeout < 10)
105 rrtimeout = 10;
107 if((chdir(spooldir)) != 0)
108 fatal("cannot change directory to spooldir %s!", spooldir);
110 init_alias(aliasfile);
112 init_screen();
114 init_files(0);
116 /* go into loop */
118 for (;;)
120 fd_set set;
121 struct timeval timeout;
123 FD_ZERO(&set);
124 FD_SET(STDIN_FILENO, &set);
125 timeout.tv_sec = rrtimeout;
126 timeout.tv_usec = 0;
128 /* if no char is available within timeout, reread spool */
130 if((select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout)) <= 0)
132 reread();
133 continue;
136 kchar = wgetch(main_w); /* get char */
138 switch (kchar)
140 case CR:
141 case LF:
142 #ifdef KEY_ENTER
143 case KEY_ENTER:
144 #endif
145 do_menu();
146 break;
148 case KEY_UP: /* up-move cursor */
149 if(cur_file && cur_file->prev)
151 cur_file = cur_file->prev;
152 cur_pos--;
154 break;
157 case TAB:
158 case KEY_DOWN: /* down-move cursor */
159 if(cur_file && cur_file->next)
161 cur_file = cur_file->next;
162 cur_pos++;
164 break;
166 case KEY_HOME: /* move cursor to first dir */
167 break;
169 case KEY_LL: /* move cursor to last file */
170 break;
172 case CNTRL_D:
173 do_quit(0);
174 break;
176 case CNTRL_L: /* refresh */
177 touchwin(curscr);
178 wrefresh(curscr);
179 break;
182 makecurrent(cur_pos, cur_file, 0);
185 do_quit(0);
187 return(0);
190 /*---------------------------------------------------------------------------*
191 * handle horizontal selection bar movement
192 *---------------------------------------------------------------------------*/
193 static void
194 makecurrent(int cur_pos, struct onefile *cur_file, int cold)
196 static int lastpos;
197 static struct onefile *lastfile;
198 char buffer[256];
200 /* un-higlight current horizontal bar */
202 if(!cold && lastfile && cur_file)
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, "");
211 wattroff(main_w, A_REVERSE);
212 mvwprintw(main_w, lastpos, 0, "%s", buffer);
213 wattroff(main_w, A_REVERSE);
216 if(cur_file == NULL)
218 lastpos = cur_pos_scr;
219 lastfile = cur_file;
220 return;
223 /* have to scroll up or down ? */
225 if(cur_pos >= bot_dis)
227 /* scroll up */
229 wscrl(main_w, 1);
231 bot_dis++;
232 top_dis++;
233 cur_pos_scr = LINES-START_O-3;
235 else if(cur_pos < top_dis)
237 /* scroll down */
239 wscrl(main_w, -1);
241 bot_dis--;
242 top_dis--;
243 cur_pos_scr = 0;
245 else
247 cur_pos_scr = cur_pos - top_dis;
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, "");
257 wattron(main_w, A_REVERSE);
258 mvwprintw(main_w, cur_pos_scr, 0, "%s", buffer);
259 wattroff(main_w, A_REVERSE);
261 lastpos = cur_pos_scr;
262 lastfile = cur_file;
264 wrefresh(main_w);
267 /*---------------------------------------------------------------------------*
268 * exit program
269 *---------------------------------------------------------------------------*/
270 void
271 do_quit(int exitval)
273 move(LINES-1, 0);
274 clrtoeol();
275 refresh();
276 endwin();
277 exit(exitval);
280 /*---------------------------------------------------------------------------*
281 * usage display and exit
282 *---------------------------------------------------------------------------*/
283 static void
284 usage(void)
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);
297 /*---------------------------------------------------------------------------*
298 * fatal error exit
299 *---------------------------------------------------------------------------*/
300 void
301 fatal(char *fmt, ...)
303 va_list ap;
305 va_start(ap, fmt);
307 if(curses_ready)
309 move(LINES-1, 0);
310 clrtoeol();
311 refresh();
312 endwin();
315 fprintf(stderr, "\nFatal error: ");
316 vfprintf(stderr, fmt, ap);
317 fprintf(stderr, "\n\n");
319 va_end(ap);
321 exit(1);
324 /*---------------------------------------------------------------------------*
325 * error printing
326 *---------------------------------------------------------------------------*/
327 void
328 error(char *fmt, ...)
330 va_list ap;
332 va_start(ap, fmt);
334 if(curses_ready)
336 wprintw(main_w, "ERROR: ");
337 vwprintw(main_w, fmt, ap);
338 wprintw(main_w, "\n");
339 wrefresh(main_w);
341 else
343 fprintf(stderr, "ERROR: ");
344 vfprintf(stderr, fmt, ap);
345 fprintf(stderr, "\n");
348 va_end(ap);
351 /*---------------------------------------------------------------------------*
352 * read files and fill display
353 *---------------------------------------------------------------------------*/
354 void
355 init_files(int inipos)
357 int i;
359 nofiles = fill_list();
361 top_dis = 0;
362 bot_dis = 0;
364 cur_file = first;
366 cur_pos = 0;
367 cur_pos_scr = 0;
369 if(nofiles == 0)
370 return;
372 for(i=0; (i < nofiles) && (i < (LINES-START_O-2)); i++)
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);
380 bot_dis++;
382 if((cur_file = cur_file->next) == NULL)
383 break;
386 cur_file = first;
388 if(inipos)
390 for(i=0; i < inipos; i++)
392 if(cur_file->next != NULL)
393 cur_file = cur_file->next;
394 else
395 break;
398 makecurrent(cur_pos, cur_file, 1);
401 /* EOF */