home *** CD-ROM | disk | FTP | other *** search
/ Fujiology Archive / fujiology_archive_v1_0.iso / !FALCON / NOCREW / MP2_0997.ZIP / mp2_0997 / src / bifs.c < prev    next >
C/C++ Source or Header  |  1999-07-27  |  12KB  |  595 lines

  1. #include <aes.h>
  2. #include <tos.h>
  3. #include <ext.h>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #include "mp2audio.h"
  10. #include "libshoe.h"
  11. #include "decoder.h"
  12. #include "stream.h"
  13. #include "replay.h"
  14. #include "window.h"
  15. #include "mp2info.h"
  16.  
  17. /* Function from mp2audio.c */
  18. extern void unix2dos(char *path);
  19.  
  20. /* Functions from mp2event.c */
  21. extern void do_formstuff(int obj_id);
  22. extern int quit_program(void);
  23. extern char *do_selectfile(char *);
  24. extern void do_play(void);
  25. extern void do_stop(void);
  26. extern void do_pause(void);
  27. extern void do_loop(void);
  28. extern void do_ff(void);
  29. extern void do_info(void);
  30. extern void do_shoe(void);
  31. extern void set_title(char *title);
  32. extern void set_subtitle(char *subtitle);
  33. extern long calc_time(void);
  34.  
  35. /* Function from console.c */
  36. extern Byte *notify_console(char *msg);
  37. extern void print_line(char *text);
  38. extern void clear_console(void);
  39. extern void prompt_console(int, int);
  40.  
  41. /* Global variable from mp2audio.c */
  42. extern int replay, count_dir, dom;
  43. extern long ext;
  44.  
  45. /* Global variables from mp2file.c */
  46. extern char path[512], filename[512];
  47.  
  48. /* Global variables from mp2event.c */
  49. extern int looping;
  50. extern int fgexit, display_time;
  51.  
  52. Byte *bif_notify_gc(Byte *args)
  53. {
  54.     return notify_console(TP(args)?"gc":""), args;
  55. }
  56.  
  57. Byte *bif_panic(Byte *msg)
  58. {
  59.     Byte panicmsg[1024];
  60.  
  61.     sprintf(panicmsg, "[1][%s][OK]", msg);
  62.     form_alert(1, panicmsg);
  63.     exit(1);
  64.     return msg;
  65. }
  66.  
  67. Byte *bif_date(Byte *args)
  68. {
  69.     Byte *r;
  70.     unsigned int y, m, d, t;
  71.     
  72.     args = args;        /* Fool Pure C. */    
  73.     t = Tgetdate();
  74.     y = (t >> 9) + 1980;
  75.     m = (t >> 5) & 0xf;
  76.     d = t & 0x1f;
  77.     sprintf(r = mem(1+4+1+2+1+2+1+1), "(%d %d %d)", y, m, d);
  78.     return r;
  79. }
  80.  
  81. Byte *bif_time(Byte *args)
  82. {
  83.     Byte *r;
  84.     unsigned int h, m, s, t;
  85.     
  86.     args = args;        /* Fool Pure C. */    
  87.     t = Tgettime();
  88.     h = t >> 11;
  89.     m = (t >> 5) & 0x3f;
  90.     s = (t & 0x1f) * 2;
  91.     sprintf(r = mem(1+2+1+2+1+2+1+1), "(%d %d %d)", h, m, s);
  92.     return r;
  93. }
  94.  
  95. Byte *bif_pwd(Byte *args)
  96. {
  97.     args = args;        /* Fool Pure C. */    
  98.     return memdup(path);
  99. }
  100.  
  101. Byte *bif_cd(Byte *args)
  102. {
  103.     char *s, old_dir[512];
  104.     int old_drv;
  105.     
  106. /*    s = decode_string(EVAL(args)); */
  107.     s = EVAL(args);
  108.  
  109.     old_drv = Dgetdrv();
  110.     Dgetpath(old_dir, 0);
  111.  
  112.     Dsetdrv((char)tolower((int)path[0])-'a');
  113.     Dsetpath(path);
  114.     
  115.     if(strlen(s) >= 2 && s[1] == ':')
  116.         Dsetdrv((char)tolower((int)s[0])-'a');
  117.     Dsetpath(s);
  118.     
  119.     path[0] = 'A' + (char)Dgetdrv();
  120.     path[1] = ':';
  121.     Dgetpath(path+2, 0);
  122.     strcat(path, "\\");
  123.     
  124.     Dsetdrv(old_drv);
  125.     Dsetpath(old_dir);
  126.  
  127.     return bif_pwd("()");
  128. }
  129.  
  130. static int match_substring(const Byte *s, const Byte *pat)
  131. {
  132.     Byte *n, sub[1024];
  133.  
  134.     while(*s && *pat) {
  135.         switch(*pat) {
  136.         case '?':
  137.             s++;
  138.             break;
  139.         case '*':
  140.             if(!pat[1])
  141.                 return 1;
  142.             strcpy(sub, pat+1);
  143.             n = strpbrk(sub, "?*");
  144.             if(n == NULL)
  145.                 n = &sub[strlen(sub)];
  146.             *n = '\0';
  147.             n = strstr(s, sub);
  148.             if(n == NULL)
  149.                 return 0;
  150.             s = n;
  151.             break;
  152.         default:
  153.             if(*s++ != *pat)
  154.                 return 0;
  155.         }
  156.         pat++;
  157.     }
  158.     
  159.     if((*s && !*pat) || (!*s && *pat))
  160.         return 0;
  161.  
  162.     return 1;
  163. }
  164.  
  165. Byte *bif_ls(Byte *args)
  166. {
  167.     int ok=1;
  168.     long err, dirhandle;
  169.     char *r, *dir, *name;
  170.     DTA *old_dta, pdta;
  171.     DIRBUF dirbuf, *dirptr;
  172.     XATTR atr, *atrptr;
  173.     
  174. /*    args = decode_string(EVAL(args)); */
  175.     args = EVAL(args);
  176.  
  177.     if(dom) {
  178.         dir = memdup(path);
  179.         dir[strlen(dir)-1] = '\0';
  180.         dirptr = &dirbuf;
  181.         atrptr = &atr;
  182.         name = dirptr->fname;
  183.         dirhandle = Dopendir(dir, 0);
  184.         mem(512);
  185.         strcpy(dir, path);
  186.         if (dirhandle > 0) {
  187.             err = Dreaddir((int)sizeof(dirbuf), dirhandle, (void *)dirptr);
  188.             if(!err && match_substring(dirptr->fname, NILP(args)?"*":args)) {
  189.                 if(Fxattr(0,strcat(dir, dirptr->fname), (void *)atrptr) == 0)
  190.                     ok = (((atrptr->mode & S_IFMT) == S_IFREG) ||
  191.                             ((atrptr->mode & S_IFMT) == S_IFDIR) || 
  192.                             ((atrptr->mode & S_IFMT) == S_IFLNK));
  193.             } else
  194.                 ok = 0;
  195.             strcpy(dir, path);
  196.         } else {
  197.             return ERR;
  198.         }
  199.     } else {
  200.         old_dta = Fgetdta();
  201.         Fsetdta(&pdta);
  202.  
  203.         dir = memdup(path);
  204.         mem(strlen(NILP(args)?"*.*":args)+1);
  205.         strcat(dir,NILP(args)?"*.*":args);
  206.         err = (long)Fsfirst(dir, FA_SUBDIR | FA_HIDDEN | FA_SYSTEM);
  207.         name = pdta.d_fname;
  208.     }
  209.         
  210.     *(r = mem(3)) = '\0';
  211.     strcat(r, "(");
  212.     while(!err) {
  213.         if(ok) {
  214.             mem(strlen(name)+1);
  215.             strcat(r, name);
  216.             strcat(r, " ");
  217.         }
  218.         if(dom) {
  219.             err = Dreaddir((int)sizeof(dirbuf), dirhandle, (void *)dirptr);
  220.             if(!err && match_substring(dirptr->fname, NILP(args)?"*":args)) {
  221.                 if(Fxattr(0,strcat(dir, dirptr->fname), (void *)atrptr) == 0)
  222.                     ok = (((atrptr->mode & S_IFMT) == S_IFREG) || 
  223.                             ((atrptr->mode & S_IFMT) == S_IFDIR) || 
  224.                             ((atrptr->mode & S_IFMT) == S_IFLNK));
  225.             } else
  226.                 ok = 0;
  227.             strcpy(dir, path);
  228.         } else {
  229.             err = (long)Fsnext();
  230.         }
  231.     }
  232.     if(strlen(r) > 1)
  233.         r[strlen(r)-1] = ')';
  234.     else
  235.         strcat(r, ")");
  236.  
  237.     if(dom)
  238.         Dclosedir(dirhandle);
  239.     else
  240.         Fsetdta(old_dta);
  241.  
  242.     return r;
  243. }
  244.  
  245. Byte *bif_load(Byte *args)
  246. {
  247.     int fp;
  248.     Byte *r;
  249.     long fret, length;
  250.  
  251. /*    fret = Fopen(decode_string(EVAL(args)), FO_READ); */
  252.     fret = Fopen(EVAL(args), FO_READ);
  253.     if(fret < 0) return F;
  254.  
  255.     fp = (int)fret;
  256.     length = filelength(fp);
  257.     Fread(fp, length, r = mem(length+1));
  258.     r[length] = '\0';
  259.     Fclose(fp);
  260.  
  261.     parse_eval(r);
  262.     return T;
  263. }
  264.  
  265. Byte *bif_exit(Byte *args)
  266. {
  267.     args = args;        /* Fool Pure C. */    
  268.     return quit_program()?T:F;
  269. }
  270.  
  271. Byte *bif_mp2_load(Byte *args)
  272. {
  273.     char *filename;
  274.  
  275. /*    file = args = decode_string(EVAL(args)); */
  276.     filename = args = EVAL(args);
  277.     if(FP(args))
  278.         return F;
  279.     if(!strchr(args, '\\')) {
  280.         filename = memdup(path);
  281.         mem(strlen(args));
  282.         strcat(filename, args);
  283.     }
  284.     do_stop();
  285.     if(stream_open(filename) < 0)
  286.         return F;
  287.     return T;
  288. }
  289.  
  290. Byte *bif_mp2_play(Byte *args)
  291. {
  292.     args = args;        /* Fool Pure C. */    
  293.     do_play();
  294.     return T;
  295. }
  296.  
  297. Byte *bif_mp2_stop(Byte *args)
  298. {
  299.     args = args;        /* Fool Pure C. */    
  300.     do_stop();
  301.     return T;
  302. }
  303.  
  304. Byte *bif_mp2_pause(Byte *args)
  305. {
  306.     args = args;        /* Fool Pure C. */    
  307.     do_pause();
  308.     return replay_pausep()?T:F;
  309. }
  310.  
  311. Byte *bif_mp2_loop(Byte *args)
  312. {
  313.     args = args;        /* Fool Pure C. */    
  314.     do_loop();
  315.     return looping?T:F;
  316. }
  317.  
  318. Byte *bif_mp2_fast_forward(Byte *args)
  319. {
  320.     args = args;        /* Fool Pure C. */    
  321.     do_ff();
  322.     return T;
  323. }
  324.  
  325. Byte *bif_mp2_window_info(Byte *args)
  326. {
  327.     args = args;        /* Fool Pure C. */    
  328.     do_info();
  329.     return T;
  330. }
  331.  
  332. Byte *bif_mp2_window_console(Byte *args)
  333. {
  334.     args = args;        /* Fool Pure C. */    
  335.     do_shoe();
  336.     return T;
  337. }
  338.  
  339. Byte *bif_mp2_select(Byte *args)
  340. {
  341.     char *pattern = dom?"*.mp?":"*.MP?";
  342.  
  343.     return memdup(((args=do_selectfile(NILP(args)?
  344.            pattern:EVAL(args)))!=0)?args:F);
  345. }
  346.  
  347. Byte *bif_mp2_type(Byte *args)
  348. {
  349.     Byte *s;
  350.  
  351.     args = args;        /* Fool Pure C. */    
  352.     sprintf(s=mem(128+strlen(filename)),
  353.         "((filename %s) "
  354.         "(layer 2) (bitrate %ld) (frequency %ld) "
  355.         "(filelength %ld) (timelength %ld))",
  356.         filename,
  357.         mp2info.bitrate, mp2info.sample_frequency,
  358.         mp2info.filelength, mp2info.timelength);
  359.  
  360.     return s;
  361. }
  362.  
  363. Byte *bif_mp2_loadedp(Byte *args)
  364. {
  365.     args = args;
  366.     return stream_loadedp()?T:F;
  367. }
  368.  
  369. Byte *bif_mp2_playp(Byte *args)
  370. {
  371.     args = args;        /* Fool Pure C. */    
  372.     return replay?T:F;
  373. }
  374.  
  375. Byte *bif_mp2_pausep(Byte *args)
  376. {
  377.     args = args;        /* Fool Pure C. */    
  378.     return replay_pausep()?T:F;
  379. }
  380.  
  381. Byte *bif_mp2_loopp(Byte *args)
  382. {
  383.     args = args;        /* Fool Pure C. */    
  384.     return looping?T:F;
  385. }
  386.  
  387. Byte *bif_mp2_play_time(Byte *args)
  388. {
  389.     Byte *buf;
  390.  
  391.     args = args;        /* Fool Pure C. */    
  392.  
  393.     buf = mem(16);
  394.     sprintf(buf, "%ld", calc_time());
  395.     
  396.     return buf;
  397. }
  398.  
  399. Byte *bif_print(Byte *args)
  400. {
  401.     args = EVAL(args);
  402. /*    print_line(decode_string(args)); */
  403.     print_line(args);
  404.     prompt_console(0, -1);
  405.     return args;
  406. }
  407.  
  408. Byte *bif_mp2_clear_console(Byte *args)
  409. {
  410.     args = args;
  411.     clear_console();
  412.     return T;
  413. }
  414.  
  415. Byte *bif_mp2_close_window(Byte *args)
  416. {
  417.     args = args;
  418.     window_close(-1);
  419.     return T;
  420. }
  421.  
  422. Byte *bif_mp2_switch_window(Byte *args)
  423. {
  424.     args = args;
  425.     window_switch();
  426.     return T;
  427. }
  428.  
  429. Byte *bif_mp2_display_time(Byte *args)
  430. {
  431.     if(NILP(args))
  432.         return (display_time = !display_time)!=0?T:F;
  433.     return (display_time = TP(EVAL(args)))!=0?T:F;
  434. }
  435.  
  436. Byte *bif_mp2_external_clock(Byte *args)
  437. {
  438.     char *arg, *new;
  439.  
  440.     if(!LISTP(args))
  441.         return ERR;
  442.  
  443.     arg = memdup(args);
  444.     arg++;
  445.     arg[strlen(arg)-1] = '\0';
  446.     
  447.     {    
  448.     DUAL_EVAL(arg, {
  449.         ext0 = atol(a);
  450.         ext1 = atol(b);
  451.  
  452.         if(ext0 != 44100L && ext0 != 48000L)
  453.             ext0 = 0;
  454.         if(ext1 != 44100L && ext1 != 48000L)
  455.             ext1 = 0;
  456.  
  457.         new = mem(20);
  458.         sprintf(new, "(%ld %ld)", ext0, ext1);
  459.  
  460.         arg = new;
  461.     });
  462.     }
  463. }
  464.  
  465.  
  466. Byte *bif_mp2_external_clockp(Byte *args)
  467. {
  468.     char *s;
  469.     
  470.     args = args;
  471.  
  472.     s = mem(20);
  473.     sprintf(s, "(%ld %ld)", ext0, ext1);
  474.  
  475.     return s;
  476. }
  477.  
  478. Byte *bif_mp2_countdown(Byte *args)
  479. {
  480.     if(NILP(args))
  481.         return (count_dir = !count_dir)!=0?T:F;
  482.     return (count_dir = TP(EVAL(args)))!=0?T:F;
  483. }
  484.  
  485. Byte *bif_mp2_title(Byte *args)
  486. {
  487.     if(NILP(args))
  488.         return set_title(""), memdup("");
  489. /*    return set_title(decode_string(args = EVAL(args))), args; */
  490.     args = EVAL(args);
  491.     set_title(args);
  492.     return args;
  493. }
  494.  
  495. Byte *bif_mp2_subtitle(Byte *args)
  496. {
  497.     if(NILP(args)) {
  498.         set_subtitle("");
  499.         return memdup("");
  500.     }
  501. /*    return set_subtitle(decode_string(args = EVAL(args))), args; */
  502.     args = EVAL(args);
  503.     set_subtitle(args);
  504.     return args;
  505. }
  506.  
  507. Byte *bif_mp2_dsp_load(Byte *args)
  508. {
  509.     Byte *s;
  510.  
  511.     args = args;        /* Fool Pure C. */    
  512.     sprintf(s = mem(128), "(%ld %ld)",
  513.             decoder_current_load(), decoder_average_load());
  514.     return s;
  515. }
  516.  
  517. Byte *bif_mp2_place_window_control(Byte *args)
  518. {
  519.     DUAL_EVAL(args, 
  520.         {window_move(WIND_CTRL, (int)atol(a), (int)atol(b)); args=T;});
  521. }
  522.  
  523. Byte *bif_mp2_place_window_info(Byte *args)
  524. {
  525.     DUAL_EVAL(args, 
  526.         {window_move(WIND_INFO, (int)atol(a), (int)atol(b)); args=T;});
  527. }
  528.  
  529. Byte *bif_mp2_place_window_console(Byte *args)
  530. {
  531.     DUAL_EVAL(args, 
  532.         {window_move(WIND_SHOE, (int)atol(a), (int)atol(b)); args=T;});
  533. }
  534.  
  535. void call_control_key(char c)
  536. {
  537.     static char s[] = "(mp2-control-x)";
  538.     s[sizeof(s)-3] = c;
  539.     eval(s);
  540. }
  541.  
  542. void load_bifs(void)
  543. {
  544.     bif("notify-gc",  bif_notify_gc);
  545.     bif("panic",      bif_panic);
  546.     /* bif("error", bif_error); */
  547.  
  548.     bif("ls",         bif_ls);
  549.     bif("pwd",        bif_pwd);
  550.     bif("cd",         bif_cd);
  551.     bif("time",       bif_time);
  552.     bif("date",       bif_date);
  553.     bif("load",       bif_load);
  554.     bif("exit",       bif_exit);
  555.  
  556.     bif("print",      bif_print);
  557.  
  558.     bif("mp2-load",         bif_mp2_load);
  559.     bif("mp2-play",         bif_mp2_play);
  560.     bif("mp2-stop",         bif_mp2_stop);
  561.     bif("mp2-pause",        bif_mp2_pause);
  562.     bif("mp2-loop",         bif_mp2_loop);
  563.     bif("mp2-fast-forward", bif_mp2_fast_forward);
  564.     bif("mp2-select",       bif_mp2_select);
  565.  
  566.     bif("mp2-type",         bif_mp2_type);
  567.     bif("mp2-play-time",    bif_mp2_play_time);
  568.  
  569.     bif("mp2-loaded?", bif_mp2_loadedp);
  570.     bif("mp2-play?",   bif_mp2_playp);
  571.     bif("mp2-pause?",  bif_mp2_pausep);
  572.     bif("mp2-loop?",   bif_mp2_loopp);
  573.  
  574.     bif("mp2-window-info",    bif_mp2_window_info);
  575.     bif("mp2-window-console", bif_mp2_window_console);
  576.  
  577.     bif("mp2-clear-console", bif_mp2_clear_console);
  578.     bif("mp2-close-window",  bif_mp2_close_window);
  579.     bif("mp2-switch-window", bif_mp2_switch_window);
  580.  
  581.     bif("mp2-display-time", bif_mp2_display_time);
  582.     bif("mp2-countdown",    bif_mp2_countdown);
  583.     bif("mp2-title",        bif_mp2_title);
  584.     bif("mp2-subtitle",     bif_mp2_subtitle);
  585.  
  586.     bif("mp2-external-clock", bif_mp2_external_clock);
  587.     bif("mp2-external-clock?", bif_mp2_external_clockp);
  588.  
  589.     bif("mp2-dsp-load", bif_mp2_dsp_load);
  590.  
  591.     bif("mp2-place-window-control", bif_mp2_place_window_control);
  592.     bif("mp2-place-window-info", bif_mp2_place_window_info);
  593.     bif("mp2-place-window-console", bif_mp2_place_window_console);
  594. }
  595.