/**************************************************************/ /* */ /* UNIX 1 / WS 92/93 Gruppe ux803 */ /* 7. Uebung - Aufgabe 5 - builtin.c */ /* */ /* Vorname Name Matrikelnr. */ /* --------- ------- ------------- */ /* Dietmar Dierks 125761 */ /* Roman Czyborra 127221 */ /* Torsten Buller 117894 */ /* Gerasimos Paliatsaras 140956 */ /* */ /**************************************************************/ #include "msh.h" /* MAXPROC struct kommando */ #include /* getenv */ #include /* MAXPATHLEN */ #include /* pw_dir */ #define EXIT 1 #define CHDIR 2 #define PWD 3 #define LISTALL 4 #define WAIT 5 #define JOBS 6 #define FG 7 #define BG 8 #define MENU 9 #define DONE 1 #define NONE 0 static struct map { char * name; int function; } builtins [] = { {"exit", EXIT}, {"quit", EXIT}, {"cd", CHDIR}, {"pwd", PWD}, {"la", LISTALL}, {"wait", WAIT}, {"jobs", JOBS}, {"fg", FG}, {"bg", BG}, {"help", MENU}, {"menu", MENU}, {NONE, NONE} }; static int jobspecified (struct kommando *kp) { static int i; if (kp->num_tok1 == 1) return -1; i = atoi (kp->token_1[1]); if (i < 0 || i >= SLOTS) { printf ("better try job[0..%d]!\n", SLOTS-1); return -1; } return i; } int do_builtin (kp) struct kommando * kp; { static struct map * mp; static char *home, buf [MAXPATHLEN]; if (!kp-> num_tok1) return DONE; for (mp = builtins; mp->name && strcmp (mp->name, kp->token_1[0]); ++mp); switch (mp -> function) { case EXIT: exit (0); case CHDIR: if (kp->num_tok1 > 1) chdir (kp->token_1[1]) == -1 ? perror ("chdir") : 0; else { home = getenv ("HOME"); if (!home) home = getpwuid (getuid ()) -> pw_dir; chdir (home); } /* flow through */ case PWD: printf ("cwd=%s\n", getwd (buf)); return DONE; case LISTALL: do_ls (kp); return DONE; case WAIT: do_wait (jobspecified (kp), 1); return DONE; case JOBS: do_jobs (); return DONE; case FG: do_fg (jobspecified (kp)); return DONE; case BG: do_bg (jobspecified (kp)); return DONE; case MENU: printf ("help menu of msh builtin functions:\n"); for (mp = builtins; mp->name; ++mp) printf ("%s ", mp->name); printf ("\n"); return DONE; case NONE: return NONE; default: printf ("error in builtin's map!\n"); return DONE; } }