home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / texmf / source / TeX / evpath / TeXSaveString.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  3.1 KB  |  164 lines

  1. /*
  2.  * TeXSaveString.c - a simple file to show how EVPaths.lib routines work.
  3.  *
  4.  * This program do not use any startup code.
  5.  *
  6.  * © 1994 by Giuseppe Ghibò
  7.  *
  8.  * Version 1.0 - 16 Oct 1994
  9.  *
  10.  * Compile with:
  11.  * 
  12.  *     SC OPT LINK LIB NOSTKCHK NOSTARTUP EVPaths.lib TeXSaveString.c
  13.  * or
  14.  *
  15.  *    SC OPT NOSTKCHK TeXSaveString.c
  16.  *      SLINK FROM TeXSaveString.o TO TeXSaveString LIB EVPaths.lib LIB:sc.lib
  17.  *
  18.  * or
  19.  *    SC OPT NOSTKCHK NOSTARTUP LINK TeXSaveString.c EVPaths.c SNPrintf.a
  20.  *
  21.  *
  22.  * Usage:
  23.  *
  24.  *    TeXSaveString VAR=MYVAR FILE=<filename> [SHOW] STRING=<string>
  25.  *
  26.  * For example, with:
  27.  *
  28.  *    SetEnv NAME=GOOFY STRING=ram:,T:
  29.  *    TeXSaveString VAR=GOOFY FILE=one.txt STRING=example
  30.  *
  31.  * obtain:
  32.  *
  33.  *     File saved: `ram:one.txt'
  34.  *
  35.  * Then type:
  36.  *
  37.  *    Protect ram:one.txt -d
  38.  *
  39.  * and again:
  40.  *
  41.  *    TeXSaveString VAR=GOOFY FILE=one.txt STRING=example SHOW
  42.  *
  43.  * Then obtain:
  44.  *
  45.  *    File saved: `ram:T.txt'
  46.  *
  47.  */
  48.  
  49. #include <exec/execbase.h>
  50. #include <workbench/startup.h>
  51.  
  52. #include <string.h>
  53.  
  54. #include <proto/dos.h>
  55. #include <proto/exec.h>
  56.  
  57. #include "evpaths.h"
  58.  
  59. #define TEMPLATE "VAR/K,FILE/A,SHOW/S,STRING/F"
  60.  
  61. enum {    OPT_VAR,
  62.     OPT_FILE,
  63.     OPT_SHOW,
  64.     OPT_STRING,
  65.     OPT_COUNT };
  66.  
  67. extern struct ExecBase *SysBase;
  68. extern struct DosLibrary *DOSBase;
  69.  
  70. #define BUFLEN 256
  71. #define EVPBUF 8192L
  72.  
  73. void __saveds Main(void) {
  74.     struct Process *me;
  75.     struct WBStartup *WBenchMsg;
  76.     struct RDArgs *rdargs;
  77.     LONG opts[OPT_COUNT];
  78.     char buf[BUFLEN];
  79.     STRPTR varname = "", fname = NULL, String;
  80.     BOOL show = 0;
  81.     struct EnvVarPath *var;
  82.     BPTR fh;
  83.  
  84.     SysBase = *(struct ExecBase **)4;
  85.  
  86.     if ((DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 36L)) == NULL)
  87.         return;
  88.  
  89.     me = (struct Process *)SysBase->ThisTask;
  90.  
  91.     if (me->pr_CLI)
  92.     {
  93.         WBenchMsg = NULL;
  94.  
  95.         memset (opts, 0, sizeof(opts));
  96.  
  97.         if (rdargs = ReadArgs(TEMPLATE, opts, NULL))
  98.         {
  99.             if (opts[OPT_VAR])
  100.                 varname = (STRPTR) opts[OPT_VAR];
  101.  
  102.             if (opts[OPT_FILE])
  103.                 fname = (STRPTR) opts[OPT_FILE];
  104.  
  105.             if (opts[OPT_STRING])
  106.                 String =  (STRPTR) opts[OPT_STRING];
  107.  
  108.             if (opts[OPT_SHOW])
  109.                 show = 1;
  110.  
  111.             var = Alloc_EnvVarPath(varname, EVPBUF);
  112.  
  113.             Init_EnvVarPath(var, NULL, NULL);
  114.             Init_EnvVarPath(var, ".", ENVPATH_DEFSTR);
  115.  
  116.             if (show)
  117.             {
  118.                 int i = 0;
  119.  
  120.                 Printf("The environment variable `%s' has length %ld\n", varname, GetVarLength(varname));
  121.                 while (var->storage.strings[i])
  122.                     Printf("%ld -> `%s'\n", i, var->storage.strings[i++]);
  123.             }
  124.  
  125. //            if ((fh = EVP_Open(fname, var, NULL, NULL, MODE_NEWFILE)) != DOSFALSE)
  126.             if ((fh = EVP_Open(fname, var, buf, 256, MODE_NEWFILE)) != DOSFALSE)
  127.             {
  128.                 Write(fh, String, strlen(String));
  129.                 Close(fh);
  130.                 Printf("File saved: `%s'\n",buf);
  131.             }
  132.             else
  133.                 Printf("Can't save file `%s'.\n",fname);
  134.  
  135.             Free_EnvVarPath(var);
  136.             FreeArgs(rdargs);
  137.         }
  138.         else
  139.             PrintFault(IoErr(), NULL);
  140.     }
  141.     else
  142.     {
  143.         BPTR StdOut;
  144.  
  145.         WaitPort(&me->pr_MsgPort);
  146.         WBenchMsg = (struct WBStartup *)GetMsg(&me->pr_MsgPort);
  147.  
  148.         if (StdOut = Open("CON:20/20/400/80/A Window/Auto/Close/Wait", MODE_NEWFILE))
  149.         {
  150.             FPrintf(StdOut,"You must run me from CLI, not from WB!\n");
  151.             Close(StdOut);
  152.         }
  153.     }
  154.  
  155.     if (DOSBase)
  156.         CloseLibrary((struct Library *)DOSBase);
  157.  
  158.     if (WBenchMsg)
  159.     {
  160.         Forbid();
  161.         ReplyMsg(&WBenchMsg->sm_Message);
  162.     }
  163. }
  164.