home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 287.lha / TY_v1.3 / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  8.7 KB  |  281 lines

  1. /*************************************************************************
  2.  ***                        main.c                       (JJB TEMPLAR) ***
  3.  *** Date modifications begun: 7/8/89.                                 ***
  4.  *** Last modified: 28/8/89.                                           ***
  5.  *************************************************************************/
  6. /*** Ahem. Here we go...                                               ***
  7.  *************************************************************************/
  8.  
  9. #include "less.h"
  10. #include "position.h"
  11.  
  12. #include <workbench/startup.h>
  13. #include <exec/memory.h>
  14.  
  15. extern struct WBStartup *WBenchMsg;
  16. char                    *filename;
  17. struct WBArg            *wbArgs;
  18.  
  19. int         new_file;
  20. char        current_file[128];
  21. int         ac;
  22. int         curr_ac;
  23.  
  24. static struct Remember *RemKey;
  25.  
  26. struct IntuitionBase    *IntuitionBase;
  27. struct GfxBase          *GfxBase;
  28. struct IconBase         *IconBase;
  29.  
  30. extern BPTR file;
  31. extern int  nbufs;
  32. extern int  quit_at_eof;
  33. extern int  f_nbufs;
  34. extern int  back_scroll;
  35. extern int  top_scroll;
  36. extern int  sc_height;
  37. extern char version[];      /* Version string. */
  38. extern struct Window *Window;
  39.  
  40. void    dolibs();
  41. BPTR    OpenArg(struct WBArg *);
  42. void    edit(int);
  43. void    add_file();
  44. void    next_file(int);
  45. void    prev_file(int);
  46. void    printf(char *);
  47. char    *strint(int);
  48. void    sprintf();
  49. void    cleanup(char *,int);
  50.  
  51. void    dolibs() /*======================================================*/
  52. {
  53.     if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0L)))
  54.         cleanup("ERROR: failed to open intuition.library!\n",20);
  55.     if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L)))
  56.         cleanup("ERROR: failed to open graphics.library!\n",20);
  57.     if (!(IconBase = (struct IconBase *)OpenLibrary("icon.library",0L)))
  58.         printf("WARNING: failed to open icon.library!\n");
  59. }
  60.  
  61. BPTR    OpenArg(wa) /*===================================================*/
  62. struct WBArg *wa;   /* All these mixed-case function names!              */
  63. {
  64. LONG    olddir;
  65. BPTR    file;
  66.  
  67.     if (wa->wa_Lock) olddir = CurrentDir(wa->wa_Lock);
  68.     file = Open(wa->wa_Name, MODE_OLDFILE);
  69.  
  70.     if (wa->wa_Lock) CurrentDir(olddir);    /* Why move back? For CLI? */
  71.     return(file);
  72. }
  73.  
  74. void    edit(filenum) /*=================================================*/
  75. int     filenum;      /* Move to file number "filenum" in wbArgs[].      */
  76. {
  77. long    f;
  78. char    message[100];
  79. static int  any_edited  = 0;
  80. static int  hold_scroll = 0;
  81.  
  82.     if (!filenum) {
  83.         if (curr_ac >= ac) {
  84.             error("No current file",1);
  85.             return;
  86.         }
  87.         filenum = curr_ac;
  88.     }
  89.  
  90.     filename = wbArgs[curr_ac].wa_Name;
  91.     if (!(f = OpenArg(&wbArgs[filenum]))) {
  92.         sprintf(message, "Cannot open %s!", filename);
  93.         if (any_edited) error(message,0);   /* Where to put error? In */
  94.         else {                              /* status line if a file */
  95.             puts(message);                  /* has already been opened, */
  96.             flush();                        /* or to screen if not. */
  97.           /* Wait two seconds for him to read message */
  98.             Delay(100L);
  99.             hold_scroll = 1;
  100.         }
  101.         return;
  102.     }
  103.  
  104.   /* Close the current input file and set up to use the new one. */
  105.     if (file > 0) Close(file);
  106.     new_file = 1;               /* Flag it (for prompt.c) */
  107.     strcpy(current_file, filename);
  108.     file = f;
  109.     opticon(filename);          /* Check icon for args */
  110.     ch_init(f_nbufs);
  111.     init_mark();                /* Just clears all the marks. */
  112.     any_edited = 1;             /* Flag this (for error message above) */
  113.  
  114.     if (hold_scroll) {
  115.       /* Before erasing screen, display filename and ask for a keystroke. */
  116.         error(filename,1);
  117.         hold_scroll = 0;        /* New edit, error message from previous */
  118.     }                          /* file still on screen (not error line). */
  119.  
  120.     jump_back(1);               /* Jump to line 1 of new file. */
  121.     setbar(2);                  /* Complete reset of scroll bar. */
  122. }
  123.  
  124. void    add_file() /*====================================================*/
  125. {
  126.     if (ac >= MAXAC) error("No room in table for new file!",0);
  127.     else {
  128.         wbArgs[ac].wa_Name = AllocRemember(&RemKey,250,MEMF_CLEAR);
  129.         if (!wbArgs[ac].wa_Name) error("No space for filename!",0);
  130.         else {
  131.             if (!r_string("Filename",wbArgs[ac].wa_Name,249)) return;
  132.             wbArgs[ac].wa_Lock = NULL;
  133.             ac++;
  134.             edit(curr_ac = ac - 1);
  135.         }
  136.     }
  137. }
  138.  
  139. void    next_file(n) /*==================================================*/
  140. int     n;
  141. {
  142.     if (curr_ac + n >= ac) {
  143.         if (quit_at_eof) cleanup(NULL,0);
  144.         error("No next file!",0);
  145.     }
  146.     else edit(curr_ac += n);
  147. }
  148.  
  149. void    prev_file(n) /*==================================================*/
  150. int     n;
  151. {
  152.     if (curr_ac - n < 0) error("No previous file!",0);
  153.     else edit(curr_ac -= n);
  154. }
  155.  
  156. void    main(argc,argv) /*===============================================*/
  157. int     argc;
  158. char    **argv;
  159. {
  160. register int    i,j;
  161. struct WBArg cliArgs[MAXAC];
  162.  
  163.     dolibs();           /* IntuitionBase */
  164.  
  165.     doopts(argc,argv);
  166.  
  167.     if (!argc) {        /* Workbench */
  168.         wbArgs = WBenchMsg->sm_ArgList;
  169.         argc   = WBenchMsg->sm_NumArgs;
  170.         if (argc < 2) cleanup(NULL,50);   /* Removed console. Bummer. */
  171.         j = 0;      argc--;
  172.         while ((j < argc) && (j < MAXAC)) {
  173.             cliArgs[j] = wbArgs[j + 1];
  174.             j++;
  175.         }
  176.         wbArgs = cliArgs;   /* Copy over, so array can be added to */
  177.     }
  178.     else {              /* CLI */
  179.         argv++;     argc--;
  180.         if (argc > 10) argc = 10;
  181.  
  182.         for (i=j=0; i < argc; i++) {      /* Only filename args. */
  183.             if ((argv[i][0] != '+') && (argv[i][0] != '-') && (argv[i][0] != '#')) {
  184.                 cliArgs[j].wa_Lock = 0;     /* So OpenArg checks for !Lock */
  185.                 cliArgs[j].wa_Name = argv[i];
  186.                 j++;
  187.             }
  188.         }
  189.  
  190.         wbArgs = cliArgs;
  191.         argc = j;       /* ac set to argc, also used by WB stuff */
  192.     }
  193.  
  194.   /* Set up list of files to be examined. */
  195.     ac = argc;      /* Filename count */
  196.     curr_ac = 0;    /* Current arg to use */
  197.  
  198.     ttopen();                   /* Opens window, and console */
  199.     initbell();                 /* Set up bell */
  200.  
  201.     if (back_scroll < 0) {
  202.         back_scroll = sc_height-1;
  203.         if (top_scroll) back_scroll--;
  204.     }
  205.  
  206.   /* Select the first file to examine. */
  207.     if (ac < 1) cleanup("ERROR: no file specified\n",50);
  208.     else {
  209.         do {
  210.             edit(0);
  211.             if (file > 0) /* We can open this file. */
  212.             break;
  213.             putc('\n');  flush();
  214.         } while (++curr_ac < ac);   /* Keeping trying. */
  215.     }
  216.  
  217.     if (file > 0) commands();       /* Managed to open a file */
  218.     cleanup(NULL,0);
  219. }
  220.  
  221. void    printf(cp) /*====================================================*/
  222. register char   *cp;
  223. {
  224.     if (!WBenchMsg) Write(Output(),cp,strlen(cp));
  225. }
  226.  
  227. char    *strint(i) /*====================================================*/
  228. register int    i; /* Simple integer to decimal string routine */
  229. {
  230. register char    *ret;
  231. static char buf[10];
  232.     ret = &buf[9];
  233.     *ret = 0;
  234.     while (i > 0) {
  235.         *(--ret) = (i % 10) + '0';
  236.         i /= 10;
  237.     }
  238.     if (!*ret) *(--ret) = '0';
  239.     return(ret);
  240. }
  241.  
  242. void    sprintf(dst,cp,pnt) /*===========================================*/
  243. register char   *dst,*cp;
  244. ULONG   pnt;
  245. {
  246. ULONG   *arg;
  247.     arg = &pnt;
  248.     while (*cp) {
  249.         if (*cp == '%') switch (*(++cp)) {
  250.             case ('d'): strcpy(dst,strint((int)*arg++));
  251.                         while (*dst) dst++;                /* On to zero */
  252.                         break;
  253.             case ('s'): strcpy(dst,(char *)*arg++);
  254.                         while (*dst) dst++;
  255.                         break;
  256.             case ('c'): *dst++ = (char)*arg++;
  257.                         break;
  258.             default: *dst++ = *cp;
  259.         }
  260.         else *dst++ = *cp;
  261.         cp++;               /* Inc it here, so in just once place */
  262.     }
  263.     *dst = 0;
  264. }
  265.  
  266. void    cleanup(cp,err) /*===============================================*/
  267. char    *cp;            /* No bra required. Just a simple exit.          */
  268. int     err;            /* I don't like doing all this if (x) Close(x)   */
  269. {                       /* stuff, but leave it in for the mo...          */
  270.     if (cp) printf(cp);
  271.     if (file > 0) Close(file);
  272.     freebell();     /* Drop bell stuff. OK if not there, so no need to check */
  273.     ttclose();      /* OKAY if tty not open, so no need to check. */
  274.     ch_memdump();   /* FreeMem bufs in ch.c */
  275.     if (RemKey) FreeRemember(&RemKey,TRUE);
  276.     if (IconBase) CloseLibrary(IconBase);
  277.     if (GfxBase) CloseLibrary(GfxBase);
  278.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  279.     XCEXIT(err);
  280. }
  281.