home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / wp_dtp / xdme1820.lha / XDME / macros.c < prev    next >
C/C++ Source or Header  |  1993-03-08  |  7KB  |  274 lines

  1. /******************************************************************************
  2.  
  3.     MODUL
  4.     lowlevelmacros.c
  5.  
  6.     DESCRIPTION
  7.     die schnittstelle zu den wichtigsten Macro-funktionen,
  8.     die von anderen Moduln verwendet werden duerfen.
  9.  
  10.     NOTES
  11.     dies ist nur ein Dummy - Modul, das zwar alle
  12.     Funktionen exportiert, die beschrieben wurden,
  13.     ihre Realisierung ist jedoch stark eingeschraenkt,
  14.     sie sind auf einfache Strings zurueckgeschraubt,
  15.     so dass die sie verwendenden Moduln wie vorher
  16.     angesprochen werden koennen.
  17.  
  18.     BUGS
  19.  
  20.     TODO
  21.     bei Gelegenheit sollte dieses Modul komplett neu geschrieben werden
  22.     und mit alias.c und main.c neu abgestimmt werden
  23.     (so dass main.c callmacro verwendet neque vero do_command)
  24.  
  25.     EXAMPLES
  26.  
  27.     SEE ALSO
  28.  
  29.     INDEX
  30.  
  31.     HISTORY
  32.  
  33. ******************************************************************************/
  34.  
  35. /**************************************
  36.         Includes
  37. **************************************/
  38. #include "defs.h"
  39.  
  40.  
  41. /**************************************
  42.         Globale Variable
  43. **************************************/
  44. Prototype char * GetArg      (int);
  45. Prototype char * CommandName (void);
  46.  
  47.  
  48. Prototype void start_recording    (void);
  49. Prototype void end_recording    (void);
  50. Prototype void replay_record    (void);
  51. Prototype void do_saverecord    (void);
  52. Prototype void add_record    (char * string);
  53.  
  54.  
  55. /**************************************
  56.       Interne Defines & Strukturen
  57. **************************************/
  58.  
  59.  
  60. /**************************************
  61.         Interne Variable
  62. **************************************/
  63. static char tempmacro_buffer[LINE_LENGTH];
  64.  
  65.  
  66. /**************************************
  67.        Interne Prototypes
  68. **************************************/
  69.  
  70.  
  71.         /***********************************************
  72.         ***********************************************
  73.  
  74.         Dies ist lediglich eine Uebergangsloesung,
  75.         solange wir alias.c und main.c noch nicht
  76.         umgeschrieben haben
  77.  
  78.         ***********************************************
  79.         ***********************************************/
  80.  
  81.  
  82.  
  83.  
  84. char * CommandName (void)
  85. {
  86.     return (av[0]);
  87. } /* CommandName */
  88.  
  89. char * GetArg (int num)
  90. {
  91.     return(av[num]);
  92. } /* GetArg */
  93.  
  94.  
  95.  
  96.  
  97. /*
  98. **  MACRO RECORDER
  99. **    we can record one input - sequence and replay it
  100. **
  101. **    there are 3 functions:
  102. **
  103. **  recording
  104. **    int Start_recording (void)
  105. **    int add_record        (char * string)
  106. **    int end_recording   (void)
  107. **
  108. **  playing
  109. **    int replay_record   (void)
  110. **
  111. **  saving
  112. **    int save_record     (FILE * fo)
  113. **        (can be sourced)
  114. **
  115. */
  116.  
  117. static struct MinList record         = { NULL, NULL, NULL };  /* list to contain the macros */
  118. static short          record_replaying     = 0;              /* security to avoid endless loops */
  119.  
  120. struct LongNode {
  121.     struct MinNode node;
  122.     char       com[0];
  123. };
  124.  
  125. __autoinit
  126. void init_recorder (void)
  127. {
  128.     if (record.mlh_Head == NULL) {
  129.     NewList ((struct List *)&record);
  130.     return;
  131.     } /* if */
  132. } /* init_recorder */
  133.  
  134. __autoexit
  135. void clear_record (void)
  136. {
  137.     struct LongNode * ln;
  138.  
  139.     while (ln = (struct LongNode *)RemHead ((struct List *)&record)) {
  140.     FreeMem (ln, sizeof (struct MinNode) + 1 + strlen (ln->com));
  141.     } /* while */
  142. } /* clear_record */
  143.  
  144. void start_recording (void)
  145. {
  146.     if (!record_replaying) {
  147.     if (MacroRecord == 0) {
  148.         clear_record();
  149.     } /* if */
  150.  
  151.     MacroRecord ++;
  152.     } else {
  153.     error ("%s:\nYou can NOT start recording,\nwhile You are replaying a record", av[0]);
  154.     } /* if */
  155. } /* start_recording */
  156.  
  157. void end_recording (void)
  158. {
  159.     if (MacroRecord) {
  160.     MacroRecord --;
  161.  
  162.     {
  163.         char ab = globalflags.Abortcommand;
  164.         error ("recorder ended!");
  165.         globalflags.Abortcommand = ab;
  166.     }
  167.  
  168. #    ifdef NOT_DEF
  169.         /* strenggenommen muessten wir zuallererstden recorderend aus dem letzten string herausfiltern */
  170.         /* was aber, wenn der nicht als command sondern als macro kam ??? */
  171.         if (MacroRecord == ~0) {
  172.         struct LongNode * ln;
  173.         if (ln = (struct LongNode *)RemTail ((struct List *)&record)) {
  174.             strcpy (tmp_buffer, ln->com);
  175.  
  176.         } /* if */
  177.         } /* if */
  178. #    endif
  179.  /* } else if (!record_replaying) { */
  180.      /* error ("%s:\nYou can NOT end recording,\nif You are NOT recording", av[0]); */
  181.     } /* if */
  182. } /* end_recording */
  183.  
  184. void add_record (char * string)
  185. {
  186.     if (!record_replaying) {
  187.     struct LongNode * ln;
  188.  
  189.     if (ln = AllocMem (sizeof (struct MinNode) + 1 + strlen (string), MEMF_ANY)) {
  190.         strcpy (ln->com, string);
  191.         AddTail ((struct List *)&record, (struct Node *)ln);
  192.     } else {
  193.         nomemory ();     /* do NOT abort - we have to execute commands, too */
  194.     } /* if */
  195.  
  196.     if (breakcheck()) {  /* that way we might have a sure record-terminator - the CTL-C signal */
  197.         end_recording ();
  198.     } /* if */
  199.     } /* if */
  200. } /* add_record */
  201.  
  202. void replay_record (void)
  203. {
  204.     if (!record_replaying) {
  205.     if (GetHead (&record)) {
  206.         char * buffer;
  207.         /* We use a run-time allocated buffer */
  208.         if (!(buffer = AllocMem (LINE_LENGTH, MEMF_ANY))) {
  209.         nomemory ();     /* error() NEEDS memory for itself */
  210.         globalflags.Abortcommand = 1;
  211.         return;
  212.         } else {
  213.         struct LongNode * ln;
  214.  
  215.         for (ln = GetHead (&record); ln; ln = GetSucc (ln)) {
  216.             strncpy (buffer, ln->com, LINE_LENGTH-1);
  217.             do_command (buffer);
  218.  
  219.             /* You can NOT terminate Your record by errors */
  220.             clearbreaks ();
  221.  
  222.             /* You can break Your record by pressing Ctrl-C */
  223.             if (breakcheck ()) {
  224.             globalflags.Abortcommand = 1;
  225.             break;
  226.             } /* if */
  227.         } /* for */
  228.  
  229.         FreeMem (buffer, LINE_LENGTH);
  230.         } /* if alloced */
  231.     } /* if */
  232.     } else {
  233.     error ("%s:\nYou can NOT start replaying a record,\nwhile You are already replaying it", av[0]);
  234.     } /* if not already replaying */
  235. } /* replay_record */
  236.  
  237. void save_record (FILE * fo)
  238. {
  239.     if (fo) {
  240.     struct LongNode * ln;
  241.     for (ln = GetHead (&record); ln; ln = GetSucc (ln)) {
  242.         sprintf (tmp_buffer, "%s\n", ln->com);
  243.         if (fputs (tmp_buffer, fo) < 0) {
  244.         error ("%s:\nError while writing the\ncurrent record!", av[0]);
  245.         break;
  246.         } /* if */
  247.     } /* for */
  248.     } /* if */
  249. } /* save_record */
  250.  
  251. void do_saverecord (void)
  252. {
  253.     FILE  * fo;
  254.  
  255.     if (!GetHead (&record)) {
  256.     error ("%s:\nCan not save a record,\n if there is NO record!", av[0]);
  257.     } else {
  258.     if (fo = fopen (av[1], "w")) {
  259.         save_record (fo);
  260.         fclose (fo);
  261.     } else {
  262.         error ("%s:\n Can not open File\n'%s' for output", av[0], av[1]);
  263.     } /* if */
  264.     } /* if */
  265. } /* do_saverecord */
  266.  
  267.  
  268.  
  269.  
  270.  
  271. /******************************************************************************
  272. *****  ENDE macros.c
  273. ******************************************************************************/
  274.