Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / isdn / isdntel / main.c
bloba755c552e71c014723061788ccb3691ec0355fd6
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.7 2009/04/16 05:56:33 lukem Exp $
32 * $FreeBSD$
34 * last edit-date: [Mon Dec 13 21:54:26 1999]
36 *----------------------------------------------------------------------------*/
38 #define MAIN
39 #include <locale.h>
40 #include "defs.h"
41 #include "alias.h"
43 static void usage( void );
45 static int top_dis = 0;
46 static int bot_dis = 0;
47 static int cur_pos_scr = 0;
49 static void makecurrent(int cur_pos, struct onefile *cur_file, int cold);
51 /*---------------------------------------------------------------------------*
52 * program entry
53 *---------------------------------------------------------------------------*/
54 int
55 main(int argc, char **argv)
57 int i;
58 int kchar;
59 struct pollfd set[1];
60 char *ncp;
62 const char *aliasfile = ALIASFILE;
63 int rrtimeout = REREADTIMEOUT;
65 extern char *optarg;
67 setlocale( LC_ALL, "");
69 while ((i = getopt(argc, argv, "a:d:p:t:")) != -1)
71 switch (i)
73 case 'a':
74 aliasfile = optarg;
75 break;
77 case 'd':
78 spooldir = optarg;
79 break;
81 case 'p':
82 playstring = optarg;
83 break;
85 case 't':
86 rrtimeout = strtoul(optarg, &ncp, 10);
87 if (*ncp)
88 usage();
89 break;
91 case '?':
92 default:
93 usage();
94 break;
98 if (rrtimeout < 10)
99 rrtimeout = 10;
101 if ((chdir(spooldir)) != 0)
102 fatal("cannot change directory to spooldir %s!", spooldir);
104 init_alias(aliasfile);
106 init_screen();
108 init_files(0);
110 /* go into loop */
112 set[0].fd = STDIN_FILENO;
113 set[0].events = POLLIN;
114 for (;;)
116 /* if no char is available within timeout, reread spool */
118 if ((poll(set, 1, rrtimeout * 1000)) <= 0)
120 reread();
121 continue;
124 kchar = wgetch(main_w); /* get char */
126 switch (kchar)
128 case CR:
129 case LF:
130 #ifdef KEY_ENTER
131 case KEY_ENTER:
132 #endif
133 do_menu();
134 break;
136 case KEY_UP: /* up-move cursor */
137 if (cur_file && cur_file->prev)
139 cur_file = cur_file->prev;
140 cur_pos--;
142 break;
145 case TAB:
146 case KEY_DOWN: /* down-move cursor */
147 if (cur_file && cur_file->next)
149 cur_file = cur_file->next;
150 cur_pos++;
152 break;
154 case KEY_HOME: /* move cursor to first dir */
155 break;
157 case KEY_LL: /* move cursor to last file */
158 break;
160 case CNTRL_D:
161 do_quit(0);
162 break;
164 case CNTRL_L: /* refresh */
165 touchwin(curscr);
166 wrefresh(curscr);
167 break;
170 makecurrent(cur_pos, cur_file, 0);
173 do_quit(0);
175 return(0);
178 /*---------------------------------------------------------------------------*
179 * handle horizontal selection bar movement
180 *---------------------------------------------------------------------------*/
181 static void
182 makecurrent(int mc_cur_pos, struct onefile *mc_cur_file, int cold)
184 static int lastpos;
185 static struct onefile *lastfile;
186 char buffer[256];
188 /* un-higlight current horizontal bar */
190 if (!cold && lastfile && mc_cur_file)
192 snprintf(buffer, sizeof(buffer),
193 "%s %s %-16s %-16s %-20s %-6s%*s",
194 lastfile->date, lastfile->time,
195 lastfile->dstnumber, lastfile->srcnumber,
196 lastfile->alias == NULL ? "-/-" : lastfile->alias,
197 lastfile->seconds, COLS - LAST_POS - 2, "");
199 wattroff(main_w, A_REVERSE);
200 mvwprintw(main_w, lastpos, 0, "%s", buffer);
201 wattroff(main_w, A_REVERSE);
204 if (mc_cur_file == NULL)
206 lastpos = cur_pos_scr;
207 lastfile = mc_cur_file;
208 return;
211 /* have to scroll up or down ? */
213 if (mc_cur_pos >= bot_dis)
215 /* scroll up */
217 wscrl(main_w, 1);
219 bot_dis++;
220 top_dis++;
221 cur_pos_scr = LINES-START_O-3;
223 else if (mc_cur_pos < top_dis)
225 /* scroll down */
227 wscrl(main_w, -1);
229 bot_dis--;
230 top_dis--;
231 cur_pos_scr = 0;
233 else
235 cur_pos_scr = mc_cur_pos - top_dis;
238 snprintf(buffer, sizeof(buffer), "%s %s %-16s %-16s %-20s %-6s%*s",
239 mc_cur_file->date, mc_cur_file->time,
240 mc_cur_file->dstnumber, mc_cur_file->srcnumber,
241 mc_cur_file->alias == NULL ? "-/-" : mc_cur_file->alias,
242 mc_cur_file->seconds,
243 COLS - LAST_POS - 2, "");
245 wattron(main_w, A_REVERSE);
246 mvwprintw(main_w, cur_pos_scr, 0, "%s", buffer);
247 wattroff(main_w, A_REVERSE);
249 lastpos = cur_pos_scr;
250 lastfile = mc_cur_file;
252 wrefresh(main_w);
255 /*---------------------------------------------------------------------------*
256 * exit program
257 *---------------------------------------------------------------------------*/
258 void
259 do_quit(int exitval)
261 move(LINES-1, 0);
262 clrtoeol();
263 refresh();
264 endwin();
265 exit(exitval);
268 /*---------------------------------------------------------------------------*
269 * usage display and exit
270 *---------------------------------------------------------------------------*/
271 static void
272 usage(void)
274 fprintf(stderr, "\n");
275 fprintf(stderr, "isdntel - isdn telephone answering management support utility (version %d.%d.%d)\n", VERSION, REL, STEP);
276 fprintf(stderr, " usage: isdntel -a <filename> -d <directory> -p <command> -t <timeout>\n");
277 fprintf(stderr, " -a <filename> use filename as alias file\n");
278 fprintf(stderr, " -d <directory> use directory as spool directory\n");
279 fprintf(stderr, " -p <command> specify commandline for play command\n");
280 fprintf(stderr, " -t <timeout> spool directory reread timeout in seconds\n");
281 fprintf(stderr, "\n");
282 exit(1);
285 /*---------------------------------------------------------------------------*
286 * fatal error exit
287 *---------------------------------------------------------------------------*/
288 void
289 fatal(const char *fmt, ...)
291 va_list ap;
293 va_start(ap, fmt);
295 if (curses_ready)
297 move(LINES-1, 0);
298 clrtoeol();
299 refresh();
300 endwin();
303 fprintf(stderr, "\nFatal error: ");
304 vfprintf(stderr, fmt, ap);
305 fprintf(stderr, "\n\n");
307 va_end(ap);
309 exit(1);
312 /*---------------------------------------------------------------------------*
313 * error printing
314 *---------------------------------------------------------------------------*/
315 void
316 error(const char *fmt, ...)
318 va_list ap;
320 va_start(ap, fmt);
322 if (curses_ready)
324 wprintw(main_w, "ERROR: ");
325 vwprintw(main_w, fmt, ap);
326 wprintw(main_w, "\n");
327 wrefresh(main_w);
329 else
331 fprintf(stderr, "ERROR: ");
332 vfprintf(stderr, fmt, ap);
333 fprintf(stderr, "\n");
336 va_end(ap);
339 /*---------------------------------------------------------------------------*
340 * read files and fill display
341 *---------------------------------------------------------------------------*/
342 void
343 init_files(int inipos)
345 int i;
347 nofiles = fill_list();
349 top_dis = 0;
350 bot_dis = 0;
352 cur_file = first;
354 cur_pos = 0;
355 cur_pos_scr = 0;
357 if (nofiles == 0)
358 return;
360 for (i=0; (i < nofiles) && (i < (LINES-START_O-2)); i++)
362 mvwprintw(main_w, i, 0, "%s %s", cur_file->date, cur_file->time);
363 mvwprintw(main_w, i, DST_POS, "%s", cur_file->dstnumber);
364 mvwprintw(main_w, i, SRC_POS, "%s", cur_file->srcnumber);
365 mvwprintw(main_w, i, ALI_POS,"%s", cur_file->alias == NULL ? "-/-" : cur_file->alias);
366 mvwprintw(main_w, i, SEC_POS,"%s", cur_file->seconds);
368 bot_dis++;
370 if ((cur_file = cur_file->next) == NULL)
371 break;
374 cur_file = first;
376 if (inipos)
378 for (i = 0; i < inipos; i++)
380 if (cur_file->next != NULL)
381 cur_file = cur_file->next;
382 else
383 break;
386 makecurrent(cur_pos, cur_file, 1);
389 /* EOF */