]> git.sur5r.net Git - openldap/blob - clients/ud/main.c
Remove extern declarations of library functions from source.c.
[openldap] / clients / ud / main.c
1 /*
2  * Copyright (c) 1991, 1992, 1993 
3  * Regents of the University of Michigan.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  * The University of Michigan would like to thank the following people for
13  * their contributions to this piece of software:
14  *
15  *      Robert Urquhart    <robert@sfu.ca>
16  *      Simon Fraser University, Academic Computing Services
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <setjmp.h>
24
25 #ifdef HAVE_PWD_H
26 #include <pwd.h>
27 #endif
28
29 #include <ac/signal.h>
30 #include <ac/string.h>
31 #include <ac/ctype.h>
32 #include <ac/termios.h>
33 #include <ac/time.h>
34 #include <ac/unistd.h>
35
36 #ifdef HAVE_SYS_FILE_H
37 #include <sys/file.h>
38 #endif
39
40 #include <lber.h>
41 #include <ldap.h>
42
43 #include <ldapconfig.h>
44 #include "ud.h"
45
46 #ifndef lint
47 char copyright[] =
48 "@(#) Copyright (c) 1991, 1992, 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
49 #endif
50
51 /*
52  *  Used with change_base() to indicate which base we are changing.
53  */
54 #define BASE_SEARCH     0
55 #define BASE_GROUPS     1
56
57 #define iscom(x)        (!strncasecmp(x, cmd, strlen(cmd)))
58
59 static char *server = NULL;
60 static char *config_file = UD_CONFIG_FILE;
61 static char *filter_file = FILTERFILE;
62 static int ldap_port = LDAP_PORT;
63 static int dereference = TRUE;
64
65 char *default_bind_object = NULL;
66
67 char *bound_dn;                 /* bound user's Distinguished Name */
68 char *group_base;               /* place in LDAP tree where groups are */
69 char *search_base;              /* place in LDAP tree where searches start */
70
71 static jmp_buf env;             /* spot to jump to on an interrupt */
72
73 int lpp;                        /* lines per page */
74 int verbose;                    /* 1 if verbose mode on */
75 int col_size;                   /* characters across on the screen */
76 int bind_status;                /* user's bind status */
77
78 LDAP *ld;                       /* LDAP descriptor */
79 LDAPFiltDesc *lfdp;             /* LDAP filter descriptor */
80
81 #ifdef DEBUG
82 int debug;                      /* debug flag */
83 #endif
84
85
86 int
87 main( int argc, char **argv )
88 {
89         register int c;                         /* for parsing argv */
90         register char *cp;                      /* for parsing Version */
91
92         verbose = 1;
93
94         /*  handle argument list */
95         while ((c = getopt(argc, argv, "c:d:Df:l:p:s:u:vV")) != -1) {
96                 switch (c) {
97                 case 'l' :
98 #ifdef LDAP_DEBUG
99                         ldap_debug = (int) strtol(optarg, (char **) NULL, 0);
100                         lber_debug = ldap_debug;
101 #endif
102                         break;
103                 case 'd' :
104 #ifdef DEBUG
105                         debug = (int) strtol(optarg, (char **) NULL, 0);
106 #endif
107                         break;
108                 case 's' :
109                         server = strdup(optarg);
110                         break;
111                 case 'c' :
112                         filter_file = strdup(optarg);
113                         break;
114                 case 'f' :
115                         config_file = optarg;
116                         break;
117                 case 'p' :
118                         ldap_port = atoi(optarg);
119                         break;
120                 case 'u' :
121                         default_bind_object = strdup(optarg);
122                         break;
123                 case 'v' :
124                         verbose = 1;    /* this is the default anyways... */
125                         break;
126                 case 'V' :
127                         verbose = 0;
128                         break;
129                 case 'D' :
130                         printf("\n\n  Debug flag values\n\n");
131                         printf("    1  function trace\n");
132                         printf("    2  find() information\n");
133                         printf("    4  group information\n");
134                         printf("    8  mod() information\n");
135                         printf("   16  parsing information\n");
136                         printf("   32  output information\n");
137                         printf("   64  authentication information\n");
138                         printf("  128  initialization information\n\n");
139                         format("These are masks, and may be added to form multiple debug levels.  For example, '-d 35' would perform a function trace, print out information about the find() function, and would print out information about the output routines too.", 75, 2);
140                         exit(0);
141                 default:
142                         fprintf(stderr, "Usage: %s [-c filter-config-file] [-d debug-level] [-l ldap-debug-level] [-s server] [-p port] [-V]\n", argv[0]);
143                         exit(-1);
144                         /* NOTREACHED */
145                 }
146         }
147
148         /* just print the first line of Version[] */
149         cp = strchr(Version, '\t');
150         if (cp != NULL)
151                 *cp = '\0';
152         printf(Version);
153         fflush( stdout );
154
155         initialize_client();
156         initialize_attribute_strings();
157
158         /* now tackle the user's commands */
159         do_commands();
160         /* NOTREACHED */
161 }
162
163 void
164 do_commands( void )
165 {
166         LDAPMessage *mp;                        /* returned by find() */
167         register char *cp;                      /* misc char pointer */
168         register char *ap;                      /* misc char pointer */
169         static char buf[MED_BUF_SIZE];          /* for prompting */
170         static char cmd[MED_BUF_SIZE];          /* holds the command */
171         static char input[MED_BUF_SIZE];        /* buffer for input */
172
173 #ifdef DEBUG
174         if (debug & D_TRACE)
175                 printf("->do_commands()\n");
176 #endif
177         if (verbose) 
178                 printf("\n  Enter a command.  If you need help, type 'h' or '?' and hit RETURN.\n\n");
179         /* jump here on an interrupt */
180         (void) setjmp(env);
181         for (;;) {
182                 printf("* ");
183                 fflush(stdout);
184                 cp = input;
185 /* Temporary kludge - if cp is null, dumps core under Solaris */
186                 if (cp == NULL)
187                         break;
188                 fetch_buffer(input, sizeof(input), stdin);
189                 if (*input == '\0') {
190                         putchar('\n');
191                         continue;
192                 }
193                 while (isspace(*cp))
194                         cp++;   
195                 ap = cmd;
196                 if (memset(cmd, '\0', sizeof(cmd)) == NULL)
197                         fatal("memset");
198                 while (!isspace(*cp) && (*cp != '\0'))
199                         *ap++ = *cp++;
200                 if (iscom("status"))
201                         status();
202                 else if (iscom("stop") || iscom("quit"))
203                         break;
204                 else if (iscom("cb") || iscom("cd") || iscom("moveto")) {
205                         while (isspace(*cp) && (*cp != '\0')) 
206                                 cp++;
207                         if (!strncasecmp(cp, "base", 4))
208                                 cp += 4;
209                         change_base(BASE_SEARCH, &search_base, nextstr(cp));
210                 }
211                 else if (iscom("memberships"))
212                         (void) list_memberships(nextstr(cp));
213                 else if (iscom("list"))
214                         (void) list_groups(nextstr(cp));
215                 else if (iscom("groupbase"))
216                         change_base(BASE_GROUPS, &group_base, nextstr(cp));
217                 else if (iscom("find") || iscom("display") || iscom("show")) {
218                         cp = nextstr(cp);
219                         if ((mp = find(cp, FALSE)) != NULL) {
220                                 parse_answer(mp);
221                                 print_an_entry();
222                                 ldap_msgfree(mp);
223                         }
224                         else
225                                 printf(" Could not find \"%s\".\n", cp);
226                 }
227 #ifdef UOFM
228                 else if (iscom("vedit") && isatty( 1 )) {
229 #else
230                 else if (iscom("vedit")) {
231 #endif
232                         (void) edit(nextstr(cp));
233                 }
234                 else if (iscom("modify") || iscom("change") || iscom("alter"))
235                         (void) modify(nextstr(cp));
236                 else if (iscom("bind") || iscom("iam"))
237                         (void) auth(nextstr(cp), 0);
238                 else if ((cmd[0] == '?') || iscom("help"))
239                         print_help(nextstr(cp));
240                 else if (iscom("join") || iscom("subscribe")) 
241                         (void) x_group(G_JOIN, nextstr(cp));
242                 else if (iscom("resign") || iscom("unsubscribe"))
243                         (void) x_group(G_RESIGN, nextstr(cp));
244                 else if (!strncasecmp("create", cmd, strlen(cmd)))
245                         add_group(nextstr(cp));
246                 else if (!strncasecmp("remove", cmd, strlen(cmd)))
247                         remove_group(nextstr(cp));
248                 else if (!strncasecmp("purge", cmd, strlen(cmd)))
249                         purge_group(nextstr(cp));
250                 else if (!strncasecmp("verbose", cmd, strlen(cmd))) {
251                         verbose = 1 - verbose;
252                         if (verbose)
253                                 printf("  Verbose mode has been turned on.\n");
254                 }
255                 else if (!strncasecmp("dereference", cmd, strlen(cmd))) {
256                         int deref;
257                         dereference = 1 - dereference;
258                         if (dereference == 1) {
259                                 deref = LDAP_DEREF_ALWAYS;
260                         } else {
261                                 deref = LDAP_DEREF_NEVER;
262                         }
263                         ldap_set_option(ld, LDAP_OPT_DEREF, (void *) &deref);
264                 }
265                 else if (!strncasecmp("tidy", cmd, strlen(cmd)))
266                         tidy_up();
267                 else if (cmd[0] == '\0')
268                         putchar('\n');
269                 else
270                         printf("  Invalid command.  Type \"help commands.\"\n");
271         }
272         printf(" Thank you!\n");
273         
274         ldap_unbind(ld);
275 #ifdef HAVE_KERBEROS
276         destroy_tickets();
277 #endif
278         exit(0);
279         /* NOTREACHED */
280 }
281
282 void
283 status( void )
284 {
285         register char **rdns;
286         char *host;
287
288 #ifdef DEBUG
289         if (debug & D_TRACE)
290                 printf("->status()\n");
291 #endif
292         printf("  Current server is %s", server);
293         if ( ld != NULL ) {
294                 char *host = NULL;
295                 
296                 ldap_get_option(ld, LDAP_OPT_HOST_NAME, &host);
297
298                 if (( host != NULL ) && (strcasecmp( host, server ) != 0 )) {
299                         printf( " (%s)", host );
300                 }
301         }
302         putchar( '\n' );
303         printbase("  Search base is ", search_base);
304         printbase("  Group  base is ", group_base);
305         if ( bound_dn != NULL ) {
306                 rdns = ldap_explode_dn(bound_dn, TRUE);
307                 printf("  Bound as \"%s\"\n", *rdns);
308                 ldap_value_free(rdns);
309         } else {
310                 printf("  Bound as Nobody\n" );
311         }
312         printf( "  Verbose mode is %sabled\n", ( verbose ? "en" : "dis" ));
313         if ( ld != NULL ) {
314                 int deref = LDAP_DEREF_NEVER;
315                 ldap_get_option(ld, LDAP_OPT_DEREF, &deref);
316                 printf( "  Aliases are %sbeing dereferenced\n",
317                         ( deref == LDAP_DEREF_ALWAYS ) ? "" : "not" );
318         }
319 }
320
321 void
322 change_base( int type, char **base, char *s )
323 {
324         register char *cp;                      /* utility pointers */
325         char **rdns;                            /* for parsing */
326         char *output_string;                    /* for nice output */
327         int num_picked;                         /* # of selected base */
328         int j;                                  /* used with num_picked */
329         int i = 1;                              /* index into choices array */
330         int matches;                            /* # of matches found */
331         int rest = 1;                           /* # left to display */
332         char tmp[MED_BUF_SIZE];                 /* temporary buffer */
333         static char *choices[MED_BUF_SIZE];     /* bases from which to choose */
334         static char resp[SMALL_BUF_SIZE];       /* for prompting user */
335         static char buf[MED_BUF_SIZE];
336         static char *attrs[] = { "objectClass", NULL };
337         LDAPMessage *mp;                        /* results from a search */
338         LDAPMessage *ep;                        /* for going thru bases */
339
340 #ifdef DEBUG
341         if (debug & D_TRACE)
342                 printf("->change_base(%s, %s)\n", s, s);
343 #endif
344         /*
345          *  If s is NULL we need to prompt the user for an argument.
346          */
347         while (s == NULL) {
348                 if (verbose) {
349                         printf("  You need to specify how the base is to be changed.  Valid choices are:\n");
350                         printf("     ?       - list the choices immediately below this level\n");
351                         printf("     ..      - move up one level in the Directory tree\n");
352                         printf("     root    - move to the root of the Directory tree\n");
353                         printf("     default - move to the default level built into this program\n");
354                         printf("     <entry> - move to the entry specified\n");
355                 }
356                 printf("  Change base to? ");
357                 fflush(stdout);
358                 fetch_buffer(buf, sizeof(buf), stdin);
359                 if ((buf != NULL) && (buf[0] != '\0'))
360                         s = buf;
361         }
362
363         /* set the output string */
364         if (type == BASE_SEARCH)
365                 output_string = "  Search base is now ";
366         else if (type == BASE_GROUPS)
367                 output_string = "  Group base is now ";
368
369         if (!strcasecmp(s, "root")) {
370                 StrFreeDup(base, NULL);
371                 printbase("  Search base is ", *base);
372                 return;
373         }
374
375         /*
376          *  User wants to ascend one level in the LDAP tree.
377          *  Easy:  Just strip off the first element of the
378          *  current search base, unless it's the root, in
379          *  which case we just do nothing.
380          */
381         if (!strcasecmp(s, "..")) {
382                 if (*base == NULL) {
383                         printf("  You are already at the root\n");
384                         return;
385                 }
386                 cp = strchr(*base, '=');
387                 cp++;
388                 /*
389                  *  If there isn't a second "=" in the base, then this was
390                  *  a one element base, and so now it should be NULL.
391                  */
392                 if ((cp = strchr(cp, '=')) == NULL)
393                         StrFreeDup(base, NULL);
394                 else {
395                         /*
396                          *  Back up to the start of this
397                          *
398                          *      attr=value
399                          *
400                          *  sequence now that 'cp' is pointing to the '='.
401                          */
402                         while(!isspace(*cp))
403                                 cp--;
404                         cp++;
405                         /*
406                          *  Goofy, but need to do it this way since both *base
407                          *  and cp point into the same chunk of memory, and
408                          *  we want to free *base, but keep part of it around.
409                          */
410                         cp = strdup(cp);
411                         StrFreeDup(base, cp);
412                         Free(cp);
413                 }
414                 printbase(output_string, *base);
415                 return;
416         }
417
418         /* user wants to see what is directly below this level */
419         if (*s == '?') {
420                 /*
421                  *  Fetch the list of entries directly below this level.
422                  *  Once we have the list, we will print it for the user, one
423                  *  screenful at a time.  At the end of each screen, we ask
424                  *  the user if they want to see more.  They can also just
425                  *  type a number at that point too.
426                  */
427                 if (ldap_search_s(ld, *base, LDAP_SCOPE_ONELEVEL, "(|(objectClass=quipuNonLeafObject)(objectClass=externalNonLeafObject))", attrs, FALSE, &mp) != LDAP_SUCCESS) {
428                         int ld_errno = 0;
429                         ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
430                         if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED) ||
431                             (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
432                                 if (verbose) {
433                                         printf("  Your query was too general and a limit was exceeded.  The results listed\n");
434                                         printf("  are not complete.  You may want to try again with a more refined query.\n\n");
435                                 }
436                                 else
437                                         printf("  Time or size limit exceeded.  Partial results follow.\n\n");
438                         } else {
439                                 ldap_perror(ld, "ldap_search_s");
440                                 return;
441                         }
442                 }
443                 if ((matches = ldap_count_entries(ld, mp)) < 1) {
444                         printf("  There is nothing below this level.\n");
445                         (void) ldap_msgfree(mp);
446                         return;
447                 }
448                 num_picked = 0;
449                 printf(" There are %d choices:\n", matches);
450                 for (ep = ldap_first_entry(ld, mp); ep != NULL; ep = ldap_next_entry(ld, ep)) {
451                         /*
452                          *  Put the last component of the DN into 'lastDN'.
453                          *  If we are at the root level, convert any country
454                          *  codes to recognizable names for printing.
455                          */
456                         choices[i] = ldap_get_dn(ld, ep);
457                         rdns = ldap_explode_dn(choices[i], TRUE);
458                         printf(" %2d. %s\n", i, friendly_name(*rdns));
459                         (void) ldap_value_free(rdns);
460                         i++;
461                         if ((rest++ > (lpp - 3)) && (i < matches)) {
462                                 printf("More? ");
463                                 fflush(stdout);
464                                 fetch_buffer(resp, sizeof(resp), stdin);
465                                 if ((resp[0] == 'n') || (resp[0] == 'N'))
466                                         break;
467                                 else if (((num_picked = atoi(resp)) != 0) && (num_picked < i))
468                                         break;
469                                 rest = 1;
470                         }
471                 }
472                 for (;;) {
473                         if (num_picked != 0) {
474                                 j = num_picked;
475                                 num_picked = 0;
476                         }
477                         else {
478                                 printf(" Which number? ");
479                                 fflush(stdout);
480                                 fetch_buffer(resp, sizeof(resp), stdin);
481                                 j = atoi(resp);
482                         }
483                         if (j == 0) {
484                                 (void) ldap_msgfree(mp);
485                                 for (i = 0; i < matches; i++)
486                                         Free(choices[i]);
487                                 return;
488                         }
489                         if ((j < 1) || (j >= i))
490                                 printf(" Invalid number\n");
491                         else {
492                                 StrFreeDup(base, choices[j]);
493                                 printbase(output_string, *base);
494                                 (void) ldap_msgfree(mp);
495                                 for (i = 0; choices[i] != NULL; i++)
496                                         Free(choices[i]);
497                                 return;
498                         }
499                 }
500         }
501         /* set the search base back to the original default value */
502         else if (!strcasecmp(s, "default")) {
503                 if (type == BASE_SEARCH)
504                         StrFreeDup(base, NULL);
505                 else if (type == BASE_GROUPS)
506                         StrFreeDup(base, UD_WHERE_GROUPS_ARE_CREATED);
507                 printbase(output_string, *base);
508         }
509         /* they typed in something -- see if it is legit */
510         else {
511                 /* user cannot do something like 'cb 33' */
512                 if (atoi(s) != 0) {
513                         printf("  \"%s\" is not a valid search base\n", s);
514                         printf("  Base unchanged.\n");
515                         printf("  Try using 'cb ?'\n");
516                         return;
517                 }
518                 /* was it a fully-specified DN? */
519                 if (vrfy(s)) {
520                         StrFreeDup(base, s);
521                         printbase(output_string, *base);
522                         return;
523                 }
524                 /* was it a RDN relative to the current base? */
525                 sprintf(tmp, "ou=%s, %s", s, *base);
526                 if (vrfy(tmp)) {
527                         StrFreeDup(base, tmp);
528                         printbase(output_string, *base);
529                         return;
530                 }
531                 printf("  \"%s\" is not a valid base\n  Base unchanged.\n", s);
532         }
533 }
534
535 void
536 initialize_client( void )
537 {
538         FILE *fp;                               /* for config file */
539         static char buffer[MED_BUF_SIZE];       /* for input */
540         struct passwd *pw;                      /* for getting the home dir */
541         register char *cp;                      /* for fiddling with buffer */
542         char *term;                             /* for tty set-up */
543         char *config;                           /* config file to use */
544         static char bp[1024];                   /* for tty set-up */
545
546 #ifdef DEBUG
547         if (debug & D_TRACE)
548                 printf("->initialize_client()\n");
549 #endif
550         /*
551          *  A per-user config file has precedence over any system-wide
552          *  config file, if one exists.
553          */
554 #ifdef HAVE_GETPWUID_H
555         if ((pw = getpwuid((uid_t) geteuid())) == (struct passwd *) NULL)
556                 config = config_file;
557         else {
558                 if (pw->pw_dir == NULL)
559                         config = config_file;
560                 else {
561                         sprintf(buffer, "%s/%s", pw->pw_dir,
562                             UD_USER_CONFIG_FILE);
563                         if (access(buffer, R_OK) == 0)
564                                 config = buffer;
565                         else
566                                 config = config_file;
567                 }
568         }
569 #endif /* getpwduid() */
570 #ifdef DEBUG
571         if (debug & D_INITIALIZE)
572                 printf("Using config file %s\n", config);
573 #endif
574
575         /*
576          *  If there is a config file, read it.
577          *
578          *  Could have lines that look like this:
579          *
580          *      server <ip-address or domain-name>
581          *      base   <default search base>
582          *      groupbase <default place where groups are created>
583          *
584          */
585         if ((fp = fopen(config, "r")) != NULL) {
586                 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
587                         buffer[strlen(buffer) - 1] = '\0';
588                         if (!strncasecmp(buffer, "server", 6)) {
589                                 if (server != NULL)
590                                         continue;
591                                 cp = buffer + 6;
592                                 while (isspace(*cp))
593                                         cp++;
594                                 if ((*cp == '\0') || (*cp == '\n'))
595                                         continue;
596                                 server = strdup(cp);
597                         } 
598                         else if (!strncasecmp(buffer, "host", 4)) {
599                                 if (server != NULL)
600                                         continue;
601                                 cp = buffer + 4;
602                                 while (isspace(*cp))
603                                         cp++;
604                                 if ((*cp == '\0') || (*cp == '\n'))
605                                         continue;
606                                 server = strdup(cp);
607                         }
608                         else if (!strncasecmp(buffer, "base", 4)) {
609                                 cp = buffer + 4;
610                                 while (isspace(*cp))
611                                         cp++;
612                                 if ((*cp == '\0') || (*cp == '\n'))
613                                         continue;
614                                 search_base = strdup(cp);
615                         }
616                         else if (!strncasecmp(buffer, "groupbase", 9)) {
617                                 cp = buffer + 9;
618                                 while (isspace(*cp))
619                                         cp++;
620                                 if ((*cp == '\0') || (*cp == '\n'))
621                                         continue;
622                                 group_base = strdup(cp);
623                         }
624                         else
625                                 fprintf(stderr, "?? -> %s\n", buffer);
626                 }
627         }
628         if (group_base == NULL)
629                 group_base = strdup(UD_WHERE_GROUPS_ARE_CREATED);
630
631         /*
632          *  Set up our LDAP connection.  The values of retry and timeout
633          *  are meaningless since we will immediately be doing a null bind
634          *  because we want to be sure to use TCP, not UDP.
635          */
636         if ((ld = ldap_open(server, ldap_port)) == NULL) {
637                 fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
638                 exit(0);
639                 /* NOTREACHED */
640         }
641         if (ldap_bind_s(ld, (char *) default_bind_object, NULL,
642             LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
643                 int ld_errno = 0;
644                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
645
646                 fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
647                 if (ld_errno != LDAP_UNAVAILABLE)
648                         ldap_perror(ld, "  ldap_bind_s");
649                 exit(0);
650                 /* NOTREACHED */
651         }
652         {
653                 int deref = LDAP_DEREF_ALWAYS;
654                 ldap_set_option(ld, LDAP_OPT_DEREF, (void *) &deref);
655         }
656         bind_status = UD_NOT_BOUND;
657         if ( default_bind_object != NULL ) {
658                 bound_dn = strdup(default_bind_object);
659         } else {
660                 bound_dn = NULL;
661         }
662
663         /* enabled local caching of ldap results, 15 minute lifetime */
664         ldap_enable_cache( ld, 60 * 15, 0 );            /* no memory limit */
665
666         /* initialize the search filters */
667         if ((lfdp = ldap_init_getfilter(filter_file)) == NULL) {
668                 fprintf(stderr, "  Problem with ldap_init_getfilter\n");
669                 fatal(filter_file);
670                 /*NOTREACHED*/
671         }
672
673         /* terminal initialization stuff goes here */
674         lpp = DEFAULT_TTY_HEIGHT;
675         col_size = DEFAULT_TTY_WIDTH;
676
677         (void) SIGNAL (SIGINT, attn);
678
679 #ifndef NO_TERMCAP
680         {
681         struct winsize win;                     /* for tty set-up */
682
683         if (((term = getenv("TERM")) == NULL) || (tgetent(bp, term) <= 0))
684                 return;
685         else {
686 #ifdef TIOCGWINSZ
687                 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
688                         lpp = tgetnum("li");
689                         col_size = tgetnum("co");
690                 } else
691 #endif
692                 {
693                         if ((lpp = win.ws_row) == 0)
694                                 lpp = tgetnum("li");
695                         if ((col_size = win.ws_col) == 0)
696                                 col_size = tgetnum("co");
697                         if ((lpp <= 0) || tgetflag("hc"))
698                                 lpp = DEFAULT_TTY_HEIGHT;
699                         if ((col_size <= 0) || tgetflag("hc"))
700                                 col_size = DEFAULT_TTY_WIDTH;
701                 }
702         }
703         (void) SIGNAL (SIGWINCH, chwinsz);
704
705         }
706 #endif
707 }
708
709 RETSIGTYPE
710 attn( int sig )
711 {
712         fflush(stderr);
713         fflush(stdout);
714         printf("\n\n  INTERRUPTED!\n");
715
716         (void) SIGNAL (SIGINT, attn);
717
718         longjmp(env, 1);
719 }
720
721 #if !defined(NO_TERMCAP) && defined(TIOCGWINSZ)
722 RETSIGTYPE
723 chwinsz( int sig )
724 {
725         struct winsize win;
726
727         (void) SIGNAL (SIGWINCH, SIG_IGN);
728         if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
729                 if (win.ws_row != 0)
730                         lpp = win.ws_row;
731                 if (win.ws_col != 0)
732                         col_size = win.ws_col;
733         }
734
735         (void) SIGNAL (SIGWINCH, chwinsz);
736 }
737 #endif