599 lines
15 KiB
C
599 lines
15 KiB
C
/*
|
|
* The new sysinstall program.
|
|
*
|
|
* This is probably the last program in the `sysinstall' line - the next
|
|
* generation being essentially a complete rewrite.
|
|
*
|
|
* $Id: index.c,v 1.6 1995/10/15 12:41:00 jkh Exp $
|
|
*
|
|
* Copyright (c) 1995
|
|
* Jordan Hubbard. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer,
|
|
* verbatim and that no modifications are made prior to this
|
|
* point in the file.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
* must display the following acknowledgement:
|
|
* This product includes software developed by Jordan Hubbard
|
|
* for the FreeBSD Project.
|
|
* 4. The name of Jordan Hubbard or the FreeBSD project may not be used to
|
|
* endorse or promote products derived from this software without specific
|
|
* prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <ncurses.h>
|
|
#include <dialog.h>
|
|
#include "sysinstall.h"
|
|
|
|
/* Macros and magic values */
|
|
#define MAX_MENU 13
|
|
|
|
/* Smarter strdup */
|
|
inline char *
|
|
_strdup(char *ptr)
|
|
{
|
|
return ptr ? strdup(ptr) : ptr;
|
|
}
|
|
|
|
static char *descrs[] = {
|
|
"Package Selection",
|
|
"To select a package or category, move to it and press SPACE.\n"
|
|
"To remove a package from consideration, press SPACE again.\n"
|
|
"To go to a previous menu, select UP item or Cancel. To search\n"
|
|
"for a package by name, press ESC.",
|
|
"Package Targets", "These are the packages you've selected for extraction.\n\n"
|
|
"If you're sure of these choices, select OK.\n"
|
|
"If not, select Cancel to go back to the package selection menu.\n",
|
|
"All", "All available packages in all categories.",
|
|
"applications", "User application software.",
|
|
"archivers", "Utilities for archiving and unarchiving data.",
|
|
"audio", "Audio utilities - most require a supported sound card.",
|
|
"benchmarks", "Utilities for measuring system performance.",
|
|
"benchmarking", "Utilities for measuring system performance.",
|
|
"cad", "Computer Aided Design utilities.",
|
|
"comms", "Communications utilities.",
|
|
"databases", "Database software.",
|
|
"devel", "Software development utilities and libraries.",
|
|
"development", "Software development utilities and libraries.",
|
|
"documentation", "Document preparation utilities.",
|
|
"editors", "Common text editors.",
|
|
"emulation", "Utilities for emulating other OS types.",
|
|
"emulators", "Utilities for emulating other OS types.",
|
|
"games", "Various and sundry amusements.",
|
|
"graphics", "Graphics libraries and utilities.",
|
|
"japanese", "Ported software for the Japanese market.",
|
|
"lang", "Computer languages.",
|
|
"languages", "Computer languages.",
|
|
"libraries", "Software development libraries.",
|
|
"mail", "Electronic mail packages and utilities.",
|
|
"math", "Mathematical computation software.",
|
|
"net", "Networking utilities.",
|
|
"networking", "Networking utilities.",
|
|
"news", "USENET News support software.",
|
|
"numeric", "Mathematical computation software.",
|
|
"orphans", "Packages without a home elsewhere.",
|
|
"plan9", "Software from the plan9 Operating System.",
|
|
"print", "Utilities for dealing with printing.",
|
|
"printing", "Utilities for dealing with printing.",
|
|
"programming", "Software development utilities and libraries.",
|
|
"russian", "Ported software for the Russian market.",
|
|
"security", "System security software.",
|
|
"shells", "Various shells (tcsh, bash, etc).",
|
|
"sysutils", "Various system utilities.",
|
|
"troff", "TROFF Text formatting utilities.",
|
|
"utils", "Various user utilities.",
|
|
"utilities", "Various user utilities.",
|
|
"x11", "X Window System based utilities.",
|
|
NULL, NULL,
|
|
};
|
|
|
|
static char *
|
|
fetch_desc(char *name)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; descrs[i]; i += 2) {
|
|
if (!strcmp(descrs[i], name))
|
|
return descrs[i + 1];
|
|
}
|
|
return "No description provided";
|
|
}
|
|
|
|
static PkgNodePtr
|
|
new_pkg_node(char *name, node_type type)
|
|
{
|
|
PkgNodePtr tmp = malloc(sizeof(PkgNode));
|
|
|
|
if (!tmp) {
|
|
fprintf(stderr, "Out of memory - unable to allocate a new package node!\n");
|
|
exit(1);
|
|
}
|
|
bzero(tmp, sizeof(PkgNode));
|
|
tmp->name = _strdup(name);
|
|
tmp->type = type;
|
|
return tmp;
|
|
}
|
|
|
|
static IndexEntryPtr
|
|
new_index(char *name, char *pathto, char *prefix, char *comment, char *descr, char *maint)
|
|
{
|
|
IndexEntryPtr tmp = malloc(sizeof(IndexEntry));
|
|
|
|
if (!tmp) {
|
|
fprintf(stderr, "Out of memory - unable to allocate a new index node!\n");
|
|
exit(1);
|
|
}
|
|
|
|
bzero(tmp, sizeof(IndexEntry));
|
|
tmp->name = _strdup(name);
|
|
tmp->path = _strdup(pathto);
|
|
tmp->prefix = _strdup(prefix);
|
|
tmp->comment = _strdup(comment);
|
|
tmp->descrfile = _strdup(descr);
|
|
tmp->maintainer = _strdup(maint);
|
|
return tmp;
|
|
}
|
|
|
|
static char *
|
|
clip(char *str, int len)
|
|
{
|
|
int len2;
|
|
|
|
len2 = strlen(str);
|
|
if (len2 > len)
|
|
str[len] = '\0';
|
|
return str;
|
|
}
|
|
|
|
static void
|
|
index_register(PkgNodePtr top, char *where, IndexEntryPtr ptr)
|
|
{
|
|
PkgNodePtr p, q;
|
|
|
|
q = NULL;
|
|
p = top->kids;
|
|
while (p) {
|
|
if (!strcmp(p->name, where)) {
|
|
q = p;
|
|
break;
|
|
}
|
|
p = p->next;
|
|
}
|
|
if (!p) {
|
|
/* Add new category */
|
|
q = new_pkg_node(where, PLACE);
|
|
q->desc = fetch_desc(where);
|
|
q->next = top->kids;
|
|
top->kids = q;
|
|
}
|
|
p = new_pkg_node(ptr->name, PACKAGE);
|
|
p->desc = clip(ptr->comment, 52 - (strlen(ptr->name) / 2));
|
|
p->data = ptr;
|
|
p->next = q->kids;
|
|
q->kids = p;
|
|
}
|
|
|
|
static int
|
|
copy_to_sep(char *to, char *from, int sep)
|
|
{
|
|
char *tok;
|
|
|
|
tok = strchr(from, sep);
|
|
if (!tok) {
|
|
fprintf(stderr, "missing '%c' token.\n", sep);
|
|
*to = '\0';
|
|
return 0;
|
|
}
|
|
*tok = '\0';
|
|
strcpy(to, from);
|
|
return tok + 1 - from;
|
|
}
|
|
|
|
int
|
|
index_parse(FILE *fp, char *name, char *pathto, char *prefix, char *comment, char *descr, char *maint,
|
|
char *cats, char *keys)
|
|
{
|
|
char line[1024];
|
|
char *cp;
|
|
int len;
|
|
|
|
if (!fgets(line, 1024, fp)) {
|
|
if (feof(fp))
|
|
return EOF;
|
|
else
|
|
return -2;
|
|
}
|
|
len = strlen(line);
|
|
if (line[len - 1] == '\n')
|
|
line[len - 1] = '\0';
|
|
cp = line;
|
|
cp += copy_to_sep(name, cp, '|');
|
|
cp += copy_to_sep(pathto, cp, '|');
|
|
cp += copy_to_sep(prefix, cp, '|');
|
|
cp += copy_to_sep(comment, cp, '|');
|
|
cp += copy_to_sep(descr, cp, '|');
|
|
cp += copy_to_sep(maint, cp, '|');
|
|
cp += copy_to_sep(cats, cp, '|');
|
|
strcpy(keys, cp);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
index_read(char *fname, PkgNodePtr papa)
|
|
{
|
|
FILE *fp;
|
|
int i;
|
|
|
|
fp = fopen(fname, "r");
|
|
if (!fp) {
|
|
fprintf(stderr, "Unable to open index file `%s' for reading.\n", fname);
|
|
i = -1;
|
|
}
|
|
else {
|
|
i = index_fread(fp, papa);
|
|
fclose(fp);
|
|
}
|
|
return i;
|
|
}
|
|
|
|
int
|
|
index_fread(FILE *fp, PkgNodePtr papa)
|
|
{
|
|
char name[127], pathto[255], prefix[255], comment[255], descr[127], maint[127], cats[511], keys[511];
|
|
|
|
while (!index_parse(fp, name, pathto, prefix, comment, descr, maint, cats, keys)) {
|
|
char *cp, *cp2, tmp[511];
|
|
IndexEntryPtr idx;
|
|
|
|
idx = new_index(name, pathto, prefix, comment, descr, maint);
|
|
/* For now, we only add things to menus if they're in categories. Keywords are ignored */
|
|
cp = strcpy(tmp, cats);
|
|
while ((cp2 = strchr(cp, ' ')) != NULL) {
|
|
*cp2 = '\0';
|
|
index_register(papa, cp, idx);
|
|
cp = cp2 + 1;
|
|
}
|
|
index_register(papa, cp, idx);
|
|
|
|
/* Add to special "All" category */
|
|
index_register(papa, "All", idx);
|
|
}
|
|
fclose(fp);
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
index_init(PkgNodePtr top, PkgNodePtr plist)
|
|
{
|
|
top->next = top->kids = NULL;
|
|
top->name = "Package Selection";
|
|
top->type = PLACE;
|
|
top->desc = fetch_desc(top->name);
|
|
|
|
plist->next = plist->kids = NULL;
|
|
plist->name = "Package Targets";
|
|
plist->type = PLACE;
|
|
plist->desc = fetch_desc(plist->name);
|
|
}
|
|
|
|
void
|
|
index_entry_free(IndexEntryPtr top)
|
|
{
|
|
safe_free(top->name);
|
|
safe_free(top->path);
|
|
safe_free(top->prefix);
|
|
safe_free(top->comment);
|
|
safe_free(top->descrfile);
|
|
safe_free(top->maintainer);
|
|
free(top);
|
|
}
|
|
|
|
void
|
|
index_node_free(PkgNodePtr top, PkgNodePtr plist)
|
|
{
|
|
PkgNodePtr tmp;
|
|
|
|
tmp = plist;
|
|
while (tmp) {
|
|
PkgNodePtr tmp2 = tmp->next;
|
|
|
|
safe_free(tmp);
|
|
tmp = tmp2;
|
|
}
|
|
|
|
tmp = top;
|
|
while (tmp) {
|
|
free(tmp->name);
|
|
if (tmp->type == PACKAGE && tmp->data)
|
|
index_entry_free((IndexEntryPtr)tmp->data);
|
|
if (tmp->kids)
|
|
index_node_free(tmp->kids, NULL);
|
|
tmp = tmp->next;
|
|
}
|
|
}
|
|
|
|
void
|
|
index_print(PkgNodePtr top, int level)
|
|
{
|
|
int i;
|
|
|
|
while (top) {
|
|
for (i = 0; i < level; i++) putchar('\t');
|
|
printf("name [%s]: %s\n", top->type == PLACE ? "place" : "package", top->name);
|
|
for (i = 0; i < level; i++) putchar('\t');
|
|
printf("desc: %s\n", top->desc);
|
|
if (top->kids)
|
|
index_print(top->kids, level + 1);
|
|
top = top->next;
|
|
}
|
|
}
|
|
|
|
/* Swap one node for another */
|
|
static void
|
|
swap_nodes(PkgNodePtr a, PkgNodePtr b)
|
|
{
|
|
PkgNode tmp;
|
|
|
|
tmp = *a;
|
|
*a = *b;
|
|
a->next = tmp.next;
|
|
tmp.next = b->next;
|
|
*b = tmp;
|
|
}
|
|
|
|
/* Use a disgustingly simplistic bubble sort to put our lists in order */
|
|
void
|
|
index_sort(PkgNodePtr top)
|
|
{
|
|
PkgNodePtr p, q;
|
|
|
|
/* Sort everything at the top level */
|
|
p = top->kids;
|
|
while (p) {
|
|
q = top->kids;
|
|
while (q) {
|
|
if (q->next && strcmp(q->name, q->next->name) > 0)
|
|
swap_nodes(q, q->next);
|
|
q = q->next;
|
|
}
|
|
p = p->next;
|
|
}
|
|
|
|
/* Now sub-sort everything n levels down */
|
|
p = top->kids;
|
|
while (p) {
|
|
if (p->kids)
|
|
index_sort(p);
|
|
p = p->next;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* No, we don't free n because someone else is still pointing at it.
|
|
* It's just clone linked from another location, which we're adjusting.
|
|
*/
|
|
void
|
|
index_delete(PkgNodePtr n)
|
|
{
|
|
if (n->next)
|
|
*n = *(n->next);
|
|
else /* Kludgy end sentinal */
|
|
n->name = NULL;
|
|
}
|
|
|
|
PkgNodePtr
|
|
index_search(PkgNodePtr top, char *str, PkgNodePtr *tp)
|
|
{
|
|
PkgNodePtr p, sp;
|
|
|
|
for (p = top->kids; p && p->name; p = p->next) {
|
|
/* Subtract out the All category from searches */
|
|
if (!strcmp(p->name, "All"))
|
|
continue;
|
|
|
|
/* If tp == NULL, we're looking for an exact package match */
|
|
if (!tp && !strcmp(p->name, str))
|
|
return p;
|
|
|
|
/* If tp, we're looking for both a package and a pointer to the place it's in */
|
|
if (tp && strstr(p->name, str)) {
|
|
*tp = top;
|
|
return p;
|
|
}
|
|
|
|
/* The usual recursion-out-of-laziness ploy */
|
|
if (p->kids)
|
|
if ((sp = index_search(p, str, tp)) != NULL)
|
|
return sp;
|
|
}
|
|
if (p && !p->name)
|
|
p = NULL;
|
|
return p;
|
|
}
|
|
|
|
/* Work function for seeing if name x is in result string y */
|
|
static Boolean
|
|
is_selected_in(char *name, char *result)
|
|
{
|
|
Boolean ret = FALSE;
|
|
|
|
while (*result) {
|
|
char *cp;
|
|
|
|
cp = index(result, '\n');
|
|
if (!cp) {
|
|
ret = !strcmp(name, result);
|
|
break;
|
|
}
|
|
else {
|
|
ret = !strncmp(name, result, cp - result);
|
|
if (ret)
|
|
break;
|
|
}
|
|
result = cp + 1;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int
|
|
index_menu(PkgNodePtr top, PkgNodePtr plist, int *pos, int *scroll)
|
|
{
|
|
int n, rval;
|
|
int curr, max;
|
|
PkgNodePtr sp, kp;
|
|
char **nitems;
|
|
char result[127];
|
|
Boolean hasPackages;
|
|
|
|
curr = max = 0;
|
|
hasPackages = FALSE;
|
|
nitems = NULL;
|
|
|
|
n = 0;
|
|
kp = top->kids;
|
|
/* Figure out if this menu is full of "leaves" or "branches" */
|
|
while (kp && kp->name) {
|
|
++n;
|
|
if (kp->type == PACKAGE) {
|
|
hasPackages = TRUE;
|
|
break;
|
|
}
|
|
kp = kp->next;
|
|
}
|
|
if (!n) {
|
|
msgConfirm("The %s menu is empty.", top->name);
|
|
return RET_DONE;
|
|
}
|
|
|
|
dialog_clear();
|
|
while (1) {
|
|
n = 0;
|
|
kp = top->kids;
|
|
if (!hasPackages && kp && kp->name && plist) {
|
|
nitems = item_add_pair(nitems, "UP", ".. RETURN TO PREVIOUS MENU ..", &curr, &max);
|
|
++n;
|
|
}
|
|
while (kp && kp->name) {
|
|
nitems = item_add_pair(nitems, kp->name, kp->desc, &curr, &max);
|
|
if (hasPackages) {
|
|
if (kp->type == PACKAGE)
|
|
nitems = item_add(nitems, index_search(plist, kp->name, NULL) ? "ON" : "OFF", &curr, &max);
|
|
else
|
|
nitems = item_add(nitems, "OFF", &curr, &max);
|
|
}
|
|
++n;
|
|
kp = kp->next;
|
|
}
|
|
nitems = item_add(nitems, NULL, &curr, &max);
|
|
|
|
if (hasPackages)
|
|
rval = dialog_checklist(top->name, top->desc, -1, -1, n > MAX_MENU ? MAX_MENU : n, n,
|
|
(unsigned char **)nitems, result);
|
|
else /* It's a categories menu */
|
|
rval = dialog_menu(top->name, top->desc, -1, -1, n > MAX_MENU ? MAX_MENU : n, n,
|
|
(unsigned char **)nitems, result, pos, scroll);
|
|
items_free(nitems, &curr, &max);
|
|
if (!rval && plist && strcmp(result, "UP")) {
|
|
kp = top->kids;
|
|
while (kp) {
|
|
if (kp->type == PACKAGE) {
|
|
sp = index_search(plist, kp->name, NULL);
|
|
if (is_selected_in(kp->name, result)) {
|
|
if (!sp) {
|
|
PkgNodePtr n = (PkgNodePtr)malloc(sizeof(PkgNode));
|
|
|
|
if (n) {
|
|
*n = *kp;
|
|
n->next = plist->kids;
|
|
plist->kids = n;
|
|
standout();
|
|
mvprintw(23, 0, "Adding package %s to selection list\n", kp->name);
|
|
standend();
|
|
refresh();
|
|
}
|
|
}
|
|
}
|
|
else if (sp) {
|
|
standout();
|
|
mvprintw(23, 0, "Deleting package %s from selection list\n", kp->name);
|
|
standend();
|
|
refresh();
|
|
index_delete(sp);
|
|
}
|
|
}
|
|
else if (!strcmp(kp->name, result)) { /* Not a package, must be a directory */
|
|
int p, s;
|
|
|
|
p = s = 0;
|
|
index_menu(kp, plist, &p, &s);
|
|
}
|
|
kp = kp->next;
|
|
}
|
|
}
|
|
else if (rval == -1) {
|
|
static char *cp;
|
|
PkgNodePtr menu;
|
|
|
|
/* Search */
|
|
if ((cp = msgGetInput(cp, "Search by package name. Please enter search string:")) != NULL) {
|
|
PkgNodePtr p = index_search(top, cp, &menu);
|
|
|
|
if (p) {
|
|
int pos, scroll;
|
|
|
|
/* These need to be set to point at the found item, actually. Hmmm! */
|
|
pos = scroll = 0;
|
|
index_menu(menu, plist, &pos, &scroll);
|
|
}
|
|
else
|
|
msgConfirm("Search string: %s yielded no hits.", cp);
|
|
}
|
|
}
|
|
else {
|
|
dialog_clear();
|
|
return rval ? RET_FAIL : RET_SUCCESS;
|
|
}
|
|
}
|
|
}
|
|
|
|
int
|
|
index_extract(Device *dev, PkgNodePtr plist)
|
|
{
|
|
PkgNodePtr tmp;
|
|
|
|
tmp = plist->kids;
|
|
while (tmp) {
|
|
if (package_extract(dev, tmp->name) != RET_SUCCESS) {
|
|
if (optionIsSet(OPT_NO_CONFIRM))
|
|
msgNotify("Unable to locate package %s..", tmp->name);
|
|
else
|
|
msgConfirm("Unable to locate package %s..", tmp->name);
|
|
}
|
|
tmp = tmp->next;
|
|
}
|
|
return RET_SUCCESS;
|
|
}
|