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 )
42 printf("->printbase(%s, %s)\n", lead, s);
49 printf("%sroot\n", lead);
52 printf("%s%s\n", lead, s);
57 fetch_buffer( char *buffer, int length, FILE *where )
64 printf("->fetch_buffer(%x, %d, %x)\n", buffer, length, where);
67 * Fetch a buffer and strip off any leading or trailing non-printing
68 * characters, namely newlines and carriage returns.
70 if (fgets(buffer, length, where) == NULL) {
72 errno = 0; /* so fatal() doesn't bitch */
75 for (i = strlen(buffer) - 1;
76 i >= 0 && !isprint((unsigned char) buffer[i]); i--)
80 while ( *p != '\0' ) {
81 if ( isprint( (unsigned char) *p )) {
84 AC_MEMCPY( p, p + 1, strlen( p + 1 ) + 1 );
107 printf("->isgroup()\n");
109 if ((i = attr_to_index("objectClass")) == -1)
111 vp = Entry.attrs[i].values;
112 for (i = 0; *vp != NULL; vp++) {
115 if (debug & D_GROUPS)
116 printf("class #%1d: (%s)\n", i, *vp);
118 if (!strcmp(*vp, "rfc822MailGroup"))
125 * Print out the string 's' on a field of 'width' chracters. Each line
126 * should be indented 'lead' characters.
129 format( char *str, int width, int lead )
131 char *s, *original, *leader = "";
136 printf("->format(%s, %d, %d)\n", str, width, lead);
139 fprintf(stderr, " Cannot format (%s, %d, %d)\n", str, width, lead);
143 leader = (char *) Malloc((unsigned) (lead + 1));
144 (void) memset(leader, ' ', lead);
145 *(leader + lead) = '\0';
149 * Some compilers get really unhappy with this function since it
150 * fiddles around with the first argument, which could be a string
151 * constant. We do a strdup() here so we can do whatever the hell
154 s = original = strdup(str);
156 if (((int) strlen(s) + lead) < width) {
157 printf("%s%s\n", leader, s);
163 cp = s + width - lead;
164 while (!isspace((unsigned char)*cp) && (cp != s))
167 while (isspace((unsigned char)*s))
169 printf("%s%s\n", leader, s);
175 * Print out the string 's' on a field of 'width' chracters. The first line
176 * should be indented 'first_indent' spaces, then followed by 'first_tag',
177 * and then followed by the first line of 's'. Subsequent lines should be
178 * indented 'indent' spaces, then followed by 'tag', and then followed by
179 * subsequent lines of 's'.
194 if (first_tag == NULL)
200 printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s,
201 first_tag, tag, first_indent, indent, width);
204 /* make sure the indents are sane */
205 if ((first_indent >= width) || (indent >= width)) {
206 fprintf(stderr, " Cannot format: indent too large\n");
210 /* make the indentations */
211 if (first_indent > 0) {
212 fi = (char *) Malloc((unsigned) (first_indent + 1));
213 (void) memset(fi, ' ', first_indent);
214 *(fi + first_indent) = '\0';
219 i = (char *) Malloc((unsigned) (indent + 1));
220 (void) memset(i, ' ', indent);
221 *(i + indent) = '\0';
226 /* now do the first line */
227 if (((int) strlen(s) + (int) strlen(first_tag) + first_indent) < width) {
228 printf("%s%s%s\n", fi, first_tag, s);
236 * 's' points to the beginning of the string we want to print.
237 * We point 'cp' to the end of the maximum amount of text we
238 * can print (i.e., total width less the indentation and the
239 * length of the tag). Once we have set 'cp' initially we
240 * back it up to the first space character.
242 cp = s + width - first_indent - strlen(first_tag);
243 while (!isspace((unsigned char)*cp) && (cp != s))
247 * Once there, we change that space character to a null, print the
248 * string, and then restore the space character.
252 printf("%s%s%s\n", fi, first_tag, s);
256 * Since 'cp' may have been set to a space initially (and so no
257 * back-tracking was performed), it could have a space after it
258 * as well. We should gobble up all of these since we don't want
259 * unexpected leading blanks.
261 for (s = cp + 1; isspace((unsigned char)*s); s++)
264 /* now do all of the other lines */
266 if (((int) strlen(s) + (int) strlen(tag) + indent) < width) {
267 printf("%s%s%s\n", i, tag, s);
273 cp = s + width - indent - strlen(tag);
274 while (!isspace((unsigned char)*cp) && (cp != s))
278 printf("%s%s%s\n", i, tag, s);
280 *cp = c; /* don't mess up 's' */
285 #define OUT_OF_QUOTE 1
288 strip_ignore_chars( char *cp )
290 int had_a_comma = FALSE;
291 int flag = OUT_OF_QUOTE;
292 register char *rcp, *cp1;
297 printf("strip_ignore_chars(%s)\n", cp);
299 for (rcp = cp; *rcp != '\0'; rcp++)
300 if (isignorechar(*rcp) || (*rcp == '"'))
305 cp1 = tmp = (char *) Malloc((unsigned) strlen(cp));
306 for (rcp = cp; *rcp != '\0'; rcp++) {
307 /* toss quotes and flip the flag */
309 flag = OUT_OF_QUOTE - flag;
310 else if (isignorechar(*rcp)) {
311 if (flag == OUT_OF_QUOTE)
316 else if (*rcp == ',') {
325 /* re-quote the name if it had a comma in it */
326 if (had_a_comma == TRUE) {
327 rcp = cp1 = (char *) Malloc((unsigned) (strlen(tmp) + 3));
342 case LDAP_MOD_ADD : return("ADD");
343 case LDAP_MOD_DELETE : return("DELETE");
344 case LDAP_MOD_REPLACE : return("REPLACE");
345 default : return("?????");
351 /* return TRUE if s has the syntax of a uniqname */
353 isauniqname( char *s )
357 if ((i < 3) || (i > 8)) /* uniqnames are 3-8 chars */
359 if (!isalpha((unsigned char)*s)) /* uniqnames begin with a letter */
361 for ( ; *s != '\0'; s++) /* uniqnames are alphanumeric */
362 if (!isalnum((unsigned char)*s))
368 /* return TRUE if this attribute should be printed as a DN */
374 for (i = 0; attrlist[i].quipu_name != NULL; i++)
375 if (!strcasecmp(s, attrlist[i].quipu_name))
377 if (attrlist[i].flags & ATTR_FLAG_IS_A_DN)
383 my_ldap_dn2ufn( char *s )
387 static char short_DN[BUFSIZ];
389 if (strstr(s, UD_BASE) == NULL)
390 return(ldap_dn2ufn(s));
391 cpp = ldap_explode_dn(s, TRUE);
392 sprintf(short_DN, "%s, %s", *cpp, *(cpp + 1));
393 ldap_value_free(cpp);
396 return(ldap_dn2ufn(s));
400 /* return TRUE if this attribute should be printed as a URL */
406 for (i = 0; attrlist[i].quipu_name != NULL; i++)
407 if (!strcasecmp(s, attrlist[i].quipu_name))
409 if (attrlist[i].flags & ATTR_FLAG_IS_A_URL)
414 /* return TRUE if this attribute should be printed as a date and time */
420 for (i = 0; attrlist[i].quipu_name != NULL; i++)
421 if (!strcasecmp(s, attrlist[i].quipu_name))
423 if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
429 Malloc( unsigned int size )
433 void_ptr = (void *) malloc(size);
434 if (void_ptr == NULL) {
436 exit( EXIT_FAILURE );
451 while (isspace((unsigned char) *s) && (*s != '\0'))
461 free_mod_struct( LDAPMod *modp )
463 if (modp->mod_values != NULL)
464 (void) ldap_value_free(modp->mod_values);
465 Free(modp->mod_type);
470 StrFreeDup( char **ptr, char *new_value )
474 if (new_value == NULL)
477 *ptr = strdup(new_value);
482 confirm_action( char *msg )
484 char tmp[SMALL_BUF_SIZE];
489 format( msg, 75, 2 );
492 printf("\n Is this OK? ");
495 fetch_buffer(tmp, sizeof(tmp), stdin);
498 ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));