home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum7.lzh / C / CHARDEF / chardef.c < prev    next >
C/C++ Source or Header  |  1988-10-04  |  11KB  |  592 lines

  1. # include <stdio.h>
  2. # define U(x) x
  3. # define NLSTATE yyprevious=YYNEWLINE
  4. # define BEGIN yybgin = yysvec + 1 +
  5. # define INITIAL 0
  6. # define YYLERR yysvec
  7. # define YYSTATE (yyestate-yysvec-1)
  8. # define YYOPTIM 1
  9. # define YYLMAX 200
  10. # define output(c) putc(c,yyout)
  11. # define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==13?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
  12. # define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;}
  13. # define yymore() (yymorfg=1)
  14. # define ECHO fprintf(yyout, "%s",yytext)
  15. # define REJECT { nstr = yyreject(); goto yyfussy;}
  16. int yyleng; extern char yytext[];
  17. int yymorfg;
  18. extern char *yysptr, yysbuf[];
  19. int yytchar;
  20. FILE *yyin ={stdin}, *yyout ={stdout};
  21. extern int yylineno;
  22. struct yysvf { 
  23.     struct yywork *yystoff;
  24.     struct yysvf *yyother;
  25.     int *yystops;};
  26. struct yysvf *yyestate;
  27. extern struct yysvf yysvec[], *yybgin;
  28. /*  chardef    ein Program um den freidefinierbaren Zeichensatz eine VT220-kompatiblen Terminals zu definieren */
  29. /*  Uwe Simon            25.9.88                                                                                   */
  30. /*                                                                                                               */
  31. #include <ctype.h>
  32.  
  33. int aktline = -1;  
  34. int aktchar = -1;  /* noch kein Zeichen aktiviert */
  35. int erase = 1;     /* kein loeschen der alten Zeichen */
  36. int line_nr=1;     /* aktuelle Zeilennummer */
  37. int    charset=0;     /* Zeichensatz fuer die definition */
  38.  
  39. int    character[94][14];
  40. int is_def[95];
  41. char    c_set = '<';     /* default: der Supplemental-charset wird ueberschrieben */
  42.  
  43. set_charset(s)
  44. char    *s;
  45. {
  46.     c_set = s[7];
  47. }
  48.  
  49. clear_charset()
  50. {
  51.     erase=0;
  52. }
  53.  
  54.  
  55. char_number(s)
  56. char *s;
  57. {
  58.     int h;
  59.     
  60.     h=atoi(s);
  61.     if((h<33) || (h>126))  /* falsche Nummer */
  62.     {
  63.         aktchar = -1;
  64.         aktline = -1;
  65.         in_line();
  66.         fprintf(stderr,"Illegal Characternumber\n");
  67.         return;        
  68.     }
  69.     aktchar = h-33;
  70.     is_def[aktchar] = 1;
  71.     aktline = 0;
  72. }
  73.  
  74. build_char(n)
  75. int    n;
  76. {
  77.     int i, j, k, mask;
  78.     char s[20];
  79.     
  80.     for(i=0;i<15;i++) s[i]='\0';
  81.  
  82.     for(i=0; i<6; i++) character[n][i] = character[n][i+6] | (character[n][i]<<7);
  83.  
  84.     mask = 0x01;
  85.     for(i=0;i<6;i++)
  86.     {
  87.         k=0;
  88.         for(j=0x2000; j; j=j>>1)
  89.         {
  90.             if(character[n][i] & j)
  91.                 s[k] |= mask;
  92.                 
  93.             k++;
  94.         }
  95.         
  96.         mask = mask <<1;
  97.     }    
  98.     
  99.     for(i=0;i<14;i++)
  100.     {
  101.         s[i]+= 0x3f;
  102.         if(i==7)
  103.             printf("/");
  104.             
  105.         printf("%c",s[i]);
  106.     }
  107.     
  108.     if(is_def[n+1])
  109.     {
  110.         printf(";");
  111.         return(0);
  112.     }
  113.     else
  114.         return(1);
  115. }
  116.  
  117. send_charset()
  118. {
  119.     int i,j;
  120.     int first;
  121.     
  122.     first=1;
  123.     
  124.     for(i=0;i<94;i++)
  125.         if(is_def[i])
  126.         {
  127.             if(first)
  128.             {
  129.                 printf("%cP1;%d;%d{%c",0x1b,i+1,erase,c_set);
  130.                 erase=1;
  131.             }
  132.                 
  133.             first = build_char(i);
  134.             
  135.             if(first)        
  136.                 printf("%c\\",0x1b);
  137.         }
  138.  
  139. }
  140.  
  141.  
  142. build_line(s)
  143. char    *s;
  144. {
  145.     int h;
  146.     
  147.     if((aktchar < 0) || (aktline < 0))   /* keine Zeichennummer gewesen */
  148.     {
  149.         in_line();
  150.         fprintf(stderr, "no Characternumber spezified\n");
  151.     }
  152.     else
  153.         if(aktline >10)   /* keine Zeichennummer gewesen */
  154.         {
  155.             in_line();
  156.             fprintf(stderr, "Only 10 lines per character\n");
  157.         }
  158.         else
  159.         {    
  160.             h=0;            
  161.             for(; *s; s++)
  162.             {
  163.                 h = h<<1;
  164.                 if(*s == '*')
  165.                     h++;
  166.             }
  167.  
  168.             character[aktchar][aktline++] = h & 0xff;
  169.         }
  170. }
  171.  
  172.  
  173. in_line()
  174. {
  175.     fprintf(stderr,"Line %d:",line_nr);
  176. }
  177.  
  178.  
  179. usage()
  180. {
  181.     fprintf(stderr,"Syntax: defchar [<path>]\n");
  182.     fprintf(stderr,"Function:defines a new Charset for VT220 Terminals\n");
  183.     fprintf(stderr,"Options: none\n");
  184.     exit(0);
  185. }
  186.  
  187.  
  188. yywrap()
  189. {
  190.     return(feof(stdin));
  191. }
  192.  
  193. main(argc, argv)
  194. int argc;
  195. char *argv[];
  196. {
  197.     int h;
  198.     int line=1;
  199.     
  200.     if(argc > 1)
  201.     {
  202.         if(strcmp(argv[1], "-?") == 0)
  203.             usage();
  204.             
  205.         if(freopen(argv[1],"r", stdin) == NULL) 
  206.         {
  207.             fprintf(stderr,"can't open %s\n", argv[1]);
  208.             exit(1);
  209.         }
  210.     }
  211.     cleareof(stdin);
  212.     
  213.     while(h=yylex())
  214.     {
  215.         switch(h)
  216.         {
  217.             default:
  218.                 in_line(line);
  219.                 printf("Syntax-Error\n");
  220.                 break;
  221.         }
  222.         line++;
  223.     }
  224. }
  225. # define YYNEWLINE 13
  226. yylex(){
  227. int nstr; extern int yyprevious;
  228. while((nstr = yylook()) >= 0)
  229. yyfussy: switch(nstr){
  230. case 0:
  231. if(yywrap()) return(0); break;
  232. case 1:
  233.     ;
  234. break;
  235. case 2:
  236.  set_charset(yytext);
  237. break;
  238. case 3:
  239.     clear_charset();
  240. break;
  241. case 4:
  242.     ;
  243. break;
  244. case 5:
  245.     char_number(yytext);
  246. break;
  247. case 6:
  248.             line_nr++;
  249. break;
  250. case 7:
  251.     build_line(yytext);
  252. break;
  253. case 8:
  254.     send_charset();
  255. break;
  256. case -1:
  257. break;
  258. default:
  259. fprintf(yyout,"bad switch yylook %d",nstr);
  260. } return(0); }
  261. /* end of yylex */
  262. int yyvstop[] ={
  263. 0,
  264.  
  265. 6,
  266. 0,
  267.  
  268. 4,
  269. 0,
  270.  
  271. -1,
  272. 0,
  273.  
  274. 7,
  275. 0,
  276.  
  277. 1,
  278. 0,
  279.  
  280. 5,
  281. 0,
  282.  
  283. 8,
  284. 0,
  285.  
  286. 3,
  287. 0,
  288.  
  289. 2,
  290. 0,
  291. 0};
  292. # define YYTYPE char
  293. struct yywork { YYTYPE verify, advance; } yycrank[] ={
  294. 0,0,    0,0,    0,0,    0,0,    
  295. 0,0,    0,0,    0,0,    0,0,    
  296. 0,0,    0,0,    0,0,    0,0,    
  297. 0,0,    0,0,    1,3,    0,0,    
  298. 0,0,    0,0,    0,0,    0,0,    
  299. 0,0,    0,0,    0,0,    0,0,    
  300. 0,0,    0,0,    0,0,    0,0,    
  301. 0,0,    0,0,    0,0,    0,0,    
  302. 0,0,    1,4,    4,4,    0,0,    
  303. 1,5,    0,0,    0,0,    0,0,    
  304. 0,0,    0,0,    0,0,    1,6,    
  305. 6,6,    0,0,    0,0,    1,6,    
  306. 6,6,    1,7,    1,7,    1,7,    
  307. 1,7,    1,7,    1,7,    1,7,    
  308. 1,7,    1,7,    1,7,    0,0,    
  309. 5,0,    5,0,    5,0,    5,0,    
  310. 5,0,    5,0,    5,0,    5,0,    
  311. 5,0,    5,0,    5,0,    5,0,    
  312. 5,10,    5,0,    5,0,    5,0,    
  313. 5,0,    5,0,    5,0,    5,0,    
  314. 5,0,    5,0,    5,0,    5,0,    
  315. 5,0,    5,0,    5,0,    5,0,    
  316. 5,0,    5,0,    5,0,    5,5,    
  317. 5,5,    0,0,    0,0,    0,0,    
  318. 0,0,    0,0,    0,0,    12,15,    
  319. 1,8,    5,5,    1,9,    13,16,    
  320. 14,17,    16,19,    8,12,    5,5,    
  321. 20,22,    0,0,    8,13,    0,0,    
  322. 9,14,    0,0,    0,0,    0,0,    
  323. 15,18,    5,5,    7,7,    7,7,    
  324. 7,7,    7,7,    7,7,    7,7,    
  325. 7,7,    7,7,    7,7,    7,7,    
  326. 7,11,    18,20,    19,21,    22,23,    
  327. 23,0,    23,0,    23,0,    23,0,    
  328. 23,0,    23,0,    23,0,    23,0,    
  329. 23,0,    23,0,    23,0,    23,0,    
  330. 23,0,    23,0,    23,0,    23,0,    
  331. 23,0,    23,0,    23,0,    23,0,    
  332. 23,0,    23,0,    23,0,    23,0,    
  333. 23,0,    23,0,    23,0,    23,0,    
  334. 23,0,    23,0,    23,0,    23,0,    
  335. 23,0,    23,0,    23,0,    23,0,    
  336. 23,0,    23,0,    23,0,    23,0,    
  337. 23,0,    23,0,    23,0,    23,0,    
  338. 23,0,    23,0,    23,0,    23,24,    
  339. 0,0,    0,0,    0,0,    0,0,    
  340. 0,0,    0,0,    5,0,    0,0,    
  341. 0,0,    23,24,    0,0,    0,0,    
  342. 0,0,    0,0,    0,0,    0,0,    
  343. 0,0,    0,0,    0,0,    0,0,    
  344. 0,0,    0,0,    0,0,    0,0,    
  345. 0,0,    0,0,    0,0,    0,0,    
  346. 0,0,    0,0,    0,0,    0,0,    
  347. 0,0,    0,0,    0,0,    0,0,    
  348. 0,0,    0,0,    0,0,    0,0,    
  349. 0,0,    0,0,    0,0,    0,0,    
  350. 0,0,    0,0,    0,0,    0,0,    
  351. 0,0,    0,0,    0,0,    0,0,    
  352. 0,0,    0,0,    0,0,    0,0,    
  353. 0,0,    0,0,    0,0,    0,0,    
  354. 0,0,    0,0,    0,0,    0,0,    
  355. 0,0,    0,0,    0,0,    0,0,    
  356. 0,0,    0,0,    0,0,    0,0,    
  357. 0,0,    0,0,    0,0,    0,0,    
  358. 0,0,    0,0,    23,0,    0,0,    
  359. 0,0};
  360. struct yysvf yysvec[] ={
  361. 0,    0,    0,
  362. yycrank+1,    0,        0,    
  363. yycrank+0,    yysvec+1,    0,    
  364. yycrank+0,    0,        yyvstop+1,
  365. yycrank+2,    0,        yyvstop+3,
  366. yycrank+-59,    0,        yyvstop+5,
  367. yycrank+2,    0,        yyvstop+7,
  368. yycrank+70,    0,        0,    
  369. yycrank+2,    0,        0,    
  370. yycrank+2,    0,        0,    
  371. yycrank+0,    0,        yyvstop+9,
  372. yycrank+0,    0,        yyvstop+11,
  373. yycrank+2,    0,        0,    
  374. yycrank+2,    0,        0,    
  375. yycrank+4,    0,        0,    
  376. yycrank+2,    0,        0,    
  377. yycrank+8,    0,        0,    
  378. yycrank+0,    0,        yyvstop+13,
  379. yycrank+14,    0,        0,    
  380. yycrank+16,    0,        0,    
  381. yycrank+7,    0,        0,    
  382. yycrank+0,    0,        yyvstop+15,
  383. yycrank+15,    0,        0,    
  384. yycrank+-131,    0,        0,    
  385. yycrank+0,    0,        yyvstop+17,
  386. 0,    0,    0};
  387. struct yywork *yytop = yycrank+258;
  388. struct yysvf *yybgin = yysvec+1;
  389. char yymatch[] ={
  390. 00  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,
  391. 01  ,01  ,01  ,01  ,01  ,015 ,01  ,01  ,
  392. 01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,
  393. 01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,
  394. 040 ,'!' ,'!' ,'!' ,'!' ,'!' ,'!' ,'!' ,
  395. '!' ,'!' ,'*' ,'!' ,'!' ,'!' ,'*' ,'!' ,
  396. '0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,
  397. '0' ,'0' ,':' ,':' ,':' ,':' ,':' ,':' ,
  398. ':' ,':' ,':' ,':' ,':' ,':' ,':' ,':' ,
  399. ':' ,':' ,':' ,':' ,':' ,':' ,':' ,':' ,
  400. ':' ,':' ,':' ,':' ,':' ,':' ,':' ,':' ,
  401. ':' ,':' ,':' ,':' ,':' ,':' ,':' ,':' ,
  402. ':' ,':' ,':' ,':' ,':' ,':' ,':' ,':' ,
  403. ':' ,':' ,':' ,':' ,':' ,':' ,':' ,':' ,
  404. ':' ,':' ,':' ,':' ,':' ,':' ,':' ,':' ,
  405. ':' ,':' ,':' ,':' ,':' ,':' ,':' ,01  ,
  406. 0};
  407. char yyextra[] ={
  408. 0,1,0,0,0,0,0,0,
  409. 0,0,0,0,0,0,0,0,
  410. 0};
  411. /*    ncform    4.1    83/08/11    */
  412.  
  413. int yylineno =1;
  414. # define YYU(x) x
  415. # define NLSTATE yyprevious=YYNEWLINE
  416. char yytext[YYLMAX];
  417. struct yysvf *yylstate [YYLMAX], **yylsp, **yyolsp;
  418. char yysbuf[YYLMAX];
  419. char *yysptr = yysbuf;
  420. int *yyfnd;
  421. extern struct yysvf *yyestate;
  422. int yyprevious = YYNEWLINE;
  423. yylook(){
  424.     register struct yysvf *yystate, **lsp;
  425.     register struct yywork *yyt;
  426.     struct yysvf *yyz;
  427.     int yych;
  428.     struct yywork *yyr;
  429. # ifdef LEXDEBUG
  430.     int debug;
  431. # endif
  432.     char *yylastch;
  433.     /* start off machines */
  434. # ifdef LEXDEBUG
  435.     debug = 0;
  436. # endif
  437.     if (!yymorfg)
  438.         yylastch = yytext;
  439.     else {
  440.         yymorfg=0;
  441.         yylastch = yytext+yyleng;
  442.         }
  443.     for(;;){
  444.         lsp = yylstate;
  445.         yyestate = yystate = yybgin;
  446.         if (yyprevious==YYNEWLINE) yystate++;
  447.         for (;;){
  448. # ifdef LEXDEBUG
  449.             if(debug)fprintf(yyout,"state %d\n",yystate-yysvec-1);
  450. # endif
  451.             yyt = yystate->yystoff;
  452.             if(yyt == yycrank){        /* may not be any transitions */
  453.                 yyz = yystate->yyother;
  454.                 if(yyz == 0)break;
  455.                 if(yyz->yystoff == yycrank)break;
  456.                 }
  457.             *yylastch++ = yych = input();
  458.         tryagain:
  459. # ifdef LEXDEBUG
  460.             if(debug){
  461.                 fprintf(yyout,"char ");
  462.                 allprint(yych);
  463.                 putchar('\n');
  464.                 }
  465. # endif
  466.             yyr = yyt;
  467.             if ( (int)yyt > (int)yycrank){
  468.                 yyt = yyr + yych;
  469.                 if (yyt <= yytop && yyt->verify+yysvec == yystate){
  470.                     if(yyt->advance+yysvec == YYLERR)    /* error transitions */
  471.                         {unput(*--yylastch);break;}
  472.                     *lsp++ = yystate = yyt->advance+yysvec;
  473.                     goto contin;
  474.                     }
  475.                 }
  476. # ifdef YYOPTIM
  477.             else if((int)yyt < (int)yycrank) {        /* r < yycrank */
  478.                 yyt = yyr = yycrank+(yycrank-yyt);
  479. # ifdef LEXDEBUG
  480.                 if(debug)fprintf(yyout,"compressed state\n");
  481. # endif
  482.                 yyt = yyt + yych;
  483.                 if(yyt <= yytop && yyt->verify+yysvec == yystate){
  484.                     if(yyt->advance+yysvec == YYLERR)    /* error transitions */
  485.                         {unput(*--yylastch);break;}
  486.                     *lsp++ = yystate = yyt->advance+yysvec;
  487.                     goto contin;
  488.                     }
  489.                 yyt = yyr + YYU(yymatch[yych]);
  490. # ifdef LEXDEBUG
  491.                 if(debug){
  492.                     fprintf(yyout,"try fall back character ");
  493.                     allprint(YYU(yymatch[yych]));
  494.                     putchar('\n');
  495.                     }
  496. # endif
  497.                 if(yyt <= yytop && yyt->verify+yysvec == yystate){
  498.                     if(yyt->advance+yysvec == YYLERR)    /* error transition */
  499.                         {unput(*--yylastch);break;}
  500.                     *lsp++ = yystate = yyt->advance+yysvec;
  501.                     goto contin;
  502.                     }
  503.                 }
  504.             if ((yystate = yystate->yyother) && (yyt= yystate->yystoff) != yycrank){
  505. # ifdef LEXDEBUG
  506.                 if(debug)fprintf(yyout,"fall back to state %d\n",yystate-yysvec-1);
  507. # endif
  508.                 goto tryagain;
  509.                 }
  510. # endif
  511.             else
  512.                 {unput(*--yylastch);break;}
  513.         contin:
  514. # ifdef LEXDEBUG
  515.             if(debug){
  516.                 fprintf(yyout,"state %d char ",yystate-yysvec-1);
  517.                 allprint(yych);
  518.                 putchar('\n');
  519.                 }
  520. # endif
  521.             ;
  522.             }
  523. # ifdef LEXDEBUG
  524.         if(debug){
  525.             fprintf(yyout,"stopped at %d with ",*(lsp-1)-yysvec-1);
  526.             allprint(yych);
  527.             putchar('\n');
  528.             }
  529. # endif
  530.         while (lsp-- > yylstate){
  531.             *yylastch-- = 0;
  532.             if (*lsp != 0 && (yyfnd= (*lsp)->yystops) && *yyfnd > 0){
  533.                 yyolsp = lsp;
  534.                 if(yyextra[*yyfnd]){        /* must backup */
  535.                     while(yyback((*lsp)->yystops,-*yyfnd) != 1 && lsp > yylstate){
  536.                         lsp--;
  537.                         unput(*yylastch--);
  538.                         }
  539.                     }
  540.                 yyprevious = YYU(*yylastch);
  541.                 yylsp = lsp;
  542.                 yyleng = yylastch-yytext+1;
  543.                 yytext[yyleng] = 0;
  544. # ifdef LEXDEBUG
  545.                 if(debug){
  546.                     fprintf(yyout,"\nmatch ");
  547.                     sprint(yytext);
  548.                     fprintf(yyout," action %d\n",*yyfnd);
  549.                     }
  550. # endif
  551.                 return(*yyfnd++);
  552.                 }
  553.             unput(*yylastch);
  554.             }
  555.         if (yytext[0] == 0  /* && feof(yyin) */)
  556.             {
  557.             yysptr=yysbuf;
  558.             return(0);
  559.             }
  560.         yyprevious = yytext[0] = input();
  561.         if (yyprevious>0)
  562.             output(yyprevious);
  563.         yylastch=yytext;
  564. # ifdef LEXDEBUG
  565.         if(debug)putchar('\n');
  566. # endif
  567.         }
  568.     }
  569. yyback(p, m)
  570.     int *p;
  571. {
  572. if (p==0) return(0);
  573. while (*p)
  574.     {
  575.     if (*p++ == m)
  576.         return(1);
  577.     }
  578. return(0);
  579. }
  580.     /* the following are only used in the lex library */
  581. yyinput(){
  582.     return(input());
  583.     }
  584. yyoutput(c)
  585.   int c; {
  586.     output(c);
  587.     }
  588. yyunput(c)
  589.    int c; {
  590.     unput(c);
  591.     }
  592.