home *** CD-ROM | disk | FTP | other *** search
- /* Systems Sciences Laboratory, Webster Research Center */
-
- static char *PROGRAM_information[] =
- {
- "Copyright (c) 1988 Xerox Corporation. All rights reserved.",
- "$Header$",
- "$Locker$"
- }
- ;
-
- /*
- * Copyright protection claimed includes all forms and matters of copyrightable
- * material and information now allowed by statutory or judicial lay or
- * herinafter granted, including without limitation, material generated from
- * the software programs which are displayed on the screen such as icons,
- * screen display looks, etc.
- */
-
-
- #include "xfilebrowser.h"
- #include <ctype.h>
- #include <sys/file.h>
-
- extern char *getenv();
- extern char *re_comp();
-
- typedef enum { JHSXEDIT, EMACS, VI, XEDIT, XMORE, ED_UNKNOWN } edittype;
- typedef enum { DELETE, MOVE, COPY } requesttype;
-
- static DialogData dialogdata;
- static unsigned int cmdsize = 1024;
-
-
- DoQuit()
- {
- XtUnmapWidget(toplevel);
- XCloseDisplay(curdisplay);
- exit(0);
- }
-
- /* ARGSUSED */
- setup_pattern(source, pattern)
- char *source, *pattern;
- {
- register char *p = source;
- register char *q = pattern;
-
- *q = '^'; q++;
- while (*p != '\0') {
- if (*p == '.') { *q = '\\'; q++; *q = '.'; }
- else if (*p == '$') *q = '.';
- else if (*p == '*') { *q = '.'; q++; *q = '*'; }
- else *q = *p;
-
- q++; p++;
- }
- *q = '$'; q++; *q = '\0';
- }
-
- /* ARGSUSED */
- DoList(w, client_data, call_data)
- Widget w;
- caddr_t client_data, call_data;
- {
- int i;
- char *error;
- char tmppattern[64], curpattern[255];
- XtTextPosition pos1, pos2;
- short dir;
- struct stat buf;
-
- switch ((int)call_data) {
- case 2: dir = -1; break;
- case 3:
- if (!check_option()) display_listoptions(w);
- return;
-
- case 0:
- case 1:
- default: dir = (short)call_data; break;
- }
-
- XtTextGetSelectionPos(listwidget, &pos1, &pos2);
- if (pos1 != pos2 && numfiles != 0 &&
- ( (i = select_file(pos1, pos2)) != -1)) {
- if (stat((*files[i]).d_name, &buf) == -1) {
- disp_message("\nList: cannot check file stats");
- return;
- }
- if (buf.st_mode & S_IFDIR)
- sprintf(curpattern, "%s/%s", (*files[i]).d_name, filepattern);
- else {
- disp_message(
- "\nList: selected file %s is not a directory\nuse current pattern",
- (*files[i]).d_name);
- if (! strcmp(filepattern, oldpattern)) return;
- else strcpy(curpattern, filepattern);
- }
- }
- else strcpy(curpattern, filepattern);
-
- if (strcmp(curpattern, oldpattern)) {
- /* old pattern and new pattern differ */
- if (get_dirpat(curpattern, curdirectory, oldpattern) == -1) {
- disp_message("\nList: access to directory?");
- return;
- }
- }
-
- setup_pattern(oldpattern, tmppattern);
- if (error = re_comp(tmppattern)) {
- disp_message("\nList: search pattern %s?", error);
- return;
- }
-
- clear_widget(grepwidget, grepsource);
- clear_widget(listwidget, listsource);
- free_direct(&files, &numfiles);
- free_hitfiles();
-
- if (prepare_list(curdirectory, dir, &files, &numfiles,
- (char *)NULL) != -1) {
- if (!(chdir(curdirectory))) {
- strcpy(filepattern, oldpattern);
- setup_dirlabel(curdirectory, oldpattern);
- }
- else disp_message("\nList: problems in changing to %s",
- curdirectory);
- display_files(0);
- }
- else clear_widget(listwidget, listsource);
-
- setup_iconname();
-
- return;
- }
-
-
- /***************************
- * Edit Routines
- ****************************/
-
- /* ARGSUSED */
- edittype check_editor(s)
- char *s;
- {
- char *p, *q;
- int len;
- edittype result;
-
- p = s; result = ED_UNKNOWN;
- while ( p != NULL) {
- if ( (q = index(p, ' ')) != NULL) len = q - p;
- else len = strlen(p);
-
- if (! strncmp(p, "jhsxedit", len)) { result = JHSXEDIT; break; }
- else if (! strncmp(p, "xedit", len)) { result = XEDIT; break; }
- else if (! strncmp(p, "emacs", len)) { result = EMACS; break; }
- else if (! strncmp(p, "vi", len)) { result = VI; break; }
- else if (! strncmp(p, "xmore", len)) { result = XMORE; break; }
-
- if (q != NULL) {
- q++;
- while (*q == ' ') q++;
- p = q;
- }
- else break;
- }
-
- return result;
- }
-
- /* ARGSUSED */
- invoke_editor(file, toline, edit)
- char *file;
- int toline;
- Boolean edit;
- {
- int cc;
- char command[255];
- char *editor = NULL, *curviewer;
- struct stat buf;
- edittype edit_view;
- static char xviewer[] = "xmore";
- static char xeditor[] = "xedit";
-
- if (stat(file, &buf) == -1) {
- disp_message("\nEdit: cannot check file stats");
- return;
- }
-
- if ( (buf.st_mode & S_IFMT) == S_IFREG ) {
- if (!edit) {
- edit_view = XMORE;
- curviewer = &xviewer[0];
- }
- else {
- if ( (editor = getenv("XEDITOR")) != NULL) {
- edit_view = check_editor(editor);
- curviewer = editor;
- }
- else {
- edit_view = XEDIT;
- curviewer = &xeditor[0];
- }
- }
-
- if (toline != 0) {
- switch(edit_view) {
- case JHSXEDIT:
- case EMACS:
- case VI:
- sprintf(command, "%s -display %s +%d %s&",
- curviewer, DisplayString(curdisplay), toline, file);
- break;
- default:
- sprintf(command, "%s -display %s %s&",
- curviewer, DisplayString(curdisplay), file);
- break;
- }
- }
- else sprintf(command, "%s -display %s %s&",
- curviewer, DisplayString(curdisplay), file);
- cc = system(command);
- }
- else disp_message("\nEdit: %s not regular file!", file);
- }
-
- DoEdit(w, client_data, call_data)
- Widget w;
- caddr_t client_data, call_data;
- {
- XtTextPosition pos1, pos2;
- int i;
-
- if ( (int)call_data == 3 ) return;
- XtTextGetSelectionPos(listwidget, &pos1, &pos2);
- if (pos1 == pos2 || numfiles == 0) {
- disp_message("\nEdit: no file selected");
- return;
- }
-
- if ( (i = select_file(pos1, pos2)) == -1) {
- disp_message("\nEdit: file selection?");
- return;
- }
-
- switch ( (int)call_data ) {
- case 1: invoke_editor(files[i]->d_name, 0, False); break;
- case 2: invoke_editor(files[i]->d_name, 0, True); break;
- }
-
- return;
- }
-
-
- /***************************
- * Grep/GrepEdit Routines
- ****************************/
-
- DoGrepEdit(w, client_data, call_data)
- Widget w;
- caddr_t client_data, call_data;
- {
- XtTextPosition pos1, pos2;
- int i, linenr;
-
- if ( (int)call_data == 3 ) return;
-
- XtTextGetSelectionPos(grepwidget, &pos1, &pos2);
- if (pos1 == pos2 || numhitfiles == 0) {
- disp_message("\nEdit: no file selected");
- return;
- }
-
- if ( (i = select_searchfile(pos1, pos2, &linenr)) == -1) {
- disp_message("\nEdit: file selection?");
- return;
- }
- switch ( (int)call_data ) {
- case 1: invoke_editor(hitfiles[i]->name, linenr, False); break;
- case 2: invoke_editor(hitfiles[i]->name, linenr, True); break;
- }
-
- return;
- }
-
-
- DoGrep(w, client_data, call_data)
- Widget w;
- caddr_t client_data, call_data;
- {
- XtTextPosition pos1, pos2, fstart, fend;
- Boolean stopwhenfound = False;
-
- switch ((int)call_data) {
- case 3:
- if (!check_search()) display_searchoptions(w);
- return;
-
- case 2: stopwhenfound = True; break;
- case 1: break;
- default: return;
- }
-
- if (numfiles == 0) {
- disp_message("\nGrep: no files listed");
- return;
- }
-
- if (strlen(searchpattern) == 0) {
- disp_message("\nGrep: search pattern?");
- return;
- }
- if (index(searchpattern, '\n') != NULL) {
- disp_message("\nGrep: search pattern contains newline");
- return;
- }
-
- clear_widget(grepwidget, grepsource);
- free_hitfiles();
-
- XtTextGetSelectionPos(listwidget, &pos1, &pos2);
- if (pos1 == pos2 ||
- select_files(pos1, pos2, &fstart, &fend) == -1)
- { fstart = 0; fend = numfiles -1; }
-
- examine_files(fstart, fend, stopwhenfound);
- }
-
-
- /***************************
- * Shell Routines
- ****************************/
-
- /* ARGSUSED */
- int execute_cmdline(command, saveout, savediag)
- char *command;
- Boolean saveout, savediag;
- {
- int cc, i;
- int cmdindex = 0;
- struct stat statbuf;
- FILE *fin;
- char tmpfile[16];
- char buf[128];
- char *cmd = command;
-
- if (saveout || savediag) {
- cmdindex = strlen(cmd);
- sprintf(tmpfile, "/tmp/xbXXXXXX");
- if (mktemp(tmpfile) == NULL) {
- disp_message("\ncannot create log file");
- return(-1);
- }
-
- strcat(cmd, (savediag) ? " 2>" : " 1>");
- strcat(cmd, tmpfile);
- if (savediag && saveout) strcat(cmd, " 1>&2");
- }
- else *tmpfile = '\0';
-
- cc = system(cmd);
- if (cc != 0)
- disp_message(
- "\nexit status is %d\ncheck command or targetfile",
- cc);
-
- if (*tmpfile != '\0' && stat(tmpfile, &statbuf) == 0) {
- if (statbuf.st_size > 0) {
- if (statbuf.st_size < 127) {
- fin = fopen(tmpfile, "r");
- i = fread(buf, 1, 127, fin);
- fclose(fin);
- buf[i] = '\0';
- disp_message("\n%s", buf);
- }
- else {
- command[cmdindex] = '\0';
- /* delete tmpfile name from command */
- log_popup(command, tmpfile);
- }
- }
- unlink(tmpfile);
- }
-
- XtTextUnsetSelection(listwidget);
- return(cc);
- }
-
-
- shellcancel(clientdata)
- DialogData *clientdata;
- {
- return;
- }
-
- /* ARGSUSED */
- shellok(clientdata)
- DialogData *clientdata;
- {
- char *answer = clientdata->answer;
- char *tilde, *expand, *begquote, *endquote;
-
- if ( (tilde = index(answer, '~')) != NULL) {
- /* check if tilde is between single quotes; then do not
- expand tilde */
- if ( ((begquote = index(answer, '\'')) == NULL) ||
- ((endquote = index(begquote+1, '\'')) == NULL) ||
- (tilde < begquote || tilde > endquote)) {
-
- if ( (expand = expand_tilde(tilde)) == NULL) {
- disp_message("\ncannot expand ~");
- return;
- }
- strcpy(tilde, expand); /* replaces tilde by the actual
- path name */
- XtFree(expand);
- }
- }
-
- execute_cmdline(answer, clientdata->out, clientdata->diag);
- return;
- }
-
- /* ARGSUSED */
- DoShell(w, client_data, call_data)
- Widget w;
- caddr_t client_data, call_data;
- {
- char command[64];
- char label[32];
- char *defvalue;
- XtTextPosition pos1, pos2, fstart, fend;
- int i, len = 0;
-
- switch ((int)call_data) {
- case 2: sprintf(command, "xterm -display %s &",
- DisplayString(curdisplay));
- system(command);
- break;
- case 1:
- if (check_prompt() == -1) return;
- /* there is already a prompt window */
- XtTextGetSelectionPos(listwidget, &pos1, &pos2);
- if (pos1 != pos2 &&
- select_files(pos1, pos2, &fstart, &fend) != -1) {
- if ( (fend - fstart + 1) == numfiles) {
- defvalue = XtMalloc(255);
- strcpy(defvalue, oldpattern);
- }
- else {
- for (i = fstart; i <= fend; i++)
- len += files[i]->d_namlen;
- defvalue = XtCalloc(len + (fend - fstart) + 64);
- for (i = fstart; i <= fend; i++) {
- strcat(defvalue, " ");
- strcat(defvalue, files[i]->d_name);
- }
- }
- }
- else defvalue = XtCalloc(255, 1);
-
- dialogdata.w = w;
- dialogdata.start = 0;
- dialogdata.end = 0;
- dialogdata.current = 0;
- dialogdata.yes = shellok;
- dialogdata.no = NULL;
- dialogdata.cancel = shellcancel;
- sprintf(label, "which shell command to execute?");
- create_toggleprompt(&dialogdata, label, defvalue);
- XtFree(defvalue);
- break;
- case 3:
- default: break;
- }
- return;
- }
-
-
-
- /***************************
- * Copy Routines
- ****************************/
-
- /* ARGSUSED */
- copyok(clientdata)
- DialogData *clientdata;
- {
- int fstart = clientdata->start;
- int fend = clientdata->end;
- int i;
- char *s = clientdata->answer;
- char *name;
-
- strcpy(cmdline, "cp ");
- for (i = fstart; i <= fend; i++) {
- strcat(cmdline, "'");
- strcat(cmdline, files[i]->d_name);
- strcat(cmdline, "' ");
- }
- if (*s == '~') {
- if ( (name = expand_tilde(s)) == NULL) {
- disp_message("\nCopy: cannot expand target name");
- return;
- }
- }
- else name = s;
- strcat(cmdline, name);
-
- if (execute_cmdline(cmdline, FALSE, TRUE) == 0) {
- if (fstart == fend)
- DoList(clientdata->w, (caddr_t)clientdata, (caddr_t)0);
-
- /* copied file may be in the same directory; if values
- are different assume files were copied into different
- directory */
- }
- return;
- }
-
- copycancel(clientdata)
- DialogData *clientdata;
- {
- return;
- }
-
- /* ARGSUSED */
- DoCopy(w, client_data, call_data)
- Widget w;
- caddr_t client_data, call_data;
- {
- XtTextPosition pos1, pos2, fstart, fend;
- char label[32];
- char defvalue[255];
-
- if (check_prompt() == -1) return;
- /* there is already a prompt window */
- if (numfiles == 0) {
- disp_message("\nCopy: no files listed");
- return;
- }
-
- XtTextGetSelectionPos(listwidget, &pos1, &pos2);
- if (pos1 == pos2 || select_files(pos1, pos2, &fstart, &fend) == -1) {
- disp_message("\nCopy: no file selected");
- return;
- }
-
- dialogdata.w = w;
- dialogdata.start = fstart;
- dialogdata.end = fend;
- dialogdata.current = fend;
- dialogdata.yes = copyok;
- dialogdata.no = NULL;
- dialogdata.cancel = copycancel;
-
- if (fstart == fend) {
- sprintf(label, "copy selected file to?");
- sprintf(defvalue, "%s", files[fstart]->d_name);
- }
- else {
- sprintf(label, "copy selected files to directory?");
- defvalue[0] = '\0';
- }
- create_prompt(&dialogdata, label, defvalue);
- return;
- }
-
-
- /***************************
- * Move Routines
- ****************************/
-
- /* ARGSUSED */
- moveok(clientdata)
- DialogData *clientdata;
- {
- int fstart = clientdata->start;
- int fend = clientdata->end;
- int i;
- char *s = clientdata->answer;
- char *name;
-
- strcpy(cmdline, "mv ");
- for (i = fstart; i <= fend; i++) {
- strcat(cmdline, "'");
- strcat(cmdline, files[i]->d_name);
- strcat(cmdline, "' ");
- }
- if (*s == '~') {
- if ( (name = expand_tilde(s)) == NULL) {
- disp_message("\nMove: cannot expand target name");
- return;
- }
- }
- else name = s;
- strcat(cmdline, name);
-
- if (execute_cmdline(cmdline, FALSE, TRUE) == 0) {
- DoList(clientdata->w, (caddr_t)clientdata, (caddr_t)0);
- }
- return;
- }
-
- movecancel(clientdata)
- DialogData *clientdata;
- {
- return;
- }
-
- /* ARGSUSED */
- DoRename(w, client_data, call_data)
- Widget w;
- caddr_t client_data, call_data;
- {
- XtTextPosition pos1, pos2, fstart, fend;
- char label[32];
- char defvalue[255];
-
- if (check_prompt() == -1) return;
- /* there is already a prompt window */
- if (numfiles == 0) {
- disp_message("\nMove: no files listed");
- return;
- }
-
- XtTextGetSelectionPos(listwidget, &pos1, &pos2);
- if (pos1 == pos2 || select_files(pos1, pos2, &fstart, &fend) == -1) {
- disp_message("\nMove: no file selected");
- return;
- }
-
- dialogdata.w = w;
- dialogdata.start = fstart;
- dialogdata.end = fend;
- dialogdata.current = fend;
- dialogdata.yes = moveok;
- dialogdata.no = NULL;
- dialogdata.cancel = movecancel;
-
- if (fstart == fend) {
- sprintf(label, "move selected file to?");
- sprintf(defvalue, "%s", files[fstart]->d_name);
- }
- else {
- sprintf(label, "move selected files to directory?");
- defvalue[0] = '\0';
- }
-
- create_prompt(&dialogdata, label, defvalue);
- return;
- }
-
-
- /***************************
- * Delete Routines
- ****************************/
-
- /* ARGSUSED */
- update_list(fstart, fend, sort)
- int fstart, fend;
- short sort;
- {
- int i;
- int next = fstart;
- int position = files[fstart]->d_pos1;
-
- for (i = fstart; i <= fend; i++) {
- if (files[i]->d_marked == 0) {
- if (next < i) files[next] = files[i];
- next++;
- }
- else if (! access(files[i]->d_name, F_OK) ) {
- disp_message("\nfile %s not removed", files[i]->d_name);
- if (next < i) files[next] = files[i];
- next++;
- }
- else XtFree(files[i]);
- }
-
- if (next == i) return; /* no files were deleted */
-
- for (i = fend + 1; i < numfiles; i++)
- files[next++] = files[i];
- numfiles = next;
- if (sort) ;/* then sort files again */
- display_files(position);
- }
-
- /* ARGSUSED */
- confirm(data, cur, op)
- DialogData *data;
- int cur;
- requesttype op;
- {
- struct stat buf;
- char label[255];
-
- data->current = cur;
- switch (op) {
- case DELETE:
- if (files[cur]->d_type == 'd')
- sprintf(label, "delete directory %s and its content?",
- files[cur]->d_name);
- else if (files[cur]->d_type == 'l') {
- if (stat(files[cur]->d_name, &buf) == -1) {
- sprintf(label,
- "cannot check status of %s; delete anyway?",
- files[cur]->d_name);
- }
- }
- else sprintf(label, "delete file %s",
- files[cur]->d_name);
- break;
-
- default: return;
- }
- create_confirm(data, label);
- }
-
-
- /* ARGSUSED */
- deleteyes(clientdata)
- DialogData *clientdata;
- {
- int cur = clientdata->current;
- struct afile *fp;
-
- fp = files[cur];
- if ( (strlen(cmdline) + strlen(fp->d_name) + 1) >= cmdsize) {
- cmdsize += 1024;
- XtRealloc(cmdsize, cmdsize);
- }
- strcat(cmdline, " '");
- strcat(cmdline, fp->d_name);
- strcat(cmdline, "'");
- fp->d_marked = 1; /* file is marked for deletion */
-
- if (cur < clientdata->end) {
- confirm(clientdata, cur+1, DELETE);
- }
- else {
- if (execute_cmdline(cmdline, FALSE, TRUE) == 0)
- update_list(clientdata->start, clientdata->end, 0);
- }
- return;
- }
-
- /* ARGSUSED */
- deleteno(clientdata)
- DialogData *clientdata;
- {
- int cur = clientdata->current;
- short found = 0;
- int i;
-
- if (cur < clientdata->end) {
- confirm(clientdata, cur+1, DELETE);
- }
- else {
- /* check if there is any file to delete */
- for (i = clientdata->start; i <= clientdata->end; i++) {
- if (files[i]->d_marked != 0) {
- found = 1;
- break;
- }
- }
- if (!found) return;
- if (execute_cmdline(cmdline, FALSE, TRUE) == 0)
- update_list(clientdata->start, clientdata->end, 0);
- }
-
- return;
- }
-
- /* ARGSUSED */
- deletecancel(clientdata)
- DialogData *clientdata;
- {
- int i;
-
- for (i = clientdata->start; i <= clientdata->end; i++)
- files[i]->d_marked = 0;
- return;
- }
-
-
- /* ARGSUSED */
- DoDelete(w, client_data, call_data)
- Widget w;
- caddr_t client_data, call_data;
- {
- XtTextPosition pos1, pos2, fstart, fend;
-
- if (check_confirm() == -1) return;
- /* there is already a prompt window */
- if (numfiles == 0) {
- disp_message("\nDelete: no files listed");
- return;
- }
-
- XtTextGetSelectionPos(listwidget, &pos1, &pos2);
- if (pos1 == pos2 || select_files(pos1, pos2, &fstart, &fend) == -1) {
- disp_message("\nDelete: no file selected");
- return;
- }
-
- dialogdata.w = w;
- dialogdata.yes = deleteyes;
- dialogdata.no = deleteno;
- dialogdata.cancel = deletecancel;
- dialogdata.start = fstart;
- dialogdata.end = fend;
- strcpy(cmdline, "rm -rf");
-
- confirm(&dialogdata, fstart, DELETE);
- return;
- }
-
-
- /*******************************
- * Parent Directory Routines
- ********************************/
-
- DoParent(w, client_data, call_data)
- Widget w;
- caddr_t client_data, call_data;
- {
- char *p;
-
- if ( (int)call_data == 3) return;
- /* right button was pressed; no action is
- taken */
-
- if ( (p = rindex(curdirectory, '/')) == NULL)
- disp_message("\nParent: cannot select parent directory");
- else {
- if ( (p - curdirectory) == 0) p[1] = '\0';
- /* already at the beginning */
- else p[0] = '\0';
- if (!(chdir(curdirectory)))
- setup_dirlabel(curdirectory, filepattern);
- else disp_message("\nParent: problems in changing to %s",
- curdirectory);
- }
-
- XtTextUnsetSelection(listwidget);
-
- switch ((int)call_data) {
- case 2:
- clear_widget(listwidget, listsource); break;
-
- case 1:
- default:
- XtTextUnsetSelection(listwidget);
- DoList(w, client_data, (caddr_t)0); break;
- }
-
- return;
- }
-