command.c (10882) | command.c (12661) |
---|---|
1/* 2 * The new sysinstall program. 3 * 4 * This is probably the last program in the `sysinstall' line - the next 5 * generation being essentially a complete rewrite. 6 * | 1/* 2 * The new sysinstall program. 3 * 4 * This is probably the last program in the `sysinstall' line - the next 5 * generation being essentially a complete rewrite. 6 * |
7 * $Id: command.c,v 1.11.4.1 1995/07/21 11:45:35 rgrimes Exp $ | 7 * $Id: command.c,v 1.12 1995/09/18 16:52:21 peter Exp $ |
8 * 9 * Copyright (c) 1995 10 * Jordan Hubbard. All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright --- 56 unchanged lines hidden (view full) --- 72 numCommands = 0; 73} 74 75static void 76addit(char *key, int type, void *cmd, void *data) 77{ 78 int i; 79 | 8 * 9 * Copyright (c) 1995 10 * Jordan Hubbard. All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright --- 56 unchanged lines hidden (view full) --- 72 numCommands = 0; 73} 74 75static void 76addit(char *key, int type, void *cmd, void *data) 77{ 78 int i; 79 |
80 /* First, look for the key already present and add a command to it */ | 80 /* First, look for the key already present and add a command to it if found */ |
81 for (i = 0; i < numCommands; i++) { 82 if (!strcmp(commandStack[i]->key, key)) { 83 if (commandStack[i]->ncmds == MAX_NUM_COMMANDS) 84 msgFatal("More than %d commands stacked up behind %s??", MAX_NUM_COMMANDS, key); 85 commandStack[i]->cmds[commandStack[i]->ncmds].type = type; 86 commandStack[i]->cmds[commandStack[i]->ncmds].ptr = cmd; 87 commandStack[i]->cmds[commandStack[i]->ncmds].data = data; 88 ++(commandStack[i]->ncmds); --- 4 unchanged lines hidden (view full) --- 93 msgFatal("More than %d commands accumulated??", MAX_CMDS); 94 95 /* If we fell to here, it's a new key */ 96 commandStack[numCommands] = safe_malloc(sizeof(Command)); 97 strcpy(commandStack[numCommands]->key, key); 98 commandStack[numCommands]->ncmds = 1; 99 commandStack[numCommands]->cmds[0].type = type; 100 commandStack[numCommands]->cmds[0].ptr = cmd; | 81 for (i = 0; i < numCommands; i++) { 82 if (!strcmp(commandStack[i]->key, key)) { 83 if (commandStack[i]->ncmds == MAX_NUM_COMMANDS) 84 msgFatal("More than %d commands stacked up behind %s??", MAX_NUM_COMMANDS, key); 85 commandStack[i]->cmds[commandStack[i]->ncmds].type = type; 86 commandStack[i]->cmds[commandStack[i]->ncmds].ptr = cmd; 87 commandStack[i]->cmds[commandStack[i]->ncmds].data = data; 88 ++(commandStack[i]->ncmds); --- 4 unchanged lines hidden (view full) --- 93 msgFatal("More than %d commands accumulated??", MAX_CMDS); 94 95 /* If we fell to here, it's a new key */ 96 commandStack[numCommands] = safe_malloc(sizeof(Command)); 97 strcpy(commandStack[numCommands]->key, key); 98 commandStack[numCommands]->ncmds = 1; 99 commandStack[numCommands]->cmds[0].type = type; 100 commandStack[numCommands]->cmds[0].ptr = cmd; |
101 commandStack[numCommands++]->cmds[0].data = data; | 101 commandStack[numCommands]->cmds[0].data = data; 102 ++numCommands; |
102} 103 104/* Add a shell command under a given key */ 105void 106command_shell_add(char *key, char *fmt, ...) 107{ 108 va_list args; 109 char *cmd; 110 | 103} 104 105/* Add a shell command under a given key */ 106void 107command_shell_add(char *key, char *fmt, ...) 108{ 109 va_list args; 110 char *cmd; 111 |
111 cmd = (char *)safe_malloc(1024); | 112 cmd = (char *)safe_malloc(256); |
112 va_start(args, fmt); | 113 va_start(args, fmt); |
113 vsnprintf(cmd, 1024, fmt, args); | 114 vsnprintf(cmd, 256, fmt, args); |
114 va_end(args); 115 116 addit(key, CMD_SHELL, cmd, NULL); 117} 118 119/* Add a shell command under a given key */ 120void 121command_func_add(char *key, commandFunc func, void *data) 122{ 123 addit(key, CMD_FUNCTION, func, data); 124} 125 | 115 va_end(args); 116 117 addit(key, CMD_SHELL, cmd, NULL); 118} 119 120/* Add a shell command under a given key */ 121void 122command_func_add(char *key, commandFunc func, void *data) 123{ 124 addit(key, CMD_FUNCTION, func, data); 125} 126 |
126/* arg to sort */ | |
127static int | 127static int |
128sort_compare(const void *p1, const void *p2) | 128sort_compare(Command *p1, Command *p2) |
129{ | 129{ |
130 return strcmp(((Command *)p1)->key, ((Command *)p2)->key); | 130 if (!p1 && !p2) 131 return 0; 132 else if (!p1 && p2) /* NULL has a "greater" value for commands */ 133 return 1; 134 else if (p1 && !p2) 135 return -1; 136 else 137 return strcmp(p1->key, p2->key); |
131} 132 133void 134command_sort(void) 135{ | 138} 139 140void 141command_sort(void) 142{ |
136 qsort(commandStack, numCommands, sizeof(Command *), sort_compare); | 143 int i, j; 144 145 commandStack[numCommands] = NULL; 146 /* Just do a crude bubble sort since the list is small */ 147 for (i = 0; i < numCommands; i++) { 148 for (j = 0; j < numCommands; j++) { 149 if (sort_compare(commandStack[j], commandStack[j + 1]) > 0) { 150 Command *tmp = commandStack[j]; 151 152 commandStack[j] = commandStack[j + 1]; 153 commandStack[j + 1] = tmp; 154 } 155 } 156 } |
137} 138 139/* Run all accumulated commands in sorted order */ 140void 141command_execute(void) 142{ 143 int i, j, ret; 144 commandFunc func; --- 5 unchanged lines hidden (view full) --- 150 msgNotify("Doing %s", commandStack[i]->cmds[j].ptr); 151 ret = vsystem((char *)commandStack[i]->cmds[j].ptr); 152 if (isDebug()) 153 msgDebug("Command `%s' returns status %d\n", commandStack[i]->cmds[j].ptr, ret); 154 } 155 else { 156 /* It's a function pointer - call it with the key and the data */ 157 func = (commandFunc)commandStack[i]->cmds[j].ptr; | 157} 158 159/* Run all accumulated commands in sorted order */ 160void 161command_execute(void) 162{ 163 int i, j, ret; 164 commandFunc func; --- 5 unchanged lines hidden (view full) --- 170 msgNotify("Doing %s", commandStack[i]->cmds[j].ptr); 171 ret = vsystem((char *)commandStack[i]->cmds[j].ptr); 172 if (isDebug()) 173 msgDebug("Command `%s' returns status %d\n", commandStack[i]->cmds[j].ptr, ret); 174 } 175 else { 176 /* It's a function pointer - call it with the key and the data */ 177 func = (commandFunc)commandStack[i]->cmds[j].ptr; |
158 msgNotify("%x: Execute(%s, %s)", func, commandStack[i]->key, commandStack[i]->cmds[j].data); | 178 if (isDebug()) 179 msgDebug("%x: Execute(%s, %s)", func, commandStack[i]->key, commandStack[i]->cmds[j].data); |
159 ret = (*func)(commandStack[i]->key, commandStack[i]->cmds[j].data); 160 if (isDebug()) 161 msgDebug("Function @ %x returns status %d\n", commandStack[i]->cmds[j].ptr, ret); 162 } 163 } 164 } 165} | 180 ret = (*func)(commandStack[i]->key, commandStack[i]->cmds[j].data); 181 if (isDebug()) 182 msgDebug("Function @ %x returns status %d\n", commandStack[i]->cmds[j].ptr, ret); 183 } 184 } 185 } 186} |