home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / gofer / Sources / c / commonui < prev    next >
Encoding:
Text File  |  1993-02-12  |  10.7 KB  |  386 lines

  1. /* --------------------------------------------------------------------------
  2.  * commonui.c:  Copyright (c) Mark P Jones 1991-1993.   All rights reserved.
  3.  *              See goferite.h for details and conditions of use etc...
  4.  *              Gofer version 2.28 January 1993
  5.  *
  6.  * Parts of user interface common to both compiler and interpreter.
  7.  * ------------------------------------------------------------------------*/
  8.  
  9. /* --------------------------------------------------------------------------
  10.  * Local function prototypes:
  11.  * ------------------------------------------------------------------------*/
  12.  
  13. static Void   local toggleSet          Args((Char,Bool));
  14. static Void   local togglesIn          Args((Bool));
  15. static Void   local optionInfo          Args((Void));
  16. static Void   local processOption     Args((String));
  17. static Int    local argToInt          Args((String *));
  18.  
  19. static Void   local loadProject       Args((String));
  20. static Void   local clearProject      Args((Void));
  21. static Void   local addScriptName     Args((String));
  22. static Void   local addScript          Args((String,Long));
  23. static Void   local forgetScriptsFrom Args((Module));
  24.  
  25. static Void   local setLastEdit       Args((String,Int));
  26.  
  27. static Void   local failed          Args((Void));
  28.  
  29. static String local strCopy          Args((String));
  30. static Int    local substr          Args((String,String));
  31.  
  32. /* --------------------------------------------------------------------------
  33.  * Local data areas:
  34.  * ------------------------------------------------------------------------*/
  35.  
  36. static String scriptName[NUM_MODULES];    /* Script file names           */
  37. static Int    numScripts;        /* Number of scripts loaded       */
  38. static Int    namesUpto;        /* Number of script names set       */
  39.  
  40. static String currProject = 0;        /* Name of current project file       */
  41. static Bool   projectLoaded = FALSE;    /* TRUE => project file loaded       */
  42. static String scriptFile;        /* Name of current script (if any) */
  43.  
  44. #if RISCOS
  45. static Bool   useDots    = TRUE;    /* TRUE => use dots in progress    */
  46. #else
  47. static Bool   useDots    = FALSE;    /* TRUE => use dots in progress    */
  48. #endif
  49. static String lastEdit     = 0;        /* Name of file to edit (if any)   */
  50. static Int    lastLine     = 0;        /* Editor line number (if possible)*/
  51. static String prompt     = 0;        /* Prompt string (gofer only)       */
  52. static String outputFile = 0;        /* User spec. output file (gofc)   */
  53.  
  54. /* --------------------------------------------------------------------------
  55.  * Command line options:
  56.  * ------------------------------------------------------------------------*/
  57.  
  58. struct options {            /* command line option toggles       */
  59.     char   c;                /* table defined in main app.       */
  60.     String description;
  61.     Bool   *flag;
  62. };
  63. extern struct options toggle[];
  64.  
  65. static Void local toggleSet(c,state)    /* Set command line toggle       */
  66. Char c;
  67. Bool state; {
  68.     Int i;
  69.     for (i=0; toggle[i].c; ++i)
  70.     if (toggle[i].c == c) {
  71.         *toggle[i].flag = state;
  72.         return;
  73.     }
  74.     ERROR(0) "Unknown toggle `%c'", c
  75.     EEND;
  76. }
  77.  
  78. static Void local togglesIn(state)    /* Print current list of toggles in*/
  79. Bool state; {                /* given state               */
  80.     Int count = 0;
  81.     Int i;
  82.     for (i=0; toggle[i].c; ++i)
  83.     if (*toggle[i].flag == state) {
  84.         if (count==0)
  85.         putchar(state ? '+' : '-');
  86.         putchar(toggle[i].c);
  87.         count++;
  88.     }
  89.     if (count>0)
  90.     putchar(' ');
  91. }
  92.  
  93. static Void local optionInfo() {    /* Print information about command */
  94.     static String fmts = "%-5s%s\n";    /* line settings           */
  95.     static String fmtc = "%-5c%s\n";
  96.     Int    i;
  97.  
  98.     printf("TOGGLES: groups begin with +/- to turn options on/off resp.\n");
  99.     for (i=0; toggle[i].c; ++i)
  100.     printf(fmtc,toggle[i].c,toggle[i].description);
  101.  
  102.     printf("\nOTHER OPTIONS: (leading + or - makes no difference)\n");
  103.     printf(fmts,"hnum","Set heap size (cannot be changed within Gofer)");
  104.     printf(fmts,"pstr","Set prompt string to str");
  105.     printf(fmts,"rstr","Set repeat last expression string to str");
  106. #ifdef TECH_TOGGLES
  107.     printf(fmts,"xnum","Set maximum depth for evidence search");
  108. #endif
  109.  
  110.     printf("\nCurrent settings: ");
  111.     togglesIn(TRUE);
  112.     togglesIn(FALSE);
  113. #ifdef TECH_TOGGLES
  114.     printf("-h%d -p%s -x%d -r%s\n",heapSize,prompt,maxEvidLevel,repeatStr);
  115. #else
  116.     printf("-h%d -p%s -r%s\n",heapSize,prompt,repeatStr);
  117. #endif
  118. }
  119.  
  120. static Void local processOption(s)    /* process option string s       */
  121. String s; {
  122.     Bool state = (s[0]=='+' ? TRUE : FALSE);
  123.  
  124.     while (*++s)
  125.     switch (*s) {
  126.         case 'n' : if (s[1]) {
  127.                if (outputFile) free(outputFile);
  128.                outputFile = strCopy(s+1);
  129.                }
  130.                return;
  131.  
  132.         case 'p' : if (s[1]) {
  133.                if (prompt) free(prompt);
  134.                prompt = strCopy(s+1);
  135.                }
  136.                return;
  137.  
  138.         case 'r' : if (s[1]) {
  139.                if (repeatStr) free(repeatStr);
  140.                repeatStr = strCopy(s+1);
  141.                }
  142.                return;    
  143.  
  144.         case 'h' : if (heapBuilt()) {
  145.                ERROR(0) "Cannot change heap size"
  146.                EEND;
  147.                }
  148.                heapSize = argToInt(&s);
  149.                if (heapSize<MINIMUMHEAP)
  150.                heapSize = MINIMUMHEAP;
  151. #if MAXIMUMHEAP > 0
  152.                else if (heapSize>MAXIMUMHEAP)
  153.                heapSize = MAXIMUMHEAP;
  154. #endif
  155.                break;
  156.  
  157. #ifdef TECH_TOGGLES
  158.         case 'x' : maxEvidLevel = argToInt(&s);
  159.                break;
  160. #endif
  161.  
  162.         default  : toggleSet(*s,state);
  163.                break;
  164.     }
  165. }
  166.  
  167. static Int local argToInt(sp)        /* read integer from argument str  */
  168. String *sp; {
  169.     Int num = 0;
  170.     while (isascii((*sp)[1]) && isdigit((*sp)[1])) {
  171.     num = 10*num + (*(++*sp) - '0');
  172.     }
  173.     return num;
  174. }
  175.  
  176. /* --------------------------------------------------------------------------
  177.  * Loading project and script files:
  178.  * ------------------------------------------------------------------------*/
  179.  
  180. static Void local loadProject(s)    /* Load project file          */
  181. String s; {
  182.     clearProject();
  183.     currProject = s;
  184.     projInput(currProject);
  185.     scriptFile = currProject;
  186.     forgetScriptsFrom(1);
  187.     while (s=readFilename())
  188.     addScriptName(s);
  189.     if (namesUpto<=1) {
  190.     ERROR(0) "Empty project file"
  191.     EEND;
  192.     }
  193.     scriptFile    = 0;
  194.     projectLoaded = TRUE;
  195. }
  196.  
  197. static Void local clearProject() {     /* clear name for current project   */
  198.     if (currProject)
  199.     free(currProject);
  200.     currProject   = 0;
  201.     projectLoaded = FALSE;
  202. }
  203.  
  204. static Void local addScriptName(s)     /* add script name to list of files */
  205. String s; {                   /* to be read in ...           */
  206.     if (s[0]=='-' || s[0]=='+')
  207.     processOption(s);
  208.     else if (namesUpto>=NUM_MODULES) {
  209.     ERROR(0) "Too many script files (maximum of %d allowed)",
  210.          NUM_MODULES
  211.     EEND;
  212.     }
  213.     else
  214.     scriptName[namesUpto++] = strCopy(s);
  215. }
  216.  
  217. static Void local addScript(fname,len) /* read single script file       */
  218. String fname;                   /* name of script file           */
  219. Long   len; {                   /* length of script file        */
  220.     scriptFile = fname;
  221.  
  222.     printf("Reading script file \"%s\":\n",fname);
  223.     setLastEdit(fname,0);
  224.  
  225.     parseScript(fname,len);           /* process script file           */
  226.     checkDefns();
  227.     if (numScripts==0)            /* initialisation to be done once  */
  228.     everybody(PRELUDE);        /* prelude Tycons and Classes known*/
  229.     typeCheckDefns();    
  230.     compileDefns();
  231.  
  232.     scriptFile = 0;
  233. }
  234.  
  235. static Void local forgetScriptsFrom(scno)/* remove scripts from system       */
  236. Module scno; {
  237.     Module i;
  238.     for (i=scno; i<namesUpto; ++i)
  239.     if (scriptName[i])
  240.         free(scriptName[i]);
  241.     dropModulesFrom(scno-1);         /* don't count prelude as module  */
  242.     namesUpto = scno;
  243.     if (numScripts>0)
  244.     numScripts = scno;
  245. }
  246.  
  247. static Void local setLastEdit(fname,line)/* keep name of last file to edit */
  248. String fname;
  249. Int    line; {
  250.     if (lastEdit)
  251.     free(lastEdit);
  252.     lastEdit = strCopy(fname);
  253.     lastLine = line;
  254. }
  255.  
  256. /* --------------------------------------------------------------------------
  257.  * Display progress towards goal:
  258.  * ------------------------------------------------------------------------*/
  259.  
  260. static Target currTarget;
  261. static Bool   aiming = FALSE;
  262. static Int    currPos;
  263. static Int    maxPos;
  264. static Int    charCount;
  265.  
  266. Void setGoal(what, t)               /* Set goal for what to be t       */
  267. String what;
  268. Target t; {
  269.     currTarget = (t?t:1);
  270.     aiming     = TRUE;
  271.     if (useDots) {
  272.     currPos = strlen(what);
  273.     maxPos  = getTerminalWidth() - 1;
  274.     printf("%s",what);
  275.     }
  276.     else
  277.     for (charCount=0; *what; charCount++)
  278.         putchar(*what++);
  279.     fflush(stdout);
  280. }
  281.  
  282. Void soFar(t)                   /* Indicate progress towards goal   */
  283. Target t; {                   /* has now reached t           */
  284.     if (useDots) {
  285.     Int newPos = (Int)((maxPos * ((long)t))/currTarget);
  286.  
  287.     if (newPos>maxPos)
  288.         newPos = maxPos;
  289.  
  290.     if (newPos>currPos) {
  291.         do
  292.         putchar('.');
  293.         while (newPos>++currPos);
  294.         fflush(stdout);
  295.     }
  296.     fflush(stdout);
  297.     }
  298. }
  299.  
  300. Void done() {                   /* Goal has now been achieved       */
  301.     if (useDots) {
  302.     while (maxPos>currPos++)
  303.         putchar('.');
  304.     putchar('\n');
  305.     aiming = FALSE;
  306.     }
  307.     else
  308.     for (; charCount>0; charCount--) {
  309.         putchar('\b');
  310.         putchar(' ');
  311.         putchar('\b');
  312.     }
  313.     fflush(stdout);
  314. }
  315.  
  316. static Void local failed() {           /* Goal cannot be reached due to    */
  317.     if (aiming) {               /* errors               */
  318.     aiming = FALSE;
  319.     putchar('\n');
  320.     fflush(stdout);
  321.     }
  322. }
  323.  
  324. /* --------------------------------------------------------------------------
  325.  * Send message to each component of system:
  326.  * ------------------------------------------------------------------------*/
  327.  
  328. Void everybody(what)        /* send command `what' to each component of*/
  329. Int what; {            /* system to respond as appropriate ...    */
  330.     machdep(what);        /* The order of calling each component is  */
  331.     storage(what);        /* important for the INSTALL command       */
  332.     input(what);
  333.     staticAnalysis(what);
  334.     typeChecker(what);
  335.     compiler(what);
  336.     machine(what);
  337.     builtIn(what);
  338. }
  339.  
  340. /* --------------------------------------------------------------------------
  341.  * Read value from environment variable:
  342.  * ------------------------------------------------------------------------*/
  343.  
  344. String fromEnv(var,def)        /* return value of:                */
  345. String var;            /*     environment variable named by var   */
  346. String def; {            /* or: default value given by def       */
  347.     String s = getenv(var);
  348.  
  349.     return (s ? s : def);
  350. }
  351.  
  352. /* --------------------------------------------------------------------------
  353.  * String manipulation routines:
  354.  * ------------------------------------------------------------------------*/
  355.  
  356. static String local strCopy(s)           /* make malloced copy of a string   */
  357. String s; {
  358.     if (s) {
  359.     char *t,*r;
  360.     if ((t=(char *)malloc(strlen(s)+1))==0) {
  361.         ERROR(0) "String storage space exhausted"
  362.         EEND;
  363.     }
  364.     for (r=t; *r++ = *s++; )
  365.         ;
  366.     return t;
  367.     }
  368.     return s;
  369. }
  370.  
  371. static Int local substr(s1,s2)        /* find posn of substring s1 in s2 */
  372. String s1, s2; {            /* (naive implementation)       */
  373.     String t;
  374.  
  375.     for (t=s2; *t; t++) {
  376.     Int i = 0;
  377.         while (s1[i] && s1[i]==t[i])
  378.         i++;
  379.     if (s1[i]=='\0')
  380.         return t-s2;
  381.     }
  382.     return (-1);
  383. }
  384.  
  385. /*-------------------------------------------------------------------------*/
  386.