2 * Copyright (c) 1992, 1993 Regents of the University of Michigan.
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.
17 #include <ac/stdlib.h>
21 #include <ac/signal.h>
22 #include <ac/string.h>
23 #include <ac/termios.h>
25 #include <ac/unistd.h>
29 #include <ldap_defaults.h>
34 printbase( char *lead, char *s )
41 printf("->printbase(%s, %s)\n", lead, s);
44 printf("%sroot\n", lead);
48 rdns = ldap_explode_dn(s, TRUE);
50 printf("%s", friendly_name(*cp));
59 ldap_value_free(rdns);
64 fetch_buffer( char *buffer, int length, FILE *where )
71 printf("->fetch_buffer(%x, %d, %x)\n", buffer, length, where);
74 * Fetch a buffer and strip off any leading or trailing non-printing
75 * characters, namely newlines and carriage returns.
77 if (fgets(buffer, length, where) == NULL) {
79 errno = 0; /* so fatal() doesn't bitch */
82 for (i = strlen(buffer) - 1;
83 i >= 0 && !isprint((unsigned char) buffer[i]); i--)
87 while ( *p != '\0' ) {
88 if ( isprint( (unsigned char) *p )) {
91 SAFEMEMCPY( p, p + 1, strlen( p + 1 ) + 1 );
117 printf("->isgroup()\n");
119 if ((i = attr_to_index("objectClass")) == -1)
121 vp = Entry.attrs[i].values;
122 for (i = 0; *vp != NULL; vp++) {
125 if (debug & D_GROUPS)
126 printf("class #%1d: (%s)\n", i, *vp);
128 if (!strcmp(*vp, "rfc822MailGroup"))
135 * Print out the string 's' on a field of 'width' chracters. Each line
136 * should be indented 'lead' characters.
139 format( char *str, int width, int lead )
141 char *s, *original, *leader = "";
146 printf("->format(%s, %d, %d)\n", str, width, lead);
149 fprintf(stderr, " Cannot format (%s, %d, %d)\n", str, width, lead);
153 leader = (char *) Malloc((unsigned) (lead + 1));
154 (void) memset(leader, ' ', lead);
155 *(leader + lead) = '\0';
159 * Some compilers get really unhappy with this function since it
160 * fiddles around with the first argument, which could be a string
161 * constant. We do a strdup() here so we can do whatever the hell
164 s = original = strdup(str);
166 if (((int) strlen(s) + lead) < width) {
167 printf("%s%s\n", leader, s);
173 cp = s + width - lead;
174 while (!isspace((unsigned char)*cp) && (cp != s))
177 while (isspace((unsigned char)*s))
179 printf("%s%s\n", leader, s);
185 * Print out the string 's' on a field of 'width' chracters. The first line
186 * should be indented 'first_indent' spaces, then followed by 'first_tag',
187 * and then followed by the first line of 's'. Subsequent lines should be
188 * indented 'indent' spaces, then followed by 'tag', and then followed by
189 * subsequent lines of 's'.
204 if (first_tag == NULL)
210 printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s,
211 first_tag, tag, first_indent, indent, width);
214 /* make sure the indents are sane */
215 if ((first_indent >= width) || (indent >= width)) {
216 fprintf(stderr, " Cannot format: indent too large\n");
220 /* make the indentations */
221 if (first_indent > 0) {
222 fi = (char *) Malloc((unsigned) (first_indent + 1));
223 (void) memset(fi, ' ', first_indent);
224 *(fi + first_indent) = '\0';
229 i = (char *) Malloc((unsigned) (indent + 1));
230 (void) memset(i, ' ', indent);
231 *(i + indent) = '\0';
236 /* now do the first line */
237 if (((int) strlen(s) + (int) strlen(first_tag) + first_indent) < width) {
238 printf("%s%s%s\n", fi, first_tag, s);
246 * 's' points to the beginning of the string we want to print.
247 * We point 'cp' to the end of the maximum amount of text we
248 * can print (i.e., total width less the indentation and the
249 * length of the tag). Once we have set 'cp' initially we
250 * back it up to the first space character.
252 cp = s + width - first_indent - strlen(first_tag);
253 while (!isspace((unsigned char)*cp) && (cp != s))
257 * Once there, we change that space character to a null, print the
258 * string, and then restore the space character.
262 printf("%s%s%s\n", fi, first_tag, s);
266 * Since 'cp' may have been set to a space initially (and so no
267 * back-tracking was performed), it could have a space after it
268 * as well. We should gobble up all of these since we don't want
269 * unexpected leading blanks.
271 for (s = cp + 1; isspace((unsigned char)*s); s++)
274 /* now do all of the other lines */
276 if (((int) strlen(s) + (int) strlen(tag) + indent) < width) {
277 printf("%s%s%s\n", i, tag, s);
283 cp = s + width - indent - strlen(tag);
284 while (!isspace((unsigned char)*cp) && (cp != s))
288 printf("%s%s%s\n", i, tag, s);
290 *cp = c; /* don't mess up 's' */
295 #define OUT_OF_QUOTE 1
298 strip_ignore_chars( char *cp )
300 int had_a_comma = FALSE;
301 int flag = OUT_OF_QUOTE;
302 register char *rcp, *cp1;
307 printf("strip_ignore_chars(%s)\n", cp);
309 for (rcp = cp; *rcp != '\0'; rcp++)
310 if (isignorechar(*rcp) || (*rcp == '"'))
315 cp1 = tmp = (char *) Malloc((unsigned) strlen(cp));
316 for (rcp = cp; *rcp != '\0'; rcp++) {
317 /* toss quotes and flip the flag */
319 flag = OUT_OF_QUOTE - flag;
320 else if (isignorechar(*rcp)) {
321 if (flag == OUT_OF_QUOTE)
326 else if (*rcp == ',') {
335 /* re-quote the name if it had a comma in it */
336 if (had_a_comma == TRUE) {
337 rcp = cp1 = (char *) Malloc((unsigned) (strlen(tmp) + 3));
352 case LDAP_MOD_ADD : return("ADD");
353 case LDAP_MOD_DELETE : return("DELETE");
354 case LDAP_MOD_REPLACE : return("REPLACE");
355 default : return("?????");
360 friendly_name( char *s )
362 static LDAPFriendlyMap *map = NULL;
365 cp = ldap_friendly_name(FRIENDLYFILE, s, &map);
373 /* return TRUE if s has the syntax of a uniqname */
375 isauniqname( char *s )
379 if ((i < 3) || (i > 8)) /* uniqnames are 3-8 chars */
381 if (!isalpha((unsigned char)*s)) /* uniqnames begin with a letter */
383 for ( ; *s != '\0'; s++) /* uniqnames are alphanumeric */
384 if (!isalnum((unsigned char)*s))
390 /* return TRUE if this attribute should be printed as a DN */
396 for (i = 0; attrlist[i].quipu_name != NULL; i++)
397 if (!strcasecmp(s, attrlist[i].quipu_name))
399 if (attrlist[i].flags & ATTR_FLAG_IS_A_DN)
405 my_ldap_dn2ufn( char *s )
408 static char short_DN[BUFSIZ];
410 if (strstr(s, NULL) == NULL)
411 return(ldap_dn2ufn(s));
412 cpp = ldap_explode_dn(s, TRUE);
413 sprintf(short_DN, "%s, %s", *cpp, *(cpp + 1));
414 ldap_value_free(cpp);
418 /* return TRUE if this attribute should be printed as a URL */
424 for (i = 0; attrlist[i].quipu_name != NULL; i++)
425 if (!strcasecmp(s, attrlist[i].quipu_name))
427 if (attrlist[i].flags & ATTR_FLAG_IS_A_URL)
432 /* return TRUE if this attribute should be printed as a date and time */
438 for (i = 0; attrlist[i].quipu_name != NULL; i++)
439 if (!strcasecmp(s, attrlist[i].quipu_name))
441 if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
447 Malloc( unsigned int size )
451 void_ptr = (void *) malloc(size);
452 if (void_ptr == NULL) {
469 while (isspace((unsigned char) *s) && (*s != '\0'))
479 free_mod_struct( LDAPMod *modp )
481 if (modp->mod_values != NULL)
482 (void) ldap_value_free(modp->mod_values);
483 Free(modp->mod_type);
488 StrFreeDup( char **ptr, char *new_value )
492 if (new_value == NULL)
495 *ptr = strdup(new_value);
500 confirm_action( char *msg )
502 char tmp[SMALL_BUF_SIZE];
507 format( msg, 75, 2 );
510 printf("\n Is this OK? ");
513 fetch_buffer(tmp, sizeof(tmp), stdin);
516 ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));