Example #1
0
/** 
 * \brief Process compiler options.
 * \param argc The number of input parameters.
 * \param argv The input parameter strings.
 * \return Indicates if processing was successful. 
 *         0 = processing successful
 *         1 = processing not successful
 *
 * Processes the input options passed to the compiler
 * and fill the compiler options structure as appropriate.
 * 
 * The following options are supported:
 *  -h: print help
 *  -p: print the IR to a file
 *  -t: test modus, only success/failure log
 *  -o: the output file name (different from 'input'.o)
 */
int process_options(int argc, char *argv[]) {
	int opt;
	int ret = 0;

	/* add a handler to resource manager to free resources
	 * in the case of an error during option processing */
	rm_register_handler(&resource_mgr, free_options, NULL);

	while ((opt = getopt(argc, argv, "hpto:")) != -1) {
		switch (opt) {
		case 'p':
			cc_options.print_ir = 1;
			break;
		case 't':
			/* fewer logs, for automated testing */
			cc_options.print_only_errors = 1;
			break;
		case 'o':
			/* output file */
			cc_options.output_file = strdup(optarg);
			if (cc_options.output_file == NULL) {
				fatal_os_error(OUT_OF_MEMORY, 1, __FILE__, __LINE__, "");
				return 1;
			}
			break;
		case 'h':
			/* print help */
			print_usage(argv[0]);
			rm_cleanup_resources(&resource_mgr);
			exit(EXIT_SUCCESS);

		default: /* '?' */
			/* print usage */
			fprintf(stderr, "ERROR: unknown parameter: %s\n", argv[optind]);
			print_usage(argv[0]);
			return 1;
		}
	}

	if (optind >= argc) {
		fprintf(stderr, "ERROR: missing input file\n");
		print_usage(argv[0]);
		ret = 1;
	} else if (optind < argc - 1) {
		fprintf(stderr, "ERROR: too many input files\n");
		print_usage(argv[0]);
		ret = 1;
	} else {
		cc_options.input_file = strdup(argv[optind]);
		if (cc_options.input_file == NULL) {
			fatal_os_error(OUT_OF_MEMORY, 1, __FILE__, __LINE__, "");
			return 1;
		}

		char *filebase = get_file_basename(cc_options.input_file);
		;
		if (filebase == NULL) {
			return 1;
		}

		if (!has_file_extension(cc_options.input_file, C_EXT)) {
			fprintf(stderr, "ERROR: no C file (.c) as input\n");
			ret = 1;
		} else {
			/* The file name has a valid .c extension */
			if (cc_options.output_file == NULL) {
				/* create output file name <input>.o */
				cc_options.output_file = get_filename_with_ext(filebase,
						OUTPUT_EXT);
				if (cc_options.output_file == NULL) {
					ret = 1;
				}
			}
			if (cc_options.print_ir == 1) {
				/* create IR file name <input>.ir */
				cc_options.ir_file = get_filename_with_ext(filebase, IR_EXT);
				if (cc_options.ir_file == NULL) {
					ret = 1;
				}
			}
		}

		free(filebase);
	}

	return ret;
}
Example #2
0
static int
FileSelectionDialog(GtkWidget *parent, int type, char *stock)
{
  struct nGetOpenFileData *data;
  GtkWidget *dlg, *rc;
  GtkFileFilter *filter;
  char *fname;

  data = &FileSelection;

  data->parent = (parent) ? parent : TopLevel;
  dlg = gtk_file_chooser_dialog_new(data->title,
				    GTK_WINDOW(data->parent),
				    type,
				    _("_Cancel"), GTK_RESPONSE_CANCEL,
				    stock, GTK_RESPONSE_ACCEPT,
				    NULL);
  gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dlg), TRUE);
  rc = gtk_check_button_new_with_mnemonic(_("_Change current directory"));
  gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg))), rc, FALSE, FALSE, 5);
  data->chdir_cb = rc;
  gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dlg), data->multi);
  data->widget = dlg;

  if (data->ext) {
    char *filter_str, *filter_name, *ext_name;

    filter_str = g_strdup_printf("*.%s", data->ext);
    ext_name = g_ascii_strup(data->ext, -1);
    filter_name = g_strdup_printf(_("%s file (*.%s)"), ext_name, data->ext);
    g_free(ext_name);

    filter = gtk_file_filter_new();
    gtk_file_filter_add_pattern(filter, filter_str);
    gtk_file_filter_set_name(filter, filter_name);
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dlg), filter);
    g_free(filter_str);
    g_free(filter_name);

    filter = gtk_file_filter_new();
    gtk_file_filter_add_pattern(filter, "*");
    gtk_file_filter_set_name(filter, _("All"));
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dlg), filter);
  }

  if (data->init_dir && *(data->init_dir)) {
    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dlg), *(data->init_dir));
  }
  gtk_widget_show_all(dlg);

  if (data->changedir && data->init_dir) {
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data->chdir_cb), data->chdir);
  } else {
    gtk_widget_hide(data->chdir_cb);
  }

  fname = get_filename_with_ext(data->init_file, data->ext);
  if (fname) {
    if (type == GTK_FILE_CHOOSER_ACTION_SAVE) {
#ifdef WINDOWS
      char *tmp;
      tmp = g_locale_from_utf8(fname, -1, NULL, NULL, NULL);
      if (tmp) {
	file_dialog_set_current_neme(dlg, tmp);
	g_free(tmp);
      }
#else  /* WINDOWS */
      file_dialog_set_current_neme(dlg, fname);
#endif	/* WINDOWS */
    } else {
      gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dlg), fname);
    }
    g_free(fname);
  }

  data->ret = IDCANCEL;

  while (1) {
    if (ndialog_run(dlg) != GTK_RESPONSE_ACCEPT)
      break;

    fsok(dlg);
    if (data->ret == IDOK && type == GTK_FILE_CHOOSER_ACTION_SAVE) {
      file_dialog_set_current_neme(dlg, FileSelection.file[0]);
      if (! data->overwrite && check_overwrite(dlg, FileSelection.file[0])) {
	data->ret = IDCANCEL;
	continue;
      }
    }
    break;
  }

  gtk_widget_destroy(dlg);
  reset_event();
  data->widget = NULL;

  return data->ret;
}