void ascii_print_aligned_line(FILE *stream, int highlighting, char *attribute_name, char *line) { if (highlighting) { char *red = get_colour_escape('r', 1); char *bold = get_typeface_escape('b'); char *normal = get_typeface_escape('n'); fprintf(stream, "%s%s-->%s:%s %s\n", red, bold, attribute_name, normal, line); } else fprintf(stream, "-->%s: %s\n", attribute_name, line); }
/* colour: r=red g=green b=blue, p=pink, y=yellow, c=cyan */ char * get_colour_escape(char colour, int foreground) { if (use_colour) { if (*(get_typeface_escape('n')) == 0) return ""; /* don't try colour if terminal doesn't support typefaces */ if (foreground) { switch(colour) { case 'r': return "\x1B[0;31m"; case 'g': return "\x1B[0;32m"; case 'y': return "\x1B[0;33m"; case 'b': return "\x1B[0;34m"; case 'p': return "\x1B[0;35m"; case 'c': return "\x1B[0;36m"; default: Rprintf( "Internal error: unknown colour '%c'.\n", colour); return "\x1B[0m"; } } else { switch(colour) { case 'r': return "\x1B[0;41m"; case 'g': return "\x1B[0;42m"; case 'y': return "\x1B[0;43m"; case 'b': return "\x1B[0;44m"; case 'p': return "\x1B[0;45m"; case 'c': return "\x1B[0;46m"; default: Rprintf( "Internal error: unknown colour '%c'.\n", colour); return "\x1B[0m"; } } } else { return ""; } }
/** * Main function for the interactive CQP program. * * Doesn't do much except call the initialisation function, * and then one of the loop-and-parse-input functions. * * @param argc Number of commandline arguments. * @param argv Pointer to commandline arguments. * @return Return value to OS. */ int main(int argc, char *argv[]) { which_app = cqp; if (!initialize_cqp(argc, argv)) { fprintf(stderr, "Can't initialize CQP\n"); exit(1); } /* Test ANSI colours (if CQP was invoked with -C switch) */ if (use_colour) { #ifndef __MINGW__ char *blue = get_colour_escape('b', 1); char *green = get_colour_escape('g', 1); char *red = get_colour_escape('r', 1); char *pink = get_colour_escape('p', 1); char *cyanBack = get_colour_escape('c', 0); char *greenBack = get_colour_escape('g', 0); char *yellowBack = get_colour_escape('y', 0); char *bold = get_typeface_escape('b'); char *underline = get_typeface_escape('u'); char *standout = get_typeface_escape('s'); char *normal = get_typeface_escape('n'); char sc_colour[256]; int i, j; printf("%s%sWelcome%s to %s%sC%s%sQ%s%sP%s -- ", green, bold, normal, red, bold, pink, bold, blue, bold, normal); printf("the %s Colourful %s Query %s Processor %s.\n", yellowBack, greenBack, cyanBack, normal); for (i = 3; i <= 4; i++) { printf("["); for (j = 0; j < 8; j++) { sprintf(sc_colour, "\x1B[0;%d%dm", i,j); printf("%d%d: %sN%s%sB%s%sU%s%sS%s ", i, j, sc_colour, sc_colour, bold, sc_colour, underline, sc_colour, standout, normal); } printf("]\n"); } #else fprintf(stderr, "We're sorry, CQP's Colourful Mode is not available under Windows.\n"); fprintf(stderr, "CQP will continue as normal without it...\n"); use_colour = 0; #endif } /* endif use_colour */ install_signal_handler(); if (child_process) { printf("CQP version " VERSION "\n"); fflush(stdout); } if (batchmode) { if (batchfd == NULL) fprintf(stderr, "Can't open batch file\n"); else cqp_parse_file(batchfd, 1); } else { #ifdef USE_READLINE if (use_readline) readline_main(); else #endif /* USE_READLINE */ cqp_parse_file(stdin, 0); } if (macro_debug) macro_statistics(); return (cqp_error_status == 0 ? 0 : 1); }
/** this function replaces cqp_parse_file(stdin,0) if we're using GNU Readline */ void readline_main(void) { char prompt[CL_MAX_LINE_LENGTH]; char *input = NULL; /* activate CQP's custom completion function */ rl_attempted_completion_function = cqp_custom_completion; /* configuration: don't break tokens on $, so word lists work correctly (everything else corresponds to readline defaults) */ rl_completer_word_break_characters = " \t\n\"\\'`@><=;|&{("; /* if CQP history file is specified, read history from file */ if (cqp_history_file != NULL) { /* ignore errors; it's probably just that the history file doesn't exist yet */ read_history(cqp_history_file); } /* == the line input loop == */ while (!exit_cqp) { if (input != NULL) { free(input); input = NULL; } if (highlighting) { printf(get_typeface_escape('n')); /* work around 'bug' in less which may not switch off display attributes when user exits */ fflush(stdout); } if (silent) { input = readline(NULL); } else { if (current_corpus != NULL) { /* don't use terminal colours for the prompt because they mess up readline's formatting */ if (STREQ(current_corpus->name, current_corpus->mother_name)) sprintf(prompt, "%s> ", current_corpus->name); else sprintf(prompt, "%s:%s[%d]> ", current_corpus->mother_name, current_corpus->name, current_corpus->size); } else sprintf(prompt, "[no corpus]> "); input = readline(prompt); } if (input != NULL) { input = ensure_semicolon(input); /* add semicolon at end of line if missing (also replaces ws-only lines by "") */ if (*input) add_history(input); /* add input line to history (unless it's an empty line) */ cqp_parse_string(input); /* parse & execute query */ } else { exit_cqp = True; /* NULL means we've had an EOF character */ } /* reinstall signal handler if necessary */ if (! signal_handler_is_installed) install_signal_handler(); } if (save_on_exit) save_unsaved_subcorpora(); if (!silent) { printf("\nDone. Share and enjoy!\n"); } }
void get_screen_escapes(void) { int status, l; char *term; sc_s_in = ""; sc_s_out = ""; sc_u_in = ""; sc_u_out = ""; sc_b_in = ""; sc_b_out = ""; sc_bl_in = ""; sc_bl_out = ""; if ((term = getenv("TERM")) == NULL) return; if ((setupterm(term, 1, &status) == ERR) || (status != 1)) { return; } /* turn off all attributes */ sc_all_out = tigetstr("sgr0"); if (sc_all_out == NULL) sc_all_out = ""; /* Linux terminfo bug? fix: tigetstr("sgr0") returns an extra ^O (\x0f) character appended to the escape sequence (this may be some code used internally by the ncurses library). Since weRprintf() the escape sequences directly, we have to remove the extra character or 'less -R' will get confused. */ l = strlen(sc_all_out); if ((l > 0) && (sc_all_out[l-1] == '\x0f')) { sc_all_out = cl_strdup(sc_all_out); sc_all_out[l-1] = 0; /* just chop of the offending character */ } /* standout mode */ sc_s_in = tigetstr("smso"); if (sc_s_in == NULL) sc_s_in = ""; sc_s_out = tigetstr("rmso"); if (sc_s_out == NULL) sc_s_out = ""; /* underline */ sc_u_in = tigetstr("smul"); if (sc_u_in == NULL) sc_u_in = sc_s_in; sc_u_out = tigetstr("rmul"); if (sc_u_out == NULL) sc_u_out = sc_s_out; /* bold */ sc_b_in = tigetstr("bold"); if (sc_b_in == NULL) { sc_b_in = sc_s_in; sc_b_out = sc_s_out; } else { sc_b_out = tigetstr("sgr0"); /* can't turn off bold explicitly */ if (sc_b_out == NULL) sc_b_out = ""; } /* blink */ sc_bl_in = tigetstr("blink"); if (sc_bl_in == NULL) { sc_bl_in = sc_s_in; sc_bl_out = sc_s_out; } else { sc_bl_out = sc_all_out; /* can't turn off blinking mode explicitly */ } escapes_initialized++; /* in highlighted mode, switch off display attributes at end of line (to be on the safe side) */ ASCIIHighlightedPrintDescriptionRecord.AfterLine = cl_malloc(strlen(sc_all_out) + 2); sprintf(ASCIIHighlightedPrintDescriptionRecord.AfterLine, "%s\n", sc_all_out); /* print cpos in blue, "print structures" in pink if we're in coloured mode */ if (use_colour) { char *blue = get_colour_escape('b', 1); char *pink = get_colour_escape('p', 1); char *normal = get_typeface_escape('n'); char *bold = get_typeface_escape('b'); ASCIIHighlightedPrintDescriptionRecord.CPOSPrintFormat = cl_malloc(strlen(blue) + strlen(normal) + 8); sprintf(ASCIIHighlightedPrintDescriptionRecord.CPOSPrintFormat, "%s%c9d:%s ", blue, '%', normal); ASCIIHighlightedPrintDescriptionRecord.BeforePrintStructures = cl_malloc(strlen(pink) + strlen(bold) + 4); sprintf(ASCIIHighlightedPrintDescriptionRecord.BeforePrintStructures, "%s%s", pink, bold); ASCIIHighlightedPrintDescriptionRecord.AfterPrintStructures = cl_malloc(strlen(normal) + 6); sprintf(ASCIIHighlightedPrintDescriptionRecord.AfterPrintStructures, ":%s ", normal); } }