3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1992, 1993 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
22 #include <ac/stdlib.h>
26 #include <ac/signal.h>
27 #include <ac/string.h>
28 #include <ac/termios.h>
30 #include <ac/unistd.h>
34 #include "ldap_defaults.h"
38 printbase( char *lead, char *s )
45 printf("->printbase(%s, %s)\n", lead, s);
48 printf("%sroot\n", lead);
52 rdns = ldap_explode_dn(s, TRUE);
54 printf("%s", friendly_name(*cp));
63 ldap_value_free(rdns);
68 fetch_buffer( char *buffer, int length, FILE *where )
75 printf("->fetch_buffer(%x, %d, %x)\n", buffer, length, where);
78 * Fetch a buffer and strip off any leading or trailing non-printing
79 * characters, namely newlines and carriage returns.
81 if (fgets(buffer, length, where) == NULL) {
83 errno = 0; /* so fatal() doesn't bitch */
86 for (i = strlen(buffer) - 1;
87 i >= 0 && !isprint((unsigned char) buffer[i]); i--)
91 while ( *p != '\0' ) {
92 if ( isprint( (unsigned char) *p )) {
95 SAFEMEMCPY( p, p + 1, strlen( p + 1 ) + 1 );
106 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
109 exit( EXIT_FAILURE );
121 printf("->isgroup()\n");
123 if ((i = attr_to_index("objectClass")) == -1)
125 vp = Entry.attrs[i].values;
126 for (i = 0; *vp != NULL; vp++) {
129 if (debug & D_GROUPS)
130 printf("class #%1d: (%s)\n", i, *vp);
132 if (!strcmp(*vp, "rfc822MailGroup"))
139 * Print out the string 's' on a field of 'width' chracters. Each line
140 * should be indented 'lead' characters.
143 format( char *str, int width, int lead )
145 char *s, *original, *leader = "";
150 printf("->format(%s, %d, %d)\n", str, width, lead);
153 fprintf(stderr, " Cannot format (%s, %d, %d)\n", str, width, lead);
157 leader = (char *) Malloc((unsigned) (lead + 1));
158 (void) memset(leader, ' ', lead);
159 *(leader + lead) = '\0';
163 * Some compilers get really unhappy with this function since it
164 * fiddles around with the first argument, which could be a string
165 * constant. We do a strdup() here so we can do whatever the hell
168 s = original = strdup(str);
170 if (((int) strlen(s) + lead) < width) {
171 printf("%s%s\n", leader, s);
177 cp = s + width - lead;
178 while (!isspace((unsigned char)*cp) && (cp != s))
181 while (isspace((unsigned char)*s))
183 printf("%s%s\n", leader, s);
189 * Print out the string 's' on a field of 'width' chracters. The first line
190 * should be indented 'first_indent' spaces, then followed by 'first_tag',
191 * and then followed by the first line of 's'. Subsequent lines should be
192 * indented 'indent' spaces, then followed by 'tag', and then followed by
193 * subsequent lines of 's'.
208 if (first_tag == NULL)
214 printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s,
215 first_tag, tag, first_indent, indent, width);
218 /* make sure the indents are sane */
219 if ((first_indent >= width) || (indent >= width)) {
220 fprintf(stderr, " Cannot format: indent too large\n");
224 /* make the indentations */
225 if (first_indent > 0) {
226 fi = (char *) Malloc((unsigned) (first_indent + 1));
227 (void) memset(fi, ' ', first_indent);
228 *(fi + first_indent) = '\0';
233 i = (char *) Malloc((unsigned) (indent + 1));
234 (void) memset(i, ' ', indent);
235 *(i + indent) = '\0';
240 /* now do the first line */
241 if (((int) strlen(s) + (int) strlen(first_tag) + first_indent) < width) {
242 printf("%s%s%s\n", fi, first_tag, s);
250 * 's' points to the beginning of the string we want to print.
251 * We point 'cp' to the end of the maximum amount of text we
252 * can print (i.e., total width less the indentation and the
253 * length of the tag). Once we have set 'cp' initially we
254 * back it up to the first space character.
256 cp = s + width - first_indent - strlen(first_tag);
257 while (!isspace((unsigned char)*cp) && (cp != s))
261 * Once there, we change that space character to a null, print the
262 * string, and then restore the space character.
266 printf("%s%s%s\n", fi, first_tag, s);
270 * Since 'cp' may have been set to a space initially (and so no
271 * back-tracking was performed), it could have a space after it
272 * as well. We should gobble up all of these since we don't want
273 * unexpected leading blanks.
275 for (s = cp + 1; isspace((unsigned char)*s); s++)
278 /* now do all of the other lines */
280 if (((int) strlen(s) + (int) strlen(tag) + indent) < width) {
281 printf("%s%s%s\n", i, tag, s);
287 cp = s + width - indent - strlen(tag);
288 while (!isspace((unsigned char)*cp) && (cp != s))
292 printf("%s%s%s\n", i, tag, s);
294 *cp = c; /* don't mess up 's' */
299 #define OUT_OF_QUOTE 1
302 strip_ignore_chars( char *cp )
304 int had_a_comma = FALSE;
305 int flag = OUT_OF_QUOTE;
306 register char *rcp, *cp1;
311 printf("strip_ignore_chars(%s)\n", cp);
313 for (rcp = cp; *rcp != '\0'; rcp++)
314 if (isignorechar(*rcp) || (*rcp == '"'))
319 cp1 = tmp = (char *) Malloc((unsigned) strlen(cp));
320 for (rcp = cp; *rcp != '\0'; rcp++) {
321 /* toss quotes and flip the flag */
323 flag = OUT_OF_QUOTE - flag;
324 else if (isignorechar(*rcp)) {
325 if (flag == OUT_OF_QUOTE)
330 else if (*rcp == ',') {
339 /* re-quote the name if it had a comma in it */
340 if (had_a_comma == TRUE) {
341 rcp = cp1 = (char *) Malloc((unsigned) (strlen(tmp) + 3));
356 case LDAP_MOD_ADD : return("ADD");
357 case LDAP_MOD_DELETE : return("DELETE");
358 case LDAP_MOD_REPLACE : return("REPLACE");
359 default : return("?????");
364 friendly_name( char *s )
366 static LDAPFriendlyMap *map = NULL;
369 cp = ldap_friendly_name(FRIENDLYFILE, s, &map);
377 /* return TRUE if s has the syntax of a uniqname */
379 isauniqname( char *s )
383 if ((i < 3) || (i > 8)) /* uniqnames are 3-8 chars */
385 if (!isalpha((unsigned char)*s)) /* uniqnames begin with a letter */
387 for ( ; *s != '\0'; s++) /* uniqnames are alphanumeric */
388 if (!isalnum((unsigned char)*s))
394 /* return TRUE if this attribute should be printed as a DN */
400 for (i = 0; attrlist[i].quipu_name != NULL; i++)
401 if (!strcasecmp(s, attrlist[i].quipu_name))
403 if (attrlist[i].flags & ATTR_FLAG_IS_A_DN)
409 my_ldap_dn2ufn( char *s )
413 static char short_DN[BUFSIZ];
415 if (strstr(s, UD_BASE) == NULL)
416 return(ldap_dn2ufn(s));
417 cpp = ldap_explode_dn(s, TRUE);
418 sprintf(short_DN, "%s, %s", *cpp, *(cpp + 1));
419 ldap_value_free(cpp);
422 return(ldap_dn2ufn(s));
426 /* return TRUE if this attribute should be printed as a URL */
432 for (i = 0; attrlist[i].quipu_name != NULL; i++)
433 if (!strcasecmp(s, attrlist[i].quipu_name))
435 if (attrlist[i].flags & ATTR_FLAG_IS_A_URL)
440 /* return TRUE if this attribute should be printed as a date and time */
446 for (i = 0; attrlist[i].quipu_name != NULL; i++)
447 if (!strcasecmp(s, attrlist[i].quipu_name))
449 if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
455 Malloc( unsigned int size )
459 void_ptr = (void *) malloc(size);
460 if (void_ptr == NULL) {
462 exit( EXIT_FAILURE );
477 while (isspace((unsigned char) *s) && (*s != '\0'))
487 free_mod_struct( LDAPMod *modp )
489 if (modp->mod_values != NULL)
490 (void) ldap_value_free(modp->mod_values);
491 Free(modp->mod_type);
496 StrFreeDup( char **ptr, char *new_value )
500 if (new_value == NULL)
503 *ptr = strdup(new_value);
508 confirm_action( char *msg )
510 char tmp[SMALL_BUF_SIZE];
515 format( msg, 75, 2 );
518 printf("\n Is this OK? ");
521 fetch_buffer(tmp, sizeof(tmp), stdin);
524 ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));