| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| %option full |
| %option never-interactive |
| %option noyywrap |
| %option outfile="wcsutrn.c" |
| %option prefix="wcsutrn" |
| %option reentrant |
|
|
| |
| %x NEXT FLUSH |
|
|
| %{ |
| |
| |
| #define _POSIX_SOURCE 1 |
|
|
| #include <setjmp.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
|
|
| #include "wcserr.h" |
| #include "wcsunits.h" |
|
|
| static int wcsutrne_scanner(int ctrl, char unitstr[], struct wcserr **err, |
| yyscan_t yyscanner); |
|
|
| |
|
|
| int wcsutrne( |
| int ctrl, |
| char unitstr[], |
| struct wcserr **err) |
|
|
| { |
| int status; |
| yyscan_t yyscanner; |
| int yylex_init(yyscan_t *yyscanner); |
| int yylex_destroy(yyscan_t yyscanner); |
|
|
| yylex_init(&yyscanner); |
| status = wcsutrne_scanner(ctrl, unitstr, err, yyscanner); |
| yylex_destroy(yyscanner); |
|
|
| return status; |
| } |
|
|
| |
|
|
| #define YY_DECL int wcsutrne_scanner(int ctrl, char unitstr[], \ |
| struct wcserr **err, yyscan_t yyscanner) |
|
|
| |
| jmp_buf wcsutrn_abort_jmp_env; |
| #define exit(status) longjmp(wcsutrn_abort_jmp_env, status) |
|
|
| %} |
|
|
| %% |
| static const char *function = "wcsutrne"; |
| |
| char orig[80], subs[80]; |
| int bracket = 0; |
| int unsafe = 0; |
| int status = -1; |
| |
| if (err) *err = 0x0; |
| |
| *orig = '\0'; |
| *subs = '\0'; |
| |
| yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner); |
| yy_scan_string(unitstr, yyscanner); |
| *unitstr = '\0'; |
| |
| |
| if (setjmp(wcsutrn_abort_jmp_env)) { |
| return wcserr_set(WCSERR_SET(UNITSERR_PARSER_ERROR), |
| "Internal units translator error parsing '%s'", unitstr); |
| } |
| |
| BEGIN(INITIAL); |
| |
| #ifdef DEBUG |
| fprintf(stderr, "\n%s ->\n", unitstr); |
| #endif |
|
|
| ^" "*"[" { |
| |
| strcat(unitstr, "["); |
| bracket = 1; |
| } |
|
|
| " "+ |
|
|
| [^A-Za-z] { |
| |
| strcat(unitstr, yytext); |
| if (bracket && *yytext == ']') { |
| BEGIN(FLUSH); |
| } |
| } |
|
|
| angstrom { |
| strcpy(orig, yytext); |
| strcpy(subs, "Angstrom"); |
| BEGIN(NEXT); |
| } |
|
|
| arcmins|ARCMINS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "arcmin"); |
| BEGIN(NEXT); |
| } |
|
|
| arcsecs|ARCSECS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "arcsec"); |
| BEGIN(NEXT); |
| } |
|
|
| BEAM { |
| strcpy(orig, yytext); |
| strcpy(subs, "beam"); |
| BEGIN(NEXT); |
| } |
|
|
| Byte { |
| strcpy(orig, yytext); |
| strcpy(subs, "byte"); |
| BEGIN(NEXT); |
| } |
|
|
| days?|DAYS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "d"); |
| BEGIN(NEXT); |
| } |
|
|
| D { |
| unsafe = 1; |
| strcpy(orig, yytext); |
| strcpy(subs, (ctrl & 4) ? "d" : "D"); |
| BEGIN(NEXT); |
| } |
|
|
| degrees?|Deg|Degrees?|DEG|DEGREES? { |
| strcpy(orig, yytext); |
| strcpy(subs, "deg"); |
| BEGIN(NEXT); |
| } |
|
|
| GHZ { |
| strcpy(orig, yytext); |
| strcpy(subs, "GHz"); |
| BEGIN(NEXT); |
| } |
|
|
| hr|HR { |
| strcpy(orig, yytext); |
| strcpy(subs, "h"); |
| BEGIN(NEXT); |
| } |
|
|
| H { |
| unsafe = 1; |
| strcpy(orig, yytext); |
| strcpy(subs, (ctrl & 2) ? "h" : "H"); |
| BEGIN(NEXT); |
| } |
|
|
| hz|HZ { |
| strcpy(orig, yytext); |
| strcpy(subs, "Hz"); |
| BEGIN(NEXT); |
| } |
|
|
| KHZ { |
| strcpy(orig, yytext); |
| strcpy(subs, "kHz"); |
| BEGIN(NEXT); |
| } |
|
|
| JY { |
| strcpy(orig, yytext); |
| strcpy(subs, "Jy"); |
| BEGIN(NEXT); |
| } |
|
|
| [kK]elvins?|KELVINS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "K"); |
| BEGIN(NEXT); |
| } |
|
|
| KM { |
| strcpy(orig, yytext); |
| strcpy(subs, "km"); |
| BEGIN(NEXT); |
| } |
|
|
| metres?|meters?|M|METRES?|METERS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "m"); |
| BEGIN(NEXT); |
| } |
|
|
| MIN { |
| strcpy(orig, yytext); |
| strcpy(subs, "min"); |
| BEGIN(NEXT); |
| } |
|
|
| MHZ { |
| strcpy(orig, yytext); |
| strcpy(subs, "MHz"); |
| BEGIN(NEXT); |
| } |
|
|
| Ohm { |
| strcpy(orig, yytext); |
| strcpy(subs, "ohm"); |
| BEGIN(NEXT); |
| } |
|
|
| [pP]ascals?|PASCALS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "Pa"); |
| BEGIN(NEXT); |
| } |
|
|
| pixels|PIXELS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "pixel"); |
| BEGIN(NEXT); |
| } |
|
|
| radians?|RAD|RADIANS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "rad"); |
| BEGIN(NEXT); |
| } |
|
|
| sec|seconds?|SEC|SECONDS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "s"); |
| BEGIN(NEXT); |
| } |
|
|
| S { |
| unsafe = 1; |
| strcpy(orig, yytext); |
| strcpy(subs, (ctrl & 1) ? "s" : "S"); |
| BEGIN(NEXT); |
| } |
|
|
| [vV]olts?|VOLTS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "V"); |
| BEGIN(NEXT); |
| } |
|
|
| years?|YR|YEARS? { |
| strcpy(orig, yytext); |
| strcpy(subs, "yr"); |
| BEGIN(NEXT); |
| } |
|
|
| [A-Za-z]+ { |
| |
| strcpy(orig, yytext); |
| strcpy(subs, orig); |
| BEGIN(NEXT); |
| } |
|
|
| <NEXT>[A-Za-z]+ { |
| |
| strcat(orig, yytext); |
| strcpy(subs, orig); |
| } |
|
|
| <NEXT>" "+[^A-Za-z] { |
| |
| unput(yytext[yyleng-1]); |
| } |
|
|
| <NEXT>" "+[A-Za-z] { |
| |
| strcat(unitstr, subs); |
| strcat(unitstr, " "); |
| if (strcmp(orig, subs)) status = 0; |
| unput(yytext[yyleng-1]); |
| *subs = '\0'; |
| BEGIN(INITIAL); |
| } |
|
|
| <NEXT>. { |
| |
| strcat(unitstr, subs); |
| if (strcmp(orig, subs)) status = 0; |
| unput(*yytext); |
| *subs = '\0'; |
| BEGIN(INITIAL); |
| } |
|
|
| <FLUSH>.* { |
| |
| strcat(unitstr, yytext); |
| } |
|
|
| <<EOF>> { |
| |
| if (*subs) { |
| strcat(unitstr, subs); |
| if (strcmp(orig, subs)) status = 0; |
| } |
| |
| if (unsafe) { |
| return wcserr_set(WCSERR_SET(UNITSERR_UNSAFE_TRANS), |
| "Unsafe unit translation in '%s'", unitstr); |
| } |
| return status; |
| } |
|
|
| %% |
|
|