home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / ShellPanel / Open.m < prev    next >
Encoding:
Text File  |  1992-11-10  |  2.1 KB  |  80 lines

  1. /* File: Open.m - (Interactive) Unix shell version of OpenPanel
  2.  *
  3.  * By: Christopher Lane (lane@sumex-aim.stanford.edu)
  4.  *
  5.  * Date: 10 November 1992
  6.  *
  7.  * Copyright: 1990, 1991 & 1992 by The Leland Stanford Junior University.
  8.  * This program may be distributed without restriction for non-commercial use.
  9.  */
  10.  
  11. #import <stdlib.h>
  12. #import <getopt.h>
  13. #import <strings.h>
  14. #import <sys/param.h>
  15. #import <appkit/OpenPanel.h>
  16. #import <appkit/Application.h>
  17.  
  18. #define USAGE "usage: %s [-f file] [-d directory] [-t type [-t type]] [-p prompt] [-T title] [-m] [-c]\n"
  19. #define EXIT_USAGE (2)
  20. #define SEPARATOR "/"
  21.  
  22. #define MAX_TYPES 64
  23.  
  24. const char *fullPath(const char *parent, const char *name)
  25. {
  26.     static char buffer[MAXPATHLEN];
  27.     
  28.     if(parent == NULL) return name;
  29.     
  30.     if(strcmp(strcpy(buffer, parent), SEPARATOR) != 0) (void) strcat(buffer, SEPARATOR);
  31.     
  32.     return strcat(buffer, name);
  33. }
  34.  
  35. void main(int argc, char *argv[])
  36. {
  37.     OpenPanel *panel;
  38.     const char *const *filenames;
  39.     int option, status = EXIT_SUCCESS, ntypes = 0;
  40.     const char *name = NULL, *path = NULL, *types[MAX_TYPES];
  41.     
  42.     NXApp = [Application new];
  43.     panel = [OpenPanel new];
  44.     
  45.     while ((option = getopt(argc, argv, "f:d:t:p:T:mc")) != EOF)
  46.         switch (option) {
  47.         case 'f': name = optarg; break;
  48.         case 'd': path = optarg; break;
  49.         case 't': types[ntypes++] = optarg; break;
  50.         case 'p': [panel setPrompt:optarg]; break;
  51.         case 'T': [panel setTitle:optarg]; break;
  52.         case 'm': [panel allowMultipleFiles:YES]; break;
  53.         case 'c': [panel chooseDirectories:YES]; break;
  54.         default : status = EXIT_USAGE;
  55.         }
  56.     if (optind < argc) status = EXIT_USAGE;
  57.     
  58.     types[ntypes] = NULL;
  59.             
  60.     if (status == EXIT_USAGE) (void) fprintf(stderr, USAGE, [NXApp appName]);
  61.     else {
  62.         int context = [NXApp activateSelf:YES];
  63.     
  64.         if ([panel runModalForDirectory:path file:name types:types]) {
  65.             path = [panel directory];
  66.             for (filenames = [panel filenames]; filenames && *filenames; filenames++)
  67.                 (void) puts(fullPath(path, *filenames));
  68.             }
  69.         else status = EXIT_FAILURE;
  70.         
  71.         if (context) (void) [NXApp activate:context];
  72.         }
  73.     
  74.     [panel free];
  75.         
  76.     [NXApp free];
  77.     
  78.     exit(status);
  79. }
  80.