/* * read fro mconfig file and set it up */ void config_file_actions(const char* home_dir) { /* * path of config file, default: /home/$USER/.threader/config */ char* config_location; if (use_custom_config_file) { config_location = print_to_string("%s", custom_config_file_location); } else { config_location = print_to_string("%s%s%s%s", home_dir, "/.", APP_NAME, "/config"); } LOG(INFO, "config_location=%s", config_location); /* * read config file */ config = read_conf_file(config_location); free(config_location); LOG(INFO, "command='%s', threads=%d, old_ext='%s', new_ext='%s', run_script_on_finish=%d, " "script_path=%s", config.command, config.threads, config.old_ext, config.new_ext, config.run_script_on_finish, config.script_path); /* CONFIG FILE OVERRIDES */ /* * override thread number set in config file if -n option was passed */ if (override_config_threads_num != 0) { LOG(INFO, "Overriding thread number from config file from %d to %d (-n option was passed)", config.threads, override_config_threads_num); set_config_threads(&config, override_config_threads_num); } }
static HyperDocPage * find_page(TextNode * node) { char *page_name; HyperDocPage *page; /* try and find the page name */ page_name = print_to_string(node); page = (HyperDocPage *) hash_find(gWindow->fPageHashTable, page_name); if (page == NULL) { /* try to find the unknown page */ page = (HyperDocPage *) hash_find(gWindow->fPageHashTable, "UnknownPage"); if (page == NULL) { /* Yikes, Even that could not be found */ fprintf(stderr, "Unknown page name %s\n", page_name); } else { if (page->type == UnloadedPageType) page->type = UlUnknownPage; else page->type = UnknownPage; } } return page; }
void filenames_input(int argc, char* argv[], int optind) { /* * must have two or more lone arguments. First n-1 is input filename, the last is the output folder */ if(optind > argc - 2) { die("No input/output folders specified"); } files_count = 0; /* * number of lone arguments */ char* output_dir = argv[argc - 1]; if (output_dir[strlen(output_dir) - 1] != PATH_SEPARATOR) { die("Output dir should end with path separator!"); } files_count = 0; int i; for(i = optind; i < argc - 1; i++) { /* * reallocate memory for next filename if neccessary */ reallocate_memory_for_next_filename(); char* input_file = strdup(argv[i]); input_files[files_count] = input_file; char* output_filename = extract_filename_from_path_no_ext(input_file, config.old_ext); output_filenames[files_count] = print_to_string("%s%s%s", output_dir, output_filename, config.new_ext); free(output_filename); files_count++; } }
/* issue a AXIOM command to the buffer associated with a page */ void issue_spadcommand(HyperDocPage *page, TextNode *command, int immediate, int type) { char *buf; int ret_val; ret_val = connect_spad(); if (ret_val == NotConnected || ret_val == SpadBusy) return; if (page->sock == NULL) start_user_buffer(page); ret_val = send_int(page->sock, TestLine); if (ret_val == -1) { page->sock = NULL; clear_execution_marks(page->depend_hash); issue_spadcommand(page, command, immediate, type); return; } issue_dependent_commands(page, command, type); ret_val = send_int(page->sock, ReceiveInputLine); buf = print_to_string(command); if (immediate) { buf[strlen(buf) + 1] = '\0'; buf[strlen(buf)] = '\n'; } if (type == Spadsrc) send_pile(page->sock, buf); else send_string(page->sock, buf); mark_as_executed(page, command, type); gIsEndOfOutput = 0; }
/* * call this only after you've checked if another instance is running * with check_if_another_instance_running() because the pid file can * contain old pid (if the process has been killed from task manager) * and we don't want to kill another potential process with the same pid. */ void read_pid_and_terminate() { int pid = 0; FILE* get_pid = fopen(PID, "r"); if (get_pid == NULL) die("failed to open the pid file for reading!"); fscanf(get_pid, "%d", &pid); fclose(get_pid); LOG(INFO, "read pid=%d from %s", pid, PID); if (pid == 0) { LOG(INFO, "Process not running, exiting"); return; } char* kill_command = print_to_string("kill %d", pid); FILE* kill = popen(kill_command, "r"); free(kill_command); if (kill == NULL) die("failed to call 'kill PID'"); LOG(INFO, "instance with pid=%d terminated", pid); pclose(kill); turn_light_off(); write_pid(0); }
static void windowlink_handler(TextNode * node) { char *page_name; /* first try and find the page */ page_name = print_to_string(node); if (init_top_window(page_name) == -1) { return; } /* gWindow->fWindowHashTable = gWindow->page->fLinkHashTable;*/ }
static int check_memostack(TextNode *node) { char *buffer; int stackp = gWindow->fMemoStackIndex; int found = 0; HyperDocPage *page; buffer = print_to_string(node->data.node); /* * Once we have done that much, search down the stack for the * proper page */ while (!found && stackp > 0) { page = gWindow->fMemoStack[--stackp]; if (!strcmp(page->name, buffer)) found = 1; } return found; }
/* start a spad buffer for the page associated with the give */ static void start_user_buffer(HyperDocPage *page) { char buf[1024], *title; char *SPAD; char spadbuf[250]; char complfile[250]; int ret_val; SPAD = (char *) getenv("AXIOM"); if (SPAD == NULL) { sprintf(SPAD, "/spad/mnt/rios"); } sprintf(spadbuf, "%s/lib/spadbuf", SPAD); sprintf(complfile, "%s/lib/command.list", SPAD); title = print_to_string(page->title); if (access(complfile, R_OK) == 0) /* * TTT says : why not invoke with "-name axiomclient" and set any * defaults in the usual way */ #ifdef RIOSplatform sprintf(buf, "aixterm -sb -sl 500 -name axiomclient -n '%s' -T '%s' -e %s %s %s&", title, title, spadbuf, page->name, complfile); else sprintf(buf, "aixterm -sb -sl 500 -name axiomclient -n '%s' -T '%s' -e %s %s&", title, title, spadbuf, page->name); #else #ifdef SUNplatform sprintf(buf, "xterm -sb -sl 500 -name axiomclient -n '%s' -T '%s' -e %s %s %s&", title, title, spadbuf, page->name, complfile); else
static void handle_button(int button, XButtonEvent * event) { HyperLink *link; HyperDocPage *page = NULL; char *page_name; /* find page name from sub-window handle */ link = get_hyper_link(event); if (link == NULL) { /* user clicked on an inactive area */ /* BeepAtTheUser(); */ /* I always thought this was annoying. RSS */ return; } switch (link->type) { case openaxiom_Pastebutton_token: page = paste_button(link->reference.paste); break; case openaxiom_Link_token: page_name = print_to_string(link->reference.node); page = (HyperDocPage *) hash_find(gWindow->fPageHashTable, page_name); break; case openaxiom_Helpbutton_token: helpForHyperDoc(); page = NULL; break; case openaxiom_Scrollbar_token: scrollScroller(event); break; case Scrollupbutton: scrollUp(); break; case Scrolldownbutton: scrollDown(); break; case openaxiom_Inputstring_token: /* We must be changing input focus or getting a selection */ change_input_focus(link); if ( button == Button2 ) { XConvertSelection(gXDisplay, XA_PRIMARY, XA_STRING, XInternAtom(gXDisplay, "PASTE_SELECTION", False), gWindow->fMainWindow, CurrentTime); gSavedInputAreaLink = link; } break; case openaxiom_SimpleBox_token: page = NULL; toggle_input_box(link); break; case openaxiom_Radiobox_token: page = NULL; toggle_radio_box(link); break; case openaxiom_Quitbutton_token: quitHyperDoc(); break; case openaxiom_Returnbutton_token: /* pop memo information */ page = returnlink(); break; case openaxiom_Upbutton_token: /* pop downlink information */ page = uplink(); break; case openaxiom_Downlink_token: page = find_page(link->reference.node); if (page && NotSpecial(page->type)) downlink(); break; case openaxiom_Memolink_token: page = find_page(link->reference.node); if (page && NotSpecial(page->type)) memolink(); break; case openaxiom_Windowlink_token: page = find_page(link->reference.node); if (page && NotSpecial(page->type)) { windowlink_handler(link->reference.node); gNeedIconName = 1; page = NULL; } break; case openaxiom_Lispwindowlink_token: lispwindowlink_handler(link); gNeedIconName = 1; page = NULL; break; case openaxiom_LispMemoLink_token: case openaxiom_Spadmemolink_token: page = issue_server_command(link); if (page && NotSpecial(page->type)) memolink(); break; case openaxiom_LispDownLink_token: case openaxiom_Spaddownlink_token: page = issue_server_command(link); if (page && NotSpecial(page->type)) downlink(); break; case openaxiom_Spadlink_token: case openaxiom_Lisplink_token: page = issue_server_command(link); break; case openaxiom_Lispcommand_token: case openaxiom_Qspadcall_token: case openaxiom_Spadcall_token: page = issue_server_command(link); break; case openaxiom_Lispcommandquit_token: case openaxiom_Spadcallquit_token: case openaxiom_Qspadcallquit_token: page = issue_server_command(link); exitHyperDoc(); break; case openaxiom_Spadcommand_token: case openaxiom_Spadgraph_token: case openaxiom_Spadsrc_token: issue_spadcommand(gWindow->page, link->reference.node, button == Button1, link->type); break; case openaxiom_Unixlink_token: page = issue_unixlink(link->reference.node); if (page && NotSpecial(page->type)) { downlink(); } break; case openaxiom_Unixcommand_token: issue_unixcommand(link->reference.node); break; default: break; } if (page) { switch (page->type) { /* check for special button types */ case openaxiom_Quitbutton_token: exitHyperDoc(); return; case openaxiom_Returnbutton_token: gWindow->page = returnlink(); break; case openaxiom_Upbutton_token: gWindow->page = uplink(); break; case ErrorPage: case UnknownPage: case UlUnknownPage: if (page->type == UlUnknownPage) page->type = UnloadedPageType; downlink(); gWindow->page = page; break; default: /* a normal link */ gWindow->page = page; break; } if (link->type != openaxiom_Pastebutton_token) display_page(gWindow->page); gWindow->fWindowHashTable = gWindow->page->fLinkHashTable; /* reset the window hash */ } }
/* * The program begins here. * * Arguments: * argc - count of arguments * argv - array of arguments * envp - array of enviroment variables */ int main(int argc, char* argv[], char* envp[]) { /* * initialize configuration (if config file isn't set) */ config.run_script_on_finish = 0; /* * path to home dir "/home/$USER", read from $HOME enviroment variable */ const char* home_dir = getenv("HOME"); /* variables used with getopt() */ extern char *optarg; extern int optind, opterr; int argument; int i_option_enabled = 0; /* * read arguments in while loop and set the neccessary variables */ while( (argument = getopt(argc, argv, "c:dhin:uV:v")) != -1 ) { LOG(DEBUG, "argument='%c', optarg='%s'", argument, optarg); switch(argument) { case 'c': LOG(INFO, "Will use custom config file"); use_custom_config_file = 1; custom_config_file_location = optarg; break; case 'd': LOG(WARN, "DRY RUN ENABLED!!!"); dry_run = 1; break; case 'h': help(); break; case 'i': LOG(INFO, "-i option enabled"); i_option_enabled = 1; break; case 'n': override_config_threads_num = atoi(optarg); if (override_config_threads_num <= 0) { die("Invalid number of threads (-n parameter)!"); } break; case 'u': disable_color_output(); LOG(INFO, "Disabling colored output"); break; case 'V': quality = optarg; break; case 'v': enable_verbose(); break; default: /* '?' */ die("Wrong arguments or argument requires an option which was not supplied"); } } config_file_actions(home_dir); size = INITIAL_SIZE; /* * allocate initial memory for array of input and output filenames (paths) */ input_files = malloc(sizeof(char*) * size); output_filenames = malloc(sizeof(char*) * size); if ( input_files == NULL || output_filenames == NULL) { die("Memory error!"); } /* * initialize input and output filenames to NULL. not sure if this is really neccessary */ int i; for (i = 0; i < size; i++) { input_files[i] = NULL; output_filenames[i] = NULL; } if (i_option_enabled) { filenames_input(argc, argv, optind); } else { folder_input(argc, argv, optind); } /* * array which stores the threads which will run. */ pthread_t threads[config.threads]; LOG(INFO, "Starting %d threads", config.threads); /* * this is only used for logging, so that each call to decode() knows which number it is. * a single variable for this cannot be used because it is passed by reference (pointer), * that's how the pthread_create works. */ int* thread_indexes[config.threads]; for (i = 0; i < config.threads; i++) { thread_indexes[i] = malloc(sizeof(int)); if (thread_indexes[i] == NULL) die("Memory error!"); *thread_indexes[i] = i; /* * create a new thread (call do_work() in a new thread) */ pthread_create(&threads[i], NULL, &do_work, (void *) thread_indexes[i]); } /* * join the just created threads (wait for them to finish! */ for (i = 0; i < config.threads; i++) { pthread_join(threads[i], NULL); free(thread_indexes[i]); } /* * if the config file has the run_script_on_finish set to a path to script, execute it. * TODO explain how this script works */ if (config.run_script_on_finish) { LOG(INFO, "Starting script: %s", config.script_path); for (i = 0; i < files_count; i++) { char* script_command = print_to_string("%s \"%s\" \"%s\"", config.script_path, input_files[i], output_filenames[i]); LOG(INFO, "Running post script on file: %s", output_filenames[i]); LOG(DEBUG, "Running post script: %s", script_command); FILE* fp = popen(script_command, "r"); if (fp == NULL) die("Unable to open script"); free(script_command); pclose(fp); } } /** FREE MEMORY **/ for (i = 0; i < size; i++) { if (input_files[i] != NULL) { free(input_files[i]); } if (output_filenames[i] != NULL) { free(output_filenames[i]); } } free(input_files); free(output_filenames); free_config(config); LOG(INFO, "Program finished successfully!"); return 0; }
void folder_input(int argc, char* argv[], int optind) { /* * must have two lone arguments (input and output folder directory) */ if(optind > argc - 2) { die("No input/output folders specified"); } else if(optind < argc - 2) { die("Too many arguments specified. Maybe you wanted to use the -i option? "); } /* first argument */ char *source_dir = argv[optind++]; /* second argument */ output_dir = argv[optind]; /* current working directory */ //getcwd(source_dir, PATH_MAX); LOG(DEBUG, "current dir: %s", source_dir); LOG(DEBUG, "output dir: %s", output_dir); if (source_dir[strlen(source_dir) - 1] != PATH_SEPARATOR) { die("Input dir should end with path separator!"); } if (output_dir[strlen(output_dir) - 1] != PATH_SEPARATOR) { die("Output dir should end with path separator!"); } char* list_files_command; /* * command to list files in source_dir */ list_files_command = print_to_string("ls \"%s\" | grep -E \"(%s$)\"", source_dir, config.old_ext); FILE *fp = popen (list_files_command, "r"); free(list_files_command); if (fp == NULL) { die("Failed to run search command"); } /* Read the output a line at a time */ char line[PATH_MAX]; files_count = 0; /* * read the output of list_files_command, one line at a time */ while( fgets(line, sizeof(line) - 1, fp) != NULL ) { /* * reallocate memory for next filename if neccessary */ reallocate_memory_for_next_filename(); char* path_to_file; char* line_without_newline = strndup(line, strlen(line) - 1); /* * concatenate directory and input filename */ path_to_file = print_to_string("%s%s", source_dir, line_without_newline); input_files[files_count] = path_to_file; /* * original filename without extension */ char* itemName = strndup(line_without_newline, strlen(line_without_newline) - strlen(config.old_ext)); /* * concatenate output filename, original filename without extension and append the new extension */ output_filenames[files_count] = print_to_string("%s%s%s", output_dir, itemName, config.new_ext); /* free unneccessary pointers */ free(itemName); free(line_without_newline); files_count++; } pclose(fp); if (files_count <= 0) { die("No files found"); } }
void Conjunct::print(FILE *output_file) { String s = print_to_string(true); fprintf(output_file, (const char *) s); }
HyperDocPage * parse_patch(PasteNode *paste) { TextNode *new_paste; TextNode *end_node; TextNode *begin_node; TextNode *arg_node; TextNode *old; TextNode *next_node; InputItem *paste_item = paste->paste_item; int where = paste->where; GroupItem *g = paste->group; ItemStack *is = paste->item_stack; PatchStore *patch; char *patch_name; int ret_value = 1; /* prepare to throw away the current paste node */ end_node = paste->end_node; next_node = end_node->next; begin_node = paste->begin_node; arg_node = paste->arg_node; old = begin_node->next; /* now read the new stuff and add it in between all this stuff */ switch (where) { case openaxiom_FromFile_input: patch_name = print_to_string(arg_node); patch = (PatchStore *) hash_find(gWindow->fPatchHashTable, patch_name); if (!patch) { fprintf(stderr, "(HyperDoc) Unknown patch name %s\n", patch_name); BeepAtTheUser(); return 0; } if (!patch->loaded) load_patch(patch); input_type = openaxiom_FromString_input; input_string = patch->string; break; case openaxiom_FromSpadSocket_input: input_type = openaxiom_FromSpadSocket_input; ret_value = issue_serverpaste(arg_node); if (ret_value < 0) { paste->where = where; paste->end_node = end_node; paste->arg_node = arg_node; paste->group = g; paste->item_stack = is; paste->haspaste = 1; return 0; } break; case openaxiom_FromUnixFD_input: input_type = openaxiom_FromUnixFD_input; issue_unixpaste(arg_node); break; default: fprintf(stderr, "(HyperDoc) \\parsebutton error: Unknown where\n"); exit(-1); break; } paste->where = 0; paste->end_node = paste->arg_node = paste->begin_node = 0; paste->group = 0; paste->item_stack = 0; paste->haspaste = 0; paste->paste_item = 0; /* set the jump buffer in case it is needed */ if (setjmp(jmpbuf)) { /*** OOOPS, an error occurred ****/ fprintf(stderr, "(HyperDoc) Had an error parsing a patch: Goodbye!\n"); exit(-1); } end_node->next = 0; free_node(old, 1); init_parse_patch(gWindow->page); init_paste_item(paste_item); get_token(); if (token.type != openaxiom_Patch_token) { fprintf(stderr, "(HyperDoc) Pastebutton %s was expecting a patch\n", paste->name); jump(); } if (input_type == openaxiom_FromString_input) { get_token(); if (token.type != openaxiom_Lbrace_token) { token_name(token.type); fprintf(stderr, "(HyperDoc) Unexpected %s \n", ebuffer); print_page_and_filename(); jump(); } get_token(); if (token.type != openaxiom_Word_token) { token_name(token.type); fprintf(stderr, "(HyperDoc) Unexpected %s \n", ebuffer); print_page_and_filename(); jump(); } get_token(); if (token.type != openaxiom_Rbrace_token) { token_name(token.type); fprintf(stderr, "(HyperDoc) Unexpected %s \n", ebuffer); print_page_and_filename(); jump(); } } new_paste = alloc_node(); curr_node = new_paste; parse_HyperDoc(); /* Once I am back, I need only reallign all the text structures */ curr_node->type = openaxiom_Noop_token; curr_node->next = next_node; begin_node->next = new_paste; begin_node->type = openaxiom_Noop_token; free(begin_node->data.text); begin_node->data.text = 0; gWindow->fDisplayedWindow = gWindow->fScrollWindow; repaste_item(); paste_page(begin_node); /* so now I should just be able to disappear */ return gWindow->page; }
static void make_input_file_from_page(HyperDocPage *page) { TextNode *node; int starting_file = 1,/* i,*/ /*len,*/ ret_val; char *buf, buf2[1024], buf3[1024]; char *b, *c, *com; FILE *file = NULL; FILE *pfile = NULL; static HyperDocPage *op = NULL; if (op == page) return; op = page; if (page == NULL) return; b = make_input_file_name(buf2, page->filename); c = make_paste_file_name(buf3, page->filename); if (inListAndNewer(b, page->filename)) { /* open and prepare the input file */ file = fopen(b, "a"); if (file == NULL) { fprintf(stderr, "couldn't open output file %s\n", b); exit(-1); } fprintf(file, "\n-- Input for page %s\n", page->name); fprintf(file, ")clear all\n\n"); for (node = page->scrolling; node != NULL; node = node->next) if (node->type == Spadcommand || node->type == Spadgraph || node->type == Spadsrc) { if (starting_file) { example_number = 1; if (make_patch_files) { send_lisp_command("(|clearCmdAll|)"); send_lisp_command("(|resetWorkspaceVariables|)"); send_lisp_command("(setq $linelength 55)"); send_lisp_command("(setq |$printLoadMsgs| NIL)"); send_lisp_command("(setq |$UserLevel| '|development|)"); pfile = fopen(c, "a"); if (pfile == NULL) { fprintf(stderr, "couldn't open output file %s\n", c); exit(-1); } } starting_file = 0; } else example_number++; buf = print_to_string(node->next); com = alloc_string(buf); fprintf(file, "%s\n", buf); fflush(file); fprintf(stderr, "writing:\t%s\n", buf); include_bf = 1; buf = print_to_string(node->next); include_bf = 0; if (make_patch_files) { if (node->type == Spadcommand || node->type == Spadsrc) print_paste(pfile, com, buf, page->name, node->type); else print_graph_paste(pfile, com, buf, page->name, node->type); } } if (!starting_file && make_patch_files) { ret_val = fclose(pfile); if (ret_val == -1) { fprintf(stderr, "couldn't close file %s\n", b); exit(-1); } } ret_val = fclose(file); if (ret_val == -1) { fprintf(stderr, "couldn't close file %s\n", b); exit(-1); } } }