home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / cpm68k / utils.lbr / PTOC.CQ / PTOC.C
Encoding:
Text File  |  1986-05-22  |  3.4 KB  |  139 lines

  1. /* -*-c,save-*- */
  2. /*------------------------------------------------------
  3.  *    PTOC.C - Pascal to C pre-translator
  4.  *    Robert Heller Fri Feb  1 09:55:14 1985
  5.  *------------------------------------------------------*/
  6. /*#define VMS /* VMS version */
  7. #define CPM /* CP/M-68K version */
  8. #ifdef VMS
  9. #include <stdio.h>    /* standard I/O */
  10. #include <ctype.h>    /* character macros */
  11. #define FAST register    /* (not really needed under VMS...) */
  12. #define ROM readonly static    /* (for Read Only stuff, if any) */
  13. #define LOCAL static    /* for static stuff */
  14. #define GLOBAL extern    /* for external stuff */
  15. #endif
  16. #ifdef CPM
  17. #include <stdio.h>    /* standard I/O */
  18. #include <ctype.h>    /* character macros */
  19. #define FAST register    /* fast access vars */
  20. #define ROM static    /* (for Read Only stuff, if any) */
  21. #define LOCAL static    /* for static stuff */
  22. #define GLOBAL extern    /* for external stuff */
  23. #define CPMEOF 0x1a    /* ^Z - EndOfFile under CP/M */
  24. #endif
  25.  
  26. /* main routine:  get words and special character and process them */
  27. main()
  28. {
  29.     FAST char *letter;
  30.     LOCAL word[100];
  31.     FAST int wordlnth,c;
  32.  
  33.     letter = (&word[0]);
  34.     wordlnth = 0;
  35.     while ((c=getchar()) != EOF
  36. #ifdef CPM
  37.     || (isatty(fileno(stdin)) && c != CPMEOF)
  38. #endif
  39.     ) {
  40.     if (isalpha(c)) {
  41.         *letter++ = c;
  42.         wordlnth++;
  43.         }
  44.     else {
  45.         if (wordlnth > 0) {
  46.         *letter = '\0';
  47.         wtest(word);
  48.         wordlnth = 0;
  49.         letter = (&word[0]);
  50.         }
  51.         ctest(c);
  52.         }
  53.     }
  54.     if (wordlnth>0) {
  55.     *letter = '\0';
  56.     wtest(word);
  57.     putchar('\n');
  58.     }
  59.     }
  60. wtest(word)
  61. FAST char *word;
  62. {
  63.     FAST char *swapword;
  64.  
  65.     swapword = word;
  66.     switch (*word) {
  67.     case 'w': case 'W':
  68.     if (cf_strcmp(word,"writeln") == 0 ||
  69.         cf_strcmp(word,"write") == 0) swapword = "printf";
  70.     break;
  71.     case 'r': case 'R':
  72.     if (cf_strcmp(word,"readln") == 0 ||
  73.         cf_strcmp(word,"read") == 0) swapword = "scanf";
  74.     break;
  75.     case 't': case 'T':
  76.     if (cf_strcmp(word,"then") == 0) swapword = "\0";
  77.     break;
  78.     case 'a': case 'A':
  79.     if (cf_strcmp(word,"and") == 0) swapword = "&&";
  80.     break;
  81.     case 'o': case 'O':
  82.     if (cf_strcmp(word,"or") == 0) swapword = "||";
  83.     break;
  84.     case 'b': case 'B':
  85.     if (cf_strcmp(word,"begin") == 0) swapword = "{";
  86.     break;
  87.     case 'e': case 'E':
  88.     if (cf_strcmp(word,"end") == 0) swapword = ";}";
  89.     break;
  90.     default: break;
  91.     }
  92.     swap(swapword);
  93.     }
  94. ctest(c)
  95. FAST c;
  96. {
  97.     switch (c) {
  98.     case '\'' : putchar('"'); break;
  99.     case '{' : swap("/*"); break;
  100.     case '}' : swap("*/"); break;
  101.     case '(' : swapif('(','*',"/*"); break;
  102.     case '*' : swapif('*',')',"*/"); break;
  103.     case ':' : swapif(':','=',"="); break;
  104.     case '<' : swapif('<','>',"!="); break;
  105.     case '>' : putchar(c); putchar(getchar()); break;
  106.     case '=' : swap("=="); break;
  107.     default: putchar(c);
  108.     }
  109.     }
  110. swap(s)
  111. FAST char *s;
  112. {
  113.     while(*s != '\0') putchar(*s++);
  114.     }
  115. swapif(first,second,replacement)
  116. FAST char first,second,*replacement;
  117. {
  118.     FAST char c;
  119.  
  120.     if ((c=getchar()) == second) swap(replacement);
  121.     else {
  122.     putchar(first);
  123.     ungetc(c,stdin);
  124.     }
  125.     }
  126. cf_strcmp(s1,s2)
  127. FAST char *s1,*s2;
  128. {
  129.     FAST int i;
  130.  
  131.     while(*s1 != '\0' && *s2 != '\0' && toupper(*s1) == toupper(*s2)) {
  132.     s1++;
  133.     s2++;
  134.     }
  135.     i = toupper(*s1) - toupper(*s2);
  136.     if (i<0) return(-1);
  137.     else if (i>0) return(1);
  138.     else return(0);
  139.     }