home *** CD-ROM | disk | FTP | other *** search
- /* vi:tabstop=4:shiftwidth=4:smartindent
- *
- * autoconv.c - Add a command name to the list for automatic
- * uname() argument conversions.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <unistd.h>
- #include "psh.h"
-
- struct cmd_list
- {
- char *command;
- struct cmd_list *next;
- } *convs = NULL;
-
- int sh_autoconv(int argc, char **argv)
- {
- struct cmd_list *ptr, *new, *old;
-
- if (argc == 1)
- {
- int len = 0, i;
-
- printf("Commands for auto-conversion:\n");
- for (ptr=convs; ptr!=NULL; ptr=ptr->next)
- {
- if ((i = strlen(ptr->command)) > len)
- {
- len = i;
- }
- }
- len = 8*((len+8)/8)-1;
-
- i = 0;
- for (ptr=convs; ptr!=NULL; ptr=ptr->next)
- {
- printf("%-*s ", len, ptr->command);
- if ((i+=len+1) > 79-len)
- {
- putchar('\n');
- i = 0;
- }
- }
- if (i)
- {
- putchar('\n');
- }
- }
- else
- {
- while (++argv, --argc)
- {
- new = (struct cmd_list *) malloc(sizeof(struct cmd_list));
- if (new == NULL)
- {
- fprintf(stderr, "Out of memory!\n");
- return 0;
- }
- new->command = strdup(*argv);
- if (new->command == NULL)
- {
- fprintf(stderr, "Out of memory!\n");
- return 0;
- }
-
- if ((convs == NULL) || (strcmp(convs->command, *argv) > 0))
- {
- new->next = convs;
- convs = new;
- }
- else
- {
- for (ptr = convs;
- (ptr->next!=NULL) && (strcmp(ptr->command, *argv) < 0);
- ptr=ptr->next) old=ptr;
-
- if (strcmp(ptr->command, *argv) == 0)
- {
- fprintf(stderr, "%s already in the list\n", *argv);
- free(new->command);
- free(new);
- }
- else
- {
- if (ptr->next == NULL)
- {
- new->next = NULL;
- ptr->next = new;
- }
- else
- {
- new->next = old->next;
- old->next = new;
- }
- }
- }
- }
- }
- return 0;
- }
-
- char *autoconv(char *line)
- {
- static char new_line[MAXLEN];
- char *optr = new_line;
- char *iptr = line;
- char *sptr, tmp;
- struct cmd_list *ptr;
-
- for (ptr=convs; ptr!=NULL; ptr=ptr->next)
- {
- if (!strncmp(line, ptr->command, strlen(ptr->command)))
- {
- break;
- }
- }
-
- if (ptr == NULL)
- {
- return line;
- }
-
- while (!isspace(*iptr) && (*iptr != '\0'))
- {
- *optr++ = *iptr++;
- }
- while (*iptr != '\0')
- {
- while (isspace(*iptr))
- {
- *optr++ = *iptr++;
- }
- if (*iptr != '\0')
- {
- sptr = iptr;
- while (!isspace(*iptr) && (*iptr != '\0'))
- {
- iptr++;
- }
- tmp = *iptr;
- *iptr = '\0';
- strcpy(optr, __uname(sptr, 0));
- *iptr = tmp;
- optr += strlen(optr);
- }
- }
- *optr = '\0';
-
- puts(new_line);
- return new_line;
- }
-