home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / unix / ftpscrip.tz / ftpscrip / FtpExtra / ftpmenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-09  |  11.3 KB  |  562 lines

  1. /* James Davis (jedavis@ucdavis.edu)  c. 1991   */
  2. /* 6708 Austin Way, Sacramento CA 95823  */
  3. /*                                            */
  4. /* Written as part of a number of scripts to make ftping easier.  THere */
  5. /* are a huge number of improvements that could be made, but I havent */
  6. /* time.  If you'd like to change something, go ahead. But send me a */
  7. /* copy so Ill know. Feel free to distribute. */
  8. /*                                                                    */
  9. /* -- This is the code that produces the ftpmenubase program.  It relies */
  10. /* -- on curses, so make sure you have that package.  Also since different */
  11. /* -- versions of curses use differnt things for cbreak mode you may have to */
  12. /* -- change the define.  e.g. on a vax 11/785 here I had to change it to */
  13. /* -- crmode and nocrmode.  */
  14. /* -- The pager define should be set to the complete path of you're pager  */
  15. /* -- I DONT check your environment variable, since I rely on some options */
  16. /* -- In 'less'. Specifically if you say 'less +/word file' less will open */
  17. /* -- file and immediately search for word. My version of more works the same*/
  18. /* -- but I suppose the only way you'll know is try.  Its only important for */
  19. /* -- the ability to look up abstracts.  So stick to less or more, and forget */
  20. /* other pagers.  You could always modify the code I suppose. */
  21. /* The long FTPMENUCBREAK is because CBREAK itself is a macro, and I  */
  22. /* didnt want to take a chance on interfering.    */
  23. /* Apologies for the lack of comments, but...  If you have questions  */
  24. /* drop a line, Ill try to explain.   */
  25. /* Compile with 'cc ftpmenu.c -lcurses -ltermcap'     */
  26. /* The important point here is the 'cc'.  For some reason if you use */
  27. /* 'gcc' then wait() corrupts my data.  If you notice that when you */
  28. /* try to look  at an abstract by hitting 'a' and when you exit the pager */
  29. /* Something behaves strangely please report it.  I never tracked down */
  30. /* Why 'gcc' didnt work, so it may be my bug, but I prefer to blame */
  31. /* Someone else.   For that matter please report any bugs  */
  32.  
  33.  
  34. #define PAGER "/usr/local/less"
  35. #define FTPMENUCBREAK cbreak
  36. #define FTPMENUNOCBREAK nocbreak
  37.  
  38. #include <stdio.h>
  39. #include <strings.h>
  40. #include <curses.h>
  41.  
  42.  struct myrec {
  43.     struct myrec *nextthis;
  44.     struct myrec *nextlower;
  45.     char fsize[8];
  46.     char month[4];
  47.     char day[3];
  48.     char name[50];
  49.     char mark;
  50.     } ;
  51.  
  52.  
  53. char line[85];
  54. FILE *datafile;
  55. FILE *outfile;
  56. char cursite[85];
  57. char curdir[85];
  58. int simple;
  59. int usedsite, useddir;
  60. struct myrec *curlvlptr[4];
  61. int pos[4];
  62. int lps;
  63. int cdone =0;
  64. int gargc;
  65. char **gargv;
  66.  
  67. int thisdirect ()
  68. {
  69. printf ("Directory :");
  70. fscanf (datafile,"%s",line);
  71. printf ("%s\n",line);
  72. strcpy (curdir,line);
  73. useddir = 0;
  74. return (0);
  75. }
  76.  
  77. void usesite ()
  78. {
  79. fprintf (outfile, "close\n");
  80. fprintf (outfile,"o %s\n",cursite); 
  81. usedsite = 1;
  82. }
  83.  
  84. void usedir ()
  85. {
  86. fprintf (outfile,"cd %s\n",curdir); 
  87. useddir =1;
  88. }
  89.  
  90.  
  91. int thissite ()
  92. {
  93. fscanf (datafile,"%s",line);
  94. if (!(line[0]=='*'))
  95.     {
  96.     printf ("\n");
  97.     printf ("Site: %s\n",line);
  98.     strcpy (cursite,line);
  99.     usedsite = 0;
  100.     }
  101. return (0);
  102. }
  103.  
  104. int thisfile ()
  105. {
  106. int i;
  107. char resp[80];
  108. char response;
  109.  
  110. printf ("Get file: ");
  111. for (i=0;i<7;i++)
  112.     {
  113.     fscanf (datafile,"%s",line);
  114.     if (i>1)
  115.         printf ("\t%s",line);
  116.     }
  117. printf ("\t:  ");
  118. gets(resp);
  119. response = resp[0];
  120.  
  121. if (response=='y' || response=='Y')
  122.     {
  123.     if (usedsite==0) usesite();
  124.     if (0==useddir) usedir();
  125.     fprintf (outfile,"get %s\n",line);
  126.     }
  127. if (response=='d' || response=='D')
  128.     return (1);
  129. if (response=='s' || response=='S')
  130.     return (2);
  131. return (0);    
  132. }    
  133.  
  134.  
  135.  
  136. void generalmethod ()
  137. {
  138. int mode; 
  139.  
  140.  
  141. while (fscanf (datafile,"%s",line) != EOF)
  142. {    
  143. if ((!strcmp(line,"257")) && (mode<2))
  144.     mode = thisdirect();
  145. if ((!strcmp(line,"SITE")) )
  146.     mode = thissite();
  147. if ((line[0]=='-') && (line[1]=='r') && (!mode))
  148.     mode = thisfile ();
  149. }
  150. }
  151.  
  152.  
  153.  
  154. int loadsite ()
  155. {
  156. fscanf (datafile,"%s",line);
  157. if (!(line[0]=='*'))
  158.     {
  159.     printf ("\nLoading : %s ",line);
  160.     curlvlptr[1]->nextthis = (struct myrec *) malloc (sizeof (struct myrec));
  161.     curlvlptr[1] = curlvlptr[1]->nextthis;
  162.     curlvlptr[1]->nextlower = (struct myrec *) malloc (sizeof (struct myrec));
  163.     curlvlptr[2] = curlvlptr[1]->nextlower;
  164.     strcpy (curlvlptr[2]->name,"..");
  165.     curlvlptr[1]->nextthis = NULL;
  166.     curlvlptr[2]->nextlower = NULL;
  167.     strcpy (curlvlptr[1]->name,line);
  168.     return(1);
  169.     }
  170.     else return (0);
  171. }
  172.  
  173.  
  174. void loaddirect ()
  175. {
  176. fscanf (datafile,"%s",line);
  177.  
  178. addstr (".");
  179. refresh();
  180. curlvlptr[2]->nextthis = (struct myrec *) malloc (sizeof (struct myrec));
  181. curlvlptr[2] = curlvlptr[2]->nextthis;
  182. curlvlptr[2]->nextlower = (struct myrec *) malloc (sizeof (struct myrec));
  183. curlvlptr[3] = curlvlptr[2]->nextlower;
  184. strcpy (curlvlptr[3]->name,"..");
  185. curlvlptr[3]->mark=0;
  186. /*curlvlptrdirptr->nextdir = NULL;
  187. curfileptr->nextfile = NULL;
  188. */
  189. strcpy (curlvlptr[2]->name,line);
  190. }
  191.  
  192. void loadfile ()
  193. {
  194. curlvlptr[3]->nextthis = (struct myrec *) malloc (sizeof(struct myrec));
  195. curlvlptr[3] = curlvlptr[3]->nextthis;
  196. curlvlptr[3]->nextthis = NULL;
  197. curlvlptr[3]->mark=0;
  198. fscanf (datafile,"%s",line); 
  199. fscanf (datafile,"%s",line); 
  200. fscanf (datafile,"%s",line); 
  201. strcpy (curlvlptr[3]->fsize,line);
  202. fscanf (datafile,"%s",line); 
  203. strcpy (curlvlptr[3]->month,line);
  204. fscanf (datafile,"%s",line); 
  205. strcpy (curlvlptr[3]->day,line);
  206. fscanf (datafile,"%s",line); 
  207. fscanf (datafile,"%s",line); 
  208. strcpy (curlvlptr[3]->name,line);
  209. }
  210.  
  211. void loadstruct()
  212. {
  213. int fc,dc,sc;
  214.  
  215. curlvlptr[0] = (struct myrec *) malloc (sizeof (struct myrec));
  216. curlvlptr[0]->nextlower = (struct myrec *) malloc (sizeof (struct myrec));
  217. curlvlptr[1] = curlvlptr[0]->nextlower;
  218. strcpy (curlvlptr[1]->name,"..");
  219.  
  220. sc=0;
  221. dc=0;
  222. fc=0;
  223. while (fscanf (datafile,"%s",line) != EOF)
  224. {    
  225. if ((!strcmp(line,"257")) && (sc < (LINES)) && (dc < (LINES-5)))
  226.      {
  227.      dc++;
  228.      fc=0;
  229.      loaddirect();
  230.      }
  231. if ((!strcmp(line,"SITE")) && (sc<(LINES-1) ))
  232.      {
  233.      sc += loadsite();
  234.      dc=0;
  235.      }
  236. if ((line[0]=='-') && (line[1]=='r') && (sc<(LINES)) && (dc<(LINES-4)) && (fc<(LINES-5)))
  237.      {
  238.      loadfile ();
  239.      fc++;
  240.      }
  241. }
  242. }
  243.  
  244. void savetofile()
  245. {
  246. curlvlptr[1]=curlvlptr[0]->nextlower->nextthis;
  247. while (curlvlptr[1] != NULL) 
  248.     {
  249.     usedsite=0;
  250.     strcpy(cursite,curlvlptr[1]->name);
  251.     curlvlptr[2]=curlvlptr[1]->nextlower->nextthis;
  252.     while (curlvlptr[2] != NULL) 
  253.         {
  254.         useddir=0;
  255.         strcpy(curdir,curlvlptr[2]->name);
  256.         curlvlptr[3]=curlvlptr[2]->nextlower->nextthis;
  257.         while (curlvlptr[3] != NULL) 
  258.             {
  259.             if (curlvlptr[3]->mark)
  260.                 {
  261.                 if (!usedsite) usesite();
  262.                 if (!useddir) usedir();
  263.                 fprintf (outfile,"get %s\n",curlvlptr[3]->name);
  264.                 }
  265.             curlvlptr[3]=curlvlptr[3]->nextthis;
  266.             }
  267.         curlvlptr[2] = curlvlptr[2]->nextthis;
  268.         }
  269.     curlvlptr[1]=curlvlptr[1]->nextthis;
  270.     }
  271. }
  272.  
  273.  
  274.  
  275. dispbottom (level)
  276. int level;
  277. {
  278. if (level == 2)
  279.     {
  280.     move (LINES-2,10);
  281.     addstr (curlvlptr[1]->name);
  282.     }
  283. if (level == 3)
  284.     {
  285.     move (LINES-2,10);
  286.     addstr (curlvlptr[1]->name);
  287.     addstr (" : ");
  288.     addstr (curlvlptr[2]->name);
  289.     }
  290. }
  291.  
  292. void displine(level,cntr)
  293. int level,cntr;
  294. {
  295. move (cntr,10);
  296. addstr (curlvlptr[level]->name);
  297. if (level == 3)
  298.     {
  299.     move (cntr,40);
  300.     addstr (curlvlptr[3]->month);
  301.     move (cntr,44);
  302.     addstr (curlvlptr[3]->day);
  303.     move (cntr,50);
  304.     addstr (curlvlptr[3]->fsize);
  305.     if (curlvlptr[3]->mark) 
  306.         {
  307.         move (cntr,9);
  308.         addstr("*");
  309.         }
  310.     }
  311. }
  312.  
  313. void helpnote()
  314. {
  315. clear();
  316. move (2,4);
  317. addstr ("j - move pointer down");
  318. move (3,4);
  319. addstr ("J - move pointer down faster");
  320. move (4,4);
  321. addstr ("k - move pointer up");
  322. move (5,4);
  323. addstr ("K - move pointer up faster");
  324. move (6,4);
  325. addstr ("a - look at the abstract for this file");
  326. move (7,4);
  327. addstr ("u - takes you one step up the hierarchy: file->directory; directory->site");
  328. move (8,4);
  329. addstr ("n - next directory");
  330. move (9,4);
  331. addstr ("N - next site");
  332. move (10,4);
  333. addstr ("x - quit, and get the marked files");
  334. move (11,4);
  335. addstr ("X - quit, but dont get anything");
  336. move (12,4);
  337. addstr ("h or ? - this help message");
  338. move (13,4);
  339. addstr ("space - show the indicated site or directory, or mark a file for retrieval");
  340. refresh();
  341. getch();
  342. }
  343.  
  344. int menu(level)
  345. int level;
  346. {
  347. char key;
  348. char k[80];
  349. int cntr;
  350. int maxitem,npos;
  351. int ref;
  352.  
  353. ref = 1;
  354. pos[level] =1;
  355.  
  356. do
  357. {
  358. if (ref==1)   /*refresh*/
  359.     {
  360.     clear();
  361.     cntr = 1;
  362.     curlvlptr[level] = curlvlptr[(level-1)]->nextlower;
  363.     if (level==1) curlvlptr[1] = curlvlptr[1]->nextthis;
  364.     while (curlvlptr[level] != NULL) 
  365.     {
  366.     displine(level,cntr);
  367.     curlvlptr[level] = curlvlptr[level]->nextthis;
  368.     cntr++;
  369.     }
  370.     maxitem = --cntr;
  371.     move (pos[level],6);
  372.     addstr ("-->");
  373.     dispbottom(level);
  374.     move (0,0);
  375.     refresh();
  376.     ref=0;
  377.     }
  378. key=getch();
  379. if ((key=='j') || (key=='J'))
  380.     {
  381.     lps=1;
  382.     if (key=='J') lps=4;
  383.     for (cntr=0;cntr<lps;cntr++)
  384.     {
  385.     if (pos[level]==maxitem)
  386.         npos=1;
  387.         else npos = pos[level]+1;
  388.     move (pos[level],6);
  389.     addstr ("   ");
  390.     move (npos,6);
  391.     addstr ("-->");
  392.     pos[level] = npos;
  393.     }
  394.     move (0,0);
  395.     refresh();
  396.     }
  397. if ((key=='k') || (key=='K') )
  398.     {
  399.     lps=1;
  400.     if (key=='K') lps=4;
  401.     for (cntr=0;cntr<lps;cntr++)
  402.     {
  403.     if (pos[level]==1)
  404.         npos = maxitem;
  405.         else npos = pos[level] -1;
  406.     move (pos[level],6);
  407.     addstr ("   ");
  408.     move (npos,6);
  409.     addstr ("-->");
  410.     pos[level] = npos;
  411.     }
  412.     move (0,0);
  413.     refresh();
  414.     }
  415. if (((key=='d') || (key==' ')) )
  416.     {
  417.     curlvlptr[level] = curlvlptr[level-1]->nextlower;
  418.     for (cntr=(level!=1);cntr<pos[level];cntr++)
  419.         curlvlptr[level] = curlvlptr[level]->nextthis;
  420.     if (level == 1) menu (2);
  421.     if (level ==  2) 
  422.         {
  423.         if (curlvlptr[2] == curlvlptr[1]->nextlower)
  424.             key='u';
  425.             else    { 
  426.                 menu(3); 
  427.                 ref=1;
  428.                 }
  429.         }
  430.     if (level ==3)
  431.         {
  432.         if (curlvlptr[3] == curlvlptr[2]->nextlower)
  433.             key='u';
  434.             else    { 
  435.                 curlvlptr[3]->mark=(-((curlvlptr[3]->mark)-1));
  436.                  move (pos[3],9);
  437.                 if (curlvlptr[3]->mark)
  438.                     addstr ("*");
  439.                     else addstr(" ");
  440.                 move (0,0);
  441.                 refresh();
  442.                 }
  443.         }
  444.     if (level<3) ref=1;
  445.     }
  446.  
  447. if (((key=='n') || (key=='N')) && (level==2))
  448.     {
  449.     curlvlptr[1]=curlvlptr[1]->nextthis;
  450.     pos[1]++;
  451.     if (curlvlptr[1]==NULL) 
  452.         {
  453.         curlvlptr[1]=curlvlptr[0]->nextlower->nextthis;
  454.         pos[1]=1;
  455.         }
  456.     curlvlptr[2]=curlvlptr[1]->nextlower;
  457.     pos[2]=1;
  458.     ref=1;
  459.     }
  460.  
  461. if ((key=='N') && (level==3))
  462.     {
  463.     curlvlptr[1]=curlvlptr[1]->nextthis;
  464.     pos[1]++;
  465.     if (curlvlptr[1]==NULL) 
  466.         {
  467.         curlvlptr[1]=curlvlptr[0]->nextlower->nextthis;
  468.         pos[1]=1;
  469.         }
  470.     curlvlptr[2]=curlvlptr[1]->nextlower->nextthis;
  471.     pos[2]=2;
  472.     curlvlptr[3]=curlvlptr[2]->nextlower;
  473.     pos[3]=1;
  474.     ref=1;
  475.     }
  476. if ((key=='n') && (level==3))
  477.     {
  478. /*printf ("%s%s%s\n",curlvlptr[1]->name,curlvlptr[2]->name,curlvlptr[3]->name);
  479. gets ();    */
  480.     curlvlptr[2]=curlvlptr[2]->nextthis;
  481.     pos[2]++;
  482.     if (curlvlptr[2]==NULL) 
  483.         {
  484.         curlvlptr[2]=curlvlptr[1]->nextlower->nextthis;
  485.         pos[2]=2;
  486.         }
  487.     pos[3]=1;
  488.     ref=1;
  489.     }
  490. if ((key=='a') && (pos[3]!=1) && (gargc>3) && (level==3))
  491.     {
  492.     clear();
  493.     refresh();
  494.     curlvlptr[3] = curlvlptr[2]->nextlower;
  495.     for (cntr =1;cntr<pos[3];cntr++)
  496.         curlvlptr[3] = curlvlptr[3]->nextthis;
  497.     strcpy(line,"+/");
  498.     strcat(line,curlvlptr[3]->name);
  499.      if (fork()==0)
  500.         execl (PAGER,PAGER,line,gargv[3],(char *) 0);
  501.         else wait(0);  
  502.     ref=1;
  503.     }
  504. if ((key=='h') || (key=='?'))
  505.     {
  506.     helpnote();
  507.     ref=1;
  508.     }
  509.     
  510. if (key=='x') cdone = 1;
  511. if (key=='X') cdone = 2;    
  512. }    
  513. while ((!cdone) && (key !='u'));
  514. return (cdone);
  515. }
  516.  
  517.  
  518.  
  519.  
  520.  
  521. void init (argc,argv)
  522. int argc;
  523. char **argv;
  524. {
  525. gargc =  argc;
  526. gargv = argv;
  527. datafile = fopen (argv[1],"r");
  528. outfile = fopen (argv[2],"w");
  529. initscr();
  530. clear();
  531. refresh();
  532. FTPMENUCBREAK();
  533. noecho();
  534. }
  535.  
  536. void finish ()
  537. {
  538. FTPMENUNOCBREAK();
  539. endwin();
  540. fclose (datafile);
  541. fclose (outfile);
  542. }
  543.  
  544. void usagemessage (argv)
  545. char **argv;
  546. {
  547. printf ("Usage: %s <directoryList> <outputToFtp> [abstractFile]\n",argv[0]);
  548. }
  549.  
  550.  
  551. void main (argc,argv)
  552. int argc;
  553. char **argv;
  554. {
  555. if (argc<3) {usagemessage(argv); return;}
  556. init(argc,argv);
  557. loadstruct();
  558. menu(1);
  559. if (cdone>0) savetofile();
  560. finish();
  561. }
  562.