bool open_output_for_unit(compilation_env_t *env, compilation_unit_t *unit, const char *default_extension) { if (env->outname == NULL) { env->outname = get_output_name(unit->original_name, default_extension); } return open_output(env); }
FILE *open_temp_file(const char *basename, const char *extension, const char **final_name) { char uniquenum_extension[32]; static unsigned nextnum = 0; snprintf(uniquenum_extension, sizeof(uniquenum_extension), "-%u%s", nextnum++, extension); const char *tmpname = get_output_name(basename, uniquenum_extension); return make_temp_file(tmpname, final_name); }
/************************************************************************** Show the Citizen Management Agent Dialog for the current city. **************************************************************************/ void show_cma_dialog(struct city *pcity, Widget citydlg) { Widget add_button, remove_button, help_button; Widget form, preset_viewport, close_button; Widget surplus_header, factor_header, prev; struct cm_parameter parameter; struct cm_result *result = cm_result_new(pcity); int i; current_city = pcity; stat_surplus_label = fc_malloc((O_LAST + 1) * sizeof(Widget)); stat_factor_label = fc_malloc((O_LAST + 1) * sizeof(Widget)); cma_dialog = I_T(XtCreatePopupShell("cmapopup", topLevelShellWidgetClass, citydlg, NULL, 0)); form = XtVaCreateManagedWidget("cmaform", formWidgetClass, cma_dialog, NULL); preset_viewport = XtVaCreateManagedWidget("cmapresetviewport", viewportWidgetClass, form, NULL); preset_list = XtVaCreateManagedWidget("cmapresetlist", listWidgetClass, preset_viewport, XtNlist, (XtArgVal)initial_preset_list, NULL); result_label = I_L(XtVaCreateManagedWidget("cmaresultlabel", labelWidgetClass, form, XtNfromHoriz, preset_viewport, NULL)); surplus_header = I_L(XtVaCreateManagedWidget("cmasurpluslabel", labelWidgetClass, form, XtNfromHoriz, preset_viewport, XtNfromVert, result_label, NULL)); /* Create labels in the minimal surplus column. */ prev = surplus_header; for (i = 0; i < (O_LAST + 1); i++) { I_L(stat_surplus_label[i] = XtVaCreateManagedWidget("cmastatlabel", labelWidgetClass, form, XtNfromHoriz, preset_viewport, XtNfromVert, prev, XtNvertDistance, 1, XtNlabel, (i == O_LAST) ? _("Celebrate") : get_output_name(i), NULL)); prev = stat_surplus_label[i]; } /* Create scrollbars in the minimal surplus column. */ prev = surplus_header; output_type_iterate(i) { surplus_slider[i] = XtVaCreateManagedWidget("cmapresetscroll", scrollbarWidgetClass, form, XtNfromHoriz, stat_surplus_label[i], XtNfromVert, prev, NULL); prev = stat_surplus_label[i]; } output_type_iterate_end; celebrate_toggle = I_L(XtVaCreateManagedWidget("cmapresettoggle", toggleWidgetClass,form, XtNfromHoriz, stat_surplus_label[0], XtNfromVert, stat_surplus_label[O_LAST - 1], NULL)); /* Create header label in the factor column. */ factor_header = I_L(XtVaCreateManagedWidget("cmafactorlabel", labelWidgetClass, form, XtNfromHoriz, surplus_slider[0], XtNfromVert, result_label, NULL)); /* Create stat labels in the factor column. */ prev = factor_header; for (i = 0; i < (O_LAST + 1); i++) { I_L(stat_factor_label[i] = XtVaCreateManagedWidget("cmastatlabel", labelWidgetClass, form, XtNfromHoriz, surplus_slider[0], XtNfromVert, prev, XtNvertDistance, 1, XtNlabel, (i == O_LAST) ? _("Celebrate") : get_output_name(i), NULL)); prev = stat_factor_label[i]; } /* Create scrollbars in the factor column. */ prev = factor_header; for (i = 0; i < (O_LAST + 1); i++) { factor_slider[i] = XtVaCreateManagedWidget("cmapresetscroll", scrollbarWidgetClass, form, XtNfromHoriz, stat_factor_label[i], XtNfromVert, prev, NULL); prev = stat_factor_label[i]; } close_button = I_L(XtVaCreateManagedWidget("cmaclosebutton", commandWidgetClass, form, XtNfromVert, preset_viewport, NULL)); control_button = I_L(XtVaCreateManagedWidget("cmacontrolbutton", commandWidgetClass, form, XtNfromVert, preset_viewport, XtNfromHoriz, close_button, NULL)); release_button = I_L(XtVaCreateManagedWidget("cmareleasebutton", commandWidgetClass, form, XtNfromVert, preset_viewport, XtNfromHoriz, control_button, NULL)); change_button = I_L(XtVaCreateManagedWidget("cmachangebutton", commandWidgetClass, form, XtNfromVert, preset_viewport, XtNfromHoriz, release_button, NULL)); add_button = I_L(XtVaCreateManagedWidget("cmaaddbutton", commandWidgetClass, form, XtNfromVert, preset_viewport, XtNfromHoriz, change_button, NULL)); remove_button = I_L(XtVaCreateManagedWidget("cmaremovebutton", commandWidgetClass, form, XtNfromVert, preset_viewport, XtNfromHoriz, add_button, NULL)); help_button = I_L(XtVaCreateManagedWidget("cmahelpbutton", commandWidgetClass, form, XtNfromVert, preset_viewport, XtNfromHoriz, remove_button, NULL)); XtAddCallback(control_button, XtNcallback, control_city, (XtPointer)preset_list); XtAddCallback(release_button, XtNcallback, release_city, (XtPointer)preset_list); XtAddCallback(change_button, XtNcallback, change_callback, (XtPointer)preset_list); XtAddCallback(add_button, XtNcallback, add_preset, (XtPointer)preset_list); XtAddCallback(remove_button, XtNcallback, remove_preset, (XtPointer)preset_list); XtAddCallback(preset_list, XtNcallback, select_preset, (XtPointer)preset_list); XtAddCallback(help_button, XtNcallback, cma_help, (XtPointer)preset_list); XtAddCallback(celebrate_toggle, XtNcallback, celebrate_callback, NULL); XtAddCallback(close_button, XtNcallback, close_callback, NULL); output_type_iterate(i) { XtAddCallback(surplus_slider[i], XtNscrollProc, sliders_scroll_callback, NULL); XtAddCallback(surplus_slider[i], XtNjumpProc, sliders_jump_callback, NULL); } output_type_iterate_end; for (i = 0; i < (O_LAST + 1); i++) { XtAddCallback(factor_slider[i], XtNscrollProc, sliders_scroll_callback, NULL); XtAddCallback(factor_slider[i], XtNjumpProc, sliders_jump_callback, NULL); } /* Update dialog with CMA parameters from city. */ cmafec_get_fe_parameter(current_city, ¶meter); output_type_iterate(i) { minimal_surplus[i] = parameter.minimal_surplus[i]; } output_type_iterate_end; XtVaSetValues(celebrate_toggle, XtNlabel, parameter.require_happy ? _("Yes") : _("No"), XtNstate, parameter.require_happy, NULL); if (parameter.happy_factor > 0) { factors[O_LAST] = parameter.happy_factor; } else { factors[O_LAST] = 1; } output_type_iterate(i) { factors[i] = parameter.factor[i]; } output_type_iterate_end; set_slider_values(); update_cma_preset_list(); XtVaSetValues(preset_list, XtNwidth, 200, NULL); XtVaSetValues(preset_list, XtNheight, 300, NULL); XtVaSetValues(celebrate_toggle, XtNwidth, 30, NULL); XtVaSetValues(result_label, XtNwidth, 360, NULL); XtVaSetValues(result_label, XtNheight, 110, NULL); output_type_iterate(i) { XtVaSetValues(stat_surplus_label[i], XtNwidth, 90, NULL); XtVaSetValues(stat_factor_label[i], XtNwidth, 90, NULL); } output_type_iterate_end; /* FIXME! Now we assume that output_type_iterate ends with O_LAST. */ XtVaSetValues(stat_factor_label[O_LAST], XtNwidth, 90, NULL); XtRealizeWidget(cma_dialog); update_stat_labels(True); cm_result_from_main_map(result, pcity); xaw_set_label(result_label, (char *) cmafec_get_result_descr(current_city, result, ¶meter)); XSetWMProtocols(display, XtWindow(cma_dialog), &wm_delete_window, 1); XtVaSetValues(preset_viewport, XtNforceBars, True, NULL); xaw_set_relative_position(citydlg, cma_dialog, 5, 5); XtPopup(cma_dialog, XtGrabNone); cm_result_destroy(result); }
int main (int argc, char *argv[]) { unsigned char key[65]; FILE *kf; char *config_file=NULL, *key_file=NULL, *output_file=NULL, *extension_string=NULL, *custom_mibs=NULL; unsigned int keylen = 0; unsigned int encode_docsis = FALSE, decode_bin = FALSE, hash = 0; int i; int resolve_oids = 1; while (argc > 0) { argc--; argv++; if (!argc) { usage(); } /* the initial command-line parameters are flags / modifiers */ if (!strcmp (argv[0], "-nohash")) { nohash = 1; continue; } if (!strcmp (argv[0], "-o")) { resolve_oids = 0; continue; } if (!strcmp (argv[0], "-M")) { if (argc < 2 ) { usage(); } custom_mibs=argv[1]; argc--; argv++; continue; } if (!strcmp (argv[0], "-na")) { if (hash) { usage(); } hash = 1; continue; } if (!strcmp (argv[0], "-eu")) { if (hash) { usage(); } hash = 2; continue; } if (!strcmp (argv[0], "-dialplan")) { dialplan = 1; continue; } /* the following command-line parameters are actions */ if (!strcmp (argv[0], "-d")) { if (argc < 2 ) { usage(); } decode_bin = TRUE; config_file = argv[1]; break; } if (!strcmp (argv[0], "-e")) { if (argc < 4 ) { usage(); } encode_docsis = TRUE; config_file = argv[1]; key_file = argv[2]; output_file = argv[3]; break; } if (!strcmp (argv[0], "-m")) { extension_string = argv[argc-1]; key_file = argv[argc-2]; encode_docsis = TRUE; continue; } if (!strcmp (argv[0], "-p")) { /* encode_docsis may already have been set via the "-m" option */ encode_docsis = 0; argc--; argv++; if (argc < 2 ) { usage(); } /* -p might be followed by -dialplan. This is allowed for backwards * compatibility */ if (!strcmp (argv[0], "-dialplan")) { dialplan = 1; argc--; argv++; } if (argc < 2 ) { usage(); } /* if -m has not already been specified, then we expect "<mta_cfg_file> <output_file>" */ if (extension_string == NULL) { config_file = argv[0]; output_file = argv[1]; } break; } /* no more recognisable options means that we've either finished parsing * all arguments or else that the remaining arguments refer to a list of * config files */ if (argc) { break; } } if (encode_docsis) { if ((kf = fopen (key_file, "r")) == NULL) { fprintf (stderr, "docsis: error: can't open keyfile %s\n", key_file); exit (-5); } keylen = fread (key, sizeof (unsigned char), 64, kf); while (keylen > 0 && (key[keylen - 1] == 10 || key[keylen - 1] == 13)) { keylen--; /* eliminate trailing \n or \r */ } fclose(kf); } init_global_symtable (); setup_mib_flags(resolve_oids,custom_mibs); if (decode_bin) { decode_file (config_file); exit(0); // TODO: clean shutdown } if (extension_string) { /* encoding multiple files */ if (encode_docsis) { /* encode argv[argc-3] to argv[0] */ for (i=0; i<argc-2; i++) { if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) { fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] ); continue; } fprintf(stderr, "Processing input file %s: output to %s\n",argv[i], output_file); if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis, hash)) { exit(2); } free (output_file); output_file = NULL; } } else { /* encode argv[argc-2] to argv[0] */ for (i=0; i<argc-1; i++) { if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) { fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] ); continue; } fprintf (stderr, "Processing input file %s: output to %s\n",argv[i], output_file); if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis, hash)) { exit(2); } free (output_file); output_file = NULL; } } } else { if (encode_one_file (config_file, output_file, key, keylen, encode_docsis, hash)) { exit(2); } /* encode argv[1] */ } free(global_symtable); shutdown_mib(); return 0; }
int main(void) { char argstr[MAX_argv][STR_LEN]; // storage for command line arguments char *argv[MAX_argv]; // ptr array to command line arguments char *token; // ptr to command line token char buffer[STR_LEN]; // character buffer for command line char delimiter[] = " |\n\t"; // delimiter for the commands int idx; // index variable pid_t PID; // process identifier int fdw; // write file descriptor int fdr; // read file descriptor int ret; // ret value char pwd_buffer[PWD_LEN]; // buffer for the current directory char *fname_in_ptr = NULL; // ptr to the input file name char *fname_out_ptr = NULL; // ptr to the output file name /* reset screen */ fprintf(stdout, "\E[0;0H"); // set cursor to 0;0 fprintf(stdout, "\E[2J"); // clear screen /* get current directory as default */ getcwd(pwd_buffer, PWD_LEN); while (1) { printf("# "); // set prompt readline(buffer, STR_LEN); // read one line from stdin idx = 0; // set index back /* get token */ token = strtok(buffer, delimiter); while (token != NULL) { strcpy(&(argstr[idx][0]), token); argv[idx] = argstr[idx]; idx++; token = strtok(NULL, delimiter); } argv[idx] = NULL; // terminate arguments with NULL pointer /* get input name if set */ fname_in_ptr = get_input_name(argv, idx); /* get output name if is set */ fname_out_ptr = get_output_name(argv, idx); /* check if logout or exit */ exit_logout(argv); /* check if cd */ ret = cd_cmd(argv, pwd_buffer); /* execute command */ if (ret) { if ((PID = fork()) == 0) { // fork child process /* check if write ptr is set */ if (fname_out_ptr != NULL) { close(1); // close stdout, redirect to file fdw = open(fname_out_ptr, O_CREAT | O_TRUNC | O_WRONLY, 0644); if (fdw < 0 ){ fprintf(stderr, "Could not write to file %s\n", fname_out_ptr); exit(-1); } } if (fname_in_ptr != NULL) { close(0); fdr = open(fname_in_ptr, O_RDONLY); if (fdr < 0 ){ fprintf(stderr, "Could not read file %s\n", fname_in_ptr); exit(-1); } } strcpy(buffer, "/bin/"); // "compose" path of command (1st arg) strcat(buffer, argv[0]); execv(buffer, &argv[0]); strcpy(buffer, "/usr/bin/"); strcat(buffer, argv[0]); execv(buffer, &argv[0]); printf("%s: command not found\n", argv[0]); exit(0); // kill process } else if (PID < 0) { printf("fork failed\n"); // fork didn't succeed exit(-1); // terminate sishell } else // here we are parents wait(0); // wait for child process to terminate } } }
int main (int argc, char *argv[]) { unsigned char key[65]; FILE *kf; char *config_file=NULL, *key_file=NULL, *output_file=NULL, *extension_string=NULL; unsigned int keylen = 0; unsigned int encode_docsis = FALSE, decode_bin = FALSE; int i; int resolve_oids = 1; if (argc < 2 ) { usage(); } if (!strcmp (argv[1], "-o") ){ resolve_oids = 0; if (!strcmp (argv[2], "-d")) { decode_bin = TRUE; config_file = argv[3]; } else { usage(); } }else if (!strcmp (argv[1], "-m") ){ /* variable number of args, encoding multiple files */ if (argc < 5 ) { usage(); } extension_string = argv[argc-1]; if (!strcmp ( argv[2], "-p")) { key_file = NULL; } else { key_file = argv[argc-2]; encode_docsis = TRUE; } } else { switch (argc) { case 3: if (strcmp (argv[1], "-d")) usage (); decode_bin = TRUE; config_file = argv[2]; break; ;; case 4: if (strcmp (argv[1], "-p")) usage (); config_file = argv[2]; output_file = argv[3]; break; ;; case 5: if (strcmp (argv[1], "-e")) usage (); encode_docsis = TRUE; config_file = argv[2]; key_file = argv[3]; output_file = argv[4]; break; ;; default: usage (); } } if (encode_docsis) { if ((kf = fopen (key_file, "r")) == NULL) { fprintf (stderr, "docsis: error: can't open keyfile %s\n", key_file); exit (-5); } keylen = fread (key, sizeof (unsigned char), 64, kf); while (keylen > 0 && (key[keylen - 1] == 10 || key[keylen - 1] == 13)) { keylen--; /* eliminate trailing \n or \r */ } } init_global_symtable (); setup_mib_flags(resolve_oids); if (decode_bin) { decode_file (config_file); exit(0); // TODO: clean shutdown } if (extension_string) { /* encoding multiple files */ if (encode_docsis) { /* encode argv[argc-3] to argv[2] */ for (i=2; i<argc-2; i++) { if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) { fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] ); continue; } fprintf(stderr, "Processing input file %s: output to %s\n",argv[i], output_file); if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis)) { exit(2); } free (output_file); output_file = NULL; } } else { /* encode argv[argc-2] to argv[3] */ for (i=3; i<argc-1; i++) { if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) { fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] ); continue; } fprintf (stderr, "Processing input file %s: output to %s\n",argv[i], output_file); if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis)) { exit(2); } free (output_file); output_file = NULL; } } } else { if (encode_one_file (config_file, output_file, key, keylen, encode_docsis)) { exit(2); } /* encode argv[1] */ } free(global_symtable); shutdown_mib(); return 0; }
static void add_screen(xcb_randr_get_screen_info_reply_t *reply) { const gchar *title = "Display"; GtkWidget *item = gtk_menu_item_new_with_label(title); GtkMenuShell *menu = GTK_MENU_SHELL(app_menu); struct screen_info *info = g_malloc(sizeof *info); xcb_randr_rotation_t rotation = normalize_rotation(reply->rotation); xcb_randr_rotation_t rotations = reply->rotations; info->label_menu_item = item; info->rotation_menu_group = NULL; info->rotation = rotation; info->config_timestamp = reply->config_timestamp; info->root = reply->root; info->sizeID = reply->sizeID; info->rate = reply->rate; screens_info = g_slist_append(screens_info, info); gtk_widget_set_sensitive(item, FALSE); gtk_menu_shell_append(menu, item); #define R(rot, title) \ if (rotations & XCB_RANDR_ROTATION_##rot) \ add_screen_rotation(info, XCB_RANDR_ROTATION_##rot, title, \ XCB_RANDR_ROTATION_##rot & rotation) R(ROTATE_0, "Landscape"); R(ROTATE_90, "Portrait"); R(ROTATE_180, "Landscape Flipped"); R(ROTATE_270, "Portrait Flipped"); if (rotations & xcb_rotations_mask && rotations & xcb_reflections_mask) gtk_menu_shell_append(menu, gtk_separator_menu_item_new()); R(REFLECT_X, "Reflected X"); R(REFLECT_Y, "Reflected Y"); #undef R gtk_menu_shell_append(menu, gtk_separator_menu_item_new()); gtk_widget_show_all(app_menu); /* Get screen resources */ xcb_randr_get_screen_resources_current_cookie_t resources_cookie; xcb_randr_get_screen_resources_current_reply_t *resources_reply; xcb_generic_error_t *err = NULL; resources_cookie = xcb_randr_get_screen_resources_current(conn, info->root); resources_reply = xcb_randr_get_screen_resources_current_reply(conn, resources_cookie, &err); if (err) { g_warning("Get Screen Resources returned error %u\n", err->error_code); return; } /* Get screen outputs */ xcb_randr_output_t *outputs; guint i; gchar *output_name; outputs = xcb_randr_get_screen_resources_current_outputs(resources_reply); for (i = 0; i < resources_reply->num_outputs; i++) { xcb_randr_get_output_info_reply_t *output_info_reply; xcb_randr_get_output_info_cookie_t output_info_cookie = xcb_randr_get_output_info_unchecked(conn, outputs[i], resources_reply->config_timestamp); output_info_reply = xcb_randr_get_output_info_reply(conn, output_info_cookie, NULL); /* Show only if connected */ switch (output_info_reply->connection) { case XCB_RANDR_CONNECTION_DISCONNECTED: case XCB_RANDR_CONNECTION_UNKNOWN: continue; case XCB_RANDR_CONNECTION_CONNECTED: break; } output_name = get_output_name(output_info_reply); /* Put output names on the menu */ gtk_menu_item_set_label(GTK_MENU_ITEM(item), output_name); g_free(output_name); /* TODO: concatenate multiple names or pick them intelligently */ } }