00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #define _GNU_SOURCE
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <getopt.h>
00029 #include <yyast/main.h>
00030 #include <yyast/utils.h>
00031 #include <yyast/node.h>
00032 #include <yyast/count.h>
00033
00034 extern FILE *yyin;
00035 int yyparse();
00036
00037 char *ya_output_filename = NULL;
00038 char *ya_input_filename = NULL;
00039
00040 void ya_usage(int exit_code)
00041 {
00042 fprintf(stderr, "Usage:\n");
00043 fprintf(stderr, " application -h\n");
00044 fprintf(stderr, " application [-c] [-o output file] input file\n");
00045 fprintf(stderr, "\n");
00046 fprintf(stderr, "Options:\n");
00047 fprintf(stderr, " -h Show help message\n");
00048 fprintf(stderr, " -c Compile, this option is ignored\n");
00049 fprintf(stderr, " -o Set the output file, the default is the same as the input file\n");
00050 fprintf(stderr, "\n");
00051 exit(exit_code);
00052 }
00053
00054 void ya_parse_options(int argc, char *argv[], char *extension)
00055 {
00056 int ch;
00057 struct option longopts[] = {
00058 {"output", required_argument, NULL, 'o'},
00059 {"compile", no_argument, NULL, 'c'},
00060 {"help", no_argument, NULL, 'h'},
00061 {NULL, 0, NULL, 0}
00062 };
00063
00064 while ((ch = getopt_long(argc, argv, "hco:", longopts, NULL)) != -1) {
00065 switch (ch) {
00066 case 'o':
00067
00068 ya_output_filename = optarg;
00069 break;
00070 case 'h':
00071
00072 ya_usage(0);
00073 case 'c':
00074
00075 break;
00076 case 0:
00077 break;
00078 case ':':
00079 fprintf(stderr, "Missing option argument.");
00080 ya_usage(2);
00081 case '?':
00082 fprintf(stderr, "Unknown option.");
00083 ya_usage(2);
00084 default:
00085 fprintf(stderr, "Unknown error in getopt_long().");
00086 ya_usage(2);
00087 }
00088 }
00089 argc -= optind;
00090 argv += optind;
00091
00092 if (argc != 1) {
00093 fprintf(stderr, "Expecting a single filename.\n");
00094 ya_usage(2);
00095 }
00096
00097 ya_input_filename = argv[0];
00098
00099 if (ya_output_filename == NULL) {
00100
00101 ya_output_filename = ya_new_extension(ya_input_filename, extension);
00102 }
00103 }
00104
00105 int ya_main(int argc, char *argv[], char *extension)
00106 {
00107 FILE *out;
00108 char *reposition_s;
00109
00110 ya_parse_options(argc, argv, extension);
00111
00112 if (strcmp(ya_input_filename, "-") == 0) {
00113 yyin = stdin;
00114 } else {
00115 if ((yyin = fopen(ya_input_filename, "r")) == NULL) {
00116 perror("Could not open input file");
00117 return -1;
00118 }
00119 }
00120
00121 if (asprintf(&reposition_s, "1 \"%s\"", ya_input_filename) >= 0) {
00122 ya_reposition(reposition_s, strlen(reposition_s));
00123 free(reposition_s);
00124 }
00125
00126 yyparse();
00127 fclose(yyin);
00128
00129 if (strcmp(ya_output_filename, "-") == 0) {
00130 out = stdout;
00131 } else {
00132 if ((out = fopen(ya_output_filename, "w")) == NULL) {
00133 perror("Could not open output file");
00134 return -1;
00135 }
00136 }
00137 ya_node_save(out, &ya_start);
00138 fclose(out);
00139
00140 return 0;
00141 }