1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
* Copyright 1998-1999, University of Notre Dame.
* Authors: Brian W. Barrett, Arun F. Rodrigues, Jeffrey M. Squyres,
* and Andrew Lumsdaine
*
* This file is part of XMPI
*
* You should have received a copy of the License Agreement for XMPI
* along with the software; see the file LICENSE. If not, contact
* Office of Research, University of Notre Dame, Notre Dame, IN 46556.
*
* Permission to modify the code and to distribute modified code is
* granted, provided the text of this NOTICE is retained, a notice that
* the code was modified is included with the above COPYRIGHT NOTICE and
* with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
* file is distributed with the modified code.
*
* LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
* By way of example, but not limitation, Licensor MAKES NO
* REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
* PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
* OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
* OR OTHER RIGHTS.
*
* Additional copyrights may follow.
*
* $Id: xmpi_browse.c,v 1.4 1999/11/11 04:47:33 arodrig6 Exp $
*
* Function: - program browser panel in builder dialog
*/
#define _NO_PROTO
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/PanedW.h>
#include <Xm/TextF.h>
#include <stdlib.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "all_list.h"
#include "lam.h"
#include "xmpi.h"
/*
* global functions
*/
void xmpi_browse_build();
void xmpi_browse_fill();
/*
* external functions
*/
extern char *getworkdir();
/*
* local functions
*/
static void browse_dir_cb();
static void browse_select_cb();
static int fnamecmp();
/*
* local variables
*/
static Widget list_w = 0; /* directory contents list */
static Widget text_w = 0; /* current directory text field */
/*
* xmpi_browse_build
*
* Function: - builds file browse panel
* Accepts: - parent widget
*/
void
xmpi_browse_build(parent_w)
Widget parent_w;
{
Widget mgr_w;
char *cwd;
XmString xstr;
mgr_w = XtVaCreateWidget("browse_mgr",
xmPanedWindowWidgetClass, parent_w,
XmNsashWidth, 1,
XmNsashHeight, 1,
XmNseparatorOn, False,
XmNleftAttachment, XmATTACH_POSITION,
XmNleftPosition, 1,
XmNleftOffset, 5,
XmNrightAttachment, XmATTACH_POSITION,
XmNrightPosition, 2,
XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
NULL);
xstr = XmStringCreateSimple("Browse Programs");
XtVaCreateManagedWidget("banner",
xmLabelWidgetClass, mgr_w,
XmNlabelString, xstr,
NULL);
XmStringFree(xstr);
text_w = XtVaCreateManagedWidget("browse_text",
xmTextFieldWidgetClass, mgr_w,
NULL);
XtAddCallback(text_w, XmNactivateCallback, browse_dir_cb, NULL);
list_w = XmCreateScrolledList(mgr_w, "browse_list", NULL, 0);
XtVaSetValues(list_w,
XmNscrollBarDisplayPolicy, XmSTATIC,
XmNscrollHorizontal, False,
XmNselectionPolicy, XmBROWSE_SELECT,
NULL);
XtAddCallback(list_w, XmNdefaultActionCallback,
browse_select_cb, NULL);
XtAddCallback(list_w, XmNbrowseSelectionCallback,
browse_select_cb, NULL);
XtManageChild(list_w);
/*
* Initialize the text field to the current directory.
* Fill the list with the current directory contents.
*/
if ((cwd = getworkdir())) {
XmTextFieldSetString(text_w, cwd);
XmTextFieldSetInsertionPosition(text_w, strlen(cwd));
xmpi_run_set_prog("", cwd);
free(cwd);
xmpi_browse_fill(list_w);
}
xmpi_nosash(mgr_w);
XtManageChild(mgr_w);
}
/*
* browse_dir_cb
*
* Function: - take action on the selected directory
* Accepts: - widget
* - client data
* - callback ptr
*/
static void
browse_dir_cb(w, cdata, p_callback)
Widget w;
XtPointer cdata;
XmAnyCallbackStruct *p_callback;
{
char *dirpath;
dirpath = XmTextFieldGetString(w);
if ((dirpath == 0) || (*dirpath == '\0')) {
XtFree(dirpath);
return;
}
/*
* Change to new directory.
*/
if (chdir(dirpath)) {
xmpi_error(w, dirpath);
XtFree(dirpath);
return;
}
xmpi_run_set_prog("", dirpath);
xmpi_browse_fill(list_w);
XtFree(dirpath);
}
/*
* browse_select_cb
*
* Function: - selects a program or new directory
* Accepts: - widget
* - client data
* - callback ptr
*/
static void
browse_select_cb(w, cdata, cbs)
Widget w;
XtPointer cdata;
XmListCallbackStruct *cbs;
{
char *filename;
char *p;
struct stat fileinfo;
XmStringGetLtoR(cbs->item, XmSTRING_DEFAULT_CHARSET, &filename);
if (lstat(filename, &fileinfo)) {
xmpi_error(w, filename);
XtFree(filename);
return;
}
/*
* If the file is a sym-link, stat() the real file.
*/
if (S_ISLNK(fileinfo.st_mode)) {
if (stat(filename, &fileinfo)) {
xmpi_error(w, filename);
XtFree(filename);
return;
}
}
/*
* If the file is a directory, change to it.
*/
if (S_ISDIR(fileinfo.st_mode) &&
(cbs->reason == (int) XmCR_DEFAULT_ACTION)) {
if (chdir(filename)) {
xmpi_error(w, filename);
XtFree(filename);
return;
}
if ((p = getworkdir()) == 0) {
xmpi_error(w, "Cannot get current directory");
XtFree(filename);
return;
}
XmTextFieldSetString(text_w, p);
XmTextFieldSetInsertionPosition(text_w, strlen(p));
xmpi_run_set_prog("", p);
xmpi_browse_fill(list_w);
free(p);
}
/*
* Write the selected file into the program text area.
*/
else if (!S_ISDIR(fileinfo.st_mode)) {
if ((p = getworkdir()) == 0) {
xmpi_error(w, "Cannot get current directory");
XtFree(filename);
return;
}
xmpi_run_set_prog(filename, p);
free(p);
}
XtFree(filename);
}
/*
* xmpi_browse_fill
*
* Function: - fills a list with the contents of the
* current directory
* Accepts: - list widget
*/
void
xmpi_browse_fill(w)
Widget w;
{
DIR *pdir; /* opened directory */
struct dirent *pfile; /* directory entry ptr */
XmString xstr; /* Motif string */
struct stat fileinfo; /* file status info */
LIST *sortlist; /* list of sorted filenames */
int fnamelen; /* filename length */
char *fname; /* filename */
char **p; /* favourite pointer */
/*
* Delete all current list entries.
*/
XmListDeselectAllItems(w);
XmListDeleteAllItems(w);
/*
* Open the directory.
*/
if ((pdir = opendir(".")) == 0) xmpi_fail("xmpi (opendir)");
/*
* Initialize the list used for sorting.
*/
sortlist = al_init((int4) sizeof(char *), fnamecmp);
if (sortlist == 0) xmpi_fail("xmpi (al_init)");
/*
* Fill the list with the directory's files.
*/
for (pfile = readdir(pdir); pfile; pfile = readdir(pdir)) {
/*
* Insert the file in the sorted list.
* Add a path delimiter if the file is a directory.
*/
if (lstat(pfile->d_name, &fileinfo)) {
xmpi_error(w, pfile->d_name);
continue;
}
/*
* If a sym-link, stat() the real file.
* Ignore any error and accept the link anyway.
*/
if (S_ISLNK(fileinfo.st_mode)) {
stat(pfile->d_name, &fileinfo);
}
fnamelen = strlen(pfile->d_name);
if (S_ISDIR(fileinfo.st_mode)) {
++fnamelen;
}
fname = malloc((unsigned) fnamelen + 1);
if (fname == 0) xmpi_fail("xmpi (malloc)");
strcpy(fname, pfile->d_name);
if (S_ISDIR(fileinfo.st_mode)) {
strcat(fname, STRSDIR);
}
if (! al_insert(sortlist, (char *) &fname))
xmpi_fail("xmpi (al_insert)");
}
/*
* Loop adding the sorted files to the scrolled list.
*/
p = (char **) al_top(sortlist);
while (p) {
xstr = XmStringCreateSimple(*p);
XmListAddItem(w, xstr, 0);
XmStringFree(xstr);
free(*p);
p = (char **) al_next(sortlist, (char *) p);
}
al_free(sortlist);
closedir(pdir);
}
/*
* fnamecmp
*
* Function: - compare two filename entries in the sorted list
* - special cases: "./" is first, "../" is second
* Accepts: - ptr to the two filenames
* Returns: - -ve/0/+ve (the usual <, ==, > comparison)
*/
static int
fnamecmp(f1, f2)
char **f1, **f2;
{
int ret;
if (strcmp(*f1, "./") == 0) {
ret = -1;
} else if (strcmp(*f2, "./") == 0) {
ret = 1;
} else if (strcmp(*f1, "../") == 0) {
ret = -1;
} else if (strcmp(*f2, "../") == 0) {
ret = 1;
} else {
ret = strcmp(*f1, *f2);
}
return(ret);
}
|